<?php

$cms['cms_dir']   = dirname(__FILE__); # PHP 5.2 compability
# fix for windows
if ( DIRECTORY_SEPARATOR === '\\' ) {
    $cms['cms_dir'] = strtolower( str_replace( '\\', '/', $cms['cms_dir'] ) );
}
$cms['site_dir']  = dirname( $cms['cms_dir'] );
$cms['referer']   = @$_SERVER['HTTP_REFERER'];
$cms['request']   = $_SERVER['REQUEST_URI'];
if ( empty( $_SERVER['HTTPS'] ) ) {
    $protocol = 'http';
} else {
    $protocol = 'https';
}
$cms['url']    = parse_url($protocol.'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
$cms['status'] = '404';
$cms['tr']     = array(); /* transliteration array */
$_SERVER['DOCUMENT_ROOT'] = rtrim( $_SERVER['DOCUMENT_ROOT'], '/\\' ); /* fix DocumentRoot */
if ( DIRECTORY_SEPARATOR === '\\' ) {
    $_SERVER['DOCUMENT_ROOT'] = strtolower( str_replace( '\\', '/', $_SERVER['DOCUMENT_ROOT'] ) );
}
$cms['base_path'] = str_replace($_SERVER['DOCUMENT_ROOT'], '', $cms['site_dir']).'/';

$esc  = str_replace( '/', '\\/', $cms['base_path'] );
$cms['cms_file'] = $cms['cms_dir'].'/'.preg_replace( "/^{$esc}/", '', $cms['url']['path'] );


/* Load config.php */
$cms['config_file'] = $cms['cms_dir'] . "/config.php";
if ( file_exists( $cms['config_file'] ) ) {
    $config_file = fopen($cms['config_file'], 'r');
    flock($config_file, LOCK_SH);
    include( $cms['config_file'] );
    flock($config_file, LOCK_UN);
}

/* Set locale */
if ( !empty( $cms['config']['locale'] ) ) {
    setlocale( LC_ALL, $cms['config']['locale'] );
}
/* Set timezone */
if ( !empty( $cms['config']['timezone'] ) ) {
    date_default_timezone_set ( $cms['config']['timezone'] );
}
    
/* Init Stages */
$cms['stage']  = 'query'; /* Default Stage */
$cms['stages'] = array(
    'query'    => array( 'disabled' => false, 'next' => 'template'),
    'template' => array( 'disabled' => false, 'next' => 'echo'    ),
    'echo'     => array( 'disabled' => false, 'next' => 'write'   ),
    'write'    => array( 'disabled' => false, 'next' => ''        ),
    'ajax'     => array( 'disabled' => false, 'next' => ''        ),
    'cron'     => array( 'disabled' => false, 'next' => ''        ),
    'admin'    => array( 'disabled' => false, 'next' => 'template'),
);

# Setup defautls URLs
$cms['urls'] = array();
$cms['urls']['^/cron$'] = 'cron';

# Load functions files
foreach( glob( '*.fn.php' ) as $fn_file ) {
    include_once( $fn_file );
}

# Load modules
foreach( glob( 'mod/*.mod.php' ) as $module ) {
    include_once( $module );
}

krsort( $cms['urls'] );
foreach( $cms['urls'] as $url => $stage ) {
    # Escape / for regexp
    $url = preg_replace( "/\//", "\\/", $url );
    if ( preg_match( "/{$url}/", $cms['url']['path'], $cms['url_matches'] ) ) {
        $cms['stage'] = $stage;
        break;
    }
}

/* RUN */
while ( !empty( $cms['stage'] ) ) {
    cms_do_stage( $cms['stage'] );
    $cms['stage'] = $cms['stages'][$cms['stage']]['next'];
}
