<?php
/* $Id: index.php 438 2008-07-28 03:01:19Z timw $ */

/**
 * Content Manager - Frontpage
 *
 * @package lizard
 * @subpackage frontpage
 * @author $Author: timw $
 * @version $Rev: 438 $ $Date: 2008-07-28 15:01:19 +1200 (Mon, 28 Jul 2008) $
 * @url $HeadURL: https://elibcsystems.dyndns.org/svn/skink/trunk/index.php $
 */


/**
 * Order to do things in:
 *
 *  + Sort out document root
 *  + Load in the php header
 *  + Database connection
 *  + Check php and mysql version
 *  + Define APP_SQL_VERSION
 *  + Update the database
 *  +
 */

/*
 * define -> SITE_VARSserver_documentroot
 *
 * Change this in each file this is loaded into
 */
$SITE_VARSserver_documentroot = dirname(__FILE__);
/**
 * Load in the php header
 */
include_once($GLOBALS['SITE_VARSserver_documentroot'].'/_lib/php/header.php');
/**
 * Database connection
 */
$SQL_VARSapplication_name = APPLICATION_NAME;
$DATABASE_VARSresourcelink = TWDdb_connect($SITE_VARSdatabase_server, $SITE_VARSdatabase_username, $SITE_VARSdatabase_password, $SITE_VARSdatabase_name, 'DATABASE_VARSresourcelink', true);
/**
 * Check php and mysql version
 */
if (TWDdb_version($GLOBALS['DATABASE_VARSresourcelink']) < 4.1 || !function_exists('version_compare')) {
  if (!function_exists('version_compare')) {
    $VERSIONerror = 'PHP version: ' . phpversion() . ' is not high enough to run this version of ' . ucwords(APPLICATION_NAME);
  } else {
    if(!version_compare('5.0', phpversion()) < 0)
      $VERSIONerror = 'PHP version: ' . phpversion() . ' is not high enough to run this version of ' . ucwords(APPLICATION_NAME);
  }
  if (TWDdb_version($GLOBALS['DATABASE_VARSresourcelink']) < 4.1)
    $VERSIONerror = 'MySQL version: ' . TWDdb_version($GLOBALS['DATABASE_VARSresourcelink']) . ' is not high enough to run this version of Panda.';
  die($VERSIONerror);
}
if (TWDdb_tableexists($GLOBALS['DATABASE_VARSresourcelink'],$SITE_VARSdatabase_name,'skink_installs')) {
  if (!defined('APP_SQL_VERSION')) define('APP_SQL_VERSION', TWDdb_reference('APP_SQL_VERSION',$GLOBALS['DATABASE_VARSresourcelink'],'skink_config','configoption','configvalue'));
  $GLOBALS['SITE_VARSdatabase_install'] = (strlen($GLOBALS['SITE_VARSdatabase_install']) > 0) ? TWDdb_reference($GLOBALS['SITE_VARSdatabase_install'], $GLOBALS['DATABASE_VARSresourcelink'], "{$SQL_VARSapplication_name}_installs", 'name', 'install_id') : '1';
} else {
  if (TWDdb_tableexists($GLOBALS['DATABASE_VARSresourcelink'],$SITE_VARSdatabase_name,'skink_config')) {
    if (!defined('APP_SQL_VERSION')) define('APP_SQL_VERSION', TWDdb_reference('APP_SQL_VERSION',$GLOBALS['DATABASE_VARSresourcelink'],'skink_config','configoption','configvalue'));
    $GLOBALS['SITE_VARSdatabase_site'] = (strlen($GLOBALS['SITE_VARSdatabase_site']) > 0) ? TWDdb_reference($GLOBALS['SITE_VARSdatabase_site'], $GLOBALS['DATABASE_VARSresourcelink'], "{$SQL_VARSapplication_name}_sites", 'name', 'site_id') : '1';
  } else {
    if (!defined('APP_SQL_VERSION')) define('APP_SQL_VERSION', TWDdb_reference('CONFIG_VARSapp_sql_version',$GLOBALS['DATABASE_VARSresourcelink'],'config','configoption','configvalue'));
    $GLOBALS['SITE_VARSdatabase_site'] = (strlen($GLOBALS['SITE_VARSdatabase_site']) > 0) ? TWDdb_reference($GLOBALS['SITE_VARSdatabase_site'], $GLOBALS['DATABASE_VARSresourcelink'], 'sites', 'name', 'site_id') : '1';
  }
}
/**
 * Define APP_SQL_VERSION - if panda_config doesn't exist application is obviously prior to version 0.02 and an update will run shortly to remedy it
 */
if (!defined('APP_SQL_VERSION')) define('APP_SQL_VERSION', ((TWDdb_tableexists($GLOBALS['DATABASE_VARSresourcelink'],$GLOBALS['SITE_VARSdatabase_name'],"{$SQL_VARSapplication_name}_config")) ? TWDdb_reference('APP_SQL_VERSION',$GLOBALS['DATABASE_VARSresourcelink'],"{$SQL_VARSapplication_name}_config",'configoption','configvalue',__LINE__,__FILE__) : '0.01.0'));
/**
 * Update the database
 */
include_once($GLOBALS['SITE_VARSserver_documentroot'].'/_sql/update.php');
/**
 * Parse database variables
 */
$ARRconfig = array();
$RS_SQLmainconfig = "SELECT * FROM `skink_config` WHERE ( install_id = '" . $GLOBALS['SITE_VARSdatabase_install'] . "' )";
$RS_OPENmainconfig = TWDdb_query($RS_SQLmainconfig, $GLOBALS['DATABASE_VARSresourcelink'], 'RS_OPENmainconfig', false,__LINE__,__FILE__);
$RS_ROWSmainconfig = TWDdb_numrows($RS_OPENmainconfig);
for ($COUNTrow = 0; $COUNTrow < $RS_ROWSmainconfig; $COUNTrow++) {
	$ARRconfig[TWDdb_result($RS_OPENmainconfig, $COUNTrow, 'configoption')] = TWDdb_result($RS_OPENmainconfig, $COUNTrow, 'configvalue');
}
$ARRconfigvariables = extract($ARRconfig);
$SITE_VARSmodules = explode('|',$SITE_VARSmodules);
/**
 * Load classes
 */
include_once($GLOBALS['SITE_VARSserver_documentroot'].'/_classes/appbase/appbase.php');
include_once($GLOBALS['SITE_VARSserver_documentroot'] . '/_classes/class_lizard/load.php');
/**
 * Start the LIZARD Content Class
 */
$LIZARDcontent = new LIZARDcontent();
/**
 * Start SMARTY Templating
 */
include_once($GLOBALS['SITE_VARSserver_documentroot'].'/smartycore/Smarty.class.php');
$smarty = new Smarty();
$smarty->assign('application_name', APPLICATION_NAME);
$smarty->error_reporting = E_WARNING | E_ERROR;
$smarty->template_dir = '_websites/' . (($GLOBALS['SITE_VARSwebsite_template']) ? $GLOBALS['SITE_VARSwebsite_template'] : 'default');
$smarty->compile_dir = 'smarty/websites_c';
$smarty->cache_dir = 'smarty/websites_cache';
$smarty->config_dir = 'smarty/configs';
$smarty->assign('app_debug_mode', (APP_DEBUG_MODE) ? 'true' : 'false');
$smarty->assign('globals_array',$GLOBALS);
$smarty->assign('SITE_VARSmodules',$GLOBALS['SITE_VARSmodules']);
$smarty->assign('googlemaps_key',$GLOBALS['SITE_VARSmapping_googlekey_local']);
$smarty->assign('query_login', 'module=membership&redirect_query=' . urlencode(TWDmisc_querystring_alter($GLOBALS['SITE_VARSserver_querystring'],'redirect_query','')));
/**
 * Redirect
 *
 * 1. Secure mode
 * 2. Website status
 * 3. Correct Page section
 */
if ($GLOBALS['SITE_VARSwebsite_secure_mode'] && $GLOBALS['SITE_VARSwebsite_secure_url'] && !(is_array($_SERVER) && $_SERVER["HTTPS"] == "on") && $GLOBALS['SITE_VARSwebsite_url_local'] != $GLOBALS['SITE_VARSserver_protocol'] . $GLOBALS['SITE_VARSserver_name'] . $GLOBALS['SITE_VARSserver_root']) {
	ob_end_clean();
	header('Location: ' . $GLOBALS['SITE_VARSwebsite_secure_url']);
	exit;
}
if ((! $GLOBALS['CONFIG_VARSwebsite_status'] || $GLOBALS['CONFIG_VARSwebsite_status'] == 'off') && $_SESSION['skinklogin_flag'] != true) {
  $LIZARDcontent->unavailable($smarty);
  exit;
}
if (! $_GET['module'] && $GLOBALS['CONFIG_VARSwebsite_splash'] == 'on') {
	ob_end_clean();
	header('Location: ' . $GLOBALS['SITE_VARSserver_protocol'] . $GLOBALS['SITE_VARSserver_name'] . $GLOBALS['SITE_VARSserver_phpself'] . '?module=content&splash=true');
	exit;
} elseif (! $_GET['module']) {
	ob_end_clean();
	header('Location: ' . $GLOBALS['SITE_VARSserver_protocol'] . $GLOBALS['SITE_VARSserver_name'] . $GLOBALS['SITE_VARSserver_phpself'] . '?module=content');
	exit;
}
/**
 * Get the content from LIZARD Content Class
 */
if ($_GET['module'] == 'content') {
  $content_id = ($_GET['content_id']) ? $_GET['content_id'] : TWDdb_reference(array('home',$GLOBALS['SITE_VARSdatabase_install']),$GLOBALS['DATABASE_VARSresourcelink'],'skink_content_items',array('section','install_id'),'content_id',__LINE__,__FILE__);
  $LIZARD_VARScontent = $LIZARDcontent->content($content_id);
}
/**
 * Initilize the classes
 */
for ($COUNTmodule = 0; $COUNTmodule < count($GLOBALS['SITE_VARSmodules']); $COUNTmodule++) {
  if ($GLOBALS['SITE_VARSmodules'][$COUNTmodule] != 'content') {
    $LIZARD_MODULEname = 'LIZARD' . $GLOBALS['SITE_VARSmodules'][$COUNTmodule];
    eval("\$$LIZARD_MODULEname = new $LIZARD_MODULEname;");
  }
}
/**
 * Iterate over the modules
 *
 * First call the module controls this is based based on the querystring
 * Second call the module classes that need replacement strings
 */
for ($COUNTmodule = 0; $COUNTmodule < count($GLOBALS['SITE_VARSmodules']); $COUNTmodule++) {
  include_once($GLOBALS['SITE_VARSserver_documentroot'] . '/_classes/class_lizard/' . $GLOBALS['SITE_VARSmodules'][$COUNTmodule] . '/content.php');
}
for ($COUNTmodule = 0; $COUNTmodule < count($GLOBALS['SITE_VARSmodules']); $COUNTmodule++) {
  include_once($GLOBALS['SITE_VARSserver_documentroot'] . '/_classes/class_lizard/' . $GLOBALS['SITE_VARSmodules'][$COUNTmodule] . '/control.php');
}
/**
 * Assign the Smarty content
 */
  //Header Content
$smarty->assign('title',$GLOBALS['SITE_VARSwebsite_title']);
$smarty->assign('meta_keywords',$LIZARD_VARScontent['meta_keywords']);
$smarty->assign('meta_description',$LIZARD_VARScontent['meta_description']);
  //Dynmaic Content from Lizard
$smarty->assign('content_id',($_GET['content_id']) ? $_GET['content_id'] : TWDdb_reference(array('home',$GLOBALS['SITE_VARSdatabase_install']),$GLOBALS['DATABASE_VARSresourcelink'],'skink_content_items',array('section','install_id'),'content_id',__LINE__,__FILE__));
$smarty->assign('content_section',$LIZARD_VARScontent['section']);
$smarty->assign('content_heading',$LIZARD_VARScontent['heading']);
$smarty->assign('content_summary',$LIZARD_VARScontent['summary']);
$smarty->assign('content_content',$LIZARD_VARScontent['content']);
$smarty->assign('content_show',$LIZARD_VARScontent['flag_contentshow']);
$smarty->assign('content_usershow',$LIZARD_VARScontent['flag_usershow']);
$smarty->assign('content_postauthor',$LIZARD_VARScontent['postauthor']);
$smarty->assign('content_postdate',$LIZARD_VARScontent['postdate']);
  //Dynamic Menu from Lizard
$smarty->assign('menu',$LIZARDcontent->menu($smarty));
$smarty->assign('submenu',$LIZARDcontent->submenu($smarty));
  //Skink login session
$smarty->assign('skink_login',$_SESSION['login_flag']);
$smarty->assign('browser_ie',((substr_count(strtoupper($_SERVER['HTTP_USER_AGENT']),'MSIE') > 0 && is_file($GLOBALS['SITE_VARSserver_documentroot'] . $GLOBALS['SITE_VARSserver_root'] . '_websites/' . (($GLOBALS['SITE_VARSwebsite_template']) ? $GLOBALS['SITE_VARSwebsite_template'] : 'default') . '/default.ie.css')) ? true : false));
/**
 * Display sections
 */
if ($GLOBALS['SITE_VARSdisplay_sections'] === true) {
  $ARRsection = explode('_',(($_GET['section']) ? $_GET['section'] : 'home'),2);
  $FILE_READtemplate = ereg_replace('####__PAGE_\[section([|A-Za-z]*)\]__####', (($ARRsection[0] != (($_GET['section']) ? $_GET['section'] : 'home')) ? $ARRsection[0] : 'home'),$FILE_READtemplate);
  $FILE_READtemplate = ereg_replace('####__PAGE_\[page([|A-Za-z]*)\]__####', (($ARRsection[1]) ? $ARRsection[1] : $ARRsection[0]),$FILE_READtemplate);
} else {
  $FILE_READtemplate = ereg_replace('####__PAGE_\[section([|A-Za-z]*)\]__####', (($_GET['section']) ? $_GET['section'] : 'home'),$FILE_READtemplate);
}
if ($_GET['function'] != 'details') $FILE_READtemplate = str_replace('<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=####__TEMPLATE_[googlemapskey]__####" type="text/javascript"></script>','',$FILE_READtemplate);
$FILE_READtemplate = ereg_replace('####__TEMPLATE_\[googlemapskey\]__####', (($GLOBALS['SITE_VARSwebsite_url_local'] == $GLOBALS['SITE_VARSserver_protocol'] . $GLOBALS['SITE_VARSserver_name'] . $GLOBALS['SITE_VARSserver_root']) ? $GLOBALS['SITE_VARSmapping_googlekey_local'] : $GLOBALS['SITE_VARSmapping_googlekey']),$FILE_READtemplate);
$FILE_READtemplate = ereg_replace('####__TEMPLATE_\[flash\]__####', (($GLOBALS['SITE_VARSwebsite_url_local'] == $GLOBALS['SITE_VARSserver_protocol'] . $GLOBALS['SITE_VARSserver_name'] . $GLOBALS['SITE_VARSserver_root']) ? $HTML_STRtemplate_root . 'default_local.swf' : $HTML_STRtemplate_root . 'default.swf'),$FILE_READtemplate);
/**
 * Website Status when editing and turned off
 */
$smarty->assign('skink_message',(((! $GLOBALS['CONFIG_VARSwebsite_status'] || $GLOBALS['CONFIG_VARSwebsite_status'] == 'off') && $_SESSION['skinklogin_flag'] == true) ? true : false));
/**
 * Use the smarty template
 */
$smarty->caching = false;
$smarty->display('default.tpl');
/**
 * End PHP
 */
?>
