<?php
require_once 'private/config.php';

// includeパスにlibraryを追加する
ini_set("include_path", LIB_PATH . PATH_SEPARATOR . ini_get("include_path"));
//echo ini_get("include_path");

if (DEMO) {
    // DBが一定時間を過ぎていたら、DBをリストアする
    
    if (!file_exists('private/platy_src.db')) {
        copy('private/platy.db', 'private/platy_src.db');
    }
    if (!file_exists('private/demo_db.txt')) {
        $fp = @fopen('private/demo_db.txt', 'w');
        if ($fp != FALSE) {
            fprintf($fp, "%d", time());
            fclose($fp);
        }
    }
    if (file_exists('private/platy.db') && file_exists('private/demo_db.txt')) {
        $fp = fopen('private/demo_db.txt', 'r');
        $uts_atime = (int)fgets($fp);
        fclose($fp);
        $uts_now = time();
        $uts_diff = $uts_now - $uts_atime;
        //echo 'atime=' . $uts_atime . ',now=' . $uts_now . ',diff=' . $uts_diff . "<br />\n";
        if ($uts_diff > 60*60) {
            unlink('private/platy.db');
            copy('private/platy_src.db', 'private/platy.db');
            $fp = @fopen('private/demo_db.txt', 'w');
            fprintf($fp, "%d", time());
            fclose($fp);
        }
    }
}

require_once 'Platy/Platy.php';
require_once 'Platy/Util.php';

//echo 'REQUEST_URI:' . $_SERVER['REQUEST_URI'] . "<br />\n"; 

if (strlen($_SERVER['REQUEST_URI']) > strlen(BASE_URL)
    && strstr($_SERVER['REQUEST_URI'], BASE_URL)
    && substr($_SERVER['REQUEST_URI'], strlen(BASE_URL), 1) >= 'A'
    && substr($_SERVER['REQUEST_URI'], strlen(BASE_URL), 1) <= 'Z') 
{
    //echo "Module<br />\n";
    require_once 'Zend/Controller/Front.php';

    Zend_Loader::loadClass('Zend_Controller_Router_Rewrite');

    $router     = new Zend_Controller_Router_Rewrite();
    $controller = Zend_Controller_Front::getInstance();
    $controller->throwExceptions(true);
    $controller->setControllerDirectory(APP_PATH . 'controllers');
    $controller->addControllerDirectory(APP_PATH . 'Manage/controllers', 'Manage');
    $controller->setRouter($router);
    $controller->setBaseUrl(BASE_URL); // ベース URL を指定します!
    
    // !
    $controller->setParam('noViewRenderer', true);
    
    $response   = $controller->dispatch();
}
else {
    //echo "Node<br />\n";
    require_once(APP_PATH . 'node.php');
}
?>