<?php
/**
 *
 * @category        Core
 * @package         Core_sys
 * @author          WebsiteBaker Project
 * @copyright      2009-, WebsiteBaker Org. e.V.
 * @link            http://websitebaker.org/
 * @license         http://www.gnu.org/licenses/gpl.html
 * @platform        WebsiteBaker 2.8.x
 * @requirements    PHP 5.2.2 and higher
 * @version         $Id: index.php 2122 2015-03-17 22:59:36Z darkviper $
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/index.php $
 * @lastmodified    $Date: 2015-03-17 23:59:36 +0100 (Tue, 17 Mar 2015) $
 *
 */

// compatibility between old and new access file format
if (isset($iPageId)) { $page_id = $iPageId; }
if (isset($page_id) && !isset($iPageId)) { $iPageId = $page_id; }
// Include config file
$sStartUpFile = __DIR__.'/framework/initialize.php';
if (file_exists($sStartUpFile) && !defined('SYSTEM_RUN')) {
    require($sStartUpFile);
}
/* ******************************************** */
// Check if the config file has been set-up
if (!defined('SYSTEM_RUN') || SYSTEM_RUN == false) {
/*
 * Remark:  HTTP/1.1 requires a qualified URI incl. the scheme, name
 * of the host and absolute path as the argument of location. Some, but
 * not all clients will accept relative URIs also.
 */
    $host       = $_SERVER['HTTP_HOST'];
    $uri        = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\');
    $file       = 'install/index.php';
    $target_url = 'http://'.$host.$uri.'/'.$file;
    $sResponse  = $_SERVER['SERVER_PROTOCOL'].' 307 Temporary Redirect';
    header($sResponse);
    header('Location: '.$target_url);
    exit; // make sure that subsequent code will not be executed
}
/* ******************************************** */
// dispatch RSS requests *** mandatory argument:  ?rss=$iSectionId
if (($iRssSectionId = intval((@$_GET['rss'] ?: 0))) && defined('SYSTEM_RUN')) {
    $sql = 'SELECT `module` FROM `'.$oDb-TablePrefix.'sections` '
         . 'WHERE `section_id`='.$iRssSectionId;
    if (($sRssAddon = $oDb->getOne($sql))) {
        $sRssClass = 'm_'.$sRssAddon.'_RssFeeder';
        if (class_exist($sRssClass)) {
            $oRss = new $sRssClass(WbAdapter::getInstance(), $iRssSectionId);
            $oRss->setTemplate();
            $oRss->doExecute();
            exit;
        }
    }
    RssFeederAbstract::sendRssError();
    exit;
}
/* ******************************************** */
// Create new frontend object
if (!isset($wb) || !($wb instanceof frontend)) {
    $wb = new frontend();
}
// Figure out which page to display
// Stop processing if intro page was shown
$wb->page_select() or die();
// Collect info about the currently viewed page
// and check permissions
$wb->get_page_details();
// Collect general website settings
$wb->get_website_settings();
// Load functions available to templates, modules and code sections
// also, set some aliases for backward compatibility
require($oReg->AppPath.'framework/frontend.functions.php');
// reload all of the already defined global constants
$oReg->getWbConstants();

// load and run template file
$output = '';
if (is_readable($oReg->TemplatePath.'index.php')) {
//    EventDispatcher::getInstance()->triggerEvent('beforeTemplateStart', 'core');
// load new Template-Code-Class if available
    if (is_readable($oReg->TemplatePath.'TemplateCode.php')) {
        require($oReg->TemplatePath.'TemplateCode.php');
        if (class_exist('TemplateCode', false)) {
            $oTpl = new TemplateCode($oReg);
            if(!$oTpl instanceof TemplateCodeAbstract) {
            // quit if TemplateCode does not implement the correct interface
                throw new AppException('TemplateCode does not extends TemplateCodeAbstract!');
            }
        }
    }
// activate language files of the template if it's exists
    $oTrans->enableTemplate(str_replace('/', '\\', rtrim($oReg->TemplateDir, '/')));
    ob_start();
    // run the template
        require($oReg->TemplatePath.'index.php');
    // catch the output of the template
    $output = ob_get_clean();
} else {
    throw new AppException('['.$oReg->TemplateDir.'] is not a readable, valid template!!');
}
// execute frontend output filters
if(file_exists($oReg->AppPath .'modules/OutputFilter/index.php')) {
    include_once($oReg->AppPath .'modules/OutputFilter/index.php');
    if(function_exists('executeFrontendOutputFilter')) {
        $output = executeFrontendOutputFilter($output);
    }
}
/* *** trigger event to replace overcomming outputFilter ****************************** */
//if (($oEvent = EventDispatcher::getInstance())) {
//    $oEvent->triggerEvent('beforeContentSend', 'core', $output);
//    $output = $oEvent->getInfo();
//}
/* ************************************************************************************ */
// check if headers already sent
$sFile = $sLine = '';
if (headers_sent($sFile, $sLine)) {
    $sMsg = 'headers already sent in '.$sFile.' in Line '.$sLine.'!! '.PHP_EOL
          . 'it is not possible to set the right charset for your site!';
    throw new RuntimeException($sMsg);
}
// send header to fix IE-Charset-bug which ignores <meta charset="xx">
// -- prepared to send the header-buffer (including Cookies) in future --//
header('Content-Type: text/html; charset='.$oReg->DefaultCharset);
flush();
// now send the complete content to the browser
echo $output;
flush();
//if (($oEvent = EventDispatcher::getInstance()->triggerEvent('afterContentSend', 'core'))) {
// end of wb-script
exit;
