Skip to content
Snippets Groups Projects
index.php 5.05 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    // Display execution time?
    //$displaytime = TRUE;
    
    // start the clock (this is just temporary cause I'm interested in how long all this takes)
    if (@$displaytime) {
    	$time = microtime();
    	$time = explode(' ', $time);
    	$time = $time[1] + $time[0];
    	$begintime = $time;
    }
    
    /* --------------------------------------------------------
        Settings
       -------------------------------------------------------- */
    
    include("settings.php");
    if (!(isset($base) && isset($preferred_hostname) && isset($dbname))) {
    	echo "Website unconfigured";
    	exit();
    }
    
    /* --------------------------------------------------------
        Libraries
       -------------------------------------------------------- */
    // Include our custom error handling business
    require("../lib/error.php");
    
    
    // Include the Smarty templating engine
    
    define('SMARTY_DIR', '/usr/share/php/smarty/libs/');
    require("/usr/share/php/smarty/libs/Smarty.class.php");
    
    $smarty = new Smarty;
    $smarty->template_dir = $base."templates";
    $smarty->compile_dir = $base."templates_c";
    $smarty->plugins_dir[] = $base."plugins";
    
    $smarty->assign("baseurl", $baseurl);
    
    // Initialise the database
    
    require("/usr/share/php/adodb/adodb.inc.php");
    
    $DB = NewADOConnection('postgres8');
    
    $DB->Connect('dbname='.$dbname.' user=apache');
    
    $DB->SetFetchMode(ADODB_FETCH_ASSOC);
    
    // Include the session library
    require($base."lib/session.php");
    $session = new Session;
    $smarty->assign_by_ref("session", $session);
    
    /* --------------------------------------------------------
        Debugging
       -------------------------------------------------------- */
    
    // Turn on adodb debugging by uncommenting:
    //$DB->debug = true;
    
    // Turn on Smarty debugging by uncommenting:
    //$smarty->assign("debug",TRUE);
    
    // Should we display unexpected output from components?
    $compdebug = TRUE;
    
    /* --------------------------------------------------------
        Read Browser's settings
       -------------------------------------------------------- */
    
    // Determine the language to serve pages in
    $languagelist = explode(',', @$_SERVER['HTTP_ACCEPT_LANGUAGE']);
    $language = array();
    $language['code'] = "en";
    /*
    The following is hideously unscientific, but works for now. What really needs
    to happen is the list get searched to see if cy is higher priority than en
    */
    
    if ($languagelist[0]=="cy") $language['code'] = "cy";
    $language['file'] = ".".$language['code'];
    $language['db'] = "_".$language['code'];
    
    if ($language['code']=="en") {
    $language['file'] = "";
    $language['db'] = "";
    }
    
    $smarty->assign("language", $language);
    
    
    // SSL?
    $ssl_path = @$_SERVER['REQUEST_URI'];
    if (($n=strpos($ssl_path,"?"))!==FALSE) $ssl_path=substr($ssl_path,0,$n);
    $ssl_url = "https://".$preferred_hostname.$ssl_path;
    $smarty->assign("ssl_url", $ssl_url);
    
    
    // Determine which component to run
    
    $pathlist = explode('/', parse_url(@$_SERVER['PATH_INFO'],PHP_URL_PATH));
    
    while (end($pathlist) === "") array_pop($pathlist);
    $smarty->assign_by_ref("pathlist", $pathlist);
    $path = '';
    $query = "select * from pagemap where path='/' ";
    foreach($pathlist as $item) {
    	if ($item && $item != '/') {
    	$query .= "or path = '".addslashes($path)."/*' ";
    	$path .= "/$item";
    	$query .= "or path = '".addslashes($path)."' ";
    	}
    }
    
    // Determine the path of the request
    $smarty->assign_by_ref("path", $path);
    
    $query .= "order by depth desc";
    $pagemap = $DB->GetAll($query);
    //echo $query;
    if (!$pagemap) $smarty->assign("error", $DB->ErrorMsg());
    if (!$pagemap || count($pagemap)<1) {
    	$smarty->assign("component", "Default");
    	$component = "";
    } else {
    	$smarty->assign("component", $pagemap[0]['component']);
    	$component = $pagemap[0];
    	
    }
    
    if ($path == "") $path="/";
    
    // Prep values for the components to use
    $smarty->assign("title", "Set Me");
    $smarty->assign("body", "Empty Body");
    
    include($base."components/menu.php");
    
    // Load the component
    $comppath = $base."components/".$component['component'].".php";
    $compoutput = "";
    if (file_exists($comppath)) {
    	ob_start();
    	include($comppath);
    	$compoutput = ob_get_contents();
    	ob_end_clean();
    } else {
    	$smarty->assign("body", "Component ".$component['component']." not found");
    }
    
    // Pass error/warning/info messages to the template
    $smarty->assign("user_messages", $messages);
    
    // Render the results
    if (!(isset($no_template)) || (!$no_template)) {
    // Send appropriate Content-Type
            if (ereg('application/xhtml\+xml', @$_SERVER['HTTP_ACCEPT'])) {
                    header('Content-Type: application/xhtml+xml');
                    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
            } else {
                    header('Content-Type: text/html');
            }
    
    	$smarty->display("head.tpl");
    	$smarty->display("index.tpl");
    	if ($compdebug) {
    		$smarty->assign("compoutput",$compoutput);
    		$smarty->display("debug.tpl");
    	}
    
    // Last chance to stop the clock and still end up with valid HTML
    if (@$displaytime) {
    	$time = microtime();
    	$time = explode(" ", $time);
    	$time = $time[1] + $time[0];
    	$endtime = $time;
    	$totaltime = ($endtime - $begintime);
    	$smarty->assign("totaltime", $totaltime);
    }
    
    $smarty->display("foot".$language['file'].".tpl");
    } else {
    	echo $compoutput;
    }
    
    // Save any changes made to the session data
    $session->save();
    
    
    ?>