. */ /** * 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 */ 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; $iAuth($_POST['sess_user'], $_POST['sess_pass']) ) { $accountService = $GLOBALS['ClassFactory']->getAegeeAccountService(); $account = $accountService->load($auth->getMyDn()); if( $account==NULL ) { $this->warn("Failed to load account of user after login: ".$auth->getMyDn()); }elseif( $account->getActive() ) { $_SESSION['sess_dn'] = $account->getDn(); $_SESSION['sess_uid'] = $account->getUid(); $_SESSION['sess_mail'] = $account->getMail(0); $_SESSION['sess_access'] = Access::VISITOR; $bodyService = $GLOBALS['ClassFactory']->getAegeeBodyService(); $memberService = $GLOBALS['ClassFactory']->getAegeeMemberService(); $members = $memberService->loadUidAccess($account->getUid()); if ( is_array($members) ){ foreach ($members as $BodyCode=>$functions){ if (! isset( $SESSION[$BodyCode]['BodyName'] ) ) { $load = $bodyService->loadNameCategory($BodyCode); $SESSION[$BodyCode]['BodyName'] = $load['BodyName']; $SESSION[$BodyCode]['BodyCategory'] = $load['BodyCategory']; $SESSION[$BodyCode]['BodyCategoryOrder'] = $load['BodyCategoryOrder']; } foreach ($functions as $key=>$memberType){ $acc=Access::getAccessNumber($memberType); $SESSION[$BodyCode][$memberType]=$acc; if ( $acc>$_SESSION['sess_access'] ) $_SESSION['sess_access'] = $acc; } } } $groupService = $GLOBALS['ClassFactory']->getAegeeGroupService(); $groups=$groupService->loadUidAccess($account->getUid()); if ( is_array($groups) ){ foreach ($groups as $BodyCode=>$functions){ if (! isset( $SESSION[$BodyCode]['BodyName'] ) ) { $load = $bodyService->loadNameCategory($BodyCode); $SESSION[$BodyCode]['BodyName'] = $load['BodyName']; $SESSION[$BodyCode]['BodyCategory'] = $load['BodyCategory']; $SESSION[$BodyCode]['BodyCategoryOrder'] = $load['BodyCategoryOrder']; } foreach ($functions as $key=>$cn){ $acc=Access::getAccessNumber($cn); $SESSION[$BodyCode][$cn]=$acc; if ( $acc>$_SESSION['sess_access'] ) $_SESSION['sess_access'] = $acc; } } } $moved = true; while ( $moved ){ $moved = false; foreach ( $SESSION as $BodyCode => $values ) { if (! isset( $preBodyCategoryOrder )){ $preBodyCategoryOrder=$values['BodyCategoryOrder']; $preBodyName=$values['BodyName']; $preBodyCode=$BodyCode; } if ( $values['BodyCategoryOrder'] < $preBodyCategoryOrder ) { $move[$preBodyCode]=$SESSION[$preBodyCode]; unset($SESSION[$preBodyCode]); $SESSION[$preBodyCode] = $move[$preBodyCode]; unset($preBodyCategoryOrder); unset($move); $moved = true; break; } $preBodyCategoryOrder=$values['BodyCategoryOrder']; $preBodyName=$values['BodyName']; $preBodyCode=$BodyCode; } } $moved = true; unset($preBodyCategoryOrder); while ( $moved ){ $moved = false; foreach ( $SESSION as $BodyCode => $values ) { if (! isset( $preBodyCategoryOrder )){ $preBodyCategoryOrder=$values['BodyCategoryOrder']; $preBodyName=$values['BodyName']; $preBodyCode=$BodyCode; } if ( $values['BodyCategoryOrder'] == $preBodyCategoryOrder ) { if ( strcasecmp($values['BodyName'], $preBodyName ) < 0) { $move[$preBodyCode]=$SESSION[$preBodyCode]; unset($SESSION[$preBodyCode]); $SESSION[$preBodyCode] = $move[$preBodyCode]; unset($preBodyCategoryOrder); unset($move); $moved = true; break; } } $preBodyCategoryOrder=$values['BodyCategoryOrder']; $preBodyName=$values['BodyName']; $preBodyCode=$BodyCode; } } $_SESSION['sess_access_bodyCodes'] = $SESSION; }else { $this->warn("User tried to log in to a deactivated account: ".$account->getUid()); } }else { $this->error("Login failed: ".$auth->getLastError()); } $auth->__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 }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['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)); } } } ?>