* @copyright 2004 by http://wombat.exit0.net * @package wombatSite * @subpackage controller */ /** * Site controller * * @version 1.0.0 * @package wombatSite * @subpackage controller */ class wbSite { /** * patTemplate object * @var object $_tmpl */ var $_tmpl = null; /** * patSession object * @var object $_sess */ var $_sess = null; /** * request variables * @var array $_request */ var $_request = array(); /** * current page * @var array $_page */ var $_page = array(); /** * store main config array * @var array $_config */ var $_config = null; /** * constructor * * @access public */ function __construct() { $this->_config =& wbFactory::getParam( 'config' ); // get request variables $this->_request = $_GET; $this->_request = array_merge( $this->_request, $_POST ); $this->_request = array_merge( $this->_request, $_FILES ); $this->_tmpl =& wbFactory::singleton( 'patTemplate' ); $this->_sess =& wbFactory::singleton( 'patSession' ); wbFactory::includeClass( 'wbJSTools' ); } /** * ozController constructor wrapper for PHP4 * * @access public * @see __construct() */ function wbSite() { $this->__construct(); } /** * overwrite exiting configuration parameter * * @access public * @param string $name key of configuration value * @param mixed $value new configuration parameter * @return boolean $result true on success * @see $_config */ function setConfigParam( $name, $value ) { $this->_config[$name] = $value; return true; } /** * add javascript to template * * @access private * @return boolean $result true on success */ function _insertJavascript() { $code = wbJSTools::getCode(); $this->_tmpl->addGlobalVar( 'javascript_code', $code['code'] ); $this->_tmpl->addGlobalVar( 'javascript_onload', $code['onload'] ); return true; } /** * main processing function * make world :-) * * @access public * @return boolean $result true on success */ function process() { // in which page we are now? $page = $this->_selectPage(); // require login-failed-template in case of failed login $this->_tmpl->readTemplatesFromInput( $this->_page['layout'] . '.tmpl' ); // find user $this->_authenticate(); // check permissions $this->_authorize(); $this->_addGlobalContent(); // process content parts $debugMsg = array( 'Parts:' ); foreach( $this->_config['content'] as $part => $default ) { // overwrite default content $conf = $default; if( isset( $this->_page['content'][$part] ) && is_array( $this->_page['content'][$part] ) ) { $conf = $this->_page['content'][$part]; } // check configuration value: href if( !isset( $conf['href'] ) || empty( $conf['href'] ) ) { $this->_insertErrorContent( $part ); patErrorManager::raiseWarning( 'wbSite:1', 'Configuration error', 'The parameter "href is empty or not set in part "'. $part.'"!' ); continue; } // check configuration value: action if( !isset( $conf['action'] ) || empty( $conf['action'] ) ) { $this->_insertErrorContent( $part ); patErrorManager::raiseWarning( 'wbSite:4', 'Configuration error', 'The parameter "action" is empty or not set in part "'. $part.'"!' ); continue; } $timeStart = wbDebugger::getUTime(); // call specified method to collect content $function = 'getContent' . ucfirst( $conf['action'] ); if( !is_callable( array( $this, $function ) ) ) { $this->_insertErrorContent( $part ); patErrorManager::raiseWarning( 'wbSite:5', 'Configuration error', 'The parameter "action" contains an undefined value in part "'. $part.'"!' ); continue; } // recieve content $content = $this->$function( $conf, $part, $page ); if( patErrorManager::isError( $content ) ) { $this->_insertErrorContent( $part ); continue; } array_push( $debugMsg, $part . ': ('.$conf['action'].'->'. $conf['href'].'): '. wbDebugger::getUTime( $timeStart, 1000, 6 ) .' ms' ); $this->_tmpl->addVar( 'page', $part, $content ); } $this->_insertJavascript(); $this->_tmpl->displayParsedTemplate( 'page' ); // debug output if( wbDebugger::isActive() ) { if( $this->_sess->isNew() ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'New session started', 'Session' ); } else { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, $this->_sess->getCounter() . ' requests', 'Session' ); } array_unshift( $debugMsg, 'Total processing time: ' . wbDebugger::getUtime( 'genesis', 1000, 6 ) . ' ms' ); $msg = implode( "\n", $debugMsg ); wbDebugger::addMsg( WBSITE_DEBUG_SECTION, wbDebugger::sprint( $msg ), 'Stopwatch' ); wbDebugger::printMsg(); // $this->_tmpl->dump(); } return true; } /** * get content for action staticString * * @access public * @param array $conf confiuration for this part * @param string $part named content part * @param string $page named page * @return string $html */ function getContentStaticString( $conf, $part, $page ) { // empty string? return $conf['href']; } /** * get content for action staticTemplate * * @access public * @param array $conf confiuration for this part * @param string $part named content part * @param string $page named page * @return string $html */ function getContentStaticTemplate( $conf, $part, $page ) { $tmpl = 'static/' . $conf['href'] . '.tmpl'; // load failed if( !$this->_tmpl->readTemplatesFromInput( $tmpl ) ) { return patErrorManager::raiseWarning( 'wbSite:2', 'Could not insert static content template', 'Loading template: "'. $conf['href'] .'" in part "'. $part.'"!' ); } return $this->_tmpl->getParsedTemplate( $part ); } /** * get content for action loadModule * * @access public * @param array $conf confiuration for this part * @param string $part named content part * @param string $page named page * @return string $html or patError */ function getContentLoadModule( $conf, $part, $page ) { if( !isset( $conf['params'] ) ) { $conf['params'] = array(); } $mod =& wbFactory::getModule( $conf['href'], $page, $part, $conf['params'], $this->_request ); if( patErrorManager::isError( $mod ) ) { return $mod; } return $mod->getHtml(); } /** * get content for action loadExtension * * @access protected * @param array $conf confiuration for this part * @param string $part named content part * @param string $page named page * @return string $html */ function getContentLoadExtension( $conf, $part, $page ) { if( !isset( $conf['params'] ) ) { $conf['params'] = array(); } // load extension $ext =& wbFactory::getExtension( $conf['href'], $page, $conf['params'], $this->_request ); if( patErrorManager::isError( $ext ) ) { return $ext; } // function not available $function = 'get' . $part; if( !is_callable( array( $ext, $function ) ) ) { return patErrorManager::raiseWarning( 'wbSite:3', 'Could not recieve content from extension', 'Extension: "'. $conf['href'] .'" for part "'. $part.'"!' ); } return $ext->$function(); } /** * get content for action cmsHtml * * @access protected * @param array $conf confiuration for this part * @param string $part named content part * @param string $page named page * @return string $html */ function getContentCmsHtml( $conf, $part, $page ) { $cm =& wbFactory::singleton( 'wbContentManager' ); $con = $cm->getContent( $conf['href'] ); if( patErrorManager::isError( $con ) ) { return $con; } $html = $con['content']; unset( $con['content'] ); $this->_tmpl->addGlobalVars( $con, $part . '_' ); return $html; } /** * insert system vars to templates * * @access protected * @return boolean $result true on success */ function _addGlobalContent() { $get = array(); foreach( $_GET as $key => $value ) { array_push( $get, $key . '=' . $value ); } // add some global data $globals = array( 'page' => $this->_page['name'], 'page_title' => $this->_page['title'], 'page_brief' => $this->_page['brief'], 'self' => $_SERVER['PHP_SELF'] . '?' . $this->_sess->getQueryString(), 'sess_name' => $this->_sess->getName(), 'sess_id' => $this->_sess->getId(), 'request_get' => implode( '&', $get ) ); $globals['path'] = $this->_page['name']; if( isset( $this->_request['path'] ) && !empty( $this->_request['path'] ) ) { $globals['path'] = $this->_request['path']; } $this->_tmpl->addGlobalVars( $globals ); return true; } /** * select page depending on request * - choose used page by path or page * - load page config and store it in $_page * * @access private * @return boolean $result true on success * @see $_page */ function _selectPage() { // default-page $page = $this->_config['home']; $conf =& wbFactory::singleton( 'patConfiguration' ); $confDir = $conf->getConfigDir(); // seelct page by path if( isset( $this->_request['path'] ) && !empty( $this->_request['path'] ) ) { $path = explode( '/', $this->_request['path'] ); if( !empty( $path[ count( $path ) - 1 ] ) ) { $page = array_pop( $path ); } } // select page directy else if( isset( $this->_request['page'] ) ) { $page = $this->_request['page']; } else { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'No page selected, use home-page: "'. $page .'"!', 'Page' ); } // only accept existing pages if( file_exists( $confDir . '/pages/' . $page. '.xml' ) ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, $page, 'Page' ); } else { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Page "' . $page . '" not found, use home-page: "'. $this->_config['home'] .'"!', 'Page' ); $page = $this->_config['home']; } // load page data $conf->loadConfig( '/pages/' . $page . '.xml' ); $this->_page = $conf->getConfigValue( 'page' ); $this->_page['name'] = $page; // use special layout $layout = $conf->getConfigValue( 'layout' ); if( empty( $layout ) ) { $layout = $this->_config['layout']; } $this->_page['layout'] = $layout; wbDebugger::addMsg( WBSITE_DEBUG_SECTION, $layout, 'Layout' ); return $page; } /** * start authetication handler * * - login user * - inform template engine about current user * * @access private * @return boolean $result true on success */ function _authenticate() { // run user authentication module first - this may affect the session-id! $params = array( 'config' => $this->_config['auth'] ); $auth =& wbFactory::singleton( 'wbAuth', $params ); if( !isset( $this->_request['login'] ) ) { $this->_request['login'] = '_no_action_'; } switch( $this->_request['login'] ) { case 'login': if( !isset( $this->_request['login_user'] ) || !isset( $this->_request['login_passwd'] ) ) { break; } $res = $auth->login( $this->_request['login_user'], $this->_request['login_passwd'] ); if( patErrorManager::isError( $res ) ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Login attempt results in error! User "'. $this->_request['login_user'] .'"', 'Login'); // error during login - try to continue break; } if( $res ) { // login successful wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Login succeeded! User "'. $this->_request['login_user'] .'"', 'Login'); } else { // login failed $this->_tmpl->setAttribute( 'login_failed', 'visibility', 'visible' ); $this->_tmpl->addGlobalVar( 'login_user', $this->_request['login_user'] ); wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Login attempt failed! User "'. $this->_request['login_user'] .'"', 'Login' ); } break; case 'logout': $userData = $auth->getUserData(); $res = $auth->logoff(); if( patErrorManager::isError( $res ) ) { return $res; } wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'User "'. $userData['user'] .'" logged out!', 'Login' ); break; // neither login or out default: break; } if( $auth->isAuthenticated() ) { $this->_tmpl->addVar( 'login', 'authenticated', 'yes' ); $userData = $auth->getUserData(); $this->_tmpl->addGlobalVars( $userData, 'user_' ); wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Current User "'. $userData['user'] .'"', 'Auth' ); } return true; } /** * run authorisation plugin * - start auth module * - mangle page-config if necessary * * @access private * @return boolean $result true on success */ function _authorize() { $module = $this->_config['auth']['module']; $param = $this->_config['auth']['param']; if( isset( $this->_page['auth'] ) && !empty( $this->_page['auth'] ) ) { $module = $this->_page['auth']['module']; $param = $this->_page['auth']['param']; } $auth =& wbFactory::singleton( 'wbAuthorize' ); $auth->setRule( $module, $param ); $result = $auth->checkAuthorisation(); // error if( patErrorManager::isError( $result ) ) { return $result; } // authorisation completed if( $result ) { return true; } // otherwise change change page-settings foreach( $this->_config['auth']['denied'] as $part => $conf ) { $this->_page['content'][$part] = $conf; } wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Permission denied! Module: "'. $module.'" Parameter: "'. $param.'"', 'Authorisation' ); return false; } /** * display error message * * @access private * @param string $part content area to insert error message * @return boolean $result true on success * @see */ function _insertErrorContent( $part ) { if( !$this->_tmpl->exists( 'error_' . $part ) ) { $this->_tmpl->readTemplatesFromInput( $this->_config['errortemplate'] . '.tmpl' ); } // $this->_tmpl->parseIntoVar( 'error_' . $part, 'page', $part ); /** * workaround for patTemplate bug in parseIntoVar */ $err = $this->_tmpl->getParsedTemplate( 'error_' . $part ); $this->_tmpl->addVar( 'page', $part, $err ); return true; } } ?>