<?php

/**
 * Routing file for phpWebSite
 *
 * Index initializes the core and database
 *
 * @version $Id: index.php,v 1.79 2005/02/28 14:35:30 matt Exp $
 * @author Matthew McNaney <matt@NOSPAM.tux.appstate.edu>
 * @modified by Steven Levin <steven@NOSPAM.tux.appstate.edu>
 * @modified by Adam Morton <adam@NOSPAM.tux.appstate.edu>
 * @package phpWebSite
 */

/* Show all errors */
//error_reporting (E_ALL);

// Change to TRUE to allow DEBUG mode
define('DEBUG_MODE', FALSE);

$GLOBALS['ALWAYS'] = array('layout', 'users', 'language', 'fatcat', 'search', 'menuman', 'comments');

/* Security against those with register globals = on */
if (isset($_POST)){
    foreach ($_POST as $postVarName=>$nullIT) {
	unset($postVarName);
    }
}


secureFiles();

/* Clean out bad script, php, and newline characters */
cleanArray($_REQUEST);
cleanArray($_GET);

if (!isset($hub_dir)) {
    $hub_dir = NULL;
}

/* Check to make sure $hub_dir is not set to an address */
if (!preg_match ("/:\/\//i", $hub_dir)) {
    loadConfig($hub_dir);
} else {
    exit('FATAL ERROR! Hub directory was malformed.');
}

if (file_exists(PHPWS_SOURCE_DIR . 'core/Core.php') && file_exists(PHPWS_SOURCE_DIR . 'core/Debug.php')) {
    require_once PHPWS_SOURCE_DIR . 'core/Core.php';
   
    if(DEBUG_MODE) {
	require_once 'Benchmark/Timer.php';
	$PHPWS_Timer =& new Benchmark_Timer();
	$PHPWS_Timer->start();
	$PHPWS_Timer->setMarker('Begin Core Initialization');
    }
} else {
    exit('FATAL ERROR! Required file <b>Core.php</b> not found.');
}

if (!isset($branchName)) {
    $branchName = NULL;
}

$GLOBALS['core'] =& new PHPWS_Core($branchName, $hub_dir);

if (DEBUG_MODE) {
    $PHPWS_Timer->setMarker('End Core Initialization');
}

$includeList = $core->initModules();

if (DEBUG_MODE) {
    /* phpWebSite debugger */
    if (!isset($_SESSION['PHPWS_Debug'])) {
	$_SESSION['PHPWS_Debug'] =& new PHPWS_Debug();
    }

    if ($_SESSION['PHPWS_Debug']->isActive()) {
	$_SESSION['PHPWS_Debug']->displayDebugInfo(TRUE);
	
	if ($_SESSION['PHPWS_Debug']->getBeforeExecution()) {
	    $_SESSION['PHPWS_Debug']->displayDebugInfo(FALSE);
	}
    }
}

$current_mod_file = NULL;

foreach ($includeList as $mod_title=>$current_mod_file) {
    if (in_array($mod_title, $GLOBALS['ALWAYS']) || (isset($_REQUEST['module']) && ($_REQUEST['module'] == $mod_title))) {	
	if (DEBUG_MODE) {
	    $PHPWS_Timer->setMarker("Begin $mod_title Execution");
	}
    
	$core->current_mod = $mod_title;
	
	if (is_file($current_mod_file)) {
	    include_once($current_mod_file);
	}
	
	if (DEBUG_MODE) {
	    $PHPWS_Timer->setMarker("End $mod_title Execution");
	}
    }
    
    if (is_file(PHPWS_SOURCE_DIR . "mod/$mod_title/inc/runtime.php")) {
	include PHPWS_SOURCE_DIR . "mod/$mod_title/inc/runtime.php";
    }
}

/* Preventing last mod loaded from being 'current_mod' */
$core->current_mod = NULL;
$core->db->disconnect();

if (DEBUG_MODE) {
    $PHPWS_Timer->stop();
    
    /* phpWebSite debugger */
    if ($_SESSION['PHPWS_Debug']->isActive()) {
	if ($_SESSION['PHPWS_Debug']->getShowTimer()) {
	    echo '<br /><font size="+1">phpWebSite Timer</font><br />';
	    $PHPWS_Timer->display();
	    echo '<br />';
	}
	if ($_SESSION['PHPWS_Debug']->getAfterExecution()) {
	    $_SESSION['PHPWS_Debug']->displayDebugInfo(FALSE);
	}
    }
}

/* Loads the hubs config file and sets the source directory */
function loadConfig($hub_dir){
    if (file_exists($hub_dir . 'conf/config.php')) {
	if (filesize($hub_dir . 'conf/config.php') > 0) {
	    include($hub_dir . 'conf/config.php');
	    define('PHPWS_SOURCE_DIR', $source_dir);
	} else {
	    header('Location: ./setup/set_config.php');
	    exit();
	}
    } else {
	header('Location: ./setup/set_config.php');
	exit();
    }  
}

/* Prevent scripting and bad new line characters from being passed via http get */
function cleanArray(&$Value) {
    if (is_array($Value)) {
	array_walk ($Value, 'cleanArray');
    } else {
	$scriptPatterns = array('/<+script/i', '/(%3C)+script/i', '/(&lt;)+script/i', '/(&#60;)+script/i');
	$phpPatterns    = array("'<+\?'", "'(%3C)+\?'", "'(&lt;)+\?'", "'(&#60;)+\?'");

	$Value = preg_replace($scriptPatterns, 'NOSCRIPT', $Value);
	$Value = preg_replace($phpPatterns, 'NOPHP', $Value);
    }
    return;
}

function secureFiles()
{
  if (!isset($_FILES)) {
    return;
  }

  foreach ($_FILES as $file_key => $checkFile) {
    $temp_name = $checkFile['tmp_name'];
    if (is_array($temp_name)) {
      foreach ($checkFile['tmp_name'] as $tmp_key => $tmp_name) {
	if (!secureImage($tmp_name)) {
	  $_FILES[$file_key]['tmp_name'][$tmp_key] = NULL;
	}
      }
    } else {
      if (!secureImage($checkFile['tmp_name'])) {
	$_FILES[$file_key]['tmp_name'] = NULL;
      }
    }

  }
}

function secureImage($filename) {
  if (empty($filename)) {
    return FALSE;
  }
  $secure_array = array('dl\(', 'exec', 'passthru', 'proc_open', 'unlink',
			'chown', 'chmod', 'chgrp',
			'proc_close', 'shell_exec', 'system', 'rmdir',
			'\$_GET', '\$_POST', '\$_REQUEST', 'fopen');
  
  $match = implode('|', $secure_array);
  
  $hdl = fopen($filename,'r');
  if ($hdl) {
    while (!feof($hdl)) {
      $check = preg_replace('/\s/', '', fgets($hdl, 1024));
      if (preg_match('/' . $match . '/i', $check)) {
	fclose($hdl); 
	return FALSE;
      }
    }
    fclose($hdl);
    return TRUE;
  } else {
    return FALSE;
  }
}

/* Uncomment to see memory usage */
//echo round((memory_get_usage()/1024)/1024, 2) . 'MB';

?>
