/** * Loading of base system configuration, creating of missing directories */ protected function construct() { if (!file_exists(CONFIG . '/main.json')) { error_code(500); Page::instance()->error(h::p('Config file not found, is system installed properly?') . h::a('How to install CleverStyle CMS', ['href' => 'https://github.com/nazar-pc/CleverStyle-CMS/wiki/Installation'])); exit; } $this->config = file_get_json_nocomments(CONFIG . '/main.json'); _include_once(CONFIG . '/main.php', false); defined('DEBUG') || define('DEBUG', false); define('DOMAIN', $this->config['domain']); date_default_timezone_set($this->config['timezone']); if ($clangs = Cache::instance()->{'languages/clangs'}) { if (is_array($clangs) && !empty($clangs)) { $clang = explode('/', trim($_SERVER['REQUEST_URI'], '/'), 2)[0]; if (in_array($clang, $clangs)) { $this->set('language', array_flip($clangs)[$clang]); define('FIXED_LANGUAGE', true); } unset($clang); } } unset($clangs); if (!is_dir(STORAGE)) { @mkdir(STORAGE, 0755); file_put_contents(STORAGE . '/.htaccess', 'Allow From All'); } if (!is_dir(CACHE)) { @mkdir(CACHE, 0700); } if (!is_dir(PCACHE)) { @mkdir(PCACHE, 0755); file_put_contents(PCACHE . '/.htaccess', '<FilesMatch "\\.(css|js)$"> Allow From All </FilesMatch> <ifModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 1 month" </ifModule> <ifModule mod_headers.c> Header set Cache-Control "max-age=2592000, public" </ifModule> AddEncoding gzip .js AddEncoding gzip .css '); } if (!is_dir(LOGS)) { @mkdir(LOGS, 0700); } if (!is_dir(TEMP)) { @mkdir(TEMP, 0755); file_put_contents(TEMP . '/.htaccess', 'Allow From All'); } if (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/json') === 0) { $_POST = _json_decode(@file_get_contents('php://input')) ?: []; $_REQUEST = array_merge($_REQUEST, $_POST); } elseif (in_array(strtolower($_SERVER['REQUEST_METHOD']), ['head', 'put', 'delete'])) { if (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'application/x-www-form-urlencoded') === 0) { @parse_str(file_get_contents('php://input'), $_POST); $_REQUEST = array_merge($_REQUEST, $_POST); } } $this->constructed = true; }
/** * Get dependencies of components between each other (only that contains some styles and scripts) and mapping styles and scripts to URL paths * * @param array $dependencies * @param array $includes_map */ protected function includes_dependencies_and_map(&$dependencies, &$includes_map) { /** * Get all includes */ $all_includes = $this->get_includes_list(true); $includes_map = []; $dependencies = []; $dependencies_aliases = []; /** * According to components's maps some files should be included only on specific pages. * Here we read this rules, and remove from whole includes list such items, that should be included only on specific pages. * Also collect dependencies. */ $Config = Config::instance(); foreach ($Config->components['modules'] as $module_name => $module_data) { if ($module_data['active'] == -1) { continue; } if (file_exists(MODULES . "/{$module_name}/meta.json")) { $meta = file_get_json_nocomments(MODULES . "/{$module_name}/meta.json"); if (isset($meta['require'])) { foreach ((array) $meta['require'] as $r) { preg_match('/([^=<>]+)/', $r, $r); $dependencies[$module_name][] = $r[0]; } unset($r); } if (isset($meta['optional'])) { foreach ((array) $meta['optional'] as $o) { $dependencies[$module_name][] = $o; } unset($o); } if (isset($meta['provide'])) { foreach ((array) $meta['provide'] as $p) { $dependencies_aliases[$p] = $module_name; } unset($p); } unset($meta); } if (!file_exists(MODULES . "/{$module_name}/includes/map.json")) { continue; } foreach (file_get_json_nocomments(MODULES . "/{$module_name}/includes/map.json") as $path => $files) { foreach ($files as $file) { $extension = explode('.', $file); $extension = array_pop($extension); $file = MODULES . "/{$module_name}/includes/{$extension}/{$file}"; $includes_map[$path][$extension][] = $file; $all_includes[$extension] = array_diff($all_includes[$extension], [$file]); } } unset($path, $files, $file); } unset($module_name, $module_data); foreach ($Config->components['plugins'] as $plugin_name) { if (file_exists(PLUGINS . "/{$plugin_name}/meta.json")) { $meta = file_get_json_nocomments(PLUGINS . "/{$plugin_name}/meta.json"); if (isset($meta['require'])) { foreach ((array) $meta['require'] as $r) { preg_match('/([^=<>]+)/', $r, $r); $dependencies[$plugin_name][] = $r[0]; } unset($r); } if (isset($meta['optional'])) { foreach ((array) $meta['optional'] as $o) { $dependencies[$plugin_name][] = $o; } unset($o); } if (isset($meta['provide'])) { foreach ((array) $meta['provide'] as $p) { $dependencies_aliases[$p] = $plugin_name; } unset($p); } unset($meta); } if (!file_exists(PLUGINS . "/{$plugin_name}/includes/map.json")) { continue; } foreach (file_get_json_nocomments(PLUGINS . "/{$plugin_name}/includes/map.json") as $path => $files) { foreach ($files as $file) { $extension = explode('.', $file); $extension = array_pop($extension); $file = PLUGINS . "/{$plugin_name}/includes/{$extension}/{$file}"; $includes_map[$path][$extension][] = $file; $all_includes[$extension] = array_diff($all_includes[$extension], [$file]); } } unset($path, $files, $file); } unset($plugin_name); /** * For consistency */ $includes_map['']['css'] = $all_includes['css']; $includes_map['']['js'] = $all_includes['js']; unset($all_includes); /** * Components can depend on each other - we need to find all dependencies and replace aliases by real names of components */ foreach ($dependencies as $component_name => &$depends_on) { foreach ($depends_on as $index => &$dependency) { if ($dependency == 'System') { continue; } if (isset($dependencies_aliases[$dependency])) { $dependency = $dependencies_aliases[$dependency]; } /** * If dependency have its own dependencies, that are nor present in current component - add them and mark, that it is necessary * to iterate through array again */ if (isset($dependencies[$dependency]) && $dependencies[$dependency] && array_diff($dependencies[$dependency], $depends_on)) { foreach (array_diff($dependencies[$dependency], $depends_on) as $new_dependency) { $depends_on[] = $new_dependency; } unset($new_dependency); } } if (empty($depends_on)) { unset($dependencies[$component_name]); } else { $depends_on = array_unique($depends_on); } } unset($dependencies_aliases, $component_name, $depends_on, $index, $dependency); /** * Clean dependencies without files */ foreach ($dependencies as &$depends_on) { foreach ($depends_on as $index => &$dependency) { if (!isset($includes_map[$dependency])) { unset($depends_on[$index]); } } } unset($depends_on, $index, $dependency); }
/** * Change language * * @param string $language * * @return bool */ function change($language) { static $changed_once = false; if ($this->fixed_language && $changed_once) { return false; } $changed_once = true; if ($language == $this->clanguage) { return true; } $Config = Config::instance(true); if (empty($language)) { if ($Config && $Config->core['multilingual']) { $language = $this->scan_aliases($Config->core['active_languages']) ?: $language; } } if (!$Config || $language == $Config->core['language'] || $Config->core['multilingual'] && in_array($language, $Config->core['active_languages'])) { $this->clanguage = $language; $return = false; $Cache = Cache::instance(); /** * If translations in cache */ if ($translate = $Cache->{"languages/{$this->clanguage}"}) { $this->set($translate); $return = true; /** * Otherwise check for system translations */ } elseif (file_exists(LANGUAGES . "/{$this->clanguage}.json")) { /** * Set system translations */ $this->set(file_get_json_nocomments(LANGUAGES . "/{$this->clanguage}.json")); $translate =& $this->translate; $translate['clanguage'] = $this->clanguage; if (!isset($translate['clang'])) { $translate['clang'] = mb_strtolower(mb_substr($this->clanguage, 0, 2)); } if (!isset($translate['clanguage_en'])) { $translate['clanguage_en'] = $this->clanguage; } if (!isset($translate['locale'])) { $translate['locale'] = $this->clang . '_' . strtoupper($this->clang); } /** * Set modules' translations */ foreach (get_files_list(MODULES, false, 'd') as $module) { if (file_exists(MODULES . "/{$module}/languages/{$this->clanguage}.json")) { $this->set(file_get_json_nocomments(MODULES . "/{$module}/languages/{$this->clanguage}.json") ?: []); } } unset($module); /** * Set plugins' translations */ foreach (get_files_list(PLUGINS, false, 'd') as $plugin) { if (file_exists(PLUGINS . "/{$plugin}/languages/{$this->clanguage}.json")) { $this->set(file_get_json_nocomments(PLUGINS . "/{$plugin}/languages/{$this->clanguage}.json") ?: []); } } unset($plugin); Trigger::instance()->run('System/general/languages/load', ['clanguage' => $this->clanguage, 'clang' => $this->clang, 'cregion' => $this->cregion, 'clanguage_en' => $this->clanguage_en]); $Cache->{"languages/{$this->clanguage}"} = $translate; $return = true; } _include(LANGUAGES . "/{$this->clanguage}.php", false, false); header("Content-Language: {$translate['content_language']}"); return $return; } return false; }