public function testGetAndSetPermissions() { $d = new Dir(__DIR__ . '/../tmp/'); $this->assertEquals(777, $d->getPermissions()); $d->setPermissions(0775, true); $this->assertEquals(775, $d->getPermissions()); $d->setPermissions(0777); }
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'); } } }
/** * Bootstrap the module * * @param Application $application * @return void */ public static function bootstrap(Application $application) { if ($application->isRegistered('phire-content')) { $theme = Table\Themes::findBy(['active' => 1]); if (isset($theme->id)) { $dir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $theme->folder, ['filesOnly' => true]); $parentDir = null; if (null !== $theme->parent_id) { $parentTheme = Table\Themes::findById($theme->parent_id); if (isset($parentTheme->id)) { $parentDir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $parentTheme->folder, ['filesOnly' => true]); } } $forms = $application->config()['forms']; if (null !== $parentDir) { $dirFiles = $dir->getFiles(); foreach ($dirFiles as $file) { if (strpos($file, '.ph') !== false && !in_array($file, $application->module('phire-themes')['invisible'])) { $forms['Phire\\Content\\Form\\Content'][0]['content_template']['value'][$file] = $file; } } foreach ($parentDir->getFiles() as $file) { if (!in_array($file, $dirFiles) && strpos($file, '.ph') !== false && !in_array($file, $application->module('phire-themes')['invisible'])) { $forms['Phire\\Content\\Form\\Content'][0]['content_template']['value'][$file] = $file . ' (parent)'; } } } else { foreach ($dir->getFiles() as $file) { if (strpos($file, '.ph') !== false && !in_array($file, $application->module('phire-themes')['invisible'])) { $forms['Phire\\Content\\Form\\Content'][0]['content_template']['value'][$file] = $file; } } } $application->mergeConfig(['forms' => $forms], true); } } }
/** * 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(); } }
public function getHistory($filename) { $history = []; $dir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/image-history'); foreach ($dir->getFiles() as $file) { if ($file == $filename) { $history[] = $file; } else { if (strpos($file, '_') !== false) { $basename = str_replace(['-', '_'], ['\\-', '\\_'], substr($filename, 0, strrpos($filename, '.'))); $ext = substr($filename, strrpos($filename, '.') + 1); $regex = '/(' . $basename . ')\\_+(\\d.*)\\.' . $ext . '/'; if (preg_match($regex, $file)) { $history[] = $file; } } } } sort($history, SORT_NATURAL); return $history; }
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'); } } }
/** * Method to create an archive file * * @param string|array $files * @return void */ public function addFiles($files) { if (!is_array($files)) { $files = array($files); } foreach ($files as $file) { // If file is a directory, loop through and add the files. if (file_exists($file) && is_dir($file)) { $realpath = realpath($file); $dir = new Dir($file, true, true); $dirFiles = $dir->getFiles(); foreach ($dirFiles as $fle) { if (file_exists($fle) && !is_dir($fle)) { $fle = $file . DIRECTORY_SEPARATOR . str_replace($realpath . DIRECTORY_SEPARATOR, '', $fle); $this->archive->add($fle); } } // Else, just add the file. } else { if (file_exists($file)) { $this->archive->add($file); } } } }
/** * 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(); } } } } } }
/** * Uninstall themes * * @param array $ids * @return void */ public function uninstall($ids) { $themePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes'; foreach ($ids as $id) { $theme = Table\Themes::findById((int) $id); if (isset($theme->id)) { // Remove the theme folder and files if (file_exists($themePath . '/' . $theme->folder)) { $dir = new Dir($themePath . '/' . $theme->folder); $dir->emptyDir(true); } // Remove the theme file if (file_exists($themePath . '/' . $theme->file) && is_writable($themePath . '/' . $theme->file)) { unlink($themePath . '/' . $theme->file); } $children = Table\Themes::findBy(['parent_id' => $theme->id]); if ($children->hasRows()) { foreach ($children->rows() as $child) { $childThemePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $child->folder; // Remove the child theme folder and files if (file_exists($childThemePath)) { $dir = new Dir($childThemePath); $dir->emptyDir(true); } $c = Table\Themes::findById($child->id); if (isset($c->id)) { $c->delete(); } } } $theme->delete(); } } }
/** * Save the config data * * @param array $post * @return void */ public function save(array $post) { $config = Table\Config::findById('domain'); if (isset($_SERVER['HTTP_HOST']) && $config->value != $_SERVER['HTTP_HOST']) { $config->value = $_SERVER['HTTP_HOST']; $config->save(); } $config = Table\Config::findById('document_root'); if (isset($_SERVER['DOCUMENT_ROOT']) && $config->value != $_SERVER['DOCUMENT_ROOT']) { $config->value = $_SERVER['DOCUMENT_ROOT']; $config->save(); } if (!empty($post['datetime_format_custom']) && $post['datetime_format'] == 'custom') { $dateFormatValue = str_replace(['"', "'"], ['', ''], strip_tags($post['datetime_format_custom'])); } else { if (!empty($post['datetime_format']) && $post['datetime_format'] != 'custom') { $dateFormatValue = $post['datetime_format']; } else { $dateFormatValue = 'M j Y'; } } $config = Table\Config::findById('datetime_format'); $config->value = $dateFormatValue; $config->save(); $config = Table\Config::findById('pagination'); $config->value = (int) $post['pagination']; $config->save(); $config = Table\Config::findById('system_theme'); $oldValue = $config->value; $config->value = $post['system_theme']; $config->save(); if (isset($_SERVER['DOCUMENT_ROOT']) && file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets/' . $oldValue)) { $dir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets/' . $oldValue); $dir->emptyDir(true); } }
/** * Uninstall modules * * @param array $ids * @param \Pop\Service\Locator $services * @return void */ public function uninstall($ids, $services) { $modulesPath = MODULES_ABS_PATH; foreach ($ids as $id) { $module = Table\Modules::findById((int) $id); if (isset($module->id)) { $assets = unserialize($module->assets); if (isset($assets['tables']) && count($assets['tables']) > 0) { $db = $services['database']; if (DB_INTERFACE == 'mysql' || DB_TYPE == 'mysql') { $db->query('SET foreign_key_checks = 0;'); foreach ($assets['tables'] as $table) { $db->query('DROP TABLE ' . $table); } $db->query('SET foreign_key_checks = 1;'); } else { if (DB_INTERFACE == 'pgsql' || DB_TYPE == 'pgsql') { foreach ($assets['tables'] as $table) { $db->query('DROP TABLE ' . $table . ' CASCADE'); } } else { foreach ($assets['tables'] as $table) { $db->query('DROP TABLE ' . $table); } } } } // Run any uninstall functions $config = (include $modulesPath . '/' . $module->folder . '/config/module.php'); if (isset($config[$module->name]) && isset($config[$module->name]['uninstall']) && !empty($config[$module->name]['uninstall'])) { call_user_func_array($config[$module->name]['uninstall'], [$services]); } // Remove any assets if (file_exists(__DIR__ . '/../../..' . CONTENT_PATH . '/assets/' . strtolower($module->name))) { $dir = new Dir(__DIR__ . '/../../..' . CONTENT_PATH . '/assets/' . strtolower($module->name)); $dir->emptyDir(true); } if (strpos($modulesPath, 'vendor') === false) { // Remove the module folder and files if (file_exists($modulesPath . '/' . $module->folder)) { $dir = new Dir($modulesPath . '/' . $module->folder); $dir->emptyDir(true); } // Remove the module file if (file_exists($modulesPath . '/' . $module->file) && is_writable($modulesPath . '/' . $module->file)) { unlink($modulesPath . '/' . $module->file); } } $module->delete(); } } }
/** * Parse actions * * @param string $folder * @return array */ protected function parseActions($folder) { $dir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $folder); $curDirs = []; $newDirs = []; foreach ($dir->getFiles() as $file) { if (is_dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $file)) { $curDirs[] = $file; } } $actions = []; foreach ($_POST as $key => $value) { if (substr($key, 0, 12) == 'action_name_') { $id = substr($key, strrpos($key, '_') + 1); if (!empty($_POST['action_name_' . $id]) && $_POST['action_method_' . $id] != '----' && !empty($_POST['action_params_' . $id])) { $actions[$_POST['action_name_' . $id]] = ['method' => $_POST['action_method_' . $id], 'params' => $_POST['action_params_' . $id], 'quality' => !empty($_POST['action_quality_' . $id]) ? (int) $_POST['action_quality_' . $id] : 80]; if (!file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $_POST['action_name_' . $id])) { mkdir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $_POST['action_name_' . $id]); chmod($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $_POST['action_name_' . $id], 0777); copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . 'index.html', $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $_POST['action_name_' . $id] . DIRECTORY_SEPARATOR . 'index.html'); } $newDirs[] = $_POST['action_name_' . $id]; } } } // Clean up directories foreach ($curDirs as $dir) { if (!in_array($dir, $newDirs)) { $d = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $dir); $d->emptyDir(true); } } return $actions; }
/** * Process and save seo config * * @param array $exclude * @return void */ public function saveAnalysis(array $exclude = []) { $config = Table\Config::findById('seo_config'); $cfg = isset($config->value) && !empty($config->value) && $config->value != '' ? unserialize($config->value) : []; $analysis = ['tracking' => false, 'sitemap' => false, 'robots' => false, 'caching' => false, 'site-verify' => false, 'content' => ['good' => [], 'bad' => []]]; if (!empty($cfg['tracking']) && $cfg['tracking'] != '') { $analysis['tracking'] = true; } $sitemap = \Phire\Table\Modules::findBy(['name' => 'phire-sitemap']); if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . '/sitemap.xml') || isset($sitemap->id) && $sitemap->active) { $analysis['sitemap'] = true; } if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . '/robots.txt') || isset($cfg['robots']) && !empty($cfg['robots']) && $cfg['robots'] != '') { $analysis['robots'] = true; } $cacheDetect = false; $curl = new Curl('http://' . $_SERVER['HTTP_HOST'] . BASE_PATH); if ($curl->getCode() == 200 && null !== $curl->getHeader('Cache-Control') && null !== $curl->getHeader('Expires') && null !== $curl->getHeader('Last-Modified') && null !== $curl->getHeader('Etag')) { $cacheDetect = true; } $cache = \Phire\Table\Modules::findBy(['name' => 'phire-cache']); if ($cacheDetect || isset($cache->id) && $cache->active) { $analysis['caching'] = true; } $dir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH, ['relative' => true, 'filesOnly' => true]); $googleFileDetect = false; foreach ($dir->getFiles() as $file) { if (substr($file, 0, 6) == 'google' && strpos(file_get_contents($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . '/' . $file), 'google-site-verification') !== false) { $googleFileDetect = true; } } $googleMetaDetect = false; if (isset($cfg['meta'])) { foreach ($cfg['meta'] as $meta) { if ($meta['name'] == 'google-site-verification') { $googleMetaDetect = true; } } } if ($googleFileDetect || $googleMetaDetect) { $analysis['site-verify'] = true; } else { if (function_exists('dns_get_record') && $_SERVER['HTTP_HOST'] != 'localhost') { $dns = dns_get_record($_SERVER['HTTP_HOST'], DNS_TXT); if (count($dns) > 0) { foreach ($dns as $record) { if (isset($record['txt']) && strpos($record['txt'], 'google-site-verification') !== false) { $analysis['site-verify'] = true; } } } } } $fields = ['seo_title' => '', 'description' => '', 'keywords' => '']; $seoTitle = \Phire\Fields\Table\Fields::findBy(['name' => 'seo_title']); $description = \Phire\Fields\Table\Fields::findBy(['name' => 'description']); $keywords = \Phire\Fields\Table\Fields::findBy(['name' => 'keywords']); if (isset($seoTitle->id)) { $fields['seo_title'] = $seoTitle->id; } if (isset($description->id)) { $fields['description'] = $description->id; } if (isset($keywords->id)) { $fields['keywords'] = $keywords->id; } $content = \Phire\Content\Table\Content::findAll(); foreach ($content->rows() as $c) { if (!in_array($c->type_id, $exclude)) { $seoTitle = ''; $metaDesc = ''; $metaKeys = ''; if ($fields['seo_title'] != '') { $seoTitleField = \Phire\Fields\Table\FieldValues::findById([$fields['seo_title'], $c->id, "Phire\\Content\\Model\\Content"]); if (isset($seoTitleField->field_id)) { $seoTitle = json_decode($seoTitleField->value); } } if ($fields['description'] != '') { $descriptionField = \Phire\Fields\Table\FieldValues::findById([$fields['description'], $c->id, "Phire\\Content\\Model\\Content"]); if (isset($descriptionField->field_id)) { $metaDesc = json_decode($descriptionField->value); } } if ($fields['keywords'] != '') { $keywordsField = \Phire\Fields\Table\FieldValues::findById([$fields['keywords'], $c->id, "Phire\\Content\\Model\\Content"]); if (isset($keywordsField->field_id)) { $metaKeys = json_decode($keywordsField->value); } } if (strlen($seoTitle) > 0 && strlen($seoTitle) <= 60 && strlen($metaDesc) > 0 && strlen($metaDesc) <= 160 && strlen($metaKeys) > 0 && strlen($metaKeys) <= 255) { $analysis['content']['good'][$c->id] = ['title' => $c->title, 'uri' => $c->uri]; } else { $analysis['content']['bad'][$c->id] = ['title' => $c->title, 'type_id' => $c->type_id, 'uri' => $c->uri, 'issues' => []]; if (strlen($seoTitle) == 0) { $analysis['content']['bad'][$c->id]['issues'][] = 'No SEO Title'; } else { if (strlen($seoTitle) > 60) { $analysis['content']['bad'][$c->id]['issues'][] = 'SEO Title is too long'; } } if (strlen($metaDesc) == 0) { $analysis['content']['bad'][$c->id]['issues'][] = 'No description meta tag'; } else { if (strlen($metaDesc) > 160) { $analysis['content']['bad'][$c->id]['issues'][] = 'Description meta tag is too long'; } } if (strlen($metaKeys) == 0) { $analysis['content']['bad'][$c->id]['issues'][] = 'No keywords meta tag'; } else { if (strlen($metaKeys) > 255) { $analysis['content']['bad'][$c->id]['issues'][] = 'Keywords meta tag is too long'; } } } } } $config = Table\Config::findById('seo_analysis'); $config->value = serialize($analysis); $config->save(); }
/** * Method to create an archive file * * @param string|array $files * @return void */ public function addFiles($files) { if (!is_array($files)) { $files = array($files); } // Directory separator clean up $search = array('\\', '../', './'); $replace = array('/', '', ''); foreach ($files as $file) { // If file is a directory, loop through and add the files. if (file_exists($file) && is_dir($file)) { $dir = new Dir($file, true, true); $this->archive->addEmptyDir(str_replace($search, $replace, $dir->getPath())); $dirFiles = $dir->getFiles(); foreach ($dirFiles as $fle) { if (file_exists($fle) && is_dir($fle)) { $this->archive->addEmptyDir(str_replace($search, $replace, $fle)); } else { if (file_exists($fle)) { $this->archive->addFile($fle, str_replace($search, $replace, $fle)); } } } // Else, just add the file. } else { if (file_exists($file)) { $this->archive->addFile($file, str_replace('\\', '/', $file)); } } } }
/** * Static method to get model types * * @param \Pop\Config $config * @return array */ public static function getResources($config = null) { $resources = array(); $exclude = array(); $override = null; // Get any exclude or override config values if (null !== $config) { $configAry = $config->asArray(); if (isset($configAry['exclude_controllers'])) { $exclude = $configAry['exclude_controllers']; } if (isset($configAry['override'])) { $override = $configAry['override']; } } // If override, set overridden resources if (null !== $override) { foreach ($override as $resource) { $resources[] = $resource; } // Else, get all controllers from the system and module directories } else { $systemDirectory = new Dir(realpath(__DIR__ . '/../../../../'), true); $systemModuleDirectory = new Dir(realpath(__DIR__ . '/../../../../../module/'), true); $moduleDirectory = new Dir(realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules'), true); $dirs = array_merge($systemDirectory->getFiles(), $systemModuleDirectory->getFiles(), $moduleDirectory->getFiles()); sort($dirs); // Dir clean up foreach ($dirs as $key => $dir) { unset($dirs[$key]); if (!(strpos($dir, 'config') !== false || strpos($dir, 'index.html') !== false)) { $k = $dir; if (substr($dir, -1) == DIRECTORY_SEPARATOR) { $k = substr($k, 0, -1); } $k = substr($k, strrpos($k, DIRECTORY_SEPARATOR) + 1); $dirs[$k] = $dir; } } // Loop through each directory, looking for controller class files foreach ($dirs as $mod => $dir) { if (file_exists($dir . 'src/' . $mod . '/Controller')) { $d = new Dir($dir . 'src/' . $mod . '/Controller', true, true, false); $dFiles = $d->getFiles(); sort($dFiles); // If found, loop through the files, getting the methods as the "permissions" foreach ($dFiles as $c) { if (strpos($c, 'index.html') === false && strpos($c, 'Abstract') === false) { // Get all public methods from class $class = str_replace(array('.php', DIRECTORY_SEPARATOR), array('', '\\'), substr($c, strpos($c, 'src') + 4)); $code = new \ReflectionClass($class); $methods = $code->getMethods(\ReflectionMethod::IS_PUBLIC); $actions = array(); foreach ($methods as $value) { if ($value->getName() !== '__construct' && $value->class == $class) { $action = $value->getName(); if (!isset($exclude[$class]) || isset($exclude[$class]) && is_array($exclude[$class]) && !in_array($action, $exclude[$class])) { $actions[] = $action; } } } $types = array(0 => '(All)'); if (strpos($class, "\\Controller\\IndexController") === false) { $classAry = explode('\\', $class); $end1 = count($classAry) - 2; $end2 = count($classAry) - 1; $model = $classAry[0] . '_Model_'; if (stripos($classAry[$end2], 'index') !== false) { $model .= $classAry[$end1]; } else { if (substr($classAry[$end2], 0, 4) == 'Type') { $model .= $classAry[$end1] . 'Type'; } else { $model .= str_replace('Controller', '', $classAry[$end2]); } } if (substr($model, -3) == 'ies') { $model = substr($model, 0, -3) . 'y'; } else { if (substr($model, -1) == 's') { $model = substr($model, 0, -1); } } $types = \Phire\Project::getModelTypes($model); // Format the resource and permissions $c = str_replace(array('Controller.php', '\\'), array('', '/'), $c); $c = substr($c, strpos($c, 'Controller') + 11); $c = str_replace('Phire/', '', $c); if (!in_array($class, $exclude) || isset($exclude[$class]) && is_array($exclude[$class])) { $resources[$class] = array('name' => $c, 'types' => $types, 'actions' => $actions); } } } } } } } return $resources; }
/** * Method to convert the image. * * @return void */ protected function convertImage() { // Define the temp converted image. $this->convertedImage = \Pop\File\Dir::getUploadTemp() . DIRECTORY_SEPARATOR . $this->img->getFilename() . '_' . time() . '.png'; // Convert the GIF to PNG, save and clear the output buffer. $this->img->convert('png')->save($this->convertedImage); // Re-instantiate the newly converted image object and re-read the image data. $this->img = new Gd($this->convertedImage); $this->imageData = $this->img->read(); }
public function testSaveTo() { $m = new Mail('Subject', array('name' => 'Bob Smith', 'email' => '*****@*****.**'), 'Hello World'); $m->setHtml('Hello'); $m->setText('Hello'); $m->saveTo(__DIR__ . '/../tmp'); $d = new Dir(__DIR__ . '/../tmp'); $files = $d->getFiles(); $exists = false; foreach ($files as $file) { if (strpos($file, 'popphpmail') !== false) { $exists = true; unlink(__DIR__ . '/../tmp/' . $file); } } $this->assertTrue($exists); }
/** * Load application assets to a public folder * * @param string $from * @param string $to * @param boolean $import * @return Module */ public function loadAssets($from, $to, $import = false) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets') && is_writable($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets')) { $toDir = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets/' . $to; if (!file_exists($toDir)) { mkdir($toDir); $dir = new Dir($from, ['absolute' => true, 'recursive' => true]); $dir->copyDir($toDir, false); } $cssDirs = ['css', 'styles', 'style']; $jsDirs = ['js', 'scripts', 'script', 'scr']; $cssType = $import ? 'import' : 'link'; foreach ($cssDirs as $cssDir) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets/' . $to . '/' . $cssDir)) { $dir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets/' . $to . '/' . $cssDir); foreach ($dir->getFiles() as $cssFile) { if ($cssFile != 'index.html') { $css = BASE_PATH . CONTENT_PATH . '/assets/' . $to . '/' . $cssDir . '/' . $cssFile; if (!in_array($css, $this->assets['css'][$cssType]) && substr($css, -4) == '.css' && stripos($css, 'public') === false) { $this->assets['css'][$cssType][] = $css; } } } } } foreach ($jsDirs as $jsDir) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets/' . $to . '/' . $jsDir)) { $dir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets/' . $to . '/' . $jsDir); foreach ($dir->getFiles() as $jsFile) { if ($jsFile != 'index.html') { $js = BASE_PATH . CONTENT_PATH . '/assets/' . $to . '/' . $jsDir . '/' . $jsFile; if (!in_array($js, $this->assets['js']) && substr($js, -3) == '.js' && stripos($js, 'public') === false) { $this->assets['js'][] = $js; } } } } } } return $this; }
/** * Method to create sub directories within the zip archive * * @param array $branch * @param string $level * @param string $orig * @return void */ public function addDir($branch, $level = null, $orig = null) { if (!is_array($branch)) { $dir = new Dir($branch); $branch = $dir->getTree(); } foreach ($branch as $leaf => $node) { if (is_array($node)) { if (null === $level) { $new = basename($leaf); $orig = substr($leaf, 0, strrpos($leaf, $new)); } else { $new = $level . $leaf; } $this->archive->addEmptyDir($new); $this->addDir($node, $new, $orig); } else { $this->archive->addFile($orig . $level . '/' . $node, $level . '/' . $node); } } }
/** * Determine whether or not the necessary system directories are writable or not. * * @param string $contentDir * @param boolean $msgs * @param string $docRoot * @return boolean|array */ public static function checkDirs($contentDir, $msgs = false, $docRoot = null) { if (null === $docRoot) { $docRoot = $_SERVER['DOCUMENT_ROOT']; } $dir = new Dir($contentDir, true, true); $files = $dir->getFiles(); $errorMsgs = array(); // Check if the necessary directories are writable for Windows. if (stripos(PHP_OS, 'win') !== false) { if (@touch($contentDir . '/writetest.txt') == false) { $errorMsgs[] = "The directory " . str_replace($docRoot, '', $contentDir) . " is not writable."; } else { unlink($contentDir . '/writetest.txt'); clearstatcache(); } foreach ($files as $value) { if (strpos($value, 'data') === false && strpos($value, 'ckeditor') === false && strpos($value, 'tinymce') === false && is_dir($value)) { if (@touch($value . '/writetest.txt') == false) { $errorMsgs[] = "The directory " . str_replace($docRoot, '', $value) . " is not writable."; } else { unlink($value . '/writetest.txt'); clearstatcache(); } } } // Check if the necessary directories are writable for Unix/Linux. } else { clearstatcache(); if (!is_writable($contentDir)) { $errorMsgs[] = "The directory " . str_replace($docRoot, '', $contentDir) . " is not writable."; } foreach ($files as $value) { if (strpos($value, 'data') === false && strpos($value, 'ckeditor') === false && strpos($value, 'tinymce') === false && is_dir($value)) { clearstatcache(); if (!is_writable($value)) { $errorMsgs[] = "The directory " . str_replace($docRoot, '', $value) . " is not writable."; } } } } // If the messaging flag was passed, return any // error messages, else return true/false. if ($msgs) { return $errorMsgs; } else { return count($errorMsgs) == 0 ? true : false; } }
/** * Perform update * * @param array $post * @param boolean $cli * @return void */ public function getUpdate($post = array(), $cli = false) { $docRoot = __DIR__ . '/../../../../../..'; // If system is writable for updates if (!isset($post['submit'])) { switch ($post['type']) { case 'system': $remoteFile = 'http://update.phirecms.org/system/latest.' . $post['format']; break; case 'module': $remoteFile = 'http://update.phirecms.org/modules/' . strtolower($post['name']) . '/latest.' . $post['format']; break; case 'theme': $remoteFile = 'http://update.phirecms.org/themes/' . strtolower($post['name']) . '/latest.' . $post['format']; break; } $remoteContents = @file_get_contents($remoteFile); if ($remoteContents !== false) { $localFile = $docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'latest.' . $post['format']; file_put_contents($localFile, $remoteContents); $arc = new \Pop\Archive\Archive($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'latest.' . $post['format']); $arc->extract($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update'); unlink($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'latest.' . $post['format']); $msg = null; if ($post['type'] == 'module') { if (file_exists($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $post['name'])) { $dir = new Dir($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $post['name']); $dir->emptyDir(null, true); rename($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update/' . $post['name'], $docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $post['name']); } $msg = $this->i18n->__('The %1 module has been updated.', $post['name']); } else { if ($post['type'] == 'theme') { if (file_exists($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $post['name'])) { $dir = new Dir($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $post['name']); $dir->emptyDir(null, true); rename($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update/' . $post['name'], $docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $post['name']); } $msg = $this->i18n->__('The %1 theme has been updated.', $post['name']); } else { if ($post['type'] == 'system') { if ($cli) { rename($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'phire-cms', $docRoot . DIRECTORY_SEPARATOR . 'phire-cms-new'); $config = Table\Config::findById('updated_on'); $config->value = date('Y-m-d H:i:s'); $config->update(); $msg = $this->i18n->__('The system has been updated.'); } else { $time = time(); mkdir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . $time); // Move old files into archive folder rename($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . 'config', $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . $time . DIRECTORY_SEPARATOR . 'config'); rename($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . 'module', $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . $time . DIRECTORY_SEPARATOR . 'module'); rename($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . 'script', $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . $time . DIRECTORY_SEPARATOR . 'script'); rename($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . 'vendor', $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . $time . DIRECTORY_SEPARATOR . 'vendor'); // Move new files into main application path rename($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'phire-cms' . DIRECTORY_SEPARATOR . 'config', $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . 'config'); rename($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'phire-cms' . DIRECTORY_SEPARATOR . 'module', $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . 'module'); rename($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'phire-cms' . DIRECTORY_SEPARATOR . 'script', $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . 'script'); rename($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'phire-cms' . DIRECTORY_SEPARATOR . 'vendor', $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . DIRECTORY_SEPARATOR . 'vendor'); $dir = new Dir($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'phire-cms'); $dir->emptyDir(null, true); $config = Table\Config::findById('updated_on'); $config->value = date('Y-m-d H:i:s'); $config->update(); $msg = $this->i18n->__('The system has been updated.'); } } } } $this->data['msg'] = '<span style="color: #347703">' . $msg . '</span>'; } else { $this->data['error'] = '<span style="color: #a00b0b">' . $this->i18n->__('The update file was not found.') . '</span>'; } // Else use cURL/FTP } else { unset($post['submit']); $curl = new \Pop\Curl\Curl('http://update.phirecms.org/update.php'); $curl->setPost(true); $curl->setFields($post); $curl->execute(); $response = json_decode($curl->getBody()); unset($curl); if ($response->error == 0) { $arc = new \Pop\Archive\Archive($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'latest.' . $post['format']); $arc->extract($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update'); unlink($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update' . DIRECTORY_SEPARATOR . 'latest.' . $post['format']); if ($post['type'] == 'system') { chmod($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update/phire-cms', 0777); } else { if (file_exists($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update/' . $post['name'])) { chmod($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update/' . $post['name'], 0777); } } $post['complete'] = 1; $curl = new \Pop\Curl\Curl('http://update.phirecms.org/update.php'); $curl->setPost(true); $curl->setFields($post); $curl->execute(); $complete = json_decode($curl->getBody()); if ($complete->error == 0) { switch ($complete->type) { case 'system': $config = Table\Config::findById('updated_on'); $config->value = date('Y-m-d H:i:s'); $config->update(); $msg = $this->i18n->__('The system has been updated.'); chmod($docRoot . APP_PATH, 0755); break; case 'module': if (file_exists($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $post['name'])) { $dir = new Dir($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $post['name']); $dir->emptyDir(null, true); rename($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update/' . $post['name'], $docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . $post['name']); } $msg = $this->i18n->__('The %1 module has been updated.', $complete->name); break; case 'theme': if (file_exists($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $post['name'])) { $dir = new Dir($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $post['name']); $dir->emptyDir(null, true); rename($docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'update/' . $post['name'], $docRoot . CONTENT_PATH . DIRECTORY_SEPARATOR . 'extensions' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $post['name']); } $msg = $this->i18n->__('The %1 theme has been updated.', $complete->name); break; } $this->data['msg'] = '<span style="color: #347703">' . $msg . '</span>'; } else { $this->data['error'] = '<span style="color: #a00b0b">' . $complete->message . '</span>'; } } else { $this->data['error'] = '<span style="color: #a00b0b">' . $response->message . '</span>'; } } $this->postUpdate($post); }
/** * 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); }
/** * Get uploaded images * * @param string $dir * @return array */ public function getAllImages($dir) { $images = []; $d = new Dir($_SERVER['DOCUMENT_ROOT'] . $dir, ['filesOnly' => true]); foreach ($d->getFiles() as $file) { if ($file != 'index.html' && preg_match('/^.*\\.(jpg|jpeg|png|gif)$/i', $file) == 1) { $images[$dir . '/' . $file] = $file; } } return $images; }
/** * Static method to get a file icon * * @param string $fileName * @param string $dir * @return array */ public static function getFileIcon($fileName, $dir = null) { if (null === $dir) { $dir = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/media'; } $iconDir = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/assets/img/'; $ext = strtolower(substr($fileName, strrpos($fileName, '.') + 1)); if ($ext == 'docx' || $ext == 'pptx' || $ext == 'xlsx') { $ext = substr($ext, 0, 3); } // If the file type is an image file type if (preg_match(self::$imageRegex, $fileName)) { $ext = strtolower(substr($fileName, strrpos($fileName, '.') + 1)); if ($ext == 'ai' || $ext == 'eps' || $ext == 'pdf' || $ext == 'psd') { $newFileName = $fileName . '.jpg'; } else { $newFileName = $fileName; } // Get the icon or the image file, searching for the smallest image file $dirs = new Dir($dir, true); $fileSizes = array(); foreach ($dirs->getFiles() as $d) { if (is_dir($d)) { $f = $d . $newFileName; if (file_exists($f)) { $f = str_replace('\\', '//', $f); $fileSizes[filesize($f)] = substr($f, strpos($f, '/media') + 6); } } } // If image files are found, get smallest image file if (count($fileSizes) > 0) { ksort($fileSizes); $vals = array_values($fileSizes); $smallest = array_shift($vals); $fileIcon = '/media' . $smallest; // Else, use filetype icon } else { if (file_exists($iconDir . 'icons/50x50/' . $ext . '.png')) { $fileIcon = '/assets/img/icons/50x50/' . $ext . '.png'; // Else, use generic file icon } else { $fileIcon = '/assets/img/icons/50x50/img.png'; } } // Else, if file type is a file type with an available icon } else { if (file_exists($iconDir . 'icons/50x50/' . $ext . '.png')) { $fileIcon = '/assets/img/icons/50x50/' . $ext . '.png'; // Else, if file type is an audio file type with an available icon } else { if ($ext == 'wav' || $ext == 'aif' || $ext == 'aiff' || $ext == 'mp3' || $ext == 'mp2' || $ext == 'flac' || $ext == 'wma' || $ext == 'aac' || $ext == 'swa') { $fileIcon = '/assets/img/icons/50x50/aud.png'; // Else, if file type is an video file type with an available icon } else { if ($ext == '3gp' || $ext == 'asf' || $ext == 'avi' || $ext == 'mpg' || $ext == 'm4v' || $ext == 'mov' || $ext == 'mpeg' || $ext == 'wmv') { $fileIcon = '/assets/img/icons/50x50/vid.png'; // Else, if file type is a generic image file type with an available icon } else { if ($ext == 'bmp' || $ext == 'ico' || $ext == 'tiff' || $ext == 'tif') { $fileIcon = '/assets/img/icons/50x50/img.png'; // Else, use the generic file icon } else { $fileIcon = '/assets/img/icons/50x50/file.png'; } } } } } // Get file size if (file_exists($dir . DIRECTORY_SEPARATOR . $fileName)) { $fileSize = filesize($dir . DIRECTORY_SEPARATOR . $fileName); if ($fileSize > 999999) { $fileSize = round($fileSize / 1000000, 2) . ' MB'; } else { if ($fileSize > 999) { $fileSize = round($fileSize / 1000, 2) . ' KB'; } else { $fileSize = ' < 1 KB'; } } } else { $fileSize = ''; } return array('fileIcon' => $fileIcon, 'fileSize' => $fileSize); }
/** * Process themes method * * @param array $post * @return void */ public function processModules($post) { foreach ($post as $key => $value) { if (strpos($key, 'module_active_') !== false) { $id = substr($key, strrpos($key, '_') + 1); $ext = Table\Extensions::findById($id); if (isset($ext->id)) { $ext->active = (int) $value; $ext->save(); } } } $path = BASE_PATH . APP_URI; if ($path == '') { $path = '/'; } $modulePath1 = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules'; $modulePath2 = __DIR__ . '/../../../../../module'; $phireCookie = null; if (php_sapi_name() != 'cli') { $cookie = Cookie::getInstance(array('path' => $path)); if (isset($cookie->phire)) { if (null === $phireCookie) { $phireCookie = $cookie->phire; } } } if (isset($post['remove_modules'])) { foreach ($post['remove_modules'] as $id) { $ext = Table\Extensions::findById($id); if (isset($ext->id)) { $modPath = file_exists($modulePath1 . '/' . $ext->file) ? $modulePath1 : $modulePath2; $assets = unserialize($ext->assets); if (count($assets['tables']) > 0) { $db = Table\Extensions::getDb(); if (DB_INTERFACE == 'Mysqli' || DB_TYPE == 'mysql') { $db->adapter()->query('SET foreign_key_checks = 0;'); foreach ($assets['tables'] as $table) { $db->adapter()->query('DROP TABLE ' . $table); } $db->adapter()->query('SET foreign_key_checks = 1;'); } else { if (DB_INTERFACE == 'Pgsql' || DB_TYPE == 'pgsql') { foreach ($assets['tables'] as $table) { $db->adapter()->query('DROP TABLE ' . $table . ' CASCADE'); } } else { foreach ($assets['tables'] as $table) { $db->adapter()->query('DROP TABLE ' . $table); } } } } $contentPath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH; $exts = array('.zip', '.tar.gz', '.tar.bz2', '.tgz', '.tbz', '.tbz2'); // Check for a config and remove function if (file_exists($modPath . '/' . $ext->name . '/config') && file_exists($modPath . '/' . $ext->name . '/config/module.php')) { $config = (include $modPath . '/' . $ext->name . '/config/module.php'); if (null !== $config[$ext->name]->remove) { $removeFunc = $config[$ext->name]->remove; $removeFunc(); } } if (file_exists($contentPath . '/extensions/modules/' . $ext->name)) { $dir = new Dir($contentPath . '/extensions/modules/' . $ext->name); $dir->emptyDir(null, true); } foreach ($exts as $e) { if (file_exists($contentPath . '/extensions/modules/' . $ext->name . $e) && is_writable($contentPath . '/extensions/modules/' . $ext->name . $e)) { unlink($contentPath . '/extensions/modules/' . $ext->name . $e); } } if (file_exists(__DIR__ . '/../../../../../module/' . $ext->name)) { $dir = new Dir(__DIR__ . '/../../../../../module/' . $ext->name); $dir->emptyDir(null, true); } foreach ($exts as $e) { if (file_exists(__DIR__ . '/../../../../../module/' . $ext->name . $e) && is_writable(__DIR__ . '/../../../../../module/' . $ext->name . $e)) { unlink(__DIR__ . '/../../../../../module/' . $ext->name . $e); } } if (file_exists($contentPath . '/assets/' . strtolower($ext->name))) { $dir = new Dir($contentPath . '/assets/' . strtolower($ext->name)); $dir->emptyDir(null, true); } if (null !== $phireCookie) { foreach ($phireCookie->modules as $key => $value) { if ($value->name == $ext->name) { $modules = (array) $phireCookie->modules; unset($modules[$key]); $phireCookie->modules = $modules; } } } $ext->delete(); } } } if (null !== $phireCookie) { $cookie = Cookie::getInstance(array('path' => $path)); $cookie->set('phire', $phireCookie); } }
/** * Get available model objects * * @param \Pop\Config $config * @return array */ public static function getModels($config = null) { $models = array('0' => '----'); $exclude = array(); $override = null; // Get any exclude or override config values if (null !== $config) { $configAry = $config->asArray(); if (isset($configAry['exclude_models'])) { $exclude = $configAry['exclude_models']; } if (isset($configAry['override'])) { $override = $configAry['override']; } } // If override, set overridden models if (null !== $override) { foreach ($override as $model) { $models[$model] = $model; } // Else, get all modules from the system and module directories } else { $systemDirectory = new Dir(realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . '/vendor'), true); $sysModuleDirectory = new Dir(realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . APP_PATH . '/module'), true); $moduleDirectory = new Dir(realpath($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules'), true); $dirs = array_merge($systemDirectory->getFiles(), $sysModuleDirectory->getFiles(), $moduleDirectory->getFiles()); sort($dirs); // Dir clean up foreach ($dirs as $key => $dir) { unset($dirs[$key]); if (!(strpos($dir, 'PopPHPFramework') !== false || strpos($dir, 'config') !== false || strpos($dir, 'index.html') !== false)) { $k = $dir; if (substr($dir, -1) == DIRECTORY_SEPARATOR) { $k = substr($k, 0, -1); } $k = substr($k, strrpos($k, DIRECTORY_SEPARATOR) + 1); $dirs[$k] = $dir; } } // Loop through each directory, looking for model class files foreach ($dirs as $mod => $dir) { if (file_exists($dir . 'src/' . $mod . '/Model')) { $d = new Dir($dir . 'src/' . $mod . '/Model'); $dFiles = $d->getFiles(); sort($dFiles); foreach ($dFiles as $m) { if (substr($m, 0, 8) !== 'Abstract') { $model = str_replace('.php', '', $mod . '\\Model\\' . $m); $wildcardModel = '*' . substr($model, strpos($model, '\\')); if (!in_array($model, $exclude) && !in_array($wildcardModel, $exclude) && strpos($model, 'index.html') === false) { $models[$model] = strpos($model, '\\') !== false ? substr($model, strrpos($model, '\\') + 1) : $model; } } } } } } return $models; }