home *** CD-ROM | disk | FTP | other *** search
/ ftp.awoke-it.ch / ftp.awoke-it.ch.tar / ftp.awoke-it.ch / public_html / index.php < prev    next >
PHP Script  |  2014-11-24  |  12KB  |  445 lines

  1. <?php
  2.  
  3. /**
  4.  * Contao Open Source CMS
  5.  *
  6.  * Copyright (c) 2005-2014 Leo Feyer
  7.  *
  8.  * @package Core
  9.  * @link    https://contao.org
  10.  * @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
  11.  */
  12.  
  13.  
  14. /**
  15.  * Set the script name
  16.  */
  17. define('TL_SCRIPT', 'index.php');
  18.  
  19.  
  20. /**
  21.  * Initialize the system
  22.  */
  23. define('TL_MODE', 'FE');
  24. require __DIR__ . '/system/initialize.php';
  25.  
  26.  
  27. /**
  28.  * Class Index
  29.  *
  30.  * Main front end controller.
  31.  * @copyright  Leo Feyer 2005-2014
  32.  * @author     Leo Feyer <https://contao.org>
  33.  * @package    Core
  34.  */
  35. class Index extends Frontend
  36. {
  37.  
  38.     /**
  39.      * Initialize the object
  40.      */
  41.     public function __construct()
  42.     {
  43.         // Try to read from cache
  44.         $this->outputFromCache();
  45.  
  46.         // Load the user object before calling the parent constructor
  47.         $this->import('FrontendUser', 'User');
  48.         parent::__construct();
  49.  
  50.         // Check whether a user is logged in
  51.         define('BE_USER_LOGGED_IN', $this->getLoginStatus('BE_USER_AUTH'));
  52.         define('FE_USER_LOGGED_IN', $this->getLoginStatus('FE_USER_AUTH'));
  53.  
  54.         // No back end user logged in
  55.         if (!$_SESSION['DISABLE_CACHE'])
  56.         {
  57.             // Maintenance mode (see #4561 and #6353)
  58.             if (Config::get('maintenanceMode'))
  59.             {
  60.                 header('HTTP/1.1 503 Service Unavailable');
  61.                 die_nicely('be_unavailable', 'This site is currently down for maintenance. Please come back later.');
  62.             }
  63.  
  64.             // Disable the debug mode (see #6450)
  65.             Config::set('debugMode', false);
  66.         }
  67.     }
  68.  
  69.  
  70.     /**
  71.      * Run the controller
  72.      */
  73.     public function run()
  74.     {
  75.         global $objPage;
  76.         $pageId = $this->getPageIdFromUrl();
  77.         $objRootPage = null;
  78.  
  79.         // Load a website root page object if there is no page ID
  80.         if ($pageId === null)
  81.         {
  82.             $objRootPage = $this->getRootPageFromUrl();
  83.             $objHandler = new $GLOBALS['TL_PTY']['root']();
  84.             $pageId = $objHandler->generate($objRootPage->id, true);
  85.         }
  86.         // Throw a 404 error if the request is not a Contao request (see #2864)
  87.         elseif ($pageId === false)
  88.         {
  89.             $this->User->authenticate();
  90.             $objHandler = new $GLOBALS['TL_PTY']['error_404']();
  91.             $objHandler->generate($pageId);
  92.         }
  93.         // Throw a 404 error if URL rewriting is active and the URL contains the index.php fragment
  94.         elseif (Config::get('rewriteURL') && strncmp(Environment::get('request'), 'index.php/', 10) === 0)
  95.         {
  96.             $this->User->authenticate();
  97.             $objHandler = new $GLOBALS['TL_PTY']['error_404']();
  98.             $objHandler->generate($pageId);
  99.         }
  100.  
  101.         // Get the current page object(s)
  102.         $objPage = PageModel::findPublishedByIdOrAlias($pageId);
  103.  
  104.         // Check the URL and language of each page if there are multiple results
  105.         if ($objPage !== null && $objPage->count() > 1)
  106.         {
  107.             $objNewPage = null;
  108.             $arrPages = array();
  109.  
  110.             // Order by domain and language
  111.             while ($objPage->next())
  112.             {
  113.                 $objCurrentPage = $objPage->current()->loadDetails();
  114.  
  115.                 $domain = $objCurrentPage->domain ?: '*';
  116.                 $arrPages[$domain][$objCurrentPage->rootLanguage] = $objCurrentPage;
  117.  
  118.                 // Also store the fallback language
  119.                 if ($objCurrentPage->rootIsFallback)
  120.                 {
  121.                     $arrPages[$domain]['*'] = $objCurrentPage;
  122.                 }
  123.             }
  124.  
  125.             $strHost = Environment::get('host');
  126.  
  127.             // Look for a root page whose domain name matches the host name
  128.             if (isset($arrPages[$strHost]))
  129.             {
  130.                 $arrLangs = $arrPages[$strHost];
  131.             }
  132.             else
  133.             {
  134.                 $arrLangs = $arrPages['*'] ?: array(); // empty domain
  135.             }
  136.  
  137.             // Use the first result (see #4872)
  138.             if (!Config::get('addLanguageToUrl'))
  139.             {
  140.                 $objNewPage = current($arrLangs);
  141.             }
  142.  
  143.             // Try to find a page matching the language parameter
  144.             elseif (($lang = Input::get('language')) != '' && isset($arrLangs[$lang]))
  145.             {
  146.                 $objNewPage = $arrLangs[$lang];
  147.             }
  148.  
  149.             // Store the page object
  150.             if (is_object($objNewPage))
  151.             {
  152.                 $objPage = $objNewPage;
  153.             }
  154.         }
  155.  
  156.         // Throw a 404 error if the page could not be found or the result is still ambiguous
  157.         if ($objPage === null || ($objPage instanceof Model\Collection && $objPage->count() != 1))
  158.         {
  159.             $this->User->authenticate();
  160.             $objHandler = new $GLOBALS['TL_PTY']['error_404']();
  161.             $objHandler->generate($pageId);
  162.         }
  163.  
  164.         // Make sure $objPage is a Model
  165.         if ($objPage instanceof Model\Collection)
  166.         {
  167.             $objPage = $objPage->current();
  168.         }
  169.  
  170.         // Load a website root page object (will redirect to the first active regular page)
  171.         if ($objPage->type == 'root')
  172.         {
  173.             $objHandler = new $GLOBALS['TL_PTY']['root']();
  174.             $objHandler->generate($objPage->id);
  175.         }
  176.  
  177.         // Inherit the settings from the parent pages if it has not been done yet
  178.         if (!is_bool($objPage->protected))
  179.         {
  180.             $objPage->loadDetails();
  181.         }
  182.  
  183.         // Set the admin e-mail address
  184.         if ($objPage->adminEmail != '')
  185.         {
  186.             list($GLOBALS['TL_ADMIN_NAME'], $GLOBALS['TL_ADMIN_EMAIL']) = String::splitFriendlyEmail($objPage->adminEmail);
  187.         }
  188.         else
  189.         {
  190.             list($GLOBALS['TL_ADMIN_NAME'], $GLOBALS['TL_ADMIN_EMAIL']) = String::splitFriendlyEmail(Config::get('adminEmail'));
  191.         }
  192.  
  193.         // Exit if the root page has not been published (see #2425)
  194.         // Do not try to load the 404 page, it can cause an infinite loop!
  195.         if (!BE_USER_LOGGED_IN && !$objPage->rootIsPublic)
  196.         {
  197.             header('HTTP/1.1 404 Not Found');
  198.             die_nicely('be_no_page', 'Page not found');
  199.         }
  200.  
  201.         // Check wether the language matches the root page language
  202.         if (Config::get('addLanguageToUrl') && Input::get('language') != $objPage->rootLanguage)
  203.         {
  204.             $this->User->authenticate();
  205.             $objHandler = new $GLOBALS['TL_PTY']['error_404']();
  206.             $objHandler->generate($pageId);
  207.         }
  208.  
  209.         // Check whether there are domain name restrictions
  210.         if ($objPage->domain != '')
  211.         {
  212.             // Load an error 404 page object
  213.             if ($objPage->domain != Environment::get('host'))
  214.             {
  215.                 $this->User->authenticate();
  216.                 $objHandler = new $GLOBALS['TL_PTY']['error_404']();
  217.                 $objHandler->generate($objPage->id, $objPage->domain, Environment::get('host'));
  218.             }
  219.         }
  220.  
  221.         // Authenticate the user
  222.         if (!$this->User->authenticate() && $objPage->protected && !BE_USER_LOGGED_IN)
  223.         {
  224.             $objHandler = new $GLOBALS['TL_PTY']['error_403']();
  225.             $objHandler->generate($pageId, $objRootPage);
  226.         }
  227.  
  228.         // Check the user groups if the page is protected
  229.         if ($objPage->protected && !BE_USER_LOGGED_IN)
  230.         {
  231.             $arrGroups = $objPage->groups; // required for empty()
  232.  
  233.             if (!is_array($arrGroups) || empty($arrGroups) || !count(array_intersect($arrGroups, $this->User->groups)))
  234.             {
  235.                 $this->log('Page "' . $pageId . '" can only be accessed by groups "' . implode(', ', (array) $objPage->groups) . '" (current user groups: ' . implode(', ', $this->User->groups) . ')', __METHOD__, TL_ERROR);
  236.  
  237.                 $objHandler = new $GLOBALS['TL_PTY']['error_403']();
  238.                 $objHandler->generate($pageId, $objRootPage);
  239.             }
  240.         }
  241.  
  242.         // Load the page object depending on its type
  243.         $objHandler = new $GLOBALS['TL_PTY'][$objPage->type]();
  244.  
  245.         try
  246.         {
  247.             // Generate the page
  248.             switch ($objPage->type)
  249.             {
  250.                 case 'root':
  251.                 case 'error_404':
  252.                     $objHandler->generate($pageId);
  253.                     break;
  254.  
  255.                 case 'error_403':
  256.                     $objHandler->generate($pageId, $objRootPage);
  257.                     break;
  258.  
  259.                 default:
  260.                     $objHandler->generate($objPage, true);
  261.                     break;
  262.             }
  263.         }
  264.         catch (UnusedArgumentsException $e)
  265.         {
  266.             // Render the error page (see #5570)
  267.             $objHandler = new $GLOBALS['TL_PTY']['error_404']();
  268.             $objHandler->generate($pageId, null, null, true);
  269.         }
  270.  
  271.         // Stop the script (see #4565)
  272.         exit;
  273.     }
  274.  
  275.  
  276.     /**
  277.      * Try to load the page from the cache
  278.      */
  279.     protected function outputFromCache()
  280.     {
  281.         // Build the page if a user is (potentially) logged in or there is POST data
  282.         if (!empty($_POST) || Input::cookie('FE_USER_AUTH') || Input::cookie('FE_AUTO_LOGIN') || $_SESSION['DISABLE_CACHE'] || isset($_SESSION['LOGIN_ERROR']) || Config::get('debugMode'))
  283.         {
  284.             return;
  285.         }
  286.  
  287.         /**
  288.          * If the request string is empty, look for a cached page matching the
  289.          * primary browser language. This is a compromise between not caching
  290.          * empty requests at all and considering all browser languages, which
  291.          * is not possible for various reasons.
  292.          */
  293.         if (Environment::get('request') == '' || Environment::get('request') == 'index.php')
  294.         {
  295.             // Return if the language is added to the URL and the empty domain will be redirected
  296.             if (Config::get('addLanguageToUrl') && !Config::get('doNotRedirectEmpty'))
  297.             {
  298.                 return;
  299.             }
  300.  
  301.             $arrLanguage = Environment::get('httpAcceptLanguage');
  302.             $strCacheKey = Environment::get('base') .'empty.'. $arrLanguage[0];
  303.         }
  304.         else
  305.         {
  306.             $strCacheKey = Environment::get('base') . Environment::get('request');
  307.         }
  308.  
  309.         // HOOK: add custom logic
  310.         if (isset($GLOBALS['TL_HOOKS']['getCacheKey']) && is_array($GLOBALS['TL_HOOKS']['getCacheKey']))
  311.         {
  312.             foreach ($GLOBALS['TL_HOOKS']['getCacheKey'] as $callback)
  313.             {
  314.                 $this->import($callback[0]);
  315.                 $strCacheKey = $this->$callback[0]->$callback[1]($strCacheKey);
  316.             }
  317.         }
  318.  
  319.         $blnFound = false;
  320.         $strCacheFile = null;
  321.  
  322.         // Check for a mobile layout
  323.         if (Input::cookie('TL_VIEW') == 'mobile' || (Environment::get('agent')->mobile && Input::cookie('TL_VIEW') != 'desktop'))
  324.         {
  325.             $strCacheKey = md5($strCacheKey . '.mobile');
  326.             $strCacheFile = TL_ROOT . '/system/cache/html/' . substr($strCacheKey, 0, 1) . '/' . $strCacheKey . '.html';
  327.  
  328.             if (file_exists($strCacheFile))
  329.             {
  330.                 $blnFound = true;
  331.             }
  332.         }
  333.  
  334.         // Check for a regular layout
  335.         if (!$blnFound)
  336.         {
  337.             $strCacheKey = md5($strCacheKey);
  338.             $strCacheFile = TL_ROOT . '/system/cache/html/' . substr($strCacheKey, 0, 1) . '/' . $strCacheKey . '.html';
  339.  
  340.             if (file_exists($strCacheFile))
  341.             {
  342.                 $blnFound = true;
  343.             }
  344.         }
  345.  
  346.         // Return if the file does not exist
  347.         if (!$blnFound)
  348.         {
  349.             return;
  350.         }
  351.  
  352.         $expire = null;
  353.         $content = null;
  354.         $type = null;
  355.  
  356.         // Include the file
  357.         ob_start();
  358.         require_once $strCacheFile;
  359.  
  360.         // The file has expired
  361.         if ($expire < time())
  362.         {
  363.             ob_end_clean();
  364.             return;
  365.         }
  366.  
  367.         // Read the buffer
  368.         $strBuffer = ob_get_contents();
  369.         ob_end_clean();
  370.  
  371.         // Session required to determine the referer
  372.         $this->import('Session');
  373.         $session = $this->Session->getData();
  374.  
  375.         // Set the new referer
  376.         if (!isset($_GET['pdf']) && !isset($_GET['file']) && !isset($_GET['id']) && $session['referer']['current'] != Environment::get('requestUri'))
  377.         {
  378.             $session['referer']['last'] = $session['referer']['current'];
  379.             $session['referer']['current'] = substr(Environment::get('requestUri'), strlen(TL_PATH) + 1);
  380.         }
  381.  
  382.         // Store the session data
  383.         $this->Session->setData($session);
  384.  
  385.         // Load the default language file (see #2644)
  386.         $this->import('Config');
  387.         System::loadLanguageFile('default');
  388.  
  389.         // Replace the insert tags and then re-replace the request_token
  390.         // tag in case a form element has been loaded via insert tag
  391.         $strBuffer = $this->replaceInsertTags($strBuffer, false);
  392.         $strBuffer = str_replace(array('{{request_token}}', '[{]', '[}]'), array(REQUEST_TOKEN, '{{', '}}'), $strBuffer);
  393.  
  394.         // Content type
  395.         if (!$content)
  396.         {
  397.             $content = 'text/html';
  398.         }
  399.  
  400.         // Send the status header (see #6585)
  401.         if ($type == 'error_403')
  402.         {
  403.             header('HTTP/1.1 403 Forbidden');
  404.         }
  405.         elseif ($type == 'error_404')
  406.         {
  407.             header('HTTP/1.1 404 Not Found');
  408.         }
  409.         else
  410.         {
  411.             header('HTTP/1.1 200 Ok');
  412.         }
  413.  
  414.         header('Vary: User-Agent', false);
  415.         header('Content-Type: ' . $content . '; charset=' . Config::get('characterSet'));
  416.  
  417.         // Send the cache headers
  418.         if ($expire !== null && (Config::get('cacheMode') == 'both' || Config::get('cacheMode') == 'browser'))
  419.         {
  420.             header('Cache-Control: public, max-age=' . ($expire - time()));
  421.             header('Expires: ' . gmdate('D, d M Y H:i:s', $expire) . ' GMT');
  422.             header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
  423.             header('Pragma: public');
  424.         }
  425.         else
  426.         {
  427.             header('Cache-Control: no-cache');
  428.             header('Cache-Control: pre-check=0, post-check=0', false);
  429.             header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  430.             header('Expires: Fri, 06 Jun 1975 15:10:00 GMT');
  431.             header('Pragma: no-cache');
  432.         }
  433.  
  434.         echo $strBuffer;
  435.         exit;
  436.     }
  437. }
  438.  
  439.  
  440. /**
  441.  * Instantiate the controller
  442.  */
  443. $objIndex = new Index();
  444. $objIndex->run();
  445.