* @copyright 2004 by gERD Schaufelberger * @package wombat * @subpackage admin */ /** * admin interface * * @version 1.0.1 * @package wombat * @subpackage Admin */ class wbAdmin { /** * patTemplate object * @var object $_tmpl */ var $_tmpl; /** * patSession object * @var object $_sess */ var $_sess; /** * visit id - session id for each visit * * Used by logging mechanism to track useers even if their session-id has changed (e.g. during log in) * * @var string $_visitId */ var $_visitId = ''; /** * wbAuth object * @var object $_auth */ var $_auth; /** * request variables * @var array $_request */ var $_request = array(); /** * store main config array * @var array $_config */ var $_config = array(); /** * store main config array * @var array $_config */ var $_configAdmin = array(); /** * current application * @var string $_app */ var $_app = 'Welcome'; /** * application meta info * @var array $_appInfo */ var $_appInfo = array(); /** * application menu items * @var array $_appMenu */ var $_appMenu = array(); /** * constructor * * @access public */ function __construct() { // load tools header( 'Content-Type: text/html; charset=utf-8;' ); $this->_sess =& wbFactory::singleton( 'patSession' ); if( !$this->_sess->has( 'wbAdmin:visitId' ) ) { $this->_sess->set( 'wbAdmin:visitId', $this->_sess->getId() ); } $this->_visitId = $this->_sess->get( 'wbAdmin:visitId' ); $params = array( 'tmplDir' => wbFactory::getParam( 'systemDir' ) . '/'. wbFactory::getParam( 'adminDir' ) . '/templates' ); $this->_tmpl =& wbFactory::singleton( 'patTemplate', $params ); // recieve main config $this->_config =& wbFactory::getParam( 'config' ); // load admin config $conf =& wbFactory::singleton( 'patConfiguration' ); $conf->loadConfig( 'admin.xml' ); $this->_configAdmin = $conf->getConfigValue(); // get request variables $this->_request = $_GET; $this->_request = array_merge( $this->_request, $_POST ); $this->_request = array_merge( $this->_request, $_FILES ); // select application path if( isset( $this->_request['app'] ) ) { $this->_app = $this->_request['app']; } // run user authentication module first - this may affect the session-id! $params = array( 'config' => $this->_configAdmin['auth'] ); $this->_auth =& wbFactory::singleton( 'wbAuth', $params ); // select session language if( isset( $this->_request['lang'] ) ) { $lang = $this->_request['lang']; if( in_array( $lang, $this->_configAdmin['languages'] ) ) { $this->initLang( $lang ); } } else { $this->initLang(); } // load meta data $menuDir = wbFactory::getParam( 'baseDir' ) . '/' . wbFactory::getParam( 'varDir' ) . '/admin/menu'; $this->_appInfo = unserialize( file_get_contents( $menuDir . '/info.ser' ) ); $this->_appMenu = unserialize( file_get_contents( $menuDir . '/menu.ser' ) ); wbFactory::includeClass( 'wbEvent' ); wbFactory::includeClass( 'wbLog' ); wbFactory::includeClass( 'wbJSTools' ); } /** * constructor wrapper for PHP4 * * @access public * @see __construct() */ function wbAdmin() { $this->__construct(); } /** * run admin interface * * @access public * @return boolean true on success */ function process() { // get applcation content $appPath = explode( '/', $this->_app ); if( !$this->_auth->isAuthenticated() ) { if( $appPath[0] != 'Login' ) { $this->_app = 'Login'; $appPath = array( 'Login' ); } wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'User not authenticated', 'Auth' ); } if( !isset( $this->_appInfo[$appPath[0] ] ) ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Invalid application "'. $this->_app . '" selected. switch to default application!', 'Application' ); $this->_app = 'Login'; $appPath = array( 'Login' ); } wbDebugger::addMsg( WBSITE_DEBUG_SECTION, $this->_app, 'Application' ); wbLog::debug( 'admin', array( $this->_visitId, 'admin', 'Application: ' . $this->_app ) ); $appHandler = array_shift( $appPath ); $application =& wbFactory::singleton( 'wbAdminApp_' . $appHandler ); if( patErrorManager::isError( $application ) ) { // exit programme! patErrorManager::raiseError( 'wbAdmin:1', 'Processing failed!', 'Could not load application: "' . $appHandler . '"' ); return false; } // run applicattion $application->setConfig( $this->_configAdmin ); $application->setLang( $this->_lang ); $application->setRequest( $this->_request ); $application->setVisitId( $this->_visitId ); // add global template variables $this->_addGlobalContent( $application ); $res = $application->process( $appPath ); if( patErrorManager::isError( $res ) ) { return $res; } if( $this->_sess->isNew() ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'New session started', 'Session' ); } else { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, $this->_sess->getCounter() . ' requests', 'Session' ); } // add messages $msg = $application->getMsg(); if( !empty( $msg ) ) { $this->_loadTemplate( 'msg' ); $this->_tmpl->addVar( 'msg_entry', 'msg', $msg ); $this->_tmpl->addGlobalVar( 'msg', $this->_tmpl->getParsedTemplate( 'msg' ) ); } // some applications don't need surrounding template $fullScreen = $application->isFullscreen(); $html = $application->getHtml(); if( patErrorManager::isError( $html ) ) { patErrorManager::raiseError( 'wbAdmin:3', 'Processing failed!', 'Could not get html from application: "' . $appHandler . '"' ); return false; } if( $fullScreen ) { echo $html; wbDebugger::printMsg(); return true; } $this->_loadTemplate( 'page' ); // build menu content $menu = $this->_getMenuHtml( $application ); if( patErrorManager::isError( $menu ) ) { return $menu; } $this->_tmpl->addVar( 'page', 'MENU', $menu ); $this->_tmpl->addVar( 'page', 'APPLICATION', $html ); $this->_insertJavascript(); $this->_tmpl->displayParsedTemplate( 'page' ); wbDebugger::printMsg(); return true; } /** * load menu and transform to html * * @access private * �aram object $application link to running application * @return string $html parsed template */ function _getMenuHtml( &$application ) { $appPath = explode( '/', $this->_app ); $this->_loadTemplate( 'menu' ); $menu = $this->_configAdmin['menu']; for( $i = 0; $i < count( $menu ); ++$i ) { $menu[$i]['state'] = '_none_'; foreach( $this->_appInfo[$menu[$i]['app'] ] as $key => $value ) { $menu[$i][$key] = $value; } if( isset( $menu[$i]['title'] ) ) { $menu[$i]['title'] = _( $menu[$i]['title'] ); } if( isset( $menu[$i]['brief'] ) ) { $menu[$i]['brief'] = _( $menu[$i]['brief'] ); } // add submenu $submenu = $this->_appMenu[$appPath[0]]; if( $appPath[0] == $menu[$i]['app'] ) { // try to load dynamic submenu if( empty( $submenu ) ) { $this->_appMenu[$appPath[0]] = $application->getMenu(); $submenu = $this->_appMenu[$appPath[0]]; } // find current path if( isset( $appPath[1] ) ) { for( $j = 0; $j < count( $submenu ); ++$j ) { if( $appPath[1] == $submenu[$j]['name'] ) { $submenu[$j]['state'] = 'selected'; } } } // translate for( $j = 0; $j < count( $submenu ); ++$j ) { if( isset( $submenu[$j]['title'] ) ) { $submenu[$j]['title'] = _( $submenu[$j]['title'] ); } if( isset( $submenu[$j]['brief'] ) ) { $submenu[$j]['brief'] = _( $submenu[$j]['brief'] ); } } $this->_tmpl->addRows( 'appmenu_entry', $submenu ); $menu[$i]['appmenu'] = $this->_tmpl->getParsedTemplate( 'appmenu_entry' ); $menu[$i]['state'] = 'selected'; } } $this->_tmpl->addRows( 'menu_entry', $menu ); return $this->_tmpl->getParsedTemplate( 'menu' ); } /** * 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; } /** * insert system vars to templates * * @access protected * @param object $application link to running application * @return boolean $result true on success */ function _addGlobalContent( &$application ) { $proc = explode( '/', $this->_app ); $title = $this->_appInfo[$proc[0]]['title']; $brief = $this->_appInfo[$proc[0]]['brief']; $parent = ''; if( count( $proc ) > 1 ) { $parent = $title; // try to load submenu from application if( empty( $this->_appMenu[$proc[0]] ) ) { $this->_appMenu[$proc[0]] = $application->getMenu(); } if( !is_array( $this->_appMenu[$proc[0]] ) ) { $this->_appMenu[$proc[0]] = array(); } foreach( $this->_appMenu[$proc[0]] as $mi ) { if( $mi['name'] == $proc[1] ) { $title = $mi['title']; $brief = $mi['brief']; } } } // add some global data $globals = array( 'url' => $this->_config['url'], 'app' => $this->_app, 'app_title' => $title, 'app_brief' => $brief, 'app_parent' => $parent, 'proc' => $proc[0], 'proc_title' => $this->_appInfo[$proc[0]]['title'], 'proc_brief' => $this->_appInfo[$proc[0]]['brief'], 'self' => $_SERVER['PHP_SELF'] . '?' . $this->_sess->getQueryString(), 'sess_name' => $this->_sess->getName(), 'sess_id' => $this->_sess->getId(), 'request_get' => '' ); // add global template variables $get = array(); foreach( $_GET as $key => $value ) { if( isset( $globals[$key] ) ) { continue; } array_push( $get, $key . '=' . $value ); } $globals['request_get'] = implode( '&', $get ); $this->_tmpl->addGlobalVars( $globals ); $application->addGlobalTemplateVars( $globals ); // add user data if( $this->_auth->isAuthenticated() ) { $user = $this->_auth->getUserData(); $this->_tmpl->addGlobalVars( $user, 'user_' ); $application->addGlobalTemplateVars( $user, 'user_' ); } // init language selector $this->_tmpl->readTemplatesFromInput( 'language.tmpl' ); $langs = array(); foreach( $this->_configAdmin['languages'] as $lang ) { if( $lang == 'C' ) { $flag = 'C'; } else { $flag = substr( $lang, 0, 2 ); } $l = array( 'value' => $lang, 'name' => $lang, 'flag' => $flag ); if( $this->_lang['lang'] == $lang ) { $l['selected'] = 'selected="selected"'; } array_push( $langs, $l ); } $this->_tmpl->addRows( 'wbAdmin_language_entry', $langs ); //$this->_tmpl->addGlobalVar( 'langselect', $this->_tmpl->getParsedTemplate( 'wbAdmin_language' ) ); //$this->_tmpl->addGlobalVar( 'langactive', $this->_lang['lang'] ); $globals = array( 'langselect' => $this->_tmpl->getParsedTemplate( 'wbAdmin_language' ), 'langactive' => $this->_lang['lang'] ); $this->_tmpl->addGlobalVars( $globals ); $application->addGlobalTemplateVars( $globals ); return true; } /** * setup langauge container * * @access public * @param string $tmpl templates base name * @return boolean $result true on success */ function initLang( $lang = null ) { $set = false; if( $lang ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'New session language "'. $lang . '"', 'Language' ); $set = true; } else if( !$this->_sess->has( 'wb:lang' ) ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'Session language from config "'. $this->_configAdmin['locale'] . '"', 'Language' ); $lang = $this->_configAdmin['locale']; $set = true; } if( $set ) { $this->_lang = array( 'lang' => $lang, 'form' => $lang, ); if( $lang != 'C' ) { $this->_lang['tmpl'] = substr( $lang, 0, 2 ); $this->_lang['form'] = substr( $lang, 0, 2 ); } $this->_sess->set( 'wb:lang', $this->_lang ); } else { $this->_lang = $this->_sess->get( 'wb:lang' ); } bindtextdomain( 'wombat', wbFactory::getParam( 'systemDir' ) . '/var/lang' ); bind_textdomain_codeset( 'wombat', 'UTF-8' ); textdomain('wombat'); setlocale( LC_ALL, $this->_lang['lang'] . '.UTF-8' ); return true; } /** * load a template using locae settings * * @access protected * @param string $tmpl templates base name * @return boolean $result true on success */ function _loadTemplate( $tmpl ) { $this->_tmpl->readTemplatesFromInput( $tmpl . '.tmpl' ); return true; } } ?>