public function testZipExtract() { if (class_exists('ZipArchive', false)) { $a = new Archive(__DIR__ . '/../tmp/test.zip'); $a->addFiles(__DIR__ . '/../tmp'); mkdir(__DIR__ . '/../tmp/test'); chmod(__DIR__ . '/../tmp/test', 0777); chmod(__DIR__ . '/../tmp/test.zip', 0777); $a->extract(__DIR__ . '/../tmp/test'); unset($a); $dir = new Dir(__DIR__ . '/../tmp/test'); $this->assertGreaterThan(0, count($dir->getFiles())); $dir->emptyDir(); rmdir(__DIR__ . '/../tmp/test'); if (file_exists(__DIR__ . '/../tmp/test.zip')) { unlink(__DIR__ . '/../tmp/test.zip'); } } }
/** * Install modules method * * @throws \Phire\Exception * @return void */ public function installModules() { try { $path = BASE_PATH . APP_URI; if ($path == '') { $path = '/'; } $modulePath1 = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules'; $modulePath2 = __DIR__ . '/../../../../../module'; $formats = Archive::formats(); $phireCookie = null; foreach ($this->data['new'] as $name => $module) { $ext = substr($module, strrpos($module, '.') + 1); if (array_key_exists($ext, $formats)) { $modPath = file_exists($modulePath1 . '/' . $module) ? $modulePath1 : $modulePath2; if (!is_writable($modPath)) { throw new \Phire\Exception($this->i18n->__('The modules folder is not writable.')); } $archive = new Archive($modPath . '/' . $module); $archive->extract($modPath . '/'); if ((stripos($module, 'gz') || stripos($module, 'bz')) && file_exists($modPath . '/' . $name . '.tar')) { unlink($modPath . '/' . $name . '.tar'); } $dbType = Table\Extensions::getSql()->getDbType(); if ($dbType == \Pop\Db\Sql::SQLITE) { $type = 'sqlite'; } else { if ($dbType == \Pop\Db\Sql::PGSQL) { $type = 'pgsql'; } else { $type = 'mysql'; } } $sqlFile = $modPath . '/' . $name . '/data/' . strtolower($name) . '.' . $type . '.sql'; $cfg = null; $tables = array(); $info = array(); // Check for a config and try to get info out of it if (file_exists($modPath . '/' . $name . '/config') && file_exists($modPath . '/' . $name . '/config/module.php')) { $cfg = file_get_contents($modPath . '/' . $name . '/config/module.php'); if (strpos($cfg, '*/') !== false) { $cfgHeader = substr($cfg, 0, strpos($cfg, '*/')); $cfgHeader = substr($cfgHeader, strpos($cfgHeader, '/*') + 2); $cfgHeaderAry = explode("\n", $cfgHeader); foreach ($cfgHeaderAry as $line) { if (strpos($line, ':')) { $ary = explode(':', $line); if (isset($ary[0]) && isset($ary[1])) { $key = trim(str_replace('*', '', $ary[0])); $value = trim(str_replace('*', '', $ary[1])); $info[$key] = $value; } } } } } if (file_exists($sqlFile)) { // Get any tables required and created by this module $sql = file_get_contents($sqlFile); $tables = array(); $matches = array(); preg_match_all('/^CREATE TABLE(.*)$/mi', $sql, $matches); if (isset($matches[0]) && isset($matches[0][0])) { foreach ($matches[0] as $table) { if (strpos($table, '`') !== false) { $table = substr($table, strpos($table, '`') + 1); $table = substr($table, 0, strpos($table, '`')); } else { if (strpos($table, '"') !== false) { $table = substr($table, strpos($table, '"') + 1); $table = substr($table, 0, strpos($table, '"')); } else { if (strpos($table, "'") !== false) { $table = substr($table, strpos($table, "'") + 1); $table = substr($table, 0, strpos($table, "'")); } else { if (stripos($table, 'EXISTS') !== false) { $table = substr($table, stripos($table, 'EXISTS') + 6); } else { $table = substr($table, stripos($table, 'TABLE') + 5); } if (strpos($table, '(') !== false) { $table = substr($table, 0, strpos($table, '(')); } $table = trim($table); } } } $tables[] = str_replace('[{prefix}]', DB_PREFIX, $table); } } $ext = new Table\Extensions(array('name' => $name, 'file' => $module, 'type' => 1, 'active' => 1, 'assets' => serialize(array('tables' => $tables, 'info' => $info)))); $ext->save(); // If DB is SQLite if (stripos($type, 'Sqlite') !== false) { $dbName = realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/.htphire.sqlite'); $dbUser = null; $dbPassword = null; $dbHost = null; $installFile = $dbName; } else { $dbName = DB_NAME; $dbUser = DB_USER; $dbPassword = DB_PASS; $dbHost = DB_HOST; $installFile = null; } $db = array('database' => $dbName, 'username' => $dbUser, 'password' => $dbPassword, 'host' => $dbHost, 'prefix' => DB_PREFIX, 'type' => DB_INTERFACE == 'Pdo' ? 'Pdo_' . ucfirst(DB_TYPE) : DB_INTERFACE); Dbs::install($dbName, $db, $sqlFile, $installFile, true, false); } else { $ext = new Table\Extensions(array('name' => $name, 'type' => 1, 'active' => 1, 'assets' => serialize(array('tables' => $tables, 'info' => $info)))); $ext->save(); } if (null !== $cfg) { $config = (include $modPath . '/' . $name . '/config/module.php'); if (null !== $config[$name]->install) { $installFunc = $config[$name]->install; $installFunc(); } } if (php_sapi_name() != 'cli') { $cookie = Cookie::getInstance(array('path' => $path)); if (isset($cookie->phire)) { if (null === $phireCookie) { $phireCookie = $cookie->phire; } $i18n = file_exists($modPath . '/' . $name . '/data/assets/i18n'); $modules = (array) $phireCookie->modules; $modules[] = array('name' => $name, 'i18n' => $i18n); $phireCookie->modules = $modules; } } } } if (null !== $phireCookie) { $cookie = Cookie::getInstance(array('path' => $path)); $cookie->set('phire', $phireCookie); } } catch (\Exception $e) { $this->data['error'] = $e->getMessage(); } }
/** * Upload template * * @param array $file * @return void */ public function upload($file) { $templatePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/templates'; if (!file_exists($templatePath)) { mkdir($templatePath); chmod($templatePath, 0777); if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/index.html')) { copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/index.html', $templatePath . '/index.html'); chmod($templatePath . '/index.html', 0777); } } $upload = new Upload($templatePath); $template = $upload->upload($file); $formats = Archive::getFormats(); if (file_exists($templatePath . '/' . $template)) { $ext = null; $name = null; if (substr($template, -4) == '.zip') { $ext = 'zip'; $name = substr($template, 0, -4); } else { if (substr($template, -4) == '.tgz') { $ext = 'tgz'; $name = substr($template, 0, -4); } else { if (substr($template, -7) == '.tar.gz') { $ext = 'tar.gz'; $name = substr($template, 0, -7); } } } if (null !== $ext && null !== $name && array_key_exists($ext, $formats)) { $archive = new Archive($templatePath . '/' . $template); $archive->extract($templatePath); if (stripos($template, 'gz') !== false && file_exists($templatePath . '/' . $name . '.tar')) { unlink($templatePath . '/' . $name . '.tar'); } if (file_exists($templatePath . '/' . $name)) { $dir = new Dir($templatePath . '/' . $name, ['filesOnly' => true]); foreach ($dir->getFiles() as $file) { if (substr($file, -5) == '.html') { $isVisible = stripos($file, 'category') === false && stripos($file, 'error') === false && stripos($file, 'tag') === false && stripos($file, 'search') === false && stripos($file, 'sidebar') === false && stripos($file, 'header') === false && stripos($file, 'footer') === false; $template = new Table\Templates(['parent_id' => null, 'name' => ucwords(str_replace(['-', '_'], [' ', ' '], substr(strtolower($file), 0, -5))), 'device' => 'desktop', 'template' => file_get_contents($templatePath . '/' . $name . '/' . $file), 'history' => null, 'visible' => (int) $isVisible]); $template->save(); } } } } } }
/** * Get theme update * * @param string $new * @param string $old * @param string $version * @return void */ public function getUpdate($new, $old, $version) { if (file_exists(__DIR__ . '/../../../../themes/' . $old . '.zip')) { unlink(__DIR__ . '/../../../../themes/' . $old . '.zip'); } if (file_exists(__DIR__ . '/../../../../themes/' . $old)) { $dir = new Dir(__DIR__ . '/../../../../themes/' . $old); $dir->emptyDir(true); } file_put_contents(__DIR__ . '/../../../../themes/' . $new . '.zip', fopen('http://updates.phirecms.org/releases/themes/' . $new . '.zip', 'r')); $basePath = realpath(__DIR__ . '/../../../../themes/'); $archive = new Archive($basePath . '/' . $new . '.zip'); $archive->extract($basePath); $theme = Table\Themes::findById($this->id); $assets = unserialize($theme->assets); if (isset($assets['info']['version'])) { $assets['info']['version'] = $version; } else { if (isset($assets['info']['Version'])) { $assets['info']['Version'] = $version; } else { if (isset($assets['info']['VERSION'])) { $assets['info']['VERSION'] = $version; } } } $theme->file = $new . '.zip'; $theme->folder = $new; $theme->version = $version; $theme->assets = serialize($assets); $theme->updated_on = date('Y-m-d H:i:s'); $theme->save(); $this->getById($this->id); }
/** * Method to get update for one-click update * * @param string $module * @param string $new * @param string $old * @param int $id * @return void */ public function getUpdate($module = null, $new = null, $old = null, $id = null) { if (null === $module) { if (file_exists(CONTENT_ABS_PATH . '/assets/phire')) { $dir = new Dir(CONTENT_ABS_PATH . '/assets/phire'); $dir->emptyDir(true); } if (file_exists(CONTENT_ABS_PATH . '/assets/default')) { $dir = new Dir(CONTENT_ABS_PATH . '/assets/default'); $dir->emptyDir(true); } if (file_exists(CONTENT_ABS_PATH . '/assets/default-flat')) { $dir = new Dir(CONTENT_ABS_PATH . '/assets/default-flat'); $dir->emptyDir(true); } if (file_exists(CONTENT_ABS_PATH . '/assets/default-top')) { $dir = new Dir(CONTENT_ABS_PATH . '/assets/default-top'); $dir->emptyDir(true); } if (file_exists(CONTENT_ABS_PATH . '/assets/default-top-flat')) { $dir = new Dir(CONTENT_ABS_PATH . '/assets/default-top-flat'); $dir->emptyDir(true); } file_put_contents(CONTENT_ABS_PATH . '/updates/phirecms.zip', fopen('http://updates.phirecms.org/releases/phire/phirecms.zip', 'r')); $basePath = realpath(CONTENT_ABS_PATH . '/updates/'); $archive = new Archive($basePath . '/phirecms.zip'); $archive->extract($basePath); unlink(CONTENT_ABS_PATH . '/updates/phirecms.zip'); $json = json_decode(stream_get_contents(fopen('http://updates.phirecms.org/releases/phire/phire.json', 'r')), true); foreach ($json as $file) { if (!file_exists(__DIR__ . '/../' . $file) && !file_exists(dirname(__DIR__ . '/../' . $file))) { mkdir(dirname(__DIR__ . '/../' . $file), 0755, true); } copy(CONTENT_ABS_PATH . '/updates/phire-cms/' . $file, __DIR__ . '/../' . $file); } $dir = new Dir(CONTENT_ABS_PATH . '/updates/phire-cms/'); $dir->emptyDir(true); } else { if (file_exists(MODULES_ABS_PATH . '/' . $module . '-' . $old . '.zip')) { unlink(MODULES_ABS_PATH . '/' . $module . '-' . $old . '.zip'); } if (file_exists(MODULES_ABS_PATH . '/' . $module . '-' . $old)) { $dir = new Dir(MODULES_ABS_PATH . '/' . $module . '-' . $old); $dir->emptyDir(true); } if (file_exists(CONTENT_ABS_PATH . '/assets/' . $module)) { $dir = new Dir(CONTENT_ABS_PATH . '/assets/' . $module); $dir->emptyDir(true); } file_put_contents(MODULES_ABS_PATH . '/' . $module . '-' . $new . '.zip', fopen('http://updates.phirecms.org/releases/modules/' . $module . '-' . $new . '.zip', 'r')); $basePath = realpath(MODULES_ABS_PATH . '/'); $archive = new Archive($basePath . '/' . $module . '-' . $new . '.zip'); $archive->extract($basePath); $mod = Table\Modules::findById($id); $assets = unserialize($mod->assets); if (isset($assets['info']['version'])) { $assets['info']['version'] = $new; } else { if (isset($assets['info']['Version'])) { $assets['info']['Version'] = $new; } else { if (isset($assets['info']['VERSION'])) { $assets['info']['VERSION'] = $new; } } } $mod->file = $module . '-' . $new . '.zip'; $mod->folder = $module . '-' . $new; $mod->assets = serialize($assets); $mod->save(); } }
/** * Process batch archive file * * @param string $file * @param array $fields * @return void */ public function processBatch($file, array $fields) { $tmp = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp'; mkdir($tmp); chmod($tmp, 0777); $batchFileName = (new Upload($tmp))->upload($file); $archive = new Archive($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $batchFileName); $archive->extract($tmp); if (stripos($archive->getFilename(), '.tar') !== false && $archive->getFilename() != $batchFileName && file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $archive->getFilename())) { unlink($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $archive->getFilename()); } if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $batchFileName)) { unlink($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/_tmp/' . $batchFileName); } $library = new MediaLibrary(); $library->getById($fields['library_id']); $settings = $library->getSettings(); $dir = new Dir($tmp, ['absolute' => true, 'recursive' => true, 'filesOnly' => true]); $upload = new Upload($settings['folder'], $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']); foreach ($dir->getFiles() as $file) { $basename = basename($file); $testFile = ['name' => $basename, 'size' => filesize($file), 'error' => 0]; if ($upload->test($testFile)) { $fileName = $upload->checkFilename($basename); copy($file, $settings['folder'] . '/' . $fileName); $title = ucwords(str_replace(['_', '-'], [' ', ' '], substr($fileName, 0, strrpos($fileName, '.')))); if (null !== $library->adapter) { $class = 'Pop\\Image\\' . $library->adapter; $formats = array_keys($class::getFormats()); $fileParts = pathinfo($fileName); if (!empty($fileParts['extension']) && in_array(strtolower($fileParts['extension']), $formats)) { $this->processImage($fileName, $library); } } $media = new Table\Media(['library_id' => $fields['library_id'], 'title' => $title, 'file' => $fileName, 'size' => filesize($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $library->folder . DIRECTORY_SEPARATOR . $fileName), 'uploaded' => date('Y-m-d H:i:s'), 'order' => 0]); $media->save(); $this->data['ids'][] = $media->id; } } $dir->emptyDir(true); }
/** * Install modules * * @param \Pop\Service\Locator $services * @throws \Exception * @return void */ public function install(\Pop\Service\Locator $services) { $modulesPath = MODULES_ABS_PATH; $modules = $this->detectNew(false); if (strpos($modulesPath, 'vendor') !== false) { foreach ($modules as $module) { $this->finalizeInstall('', $module, $modulesPath, $services); } } else { if (!is_writable($modulesPath)) { throw new \Phire\Exception('Error: The module folder is not writable.'); } $formats = Archive::getFormats(); foreach ($modules as $module) { if (file_exists($modulesPath . '/' . $module)) { $ext = null; $folder = null; if (substr($module, -4) == '.zip') { $ext = 'zip'; $folder = substr($module, 0, -4); } else { if (substr($module, -4) == '.tgz') { $ext = 'tgz'; $folder = substr($module, 0, -4); } else { if (substr($module, -7) == '.tar.gz') { $ext = 'tar.gz'; $folder = substr($module, 0, -7); } } } if (null !== $ext && null !== $folder && array_key_exists($ext, $formats)) { $archive = new Archive($modulesPath . '/' . $module); $archive->extract($modulesPath); if (stripos($module, 'gz') !== false && file_exists($modulesPath . '/' . $folder . '.tar')) { unlink($modulesPath . '/' . $folder . '.tar'); } if (file_exists($modulesPath . '/' . $folder) && file_exists($modulesPath . '/' . $folder . '/config/module.php')) { $this->finalizeInstall($module, $folder, $modulesPath, $services); } } } } } }
public function testTbz2Extract() { $tar = false; $includePath = explode(PATH_SEPARATOR, get_include_path()); foreach ($includePath as $path) { if (file_exists($path . DIRECTORY_SEPARATOR . 'Archive' . DIRECTORY_SEPARATOR . 'Tar.php')) { $tar = true; } } if ($tar && function_exists('bzopen')) { $a = new Archive(__DIR__ . '/../tmp/test.tar'); $a->addFiles(__DIR__ . '/../tmp'); $a->compress('bz2'); chmod(__DIR__ . '/../tmp/test.tar.bz2', 0777); unset($a); $a = new Archive(__DIR__ . '/../tmp/test.tar.bz2'); mkdir(__DIR__ . '/../tmp/test'); chmod(__DIR__ . '/../tmp/test', 0777); $a->extract(__DIR__ . '/../tmp/test'); $dir = new Dir(__DIR__ . '/../tmp/test'); $this->assertGreaterThan(0, count($dir->getFiles())); $dir->emptyDir(); rmdir(__DIR__ . '/../tmp/test'); if (file_exists(__DIR__ . '/../tmp/test.tar')) { unlink(__DIR__ . '/../tmp/test.tar'); } if (file_exists(__DIR__ . '/../tmp/test.tar.bz2')) { unlink(__DIR__ . '/../tmp/test.tar.bz2'); } } }