コード例 #1
0
ファイル: Handler.php プロジェクト: tigron/skeleton-core
 /**
  * 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();
     }
 }
コード例 #2
0
ファイル: Media.php プロジェクト: tigron/skeleton-core
 /**
  * Detect if the file should be resent to the client or if it can use its cache
  *
  * @param string filename requested
  * @access private
  */
 private static function cache($mtime)
 {
     $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
     header('Cache-Control: public');
     header('Pragma: public');
     if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
         if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $gmt_mtime) {
             header('Expires: ');
             HTTP\Status::code_304();
         }
     }
     header('Last-Modified: ' . $gmt_mtime);
     header('Expires: ' . gmdate('D, d M Y H:i:s', strtotime('+30 minutes')) . ' GMT');
 }