<?php
/**
 * Index page for jaws
 *
 * @category   Application
 * @package    Core
 * @author     Jonathan Hernandez <ion@suavizado.com>
 * @author     Pablo Fischer <pablo@pablo.com.mx>
 * @author     Helgi Žormar <dufuz@php.net>
 * @author     Ali Fazelzadeh <afz@php.net>
 * @copyright  2005-2011 Jaws Development Group
 * @license    http://www.gnu.org/copyleft/lesser.html
 */
define('JAWS_SCRIPT', 'index');
define('BASE_SCRIPT', basename(__FILE__));
define('APP_TYPE',    'web');

// Redirect to the installer if JawsConfig can't be found.
$root = dirname(__FILE__);
if (!file_exists($root . '/config/JawsConfig.php')) {
    header('Location: install/index.php');
    exit;
} else {
    require $root . '/config/JawsConfig.php';
}

require_once JAWS_PATH . 'include/Jaws/InitApplication.php';
if (DEBUG_ACTIVATED) {
    // Log start time (microseconds)
    $mtime = microtime();
    $mtime = explode(' ', $mtime);
    $mtime = (double) $mtime[0] + $mtime[1];
    $tstart = $mtime;
}

//now developers can add ACL to gadget for check in frontend area
$GLOBALS['app']->loadClass('ACL', 'Jaws_ACL');

// Init layout...
$GLOBALS['app']->InstanceLayout();
$GLOBALS['app']->Layout->Load();

if ($GLOBALS['app']->Map->Parse()) {
    $request =& Jaws_Request::getInstance();
    $ReqGadget = $request->get('gadget', 'get');
    if (is_null($ReqGadget)) {
        $ReqGadget = $request->get('gadget', 'post');
    }

    $ReqAction = $request->get('action', 'get');
    if (is_null($ReqAction)) {
        $ReqAction = $request->get('action', 'post');
    }

    // Get requested error via htaccess
    $ReqError = $request->get('http_error', 'get');
} else {
    $ReqError = '404';
    $ReqGadget = null;
    $ReqAction = null;
}

// Run auto-load methods before standalone actions too
$GLOBALS['app']->RunAutoload();

$IsIndex = false;
$ReqResult = '';
$IsReqActionStandAlone = false;
if (empty($ReqGadget)) {
    $IsIndex = true;
    $ReqGadget = empty($ReqError)? $GLOBALS['app']->Registry->Get('/config/main_gadget') : '';
}

if (!empty($ReqGadget)) {
    if (Jaws_Gadget::IsValid($ReqGadget)) {
        $goGadget = $GLOBALS['app']->LoadGadget($ReqGadget);
        if (Jaws_Error::IsError($goGadget)) {
            Jaws_Error::Fatal("Error loading gadget: $ReqGadget", __FILE__, __LINE__);
        }

        $ReqAction = empty($ReqAction)? 'DefaultAction' : $ReqAction;
        $goGadget->SetAction($ReqAction);
        $ReqAction = $goGadget->GetAction();
        $GLOBALS['app']->SetMainRequest($IsIndex, $ReqGadget, $ReqAction);

        $ReqResult = $goGadget->Execute();
        if (Jaws_Error::isError($ReqResult)) {
            $ReqResult = $ReqResult->GetMessage();
            if (isset($GLOBALS['log'])) {
                $GLOBALS['log']->Log(JAWS_LOG_ERR, 'In '.$ReqGadget.'::'.$ReqAction.','.$ReqResult);
            }
        }

        $IsReqActionStandAlone = $goGadget->isStandAlone($ReqAction);
    } else {
        $ReqError = '404';
    }
} else {
    $goGadget = null;
}

if (!empty($ReqError)) {
    require_once JAWS_PATH . 'include/Jaws/HTTPError.php';
    $ReqResult = Jaws_HTTPError::Get($ReqError);
}

if (!$IsReqActionStandAlone) {
    $GLOBALS['app']->Layout->Populate($goGadget, $IsIndex, $ReqResult);
    $ReqResult = $GLOBALS['app']->Layout->Get();
}

// Send content to client
echo $ReqResult;

// Sync session
$GLOBALS['app']->Session->Synchronize();

if (DEBUG_ACTIVATED) {
    $client = $request->get('client', 'get');
    if ($action != 'Ajax' && $client === null) {
        // Log generation time
        $mtime = microtime();
        $mtime = explode(' ', $mtime);
        $mtime = $mtime[1] + $mtime[0];
        $tend  = $mtime;
        $totaltime = ($tend - $tstart);
        $GLOBALS['log']->Log(JAWS_LOG_INFO, 'Page was generated in '. $totaltime . ' seconds');
        $GLOBALS['log']->Log(JAWS_LOG_INFO, '[Jaws End] ' . date('M/d/Y H:i:s') . ':' . __FILE__ . ':' .  __LINE__);
        if (function_exists('memory_get_usage')) {
            $GLOBALS['log']->Log(JAWS_LOG_INFO, 'Memory Usage: ' . round(memory_get_usage() / 1024) . ' KB');
        }
        $GLOBALS['log']->LogStackToScreen();
    }
}
