/** * Execute the Command * * @access protected * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { // Fetch paths for Applications $applications = \Skeleton\Core\Application::get_all(); // If the paths array hasn't been defined yet, make sure it exists if (!isset($paths) or !is_array($paths)) { $paths = []; } foreach ($applications as $application) { $paths[$application->name] = $application->path; } // Fetch additional paths to translate foreach (\Skeleton\I18n\Config::$additional_template_paths as $name => $path) { $paths[$name] = $path; } // Translate all the applications foreach ($paths as $application => $directory) { $log = $this->translate_application($application, $directory); $output->writeln($log); } $packages = \Skeleton\Core\Package::get_all(); foreach ($packages as $package) { $log = $this->translate_skeleton_package($package); $output->writeln($log); } return 0; }
/** * Show a snippet */ function smarty_function_snippet($params, &$smarty) { /** * Supported parameters: * - source (the source file in WEB_PATH . /snippet/ * - * (will be converted to <snippet_name>_* */ $template = new \Skeleton\Template\Template(); if (empty($params['source'])) { throw new Exception('source required for snippet'); } else { $source = $params['source']; $var_identifier = substr($source, 0, -4); } foreach ($smarty->getTemplateVars() as $key => $value) { $template->assign($key, $value); } foreach ($params as $key => $value) { $template->assign($var_identifier . '_' . $key, $value); } foreach ($params as $key => $value) { $template->assign($key, $value); } $template->set_template_directory(\Skeleton\Core\Application::Get()->template_path); $content = $template->render('../snippet/' . $source); return $content; }
/** * Display method * * @access public */ public function display() { $application = \Skeleton\Core\Application::get(); $module_path = $application->module_path; $files = $this->recursive_scan($module_path); $modules = []; foreach ($files as $file) { require_once $file; $module_name = str_replace($module_path, '', $file); $module_name = str_replace('.php', '', $module_name); if ($module_name[0] == '/') { $module_name = substr($module_name, 1); } $module_name = str_replace('/', '_', $module_name); $classname = '\\Web_Module_' . $module_name; if (class_exists($classname)) { $module = new $classname(); if (is_a($module, '\\Skeleton\\Package\\Api\\Web\\Module\\Call')) { $modules[] = $module; } } } $template = \Skeleton\Core\Web\Template::get(); $template->assign('modules', $modules); }
/** * Get function, returns a Config object * * @return Config * @access public */ public static function Get() { if (!isset(self::$config)) { try { self::$config = \Skeleton\Core\Application::Get()->config; } catch (Exception $e) { return new Config(); } } return self::$config; }
/** * Can this handler run? * * @return bool */ public function can_run() { // This handler can only run when we have an application defined if (class_exists('\\Skeleton\\Core\\Application') === false) { return false; } try { \Skeleton\Core\Application::get(); return true; } catch (\Exception $e) { } return false; }
/** * Constructor */ public function __construct() { $this->template = new \Skeleton\Template\Template(); $application = \Skeleton\Core\Application::Get(); if (file_exists($application->template_path)) { $this->template->add_template_directory($application->template_path, $application->name); } $packages = \Skeleton\Core\Package::get_all(); foreach ($packages as $package) { if (file_exists($package->template_path)) { $this->template->add_template_directory($package->template_path, $package->name); } } }
/** * Produce a subject based on the error * * @param \Exception $exception * @return string */ public static function get_subject(\Exception $exception) { $application = null; try { $application = \Skeleton\Core\Application::get(); } catch (\Exception $e) { } if ($application === null) { $hostname = 'unknown'; $name = 'unknown'; } else { $hostname = $application->hostname; $name = $application->name; } if ($exception instanceof \ErrorException) { $subject = get_class($exception) . ' (' . \Skeleton\Error\Util\Misc::translate_error_code($exception->getSeverity()) . ') on ' . $hostname . ' (' . $name . ')'; } else { $subject = get_class($exception) . ' on ' . $hostname . ' (' . $name . ')'; } return $subject; }
/** * Handle the request and send it to the correct module * * @access public */ public static function run() { /** * Record the start time in microseconds */ $start = microtime(true); mb_internal_encoding('utf-8'); /** * Hide PHP powered by */ header('X-Powered-By: Me'); /** * Parse the requested URL */ $components = parse_url($_SERVER['REQUEST_URI']); if (isset($components['query'])) { $query_string = $components['query']; } else { $query_string = ''; } if (isset($components['path']) and $components['path'] !== '/') { $request_uri_parts = explode('/', $components['path']); array_shift($request_uri_parts); } else { $request_uri_parts = []; } $request_uri = '/' . implode('/', $request_uri_parts) . '/'; // Find out what the hostname is, if none was found, bail out if (!empty($_SERVER['SERVER_NAME'])) { $hostname = $_SERVER['SERVER_NAME']; } elseif (!empty($_SERVER['HTTP_HOST'])) { $hostname = $_SERVER['HTTP_HOST']; } else { throw new \Exception('Not a web request'); } /** * Define the application */ try { $application = Application::detect($hostname, $request_uri); } catch (Exception $e) { HTTP\Status::code_404('application'); } /** * Handle the media */ if (isset($application->config->detect_media) and $application->config->detect_media === true or !isset($application->config->detect_media)) { Media::detect($application->request_relative_uri); } /** * Start the session */ Session::start(); /** * Find the module to load * * FIXME: this nested try/catch is not the prettiest of things */ $module = null; try { // Attempt to find the module by matching defined routes $module = $application->route($request_uri); } catch (\Exception $e) { try { // Attempt to find a module by matching paths $module = Module::get($application->request_relative_uri); } catch (\Exception $e) { if (Hook::exists('module_not_found')) { Hook::call('module_not_found'); } else { HTTP\Status::code_404('module'); } } } /** * Set language */ // Set the language to something sensible if it isn't set yet if (class_exists('\\Skeleton\\I18n\\Config') and isset(\Skeleton\I18n\Config::$language_interface)) { $language_interface = \Skeleton\I18n\Config::$language_interface; if (!class_exists($language_interface)) { throw new \Exception('The language interface does not exists: ' . $language_interface); } if (!isset($_SESSION['language'])) { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $languages = $language_interface::get_all(); foreach ($languages as $language) { if (strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], $language->name_short) !== false) { $language = $language; $_SESSION['language'] = $language; } } } if (!isset($_SESSION['language'])) { $language = $language_interface::get_by_name_short($application->config->default_language); $_SESSION['language'] = $language; } } if (isset($_GET['language'])) { try { $language = $language_interface::get_by_name_short($_GET['language']); $_SESSION['language'] = $language; } catch (\Exception $e) { $_SESSION['language'] = $language_interface::get_by_name_short($application->config->default_language); } } $application->language = $_SESSION['language']; } if ($module !== null) { $module->accept_request(); } }
/** * Find out how we should refer to the current page * * @access private * @return string $uri */ private static function find_page_uri() { // We need to remove the base_uri from the link, because it will get // rewritten afterwards. If we leave it, it will be prepended again, // which makes the link invalid. if (class_exists('\\Skeleton\\Core\\Application')) { $application = \Skeleton\Core\Application::get(); } $request_uri = str_replace('?' . $_SERVER['QUERY_STRING'], '', $_SERVER['REQUEST_URI']); if (isset($application->config->base_uri) and strpos($request_uri, $application->config->base_uri) === 0) { $url = substr($request_uri, strlen($application->config->base_uri) - 1); } else { $url = $request_uri; } return $url; }
/** * Do a reverse rewrite of a link * * @access private * @param string $url_raw * @return string $reverse_rewrite */ private static function rewrite_reverse_routes($url_raw) { $url = parse_url($url_raw); $params = []; $application = \Skeleton\Core\Application::Get(); $routes = $application->config->routes; if (isset($url['query'])) { // Allow & instead of & $url['query'] = str_replace('&', '&', $url['query']); parse_str($url['query'], $params); } /** * Add language to the known parameters */ if (isset($application->language) and !isset($params['language'])) { $params['language'] = $application->language->name_short; } /** * Search for the requested module */ if (!isset($url['path'])) { return $url_raw; } if ($url['path'] != '' and $url['path'][0] == '/') { $url['path'] = substr($url['path'], 1); } $module_name = null; /** * Check skeleton packages */ $packages = \Skeleton\Core\Package::get_all(); foreach ($packages as $package) { $parts = explode('/', $url['path']); if (isset($parts[0]) and $parts[0] == $package->name) { unset($parts[0]); $package_parts = explode('-', str_replace('skeleton-package-', '', $package->name)); foreach ($package_parts as $key => $package_part) { $package_parts[$key] = ucfirst($package_part); } $module_name = '\\Skeleton\\Package\\' . implode('\\', $package_parts) . '\\Web\\Module\\' . str_replace('_', '\\', implode('/', $parts)); } } if ($module_name === null) { $module_name = 'web_module_' . str_replace('/', '_', $url['path']); } $module_defined = false; $package_module = false; if (isset($routes[$module_name])) { $module_defined = true; } elseif (isset($routes[$module_name . '_index'])) { $module_name = $module_name . '_index'; $module_defined = true; } else { foreach ($routes as $classname => $dummy) { $application = Application::get(); $module_filename = str_replace('web_module_', '', $classname); $filename_parts = explode('_', $module_filename); $module_filename = ''; foreach ($filename_parts as $filename_part) { $module_filename .= '/' . strtolower($filename_part); } $module_filename .= '.php'; $module_filename = $application->module_path . $module_filename; if (file_exists($module_filename) and !class_exists($classname)) { require_once $module_filename; } if (class_exists($classname) and (strtolower(get_parent_class($classname)) == strtolower($module_name) or is_subclass_of($classname, $module_name))) { $module_name = strtolower($classname); $module_defined = true; $package_module = true; } } } if (!$module_defined) { return $url_raw; } $routes = $routes[$module_name]; $correct_route = null; foreach ($routes as $route) { $route_parts = explode('/', $route); $route_part_matches = 0; foreach ($route_parts as $key => $route_part) { if (trim($route_part) == '') { unset($route_parts[$key]); continue; } if ($route_part[0] != '$') { $route_part_matches++; continue; } /** * $language[en,nl] => language[en,nl] */ $route_part = substr($route_part, 1); /** * Fetch required values */ $required_values = []; preg_match_all('/(\\[(.*?)\\])/', $route_part, $matches); if (count($matches[2]) > 0) { /** * There are required values, parse them */ $required_values = explode(',', $matches[2][0]); $route_part = str_replace($matches[0][0], '', $route_part); $route_parts[$key] = '$' . $route_part; } if (isset($params[$route_part])) { /** * if there are no required values => Proceed */ if (count($required_values) == 0) { $route_part_matches++; continue; } /** * Check the required values */ $values_ok = false; foreach ($required_values as $required_value) { if ($required_value == $params[$route_part]) { $values_ok = true; } } if ($values_ok) { $route_part_matches++; continue; } } } if ($route_part_matches == count($route_parts)) { $correct_route = $route_parts; } } if ($correct_route === null and !$package_module) { return $url_raw; } elseif ($correct_route === null and $package_module) { $module_name = str_replace('web_module_', '', $module_name); $new_url = '/' . str_replace('_', '/', $module_name); } else { $new_url = ''; foreach ($correct_route as $url_part) { if ($url_part[0] !== '$') { $new_url .= '/' . $url_part; continue; } $url_part = substr($url_part, 1); $new_url .= '/' . $params[$url_part]; unset($params[$url_part]); } } /** * If the first character is a /, remove it */ if ($new_url[0] == '/') { $new_url = substr($new_url, 1); } if (count($params) > 0) { $new_url .= '?' . urldecode(http_build_query($params)); } /** * Is there a fragment ('#') available? */ if (isset($url['fragment'])) { $new_url .= '#' . $url['fragment']; } return $new_url; }
/** * Fetch the contents and mtime of a file * * @access private * @param string $type * @param string $path * @param string $extension * @return mixed $content Returns a string with the content, false if it * couldn't be found or null if it shouldn't be handled by us anyway */ private static function fetch($type, $path, $extension) { $packages = \Skeleton\Core\Package::get_all(); foreach (self::$filetypes as $filetype => $extensions) { $filepaths = [\Skeleton\Core\Config::$asset_dir . '/' . $path, Application::get()->media_path . '/' . $filetype . '/' . $path]; foreach ($packages as $package) { $path_parts = explode('/', $path); if (!isset($path_parts[0]) or $path_parts[0] != $package->name) { continue; } unset($path_parts[0]); $package_path = $package->asset_path . '/' . $filetype . '/' . implode('/', $path_parts); $filepaths[] = $package_path; } if (in_array($extension, $extensions)) { foreach ($filepaths as $filepath) { if (file_exists($filepath)) { if ($type == 'mtime') { return filemtime($filepath); } else { return file_get_contents($filepath); } } } return false; } } return null; }
/** * Get the requested module * * @param string Module name * @access public * @return Web_Module Requested module * @throws Exception */ public static function get($request_relative_uri) { $application = \Skeleton\Core\Application::get(); $relative_uri_parts = array_values(array_filter(explode('/', $request_relative_uri))); $filename = trim($request_relative_uri, '/'); if (file_exists($application->module_path . '/' . $filename . '.php')) { require $application->module_path . '/' . $filename . '.php'; $classname = 'Web_Module_' . implode('_', $relative_uri_parts); } elseif (file_exists($application->module_path . '/' . $filename . '/' . $application->config->module_default . '.php')) { require $application->module_path . '/' . $filename . '/' . $application->config->module_default . '.php'; if ($filename == '') { $classname = 'Web_Module_' . $application->config->module_default; } else { $classname = 'Web_Module_' . implode('_', $relative_uri_parts) . '_' . $application->config->module_default; } } elseif (file_exists($application->module_path . '/' . $application->config->module_404 . '.php')) { require $application->module_path . '/' . $application->config->module_404 . '.php'; $classname = 'Web_Module_' . $application->config->module_404; } else { throw new \Exception('Module not found'); } return new $classname(); }
/** * Render * * @access public * @param string $template * @return string $html */ public function render($template) { if (strpos($template, '.') === false) { throw new \Exception('Please provide a valid template filename. Incorrect filename "' . $template . '"'); } list($filename, $extension) = explode('.', basename($template)); switch ($extension) { case 'twig': $renderer = new \Skeleton\Template\Twig\Twig(); break; case 'tpl': $renderer = new \Skeleton\Template\Smarty\Smarty(); break; default: throw new \Exception('Unknown template type'); } if (count($this->template_directories) == 0) { throw new \Exception('No template directory set, please set $template->set_template_directory()'); } // Set the template path foreach ($this->template_directories as $template_directory) { $renderer->add_template_directory($template_directory['directory'], $template_directory['namespace']); } // Pass the environment variables to the template renderer if (count($this->environment) > 0) { foreach ($this->environment as $key => $value) { $renderer->add_environment($key, $value); } } // Pass the variables to the template renderer foreach ($this->variables as $key => $value) { $renderer->assign($key, $value); } // Set the translation object if ($this->translation !== null) { $renderer->set_translation($this->translation); } else { if (class_exists('\\Skeleton\\I18n\\Translation') and class_exists('Skeleton\\Core\\Application')) { try { $language = \Skeleton\Core\Application::Get()->language; $application_name = \Skeleton\Core\Application::Get()->name; $translation = \Skeleton\I18n\Translation::Get($language, $application_name); $renderer->set_translation($translation); } catch (\Exception $e) { } } } return $renderer->render($template); }