<?php
/* ================================================================================================
WaypointHR
www.waypointhr.com
Copyright 2009 HR-Fundamentals ltd.
----------------------------------------------------------------------------------------------------
This file is part of WaypointHR.
WaypointHR is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
WaypointHR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with WaypointHR.
If not, see <http://www.gnu.org/licenses/>.
----------------------------------------------------------------------------------------------------
ROOT CONTROLLER
$Revision: 591 $
$LastChangedDate: 2009-07-27 23:57:02 +0100 (Mon, 27 Jul 2009) $
----------------------------------------------------------------------------------------------------
Load and instantiate various common components
================================================================================================= */
// Environment setup:
error_reporting(E_ALL); //error_reporting(0);
ob_start();
define('application_directory', dirname(__FILE__));
define('current_build', 1505);
// Request data (query/post) setup:
$global_params = null;
import_request_data();
// View base class:
require_once constant('application_directory').'/components/view.php';
// Internationalisation component:
require_once constant('application_directory').'/components/i18n.php';
$i18n = new i18n;
// Configuration component:
if (file_exists(constant('application_directory').'/conf/installation_settings.php')) {
require_once constant('application_directory').'/conf/installation_settings.php';
$global_config = new config_sys();
// Diagnostic component:
if (defined('development_setting_debug_console')&&constant('development_setting_debug_console')===true) {
require_once constant('application_directory').'/components/firephp/FirePHP.class.php';
@$global_params['debug'] = FirePHP::getInstance(true);
}
// Database component:
require_once constant('application_directory').'/components/db_engine.php';
$global_db_engine = new db_engine;
if ($global_db_engine->valid_connection()===true) {
// User authentication component:
$auth_cookie_data = '';
if (array_key_exists('whr_auth',$_COOKIE)) {
$auth_cookie_data = $_COOKIE['whr_auth'];
}
if (qs_value('module')==='logout') {
// Delete the cookie
setcookie('whr_auth', 'undefined', time()-3600);
$global_params['qs']['module'] = 'login';
$auth_cookie_data = '';
// TODO: Remove authentication record from the database
} else {
require_once constant('application_directory').'/components/authentication.php';
$authentication_manager=new authentication();
if ($auth_cookie_data==='' || !$authentication_manager->user_authenticate($auth_cookie_data)) {
// If no cookie presented, or the cookie could not be authenticated (expired?)
if (is_array($global_params['post']) && array_key_exists('username', $global_params['post']) && array_key_exists('password', $global_params['post'])) {
// If login form postback...
$username = $global_params['post']['username'];
$password = $global_params['post']['password'];
$authentication_manager->user_login($username, $password);
}
}
}
}
}
if (!is_array($global_params['qs']) || !array_key_exists('module', $global_params['qs'])) {
// Default module if none provided
$global_params['qs']['module'] = 'employees';
}
if (defined('auth_user') && (int)constant('auth_user')>0 && $global_params['qs']['module']==='login') {
// If recently authenticated, switch from login to default module
$global_params['qs']['module'] = 'employees';
}
if (class_exists('config_sys')) {
if ($global_db_engine->valid_connection()===true) {
if (defined('auth_user') && (int)constant('auth_user')>0) {
$content_data['PAGE_TITLE'] = 'WaypointHR';
$content_data['NAVIGATION'] = navigation();
} else {
$content_data['PAGE_TITLE'] = 'WaypointHR - ##I18N:login.login##';
$content_data['NAVIGATION'] = navigation_login();
if (qs_value('module')!=='setlang') { // Only 'login' and 'setlang' permitted if not authenticated
$global_params['qs']['module'] = 'login';
}
}
} else {
$content_data['PAGE_TITLE'] = 'WaypointHR - ##I18N:connection_problem##';
$content_data['NAVIGATION'] = navigation_database_failure();
$global_params['qs']['module'] = '';
$global_params['qs']['view'] = '';
}
} else {
$content_data['PAGE_TITLE'] = 'WaypointHR - ##I18N:setup.initial_setup##';
$content_data['NAVIGATION'] = navigation_installation();
$global_params['qs']['module'] = 'setup';
}
if (file_exists(constant('application_directory') . "/modules/{$global_params['qs']['module']}/controller.php")) {
include_once constant('application_directory') . "/modules/{$global_params['qs']['module']}/controller.php";
} elseif (defined('addons_local_dir') && file_exists(constant('application_directory') . '/' . constant('addons_local_dir') . "/{$global_params['qs']['module']}/controller.php")) {
include_once constant('application_directory') . '/' . constant('addons_local_dir') . "/{$global_params['qs']['module']}/controller.php";
}
if (isset($view)) {
$content_data['TOOLBAR'] = $view->render_toolbar();
$content_data['LOAD_EVENT'] = $view->render_loadevent();
$content_data['BODY'] = $view->render();
$content_data['HEAD'] = $view->additional_stylesheets() . $view->additional_scripts();
} else {
if ($global_db_engine->valid_connection()===true) {
$summary = '<h1>##I18N:sorry##</h1>##I18N:no_such_page##';
} else {
$summary = '<h1>##I18N:sorry##</h1>##I18N:db_unavailable##';
}
$menu = '<div id="nav-shell"><ul class="nav_section"><li class="current"><span class="nolink">WaypointHR</span></li><li><a href="http://waypointhr.com">##I18N:setup.help_and_support##</a></li></ul></div>';
$content = "<div id=\"content_shell\"><div id=\"content_body\">$summary</div></div>\n<div id=\"nav_section\">{$menu}</div>\n";
$content_data['TOOLBAR'] = ' ';
$content_data['BODY'] = $content;
$content_data['HEAD'] = '';
}
$error_text = ob_get_contents();
ob_end_clean();
if (qs_value('method')==='async') {
if (defined('auth_user') && (int)constant('auth_user')>0) {
header('Cache-Control: no-cache');
header('Content-Type: text/html; charset=utf-8');
if ($error_text!=='') {
if (array_key_exists('BODY',$content_data)) {
$error_text = str_replace("\n",'',$error_text); // Strip return codes that would otherwise break our handler
$content_data['BODY'] .= "##error_text##<h1>##I18N:an_error_occurred##</h1>{$error_text}";
}
}
} else {
$content_data['BODY'] .= '##javascript##window.location="./?module=login";';
}
$i18n->translation($content_data['BODY']);
echo $content_data['BODY'];
} else {
// Check for ['inhibit_html'] if no HTML templating required (e.g. PDF streaming)
if (!array_key_exists('inhibit_html', $global_params) || $global_params['inhibit_html']!==true) {
header('Content-Type: text/html; charset=utf-8');
$global_params['skin'] = 'default';
$template = file_get_contents(constant('application_directory') . "/skins/{$global_params['skin']}/common.html");
if ($error_text!=='') {
if (array_key_exists('BODY',$content_data)) {
$content_data['MESSAGE_BODY'] = "<h1>##I18N:an_error_occurred##</h1>{$error_text}";
} else {
echo "{$error_text}<br>";
}
}
//TODO: This needs to be moved out (or replicated into the above async block) to allow async content translation:
if (isset($content_data) && is_array($content_data) && count($content_data)>0) {
foreach ($content_data as $content_key=>$content_value) {
$template = str_replace("##{$content_key}##", $content_value, $template);
}
}
$i18n->translation($template);
$template = preg_replace('/##([\-0-9:A-Z_ ]+)##/i','',$template);
echo $template;
} else {
echo $error_text;
}
}
/**
* Private functions
* @private
*/
function navigation() {
$module_list = null;
$module_list['employees'] = array('title' => '##I18N:MODULE_TITLE_EMPLOYEES##', 'display_style' => 0);
//TODO: Cache this routine, refreshing cache on 'install new modules'
if (defined('addons_local_dir') && constant('addons_local_dir')!=='' && is_dir(constant('addons_local_dir'))) {
if ($dir_handle = opendir(constant('addons_local_dir'))) {
while (($addon_item_dir = readdir($dir_handle))!==false) {
if (is_dir(constant('addons_local_dir') . $addon_item_dir) && $addon_item_dir!=='.' && $addon_item_dir!=='..') {
// TODO: Relocate outside of the navigation fn
// Read the addon's language file (if available)
if (file_exists(constant('addons_local_dir') . $addon_item_dir . '/i18n.php')) {
include_once constant('addons_local_dir') . $addon_item_dir . '/i18n.php';
$addon_item_class = $addon_item_dir . '_i18n';
if (class_exists($addon_item_class)) {
$addon_i18n = new $addon_item_class;
if (isset($addon_i18n->strings)) {
//$i18n->add_strings($addon_i18n->strings);
}
}
}
if (!is_array($module_list)||!array_key_exists($addon_item_dir,$module_list)) {
$module_list[$addon_item_dir] = array(
'title' => "##I18N:MODULE_TITLE_{$addon_item_dir}##",
'display_style' => 0
);
}
}
}
}
}
$module_list['reports'] = array('title' => '##I18N:MODULE_TITLE_REPORTS##', 'display_style' => 0);
$module_list['options'] = array('title' => '##I18N:MODULE_TITLE_OPTIONS##', 'display_style' => 1);
$module_list['logout'] = array('title' => '##I18N:MODULE_TITLE_LOGOUT##', 'display_style' => 1);
$output = "<ul>\n";
if ($module_list!==false) {
foreach ($module_list as $module_key => $module) {
if ($module['display_style']===1) {
$class = 'notab';
} else {
$class = '';
}
if (qs_value('module')===$module_key) {
if ($class==='') {
$class = 'current';
} else {
$class .= '_current';
}
}
$output .= "<li id=\"nav_perm_{$module_key}\" class=\"{$class}\"><a href=\"./?module={$module_key}\">{$module['title']}</a></li>\n";
}
}
$output .= "</ul>\n";
return $output;
}
function navigation_installation() {
return "<ul>\n\t<li id=\"nav_perm_opts\" class=\"current\"><a href=\"./?module=setup\">##I18N:setup.initial_setup##</a></li>\n</ul>";
}
function navigation_login() {
switch(qs_value('module')) {
case 'setlang':
$class_login='';
$class_setlang='notab_current';
break;
default:
$class_login='current';
$class_setlang='notab';
break;
}
$output="<ul>\n
\t<li id=\"nav_perm_opts\" class=\"{$class_login}\"><a href=\"./?module=login\">##I18N:welcome_to_waypointhr##</a></li>\n
\t<li id=\"nav_perm_language\" class=\"{$class_setlang}\"><a href=\"./?module=setlang\">##I18N:module_title_language##</a></li>\n
</ul>\n";
return $output;
}
function navigation_database_failure() {
return "<ul>\n\t<li id=\"nav_perm_opts\" class=\"current\"><a href=\"./?module=options&view=installation\">##I18N:setup_and_diagnostics##</a></li>\n</ul>\n";
}
/**
* Insert querystring parameters and post data into arrays
*/
function import_request_data() {
global $global_params;
$global_params['qs'] = '';
$global_params['post'] = '';
if (get_magic_quotes_gpc()===0) {
foreach ($_GET as $qs_key => $qv_value) {
$global_params['qs'][$qs_key] = $qv_value;
}
foreach ($_POST as $post_key => $post_value) {
$global_params['post'][$post_key] = $post_value;
}
} else {
foreach ($_GET as $qs_key => $qv_value) {
$global_params['qs'][$qs_key] = stripslashes_deep($qv_value);
}
foreach ($_POST as $post_key => $post_value) {
$global_params['post'][$post_key] = stripslashes_deep($post_value);
}
}
}
function qs_value($key,$cookie_name='') {
global $global_params;
if ($key!=='' && is_array($global_params['qs']) && array_key_exists($key, $global_params['qs'])) {
return $global_params['qs'][$key];
} else {
if ($cookie_name==='') {
return '';
} else {
if (array_key_exists($cookie_name, $_COOKIE)) {
return $_COOKIE[$cookie_name];
} else {
return '';
}
}
}
}
function stripslashes_deep($value) {
if (is_array($value)) {
return array_map('stripslashes_deep', $value);
} else {
return stripslashes($value);
}
}
function debug($var) {
$output = '';
if (is_array($var)) {
ob_start();
echo "<h2>{$var}</h2>";
var_dump($var);
$output = ob_get_contents();
ob_end_clean();
$output .= '<br>';
} elseif (is_string($var)) {
return "<p>String: {$var}</p>";
} elseif (is_int($var)) {
return "<p>Int: {$var}</p>";
} else {
ob_start();
echo "<h2>{$var}</h2>";
var_dump($var);
$output = ob_get_contents();
ob_end_clean();
}
return $output;
}
function str2hex($string) {
$output = null;
for ($i=0; $i<strlen($string); $i++) {
$output[] = dechex(ord($string[$i])) . "[{$string[$i]}]";
}
return implode('.', $output);
}