public function action_plugin_ui($plugin_id, $action) { if ($plugin_id == $this->plugin_id()) { switch ($action) { case _('Configure'): $ui = new FormUI(strtolower(get_class($this))); // present the user with a list of // URL shortening services $service = $ui->append('select', 'service', 'lilliputian__service', _t('The URL shortening service to use: ')); $services = array(); $services['internal'] = 'internal'; $list = Utils::glob(dirname(__FILE__) . '/*.helper.php'); if (count($list) > 0) { foreach ($list as $item) { $item = basename($item, '.helper.php'); $services[$item] = $item; } } $service->options = $services; if (Options::get('lilliputian__service') == 'internal') { $secret = $ui->append('text', 'secret', 'lilliputian__secret', _t('The secret word that must be passed when generating short URLs. May be blank to disable this security feature.')); } $ui->append('submit', 'save', _t('Save')); $ui->out(); break; } } }
/** * Returns all theme information -- dir, path, theme.xml, screenshot url * @return array An array of Theme data **/ public static function get_all_data() { if ( !isset( self::$all_data ) ) { foreach ( self::get_all() as $theme_dir => $theme_path ) { $themedata = array(); $themedata['dir'] = $theme_dir; $themedata['path'] = $theme_path; $themedata['theme_dir'] = $theme_path; $themedata['info'] = simplexml_load_file( $theme_path . '/theme.xml' ); if ( $themedata['info']->getName() != 'pluggable' || (string) $themedata['info']->attributes()->type != 'theme' ) { $themedata['screenshot'] = Site::get_url( 'admin_theme' ) . "/images/screenshot_default.png"; $themedata['info']->description = '<span class="error">' . _t( 'This theme is a legacy theme that is not compatible with Habari ' ) . Version::get_habariversion() . '. <br><br>Please update your theme.</span>'; $themedata['info']->license = ''; } else { foreach ( $themedata['info'] as $name=>$value ) { $themedata[$name] = (string) $value; } if ( $screenshot = Utils::glob( $theme_path . '/screenshot.{png,jpg,gif}', GLOB_BRACE ) ) { $themedata['screenshot'] = Site::get_url( 'habari' ) . dirname( str_replace( HABARI_PATH, '', $theme_path ) ) . '/' . basename( $theme_path ) . "/" . basename( reset( $screenshot ) ); } else { $themedata['screenshot'] = Site::get_url( 'admin_theme' ) . "/images/screenshot_default.png"; } } self::$all_data[$theme_dir] = $themedata; } } return self::$all_data; }
private static function get_color_schemes() { $color_scheme_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'color-schemes' . DIRECTORY_SEPARATOR; $files = Utils::glob($color_scheme_dir . '*.css'); $color_schemes = array(); foreach ($files as $color_scheme) { $name = basename($color_scheme, '.css'); $color_schemes[$name] = $name; } return $color_schemes; }
/** * Returns all theme information -- dir, path, theme.xml, screenshot url * @return array An array of Theme data **/ public static function get_all_data() { if (!isset(self::$all_data)) { $themedata = array(); foreach (self::get_all() as $theme_dir => $theme_path) { $themedata['dir'] = $theme_dir; $themedata['path'] = $theme_path; $themedata['info'] = simplexml_load_file($theme_path . '/theme.xml'); if ($screenshot = Utils::glob($theme_path . '/screenshot.{png,jpg,gif}', GLOB_BRACE)) { $themedata['screenshot'] = Site::get_url('habari') . dirname(str_replace(HABARI_PATH, '', $theme_path)) . '/' . basename($theme_path) . "/" . basename(reset($screenshot)); } else { $themedata['screenshot'] = Site::get_url('admin_theme') . "/images/screenshot_default.png"; } self::$all_data[$theme_dir] = $themedata; } } return self::$all_data; }
/** * function list_all * Gets a list of all plugin filenames that are available * @return array An array of filenames */ public static function list_all() { $plugins = array(); $plugindirs = array( HABARI_PATH . '/system/plugins/', HABARI_PATH . '/3rdparty/plugins/', HABARI_PATH . '/user/plugins/' ); if ( Site::CONFIG_LOCAL != Site::$config_type ) { // include site-specific plugins $plugindirs[] = Site::get_dir( 'config' ) . '/plugins/'; } $dirs = array(); foreach ( $plugindirs as $plugindir ) { if ( file_exists( $plugindir ) ) { $dirs = array_merge( $dirs, Utils::glob( $plugindir . '*', GLOB_ONLYDIR | GLOB_MARK ) ); } } foreach ( $dirs as $dir ) { $dirfiles = Utils::glob( $dir . '*.plugin.php' ); if ( ! empty( $dirfiles ) ) { $dirfiles = array_combine( // Use the basename of the file as the index to use the named plugin from the last directory in $dirs array_map( 'basename', $dirfiles ), // massage the filenames so that this works on Windows array_map( create_function( '$s', 'return str_replace(\'\\\\\', \'/\', $s);' ), $dirfiles ) ); $plugins = array_merge( $plugins, $dirfiles ); } $dirfiles = Utils::glob( $dir . '*.phar' ); foreach($dirfiles as $filename) { if(preg_match('/\.phar$/i', $filename)) { $d = new DirectoryIterator('phar://' . $filename); foreach ($d as $fileinfo) { if ($fileinfo->isFile() && preg_match('/\.plugin.php$/i', $fileinfo->getFilename())) { $plugins[$fileinfo->getFilename()] = str_replace('\\', '/', $fileinfo->getPathname()); } } } } } ksort( $plugins ); return $plugins; }
/** * get_dir returns a complete filesystem path to the requested item * 'config_file' returns the complete path to the config.php file, including the filename * 'config' returns the path of the directory containing config.php * 'user' returns the path of the user directory * 'theme' returns the path of the site's active theme * 'admin_theme' returns the path to the admin directory * 'vendor' returns the path to the vendor directory * @param string the name of the path item to return * @param bool whether to include a trailing slash. Default: No * @return string Path */ public static function get_dir($name, $trail = false) { $path = ''; switch (strtolower($name)) { case 'config_file': $path = Site::get_dir('config') . '/config.php'; break; case 'config': if (self::$config_path) { return self::$config_path; } self::$config_path = HABARI_PATH; $config_dirs = preg_replace('/^' . preg_quote(HABARI_PATH, '/') . '\\/user\\/sites\\/(.*)/', '$1', Utils::glob(HABARI_PATH . '/user/sites/*', GLOB_ONLYDIR)); if (empty($config_dirs)) { return self::$config_path; } $server = InputFilter::parse_url(Site::get_url('habari')); $request = array(); if (isset($server['port']) && $server['port'] != '' && $server['port'] != '80') { $request[] = $server['port']; } $request = array_merge($request, explode('.', $server['host'])); $basesegments = count($request); $request = array_merge($request, explode('/', trim($_SERVER['REQUEST_URI'], '/'))); $x = 0; do { $match = implode('.', $request); if (in_array($match, $config_dirs)) { self::$config_dir = $match; self::$config_path = HABARI_PATH . '/user/sites/' . self::$config_dir; self::$config_type = $basesegments > count($request) ? Site::CONFIG_SUBDOMAIN : Site::CONFIG_SUBDIR; self::$config_urldir = implode('/', array_slice($request, $basesegments)); break; } array_pop($request); $x--; if ($x < -10) { echo $x; var_dump($request); die('too many '); } } while (count($request) > 0); $path = self::$config_path; break; case 'user': if (Site::get_dir('config') == HABARI_PATH) { $path = HABARI_PATH . '/user'; } else { $path = Site::get_dir('config'); } break; case 'theme': $theme = Themes::get_theme_dir(); if (file_exists(Site::get_dir('config') . '/themes/' . $theme)) { $path = Site::get_dir('user') . '/themes/' . $theme; } elseif (file_exists(HABARI_PATH . '/user/themes/' . $theme)) { $path = HABARI_PATH . '/user/themes/' . $theme; } elseif (file_exists(HABARI_PATH . '/3rdparty/themes/' . $theme)) { $url = Site::get_url('habari') . '/3rdparty/themes/' . $theme; } else { $path = HABARI_PATH . '/system/themes/' . $theme; } break; case 'admin_theme': $path = HABARI_PATH . '/system/admin'; break; case 'vendor': $path = HABARI_PATH . '/system/vendor'; break; } $path .= Utils::trail($trail); $path = Plugins::filter('site_dir_' . $name, $path); return $path; }
/** * function list_all * Retrieves an array of the Habari locales that are installed * * @return array. An array of Habari locales in the installation **/ public static function list_all() { $localedirs = array(HABARI_PATH . '/system/locale/', HABARI_PATH . '/3rdparty/locale/', HABARI_PATH . '/user/locale/'); if (Site::CONFIG_LOCAL != Site::$config_type) { // include site-specific locales $localedirs[] = Site::get_dir('config') . '/locale/'; } $dirs = array(); foreach ($localedirs as $localedir) { if (file_exists($localedir)) { $dirs = array_merge($dirs, Utils::glob($localedir . '*', GLOB_ONLYDIR | GLOB_MARK)); } } $dirs = array_filter($dirs, create_function('$a', 'return file_exists($a . "LC_MESSAGES/habari.mo");')); $locales = array_map('basename', $dirs); ksort($locales); return $locales; }
/** * Updates the content of the database between versions. * Implemented in child classes. * * @param integer $old_version The old Version::DB_VERSION * @param string $upgrade_path * @return bool */ public function upgrade($old_version, $upgrade_path = '') { // Get all the upgrade files $upgrade_files = Utils::glob("{$upgrade_path}/*.sql"); // Put the upgrade files into an array using the 0-padded revision + '_0' as the key $upgrades = array(); foreach ($upgrade_files as $file) { if (intval(basename($file, '.sql')) > $old_version) { $upgrades[sprintf('%010s_0', basename($file, '.sql'))] = $file; } } // Put the upgrade functions into an array using the 0-padded revision + '_1' as the key $upgrade_functions = get_class_methods($this); foreach ($upgrade_functions as $fn) { if (preg_match('%^upgrade_([0-9]+)$%i', $fn, $matches)) { if (intval($matches[1]) > $old_version) { $upgrades[sprintf('%010s_1', $matches[1])] = array($this, $fn); } } } // Sort the upgrades by revision, ascending ksort($upgrades); // Execute all of the upgrade functions $result = true; foreach ($upgrades as $upgrade) { if (is_array($upgrade)) { /** @var Callable $upgrade */ $result &= $upgrade(); } else { $result &= $this->query_file($upgrade); } if (!$result) { break; } } return $result; }
/** * Handles get requests for the system information page. */ public function get_sysinfo() { $sysinfo = array(); $siteinfo = array(); // Assemble Site Info $siteinfo[_t('Habari Version')] = Version::get_habariversion(); if (Version::is_devel()) { $siteinfo[_t('Habari Version')] .= " r" . Version::get_svn_revision(); } $siteinfo[_t('Habari API Version')] = Version::get_apiversion(); $siteinfo[_t('Habari DB Version')] = Version::get_dbversion(); $siteinfo[_t('Active Theme')] = Options::get('theme_name'); $siteinfo[_t('Site Language')] = strlen(Options::get('system_locale')) ? Options::get('system_locale') : 'en-us'; $this->theme->siteinfo = $siteinfo; // Assemble System Info $sysinfo[_t('PHP Version')] = phpversion(); $sysinfo[_t('Server Software')] = $_SERVER['SERVER_SOFTWARE']; $sysinfo[_t('Database')] = DB::get_driver_name() . ' - ' . DB::get_driver_version(); $sysinfo[_t('PHP Extensions')] = implode(', ', get_loaded_extensions()); if (defined('PCRE_VERSION')) { $sysinfo[_t('PCRE Version')] = PCRE_VERSION; } else { // probably PHP < 5.2.4 ob_start(); phpinfo(8); $phpinfo = ob_get_contents(); ob_end_clean(); preg_match('/PCRE Library Version.*class="v">(.*)$/mi', $phpinfo, $matches); $sysinfo[_t('PCRE Version')] = $matches[1]; } $sysinfo[_t('Browser')] = $_SERVER['HTTP_USER_AGENT']; $this->theme->sysinfo = $sysinfo; // Assemble Class Info $classinfo = Utils::glob(HABARI_PATH . "/user/classes/*.php"); if (count($classinfo)) { $classinfo = array_map('realpath', $classinfo); } $this->theme->classinfo = $classinfo; // Assemble Plugin Info $raw_plugins = Plugins::get_active(); $plugins = array('system' => array(), 'user' => array(), '3rdparty' => array(), 'other' => array()); foreach ($raw_plugins as $plugin) { $file = $plugin->get_file(); if (preg_match('%[\\\\/](system|3rdparty|user)[\\\\/]plugins[\\\\/]%i', $file, $matches)) { // A plugin's info is XML, cast the element to a string. See #1026. $plugins[strtolower($matches[1])][(string) $plugin->info->name] = $file; } else { $plugins['other'][$plugin->info->name] = $file; } } $this->theme->plugins = $plugins; $this->display('sysinfo'); }
/** * function list_all * Gets a list of all plugin filenames that are available * @return array An array of filenames */ public static function list_all() { $plugins = array(); $plugindirs = array(HABARI_PATH . '/system/plugins/', HABARI_PATH . '/3rdparty/plugins/', HABARI_PATH . '/user/plugins/'); if (Site::CONFIG_LOCAL != Site::$config_type) { // include site-specific plugins $plugindirs[] = Site::get_dir('config') . '/plugins/'; } $dirs = array(); foreach ($plugindirs as $plugindir) { if (file_exists($plugindir)) { $dirs = array_merge($dirs, Utils::glob($plugindir . '*', GLOB_ONLYDIR | GLOB_MARK)); } } foreach ($dirs as $dir) { $dirfiles = Utils::glob($dir . '*.plugin.php'); if (!empty($dirfiles)) { $dirfiles = array_combine(array_map('basename', $dirfiles), array_map(function ($s) { return str_replace('\\', '/', $s); }, $dirfiles)); $plugins = array_merge($plugins, $dirfiles); } $dirfiles = Utils::glob($dir . '*.phar'); foreach ($dirfiles as $filename) { if (preg_match('/\\.phar$/i', $filename)) { $d = new DirectoryIterator('phar://' . $filename); foreach ($d as $fileinfo) { if ($fileinfo->isFile() && preg_match('/\\.plugin.php$/i', $fileinfo->getFilename())) { $plugins[$fileinfo->getFilename()] = str_replace('\\', '/', $fileinfo->getPathname()); } } } } } ksort($plugins); return $plugins; }
/** * Search directories for templates to use * Templates are always taken from the first directory they're found in. * To override this behavior, the template must be specifically added via ->add_template() * @see add_template * @param string|array $dirs A directory to look for templates in */ public function queue_dirs($dirs) { $dirs = Utils::single_array($dirs); $alltemplates = array(); // If multiple directories are passed, the earlier ones should override the later ones $dirs = array_reverse($dirs); foreach ($dirs as $dir) { $templates = Utils::glob(Utils::end_in_slash($dir) . '*.*'); $alltemplates = array_merge($alltemplates, $templates); } // Convert the template files into template names and produce a map from name => file $available_templates = array_map('basename', $alltemplates, array_fill(1, count($alltemplates), '.php')); $template_map = array_combine($available_templates, $alltemplates); $this->template_map = array_merge($this->template_map, $template_map); // Workaround for the 404 template key being merged into the 0 integer index unset($this->template_map[0]); if (isset($template_map[404])) { $this->template_map['404'] = $template_map[404]; } // The templates in the list should be uniquely identified array_unique($available_templates); // Filter the templates that are available $available_templates = Plugins::filter('available_templates', $available_templates, __CLASS__); $this->available_templates = array_merge($available_templates, $this->available_templates); }
/** * function list_all * Gets a list of all plugin filenames that are available * @return array An array of filenames */ public static function list_all() { $plugins = array(); $plugindirs = array(HABARI_PATH . '/system/plugins/', HABARI_PATH . '/3rdparty/plugins/', HABARI_PATH . '/user/plugins/'); if (Site::CONFIG_LOCAL != Site::$config_type) { // include site-specific plugins $plugindirs[] = Site::get_dir('config') . '/plugins/'; } $dirs = array(); foreach ($plugindirs as $plugindir) { if (file_exists($plugindir)) { $dirs = array_merge($dirs, Utils::glob($plugindir . '*', GLOB_ONLYDIR | GLOB_MARK)); } } foreach ($dirs as $dir) { $dirfiles = Utils::glob($dir . '*.plugin.php'); if (!empty($dirfiles)) { $dirfiles = array_combine(array_map('basename', $dirfiles), array_map(create_function('$s', 'return str_replace(\'\\\\\', \'/\', $s);'), $dirfiles)); $plugins = array_merge($plugins, $dirfiles); } } ksort($plugins); return $plugins; }
/** * Delete old logs */ private function cleaner() { $deletedFiles = 0; $deletedDirs = 0; /** * Clean the logs' directory according to configuration data */ $flush = Options::get('clickheat__flush'); if ($flush >= 0) { $logs = Utils::glob($this->logs . "/*/*.log"); $oldestDate = mktime(0, 0, 0, date('m'), date('d') - $flush, date('Y')); $deletedAll = array(); foreach ($logs as $log) { // dont process .htaccess if (count(explode('.', $log)) !== 2) { continue; } $dir = dirname($log); if (!isset($deletedAll[$dir])) { $deletedAll[$dir] = true; } if (filemtime($log) <= $oldestDate) { @unlink($log); $deletedFiles++; continue; } $deletedAll[$dir] = false; } /** * If every log file (but the url.txt) has been deleted, * then we should delete the directory too */ foreach ($deletedAll as $dir => $do) { if ($do === true) { @unlink($dir . '/url.xml'); $deletedFiles++; @rmdir($dir); $deletedDirs++; } } } /** * Clean the cache directory for every file older than 2 minutes * 2 minutes is to make sure images are kept up to date with all * new clicks. */ $glob = Utils::glob($this->cache . "/*"); foreach ($glob as $file) { $ext = explode('.', $file); // dont process .htaccess if (count($ext) !== 2) { continue; } $filemtime = filemtime($file); switch ($ext[1]) { case 'html': case 'png': case 'png_temp': case 'png_log': if ($filemtime + 86400 < time()) { @unlink($file); $deletedFiles++; } } } // Did we delete anything? if ($deletedDirs + $deletedFiles === 0) { return 'OK'; } else { return sprintf(_t("Cleaning finished: %d files and %d directories have been deleted"), $deletedFiles, $deletedDirs); } }
/** * Load in all available reports * @return array array of available report files **/ private function load_available_reports() { $valid_reports = array(); $reports = array(); $templates = array(); $searchdirs = array(dirname(__FILE__) . '/default', dirname(__FILE__) . '/custom'); $dirs = array(); foreach ($searchdirs as $searchdir) { if (file_exists($searchdir)) { $dirs = array_merge($dirs, Utils::glob($searchdir . '*', GLOB_ONLYDIR | GLOB_MARK)); } } // Load in the available reports and templates foreach ($dirs as $dir) { $reportfiles = Utils::glob($dir . '*.xml'); $templatefiles = Utils::glob($dir . '*.php'); if (!empty($reportfiles)) { $reportfiles = array_combine(array_map('basename', $reportfiles), array_map(create_function('$s', 'return str_replace(\'\\\\\', \'/\', $s);'), $reportfiles)); $reports = array_merge($reports, $reportfiles); } if (!empty($templatefiles)) { $templatefiles = array_combine(array_map('basename', $templatefiles), array_map(create_function('$s', 'return str_replace(\'\\\\\', \'/\', $s);'), $templatefiles)); $templates = array_merge($templates, $templatefiles); } } // Now go through and generate our valid_reports() foreach ($reports as $rptKey => $rptVal) { $tmpKey = array_search(str_replace('.xml', '.php', $rptVal), $templates); if ($tmpKey) { $key = basename($rptVal, ".xml"); $valid_reports[$key] = array('xml' => $rptVal, 'template' => $templates[$tmpKey]); } } ksort($valid_reports); return $valid_reports; }
/** * Retreive the Theme used to display the form component * * @param boolean $forvalidation If true, perform validation on control and add error messages to output * @param FormControl $control The control to output using a template * @return Theme The theme object to display the template for the control */ function get_theme($forvalidation = false, $control = null) { if (!isset($this->theme_obj)) { $theme_dir = Plugins::filter('control_theme_dir', Plugins::filter('admin_theme_dir', Site::get_dir('admin_theme', true)) . 'formcontrols/', $control); $this->theme_obj = Themes::create(); // Create the current theme instead of: 'admin', 'RawPHPEngine', $theme_dir // Add the templates for the form controls tothe current theme, // and allow any matching templates from the current theme to override $formcontrol_templates = Utils::glob($theme_dir . '*.php'); foreach ($formcontrol_templates as $template) { $template_name = basename($template, '.php'); $this->theme_obj->add_template($template_name, $template); } $list = array(); $list = Plugins::filter('available_templates', $list); foreach ($list as $template_name) { if ($template = Plugins::filter('include_template_file', null, $template_name)) { $this->theme_obj->add_template($template_name, $template); } } } $this->theme_obj->start_buffer(); if ($control instanceof FormControl) { // PHP doesn't allow __get() to return pointers, and passing this array to foreach directly generates an error. $properties = $control->properties; foreach ($properties as $name => $value) { $this->theme_obj->{$name} = $value; } $this->theme_obj->field = $control->field; $this->theme_obj->value = $control->value; $this->theme_obj->caption = $control->caption; $this->theme_obj->id = (string) $control->id; $class = (array) $control->class; $message = ''; if ($forvalidation) { $validate = $control->validate(); if (count($validate) != 0) { $class[] = 'invalid'; $message = implode('<br>', (array) $validate); } } $this->theme_obj->class = implode(' ', (array) $class); $this->theme_obj->message = $message; } return $this->theme_obj; }
/** * get_dir returns a complete filesystem path to the requested item * 'config_file' returns the complete path to the config.php file, including the filename * 'config' returns the path of the directory containing config.php * 'user' returns the path of the user directory * 'theme' returns the path of the site's active theme * 'admin_theme' returns the path to the admin directory * 'vendor' returns the path to the vendor directory * @param string $name the name of the path item to return * @param bool|string $trail whether to include a trailing slash, or a string to use as the trailing value. Default: false * @return string Path */ public static function get_dir($name, $trail = false) { $path = ''; switch (strtolower($name)) { case 'config_file': $path = Site::get_dir('config') . '/config.php'; break; case 'config': if (self::$config_path) { return self::$config_path; } self::$config_path = HABARI_PATH; $config_dirs = preg_replace('/^' . preg_quote(HABARI_PATH, '/') . '\\/user\\/sites\\/(.*)/', '$1', Utils::glob(HABARI_PATH . '/user/sites/*', GLOB_ONLYDIR)); if (empty($config_dirs)) { return self::$config_path; } // Collect host parts $server = InputFilter::parse_url(Site::get_url('habari')); $request = array(); if (isset($server['port']) && $server['port'] != '' && $server['port'] != '80') { $request[] = $server['port']; } $request = array_merge($request, explode('.', $server['host'])); // Collect the subdirectory(s) the core is located in to determine the base path later // Don't add them to $request, they will be added with $_SERVER['REQUEST_URI'] $basesegments = count($request); if (!empty($server['path'])) { $coresubdir = explode('/', trim($server['path'], '/')); $basesegments += count($coresubdir); } // Preserve $request without directories for fallback $request_base = $request; // Add directory part if (!empty(trim($_SERVER['REQUEST_URI'], '/'))) { $subdirectories = explode('/', trim($_SERVER['REQUEST_URI'], '/')); $request = array_merge($request, $subdirectories); } // Now cut parts until we found a matching site directory $cuts = 0; $dir_cuts = 0; do { $match = implode('.', $request); if (in_array($match, $config_dirs)) { self::$config_dir = $match; self::$config_path = HABARI_PATH . '/user/sites/' . self::$config_dir; self::$config_type = isset($subdirectories) && array_intersect($subdirectories, $request) == $subdirectories ? Site::CONFIG_SUBDIR : Site::CONFIG_SUBDOMAIN; self::$config_urldir = implode('/', array_slice($request, $basesegments + $cuts)); break; } // Cut a part from the beginning array_shift($request); $cuts--; // Do not fallback to only the directory part, instead, cut one and start over if ($basesegments + $cuts <= 0 && isset($subdirectories) && $dir_cuts < count($subdirectories)) { $cuts = 0; $dir_cuts++; $request = array_merge($request_base, array_slice($subdirectories, 0, count($subdirectories) - $dir_cuts)); } // What does the following check do?! if ($cuts < -10) { echo $cuts; var_dump($request); die('too many '); } } while (count($request) > 0); $path = self::$config_path; break; case 'user': if (Site::get_dir('config') == HABARI_PATH) { $path = HABARI_PATH . '/user'; } else { $path = Site::get_dir('config'); } break; case 'theme': $theme = Themes::get_theme_dir(); if (file_exists(Site::get_dir('config') . '/themes/' . $theme)) { $path = Site::get_dir('user') . '/themes/' . $theme; } elseif (file_exists(HABARI_PATH . '/user/themes/' . $theme)) { $path = HABARI_PATH . '/user/themes/' . $theme; } elseif (file_exists(HABARI_PATH . '/3rdparty/themes/' . $theme)) { $path = Site::get_dir('habari') . '/3rdparty/themes/' . $theme; } else { $path = HABARI_PATH . '/system/themes/' . $theme; } break; case 'admin_theme': $path = HABARI_PATH . '/system/admin'; break; case 'vendor': $path = HABARI_PATH . '/system/vendor'; break; } $path .= Utils::trail($trail); $path = Plugins::filter('site_dir_' . $name, $path); return $path; }
/** * Load and return a list of all assets in the current theme chain's /assets/ directory * @param bool $refresh If True, clear and reload all assets * @return array An array of URLs of assets in the assets directories of the active theme chain */ public function load_assets($refresh = false) { static $assets = null; if (is_null($assets) || $refresh) { $themedirs = $this->theme_dir; $assets = array('css' => array(), 'js' => array()); foreach ($themedirs as $dir) { if (file_exists(Utils::end_in_slash($dir) . 'assets')) { $theme_assets = Utils::glob(Utils::end_in_slash($dir) . 'assets/*.*'); foreach ($theme_assets as $asset) { $extension = strtolower(substr($asset, strrpos($asset, '.') + 1)); $assets[$extension][basename($asset)] = $this->dir_to_url($asset); } } } } return $assets; }
/** * Handles get requests for the system information page. */ public function get_sysinfo() { $sysinfo = array(); $siteinfo = array(); // Assemble Site Info $siteinfo[_t('Habari Version')] = Version::get_habariversion(); if (Version::is_devel()) { $siteinfo[_t('Habari Version')] .= " " . Version::get_git_short_hash(); } $siteinfo[_t('Habari API Version')] = Version::get_apiversion(); $siteinfo[_t('Habari DB Version')] = Version::get_dbversion(); $siteinfo[_t('Active Theme')] = Options::get('theme_name'); $siteinfo[_t('System Locale')] = HabariLocale::get(); $siteinfo[_t('Cache Class')] = Cache::get_class(); $this->theme->siteinfo = $siteinfo; // Assemble System Info $sysinfo[_t('PHP Version')] = phpversion(); $sysinfo[_t('Server Software')] = $_SERVER['SERVER_SOFTWARE']; $sysinfo[_t('Database')] = DB::get_driver_name() . ' - ' . DB::get_driver_version(); $sysinfo[_t('PHP Extensions')] = implode(', ', get_loaded_extensions()); $sysinfo[_t('PHP Configuration Settings')] = implode("<br>", Utils::get_ini_settings()); if (defined('PCRE_VERSION')) { $sysinfo[_t('PCRE Version')] = PCRE_VERSION; } else { // probably PHP < 5.2.4 ob_start(); phpinfo(8); $phpinfo = ob_get_contents(); ob_end_clean(); preg_match('/PCRE Library Version.*class="v">(.*)$/mi', $phpinfo, $matches); $sysinfo[_t('PCRE Version')] = $matches[1]; } $sysinfo[_t('Browser')] = $_SERVER['HTTP_USER_AGENT']; $this->theme->sysinfo = $sysinfo; // Assemble Class Info $classinfo = Utils::glob(HABARI_PATH . "/user/classes/*.php"); if (count($classinfo)) { $classinfo = array_map('realpath', $classinfo); } $this->theme->classinfo = $classinfo; // Assemble Plugin Info $raw_plugins = Plugins::get_active(); $plugins = array('system' => array(), 'user' => array(), '3rdparty' => array(), 'other' => array()); foreach ($raw_plugins as $plugin) { $file = $plugin->get_file(); // Catch plugins that are symlinked from other locations as ReflectionClass->getFileName() only returns the ultimate file path, not the symlink path, and we really want the symlink path $all_plugins = Plugins::list_all(); $filename = basename($file); if (array_key_exists($filename, $all_plugins) && $all_plugins[$filename] != $file) { $file = $all_plugins[$filename]; } if (preg_match('%[\\\\/](system|3rdparty|user)[\\\\/]plugins[\\\\/]%i', $file, $matches)) { // A plugin's info is XML, cast the element to a string. See #1026. $plugins[strtolower($matches[1])][(string) $plugin->info->name] = $file; } else { // A plugin's info is XML, cast the element to a string. $plugins['other'][(string) $plugin->info->name] = $file; } } $this->theme->plugins = $plugins; $this->theme->admin_page = _t('System Information'); $this->display('sysinfo'); }
/** * get_dir returns a complete filesystem path to the requested item * 'config_file' returns the complete path to the config.php file, including the filename * 'config' returns the path of the directory containing config.php * 'user' returns the path of the user directory * 'theme' returns the path of the site's active theme * @param string the name of the path item to return * @param bool whether to include a trailing slash. Default: No * @return string Path */ public static function get_dir( $name, $trail = false ) { $path = ''; switch ( strtolower( $name ) ) { case 'config_file': $path = Site::get_dir( 'config' ) . '/config.php'; break; case 'config': if ( self::$config_path ) { return self::$config_path; } self::$config_path = HABARI_PATH; $config_dirs = preg_replace( '/^' . preg_quote( HABARI_PATH, '/' ) . '\/user\/sites\/(.*)\/config.php/', '$1', Utils::glob( HABARI_PATH . '/user/sites/*/config.php' ) ); if ( empty( $config_dirs ) ) { return self::$config_path; } $server = InputFilter::parse_url( Site::get_url( 'habari' ) ); $server = ( isset( $server['port'] ) ) ? $server['port'] . '.' . $server['host'] . '.' : $server['host'] . '.'; $request = explode( '/', trim( $_SERVER['REQUEST_URI'], '/' ) ); $match = trim( $server, '.' ); $x = count( $request ); do { if ( in_array( $match, $config_dirs ) ) { self::$config_dir = $match; self::$config_path = HABARI_PATH . '/user/sites/' . self::$config_dir; self::$config_type = ( $x > 0 ) ? Site::CONFIG_SUBDOMAIN : Site::CONFIG_SUBDIR; break; } $match = MultiByte::substr( $match, MultiByte::strpos( $match, '.' ) + 1 ); $x--; } while ( MultiByte::strpos( $match, '.' ) !== false ); $path = self::$config_path; break; case 'user': if ( Site::get_dir( 'config' ) == HABARI_PATH ) { $path = HABARI_PATH . '/user'; } else { $path = Site::get_dir( 'config' ); } break; case 'theme': $theme = Themes::get_theme_dir(); if ( file_exists( Site::get_dir( 'config' ) . '/themes/' . $theme ) ) { $path = Site::get_dir( 'user' ) . '/themes/' . $theme; } elseif ( file_exists( HABARI_PATH . '/user/themes/' . $theme ) ) { $path = HABARI_PATH . '/user/themes/' . $theme; } elseif ( file_exists( HABARI_PATH . '/3rdparty/themes/' . $theme ) ) { $url = Site::get_url( 'habari' ) . '/3rdparty/themes/' . $theme; } else { $path = HABARI_PATH . '/system/themes/' . $theme; } break; case 'admin_theme': $path = HABARI_PATH . '/system/admin'; break; case 'vendor': $path = HABARI_PATH . '/system/vendor'; break; } $path .= Utils::trail( $trail ); $path = Plugins::filter( 'site_dir_' . $name, $path ); return $path; }
/** * Gathers information about the system in order to make sure * requirements for install are met * * @returns bool are all requirements met? */ private function meets_all_requirements() { // Required extensions, this list will augment with time // Even if they are enabled by default, it seems some install turn them off // We use the URL in the Installer template to link to the installation page $required_extensions = array('date' => 'http://php.net/datetime', 'pdo' => 'http://php.net/pdo', 'hash' => 'http://php.net/hash', 'json' => 'http://php.net/json', 'mbstring' => 'http://php.net/mbstring', 'pcre' => 'http://php.net/pcre', 'session' => 'http://php.net/session', 'simplexml' => 'http://php.net/simplexml', 'spl' => 'http://php.net/spl', 'tokenizer' => 'http://php.net/tokenizer'); $requirements_met = true; /* Check versions of PHP */ $php_version_ok = version_compare(phpversion(), MIN_PHP_VERSION, '>='); $this->theme->assign('php_version_ok', $php_version_ok); $this->theme->assign('PHP_OS', PHP_OS); $this->theme->assign('PHP_VERSION', phpversion()); if (!$php_version_ok) { $requirements_met = false; } /* Check for mod_rewrite on Apache */ $mod_rewrite = true; if (function_exists('apache_get_modules') && !in_array('mod_rewrite', apache_get_modules())) { $requirements_met = false; $mod_rewrite = false; } $this->theme->assign('mod_rewrite', $mod_rewrite); /* Check for required extensions */ $missing_extensions = array(); foreach ($required_extensions as $ext_name => $ext_url) { if (!extension_loaded($ext_name)) { $missing_extensions[$ext_name] = $ext_url; $requirements_met = false; } } $this->theme->assign('missing_extensions', $missing_extensions); if (extension_loaded('pdo')) { /* Check for PDO drivers */ $pdo_drivers = \PDO::getAvailableDrivers(); if (!empty($pdo_drivers)) { $pdo_drivers = array_combine($pdo_drivers, $pdo_drivers); // Include only those drivers that we include database support for $pdo_schemas = array_map('basename', Utils::glob(HABARI_PATH . '/system/schema/*', GLOB_ONLYDIR)); $pdo_schemas = array_combine($pdo_schemas, $pdo_schemas); $pdo_drivers = array_intersect_key($pdo_drivers, $pdo_schemas); $pdo_missing_drivers = array_diff($pdo_schemas, $pdo_drivers); $pdo_drivers_ok = count($pdo_drivers); $this->theme->assign('pdo_drivers_ok', $pdo_drivers_ok); $this->theme->assign('pdo_drivers', $pdo_drivers); $this->theme->assign('pdo_missing_drivers', $pdo_missing_drivers); } else { $pdo_drivers_ok = false; $this->theme->assign('pdo_drivers_ok', $pdo_drivers_ok); } if (!$pdo_drivers_ok) { $requirements_met = false; } } else { $this->theme->assign('pdo_drivers_ok', false); $this->theme->assign('pdo_drivers', array()); $requirements_met = false; } if ($requirements_met && !@preg_match('/\\p{L}/u', 'a')) { $requirements_met = false; } /** * $local_writable is used in the template, but never set in Habari * Won't remove the template code since it looks like it should be there * * This will only meet the requirement so there's no "undefined variable" exception */ $this->theme->assign('local_writable', true); return $requirements_met; }
/** * Replacement for system glob that returns an empty array if there are no results * * @param string $pattern The glob() file search pattern * @param integer $flags Standard glob() flags * @return array An array of result files, or an empty array if no results found */ public static function glob($pattern, $flags = 0) { if (!defined('GLOB_NOBRACE') || !(($flags & GLOB_BRACE) == GLOB_BRACE)) { // this platform supports GLOB_BRACE out of the box or GLOB_BRACE wasn't requested $results = glob($pattern, $flags); } elseif (!preg_match_all('/\\{.*?\\}/', $pattern, $m)) { // GLOB_BRACE used, but this pattern doesn't even use braces $results = glob($pattern, $flags ^ GLOB_BRACE); } else { // pattern uses braces, but platform doesn't support GLOB_BRACE $braces = array(); foreach ($m[0] as $raw_brace) { $braces[preg_quote($raw_brace)] = '(?:' . str_replace(',', '|', preg_quote(substr($raw_brace, 1, -1), '/')) . ')'; } $new_pattern = preg_replace('/\\{.*?\\}/', '*', $pattern); $pattern = preg_quote($pattern, '/'); $pattern = str_replace('\\*', '.*', $pattern); $pattern = str_replace('\\?', '.', $pattern); $regex = '/' . str_replace(array_keys($braces), array_values($braces), $pattern) . '/'; $results = preg_grep($regex, Utils::glob($new_pattern, $flags ^ GLOB_BRACE)); } if ($results === false) { $results = array(); } return $results; }
/** * Remove all cache files */ protected function _purge() { Plugins::act('cache_purge_before'); $glob = Utils::glob(FILE_CACHE_LOCATION . '*.data'); foreach ($glob as $file) { unlink($file); } $glob = Utils::glob(FILE_CACHE_LOCATION . '*.cache'); foreach ($glob as $file) { unlink($file); } Plugins::act('cache_purge_after'); }
/** * Returns the existance of the specified template name * * @param string $template Name of template to detect * @returns boolean True if the template exists, false if not */ public function template_exists($template) { if (!$this->loaded_templates) { if (!is_array($this->template_dir)) { $this->template_dir = array($this->template_dir); } $alltemplates = array(); $dirs = array_reverse($this->template_dir); foreach ($dirs as $dir) { $templates = Utils::glob($dir . '*.*'); $alltemplates = array_merge($alltemplates, $templates); } $available_templates = array_map('basename', $alltemplates, array_fill(1, count($alltemplates), '.php')); $template_map = array_combine($available_templates, $alltemplates); $this->template_map = array_merge($this->template_map, $template_map); // Workaround for the 404 template key being merged into the 0 integer index unset($this->template_map[0]); if (isset($template_map[404])) { $this->template_map['404'] = $template_map[404]; } array_unique($available_templates); $available_templates = Plugins::filter('available_templates', $available_templates, __CLASS__); $this->available_templates = array_merge($this->available_templates, $available_templates); $this->loaded_templates = true; } return in_array($template, $this->available_templates); }
/** * Get the file from the specified path * @param string $path The path of the file to retrieve * @param array $qualities Qualities that specify the version of the file to retrieve. * @return MediaAsset The requested asset **/ public function silo_get( $path, $qualities = null ) { if ( ! isset( $this->root ) ) { return false; } $path = preg_replace('%\.{2,}%', '.', $path); $file = basename( $path ); $props = array( 'title' => basename( $path ), ); $realfile = $this->root . '/' . $path; $thumbnail_suffix = HabariSilo::DERIV_DIR . '/' . $file . '.thumbnail.jpg'; $thumbnail_url = $this->url . '/' . dirname($path) . (dirname($path) == '' ? '' : '/') . $thumbnail_suffix; $mimetype = preg_replace('%[^a-z_0-9]%', '_', Utils::mimetype($realfile)); $mtime = ''; if ( !file_exists( dirname( $realfile ) . '/' . $thumbnail_suffix ) ) { switch (strtolower(substr($realfile, strrpos($realfile, '.') + 1))) { case 'jpg': case 'png': case 'gif': if ( !$this->create_thumbnail( $realfile ) ) { // there is no thumbnail so use icon based on mimetype. $icon_path = Plugins::filter( 'habarisilo_icon_base_path', dirname($this->get_file()) . '/icons' ); $icon_url = Plugins::filter( 'habarisilo_icon_base_url', $this->get_url() . '/icons' ); if ( ( $icons = Utils::glob($icon_path . '/*.{png,jpg,gif,svg}', GLOB_BRACE) ) && $mimetype ) { $icon_keys = array_map( create_function('$a', 'return pathinfo($a, PATHINFO_FILENAME);'), $icons ); $icons = array_combine($icon_keys, $icons); $icon_filter = create_function('$a, $b', "\$mime = '$mimetype';".'return (((strpos($mime, $a)===0) ? (strlen($a) / strlen($mime)) : 0) >= (((strpos($mime, $b)===0)) ? (strlen($b) / strlen($mime)) : 0)) ? $a : $b;'); $icon_key = array_reduce($icon_keys, $icon_filter); if ($icon_key) { $icon = basename($icons[$icon_key]); $thumbnail_url = $icon_url .'/'. $icon; } else { // couldn't find an icon so use default $thumbnail_url = $icon_url .'/default.png'; } } } break; } } // If the asset is an image, obtain the image dimensions if ( in_array( $mimetype, array( 'image_jpeg', 'image_png', 'image_gif' ) ) ) { list( $props['width'], $props['height'] ) = getimagesize( $realfile ); $mtime = '?' . filemtime( $realfile ); } $props = array_merge( $props, array( 'url' => $this->url . '/' . dirname($path) . ($path == '' ? '' : '/') . $file, 'thumbnail_url' => $thumbnail_url . $mtime, 'filetype' => $mimetype, ) ); $asset = new MediaAsset( self::SILO_NAME . '/' . $path, false, $props ); if ( file_exists( $realfile ) && is_file( $realfile ) ) { $asset->content = file_get_contents( $realfile ); } return $asset; }
/** * Returns the existance of the specified template name * * @param template $template Name of template to detect * @returns boolean True if the template exists, false if not */ public function template_exists($template) { if (empty($this->available_templates)) { if (!is_array($this->template_dir)) { $this->template_dir = array($this->template_dir); } $alltemplates = array(); $dirs = array_reverse($this->template_dir); foreach ($dirs as $dir) { $templates = Utils::glob($dir . '*.*'); $alltemplates = array_merge($alltemplates, $templates); } $this->available_templates = array_map('basename', $alltemplates, array_fill(1, count($alltemplates), '.tpl')); $this->template_map = array_combine($this->available_templates, $alltemplates); array_unique($this->available_templates); $this->available_templates = Plugins::filter('available_templates', $this->available_templates, __CLASS__); } return in_array($template, $this->available_templates); }
/** * Return directory contents for the silo path * @param string $path The path to retrieve the contents of * @return array An array of MediaAssets describing the contents of the directory **/ public function silo_dir($path) { if (!isset($this->root)) { return array(); } $path = preg_replace('%\\.{2,}%', '.', $path); $results = array(); $dir = Utils::glob($this->root . ($path == '' ? '' : '/') . $path . '/*'); foreach ($dir as $item) { if (substr(basename($item), 0, 1) == '.') { continue; } if (basename($item) == 'desktop.ini') { continue; } $file = basename($item); $props = array('title' => basename($item)); if (!is_dir($item)) { $thumbnail_suffix = HabariSilo::DERIV_DIR . '/' . $file . '.thumbnail.jpg'; $thumbnail_url = $this->url . '/' . $path . ($path == '' ? '' : '/') . $thumbnail_suffix; $mimetype = preg_replace('%[^a-z_0-9]%', '_', Utils::mimetype($item)); $mtime = ''; if (!file_exists(dirname($item) . '/' . $thumbnail_suffix)) { if (!$this->create_thumbnail($item)) { // there is no thumbnail so use icon based on mimetype. $icon_path = Plugins::filter('habarisilo_icon_base_path', dirname($this->get_file()) . '/icons'); $icon_url = Plugins::filter('habarisilo_icon_base_url', $this->get_url() . '/icons'); if (($icons = Utils::glob($icon_path . '/*.{png,jpg,gif,svg}', GLOB_BRACE)) && $mimetype) { $icon_keys = array_map(create_function('$a', 'return pathinfo($a, PATHINFO_FILENAME);'), $icons); $icons = array_combine($icon_keys, $icons); $icon_filter = create_function('$a, $b', "\$mime = '{$mimetype}';" . 'return (((strpos($mime, $a)===0) ? (strlen($a) / strlen($mime)) : 0) >= (((strpos($mime, $b)===0)) ? (strlen($b) / strlen($mime)) : 0)) ? $a : $b;'); $icon_key = array_reduce($icon_keys, $icon_filter); if ($icon_key) { $icon = basename($icons[$icon_key]); $thumbnail_url = $icon_url . '/' . $icon; } else { // couldn't find an icon so use default $thumbnail_url = $icon_url . '/default.png'; } } } } // If the asset is an image, obtain the image dimensions if (in_array($mimetype, array('image_jpeg', 'image_png', 'image_gif'))) { list($props['width'], $props['height']) = getimagesize($item); $mtime = '?' . filemtime($item); } $props = array_merge($props, array('url' => $this->url . '/' . $path . ($path == '' ? '' : '/') . $file, 'thumbnail_url' => $thumbnail_url . $mtime, 'filetype' => $mimetype)); } $results[] = new MediaAsset(self::SILO_NAME . '/' . $path . ($path == '' ? '' : '/') . basename($item), is_dir($item), $props); } //print_r($results); return $results; }