.. * @var string */ var $html_header = NULL; /** * class names of
* @var array */ var $body_class = array(); /** * codes after * @var string */ var $body_header = NULL; /** * class names before * @var string */ var $html_footer = NULL; /** * path of Xpress Engine * @var string */ var $path = ''; // language information - it is changed by HTTP_USER_AGENT or user's cookie /** * language type * @var string */ var $lang_type = ''; /** * contains language-specific data * @var object */ var $lang = NULL; /** * list of loaded languages (to avoid re-loading them) * @var array */ var $loaded_lang_files = array(); /** * site's browser title * @var string */ var $site_title = ''; /** * variables from GET or form submit * @var mixed */ var $get_vars = NULL; /** * Checks uploaded * @var bool true if attached file exists */ var $is_uploaded = false; /** * returns static context object (Singleton). It's to use Context without declaration of an object * * @return object Instance */ function &getInstance() { static $theInstance = null; if(!$theInstance) $theInstance = new Context(); // include ssl action cache file $theInstance->sslActionCacheFile = FileHandler::getRealPath($theInstance->sslActionCacheFile); if(is_readable($theInstance->sslActionCacheFile)) { require_once($theInstance->sslActionCacheFile); if(isset($sslActions)) { $theInstance->ssl_actions = $sslActions; } } return $theInstance; } /** * Cunstructor * * @return void */ function Context() { $this->oFrontEndFileHandler = new FrontEndFileHandler(); } /** * Initialization, it sets DB information, request arguments and so on. * * @see This function should be called only once * @return void */ function init() { // set context variables in $GLOBALS (to use in display handler) $this->context = &$GLOBALS['__Context__']; $this->context->lang = &$GLOBALS['lang']; $this->context->_COOKIE = $_COOKIE; $this->setRequestMethod(''); $this->_setXmlRpcArgument(); $this->_setJSONRequestArgument(); $this->_setRequestArgument(); $this->_setUploadedArgument(); $this->loadDBInfo(); // If XE is installed, get virtual site information if(Context::isInstalled()) { $oModuleModel = &getModel('module'); $site_module_info = $oModuleModel->getDefaultMid(); // if site_srl of site_module_info is 0 (default site), compare the domain to default_url of db_config if($site_module_info->site_srl == 0 && $site_module_info->domain != $this->db_info->default_url) { $site_module_info->domain = $this->db_info->default_url; } $this->set('site_module_info', $site_module_info); if($site_module_info->site_srl && isSiteID($site_module_info->domain)) $this->set('vid', $site_module_info->domain, true); $this->db_info->lang_type = $site_module_info->default_language; if(!$this->db_info->lang_type) $this->db_info->lang_type = 'en'; if(!$this->db_info->use_db_session) $this->db_info->use_db_session = 'N'; } // Load Language File $lang_supported = $this->loadLangSelected(); // Retrieve language type set in user's cookie if($this->get('l')) { $this->lang_type = $this->get('l'); if($_COOKIE['lang_type'] != $this->lang_type) { setcookie('lang_type', $this->lang_type, time()+3600*24*1000, '/'); } } elseif($_COOKIE['lang_type']) { $this->lang_type = $_COOKIE['lang_type']; } // If it's not exists, follow default language type set in db_info if(!$this->lang_type) $this->lang_type = $this->db_info->lang_type; // if still lang_type has not been set or has not-supported type , set as English. if(!$this->lang_type) $this->lang_type = 'en'; if(is_array($lang_supported)&&!isset($lang_supported[$this->lang_type])) $this->lang_type = 'en'; $this->set('lang_supported', $lang_supported); $this->setLangType($this->lang_type); // load module module's language file according to language setting $this->loadLang(_XE_PATH_.'modules/module/lang'); // set session handler if(Context::isInstalled() && $this->db_info->use_db_session == 'Y') { $oSessionModel = &getModel('session'); $oSessionController = &getController('session'); session_set_save_handler( array(&$oSessionController, 'open'), array(&$oSessionController, 'close'), array(&$oSessionModel, 'read'), array(&$oSessionController, 'write'), array(&$oSessionController, 'destroy'), array(&$oSessionController, 'gc') ); } session_start(); if($sess=$_POST[session_name()]) session_id($sess); // set authentication information in Context and session if(Context::isInstalled()) { $oModuleModel = &getModel('module'); $oModuleModel->loadModuleExtends(); $oMemberModel = &getModel('member'); $oMemberController = &getController('member'); if($oMemberController && $oMemberModel) { // if signed in, validate it. if($oMemberModel->isLogged()) { $oMemberController->setSessionInfo(); } // check auto sign-in elseif($_COOKIE['xeak']) { $oMemberController->doAutologin(); } $this->set('is_logged', $oMemberModel->isLogged() ); $this->set('logged_info', $oMemberModel->getLoggedInfo() ); } } // load common language file $this->lang = &$GLOBALS['lang']; $this->loadLang(_XE_PATH_.'common/lang/'); // check if using rewrite module if(file_exists(_XE_PATH_.'.htaccess')&&$this->db_info->use_rewrite == 'Y') $this->allow_rewrite = true; else $this->allow_rewrite = false; // set locations for javascript use if($_SERVER['REQUEST_METHOD'] == 'GET') { if($this->get_vars) { foreach($this->get_vars as $key=>$val) { if(is_array($val)&&count($val)) { foreach($val as $k => $v) { $url .= ($url?'&':'').$key.'['.$k.']='.urlencode($v); } } elseif ($val) { $url .= ($url?'&':'').$key.'='.urlencode($val); } } $this->set('current_url',sprintf('%s?%s', Context::getRequestUri(), $url)); } else { $this->set('current_url',$this->getUrl()); } } else { $this->set('current_url',Context::getRequestUri()); } $this->set('request_uri',Context::getRequestUri()); } /** * Finalize using resources, such as DB connection * * @return void */ function close() { // Session Close if(function_exists('session_write_close')) session_write_close(); // DB close $oDB = &DB::getInstance(); if(is_object($oDB)&&method_exists($oDB, 'close')) $oDB->close(); } /** * Load the database information * * @return void */ function loadDBInfo() { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); if(!$self->isInstalled()) return; $config_file = $self->getConfigFile(); if(is_readable($config_file)) @include($config_file); // If master_db information does not exist, the config file needs to be updated if(!isset($db_info->master_db)) { $db_info->master_db = array(); $db_info->master_db["db_type"] = $db_info->db_type; unset($db_info->db_type); $db_info->master_db["db_port"] = $db_info->db_port; unset($db_info->db_port); $db_info->master_db["db_hostname"] = $db_info->db_hostname; unset($db_info->db_hostname); $db_info->master_db["db_password"] = $db_info->db_password; unset($db_info->db_password); $db_info->master_db["db_database"] = $db_info->db_database; unset($db_info->db_database); $db_info->master_db["db_userid"] = $db_info->db_userid; unset($db_info->db_userid); $db_info->master_db["db_table_prefix"] = $db_info->db_table_prefix; unset($db_info->db_table_prefix); if(substr($db_info->master_db["db_table_prefix"],-1)!='_') $db_info->master_db["db_table_prefix"] .= '_'; $slave_db = $db_info->master_db; $db_info->slave_db = array($slave_db); $self->setDBInfo($db_info); $oInstallController = &getController('install'); $oInstallController->makeConfigFile(); } if(!$db_info->use_prepared_statements) { $db_info->use_prepared_statements = 'Y'; } if(!$db_info->time_zone) $db_info->time_zone = date('O'); $GLOBALS['_time_zone'] = $db_info->time_zone; if($db_info->qmail_compatibility != 'Y') $db_info->qmail_compatibility = 'N'; $GLOBALS['_qmail_compatibility'] = $db_info->qmail_compatibility; if(!$db_info->use_db_session) $db_info->use_db_session = 'N'; if(!$db_info->use_ssl) $db_info->use_ssl = 'none'; $this->set('_use_ssl', $db_info->use_ssl); if($db_info->http_port) $self->set('_http_port', $db_info->http_port); if($db_info->https_port) $self->set('_https_port', $db_info->https_port); $self->setDBInfo($db_info); } /** * Get DB's db_type * * @return string DB's db_type */ function getDBType() { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); return $self->db_info->master_db["db_type"]; } /** * Set DB information * * @param object $db_info DB information * @return void */ function setDBInfo($db_info) { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); $self->db_info = $db_info; } /** * Get DB information * * @return object DB information */ function getDBInfo() { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); return $self->db_info; } /** * Return ssl status * * @return object SSL status (Optional - none|always|optional) */ function getSslStatus() { $dbInfo = Context::getDBInfo(); return $dbInfo->use_ssl; } /** * Return default URL * * @return string Default URL */ function getDefaultUrl() { $db_info = Context::getDBInfo(); return $db_info->default_url; } /** * Find supported languages * * @return array Supported languages */ function loadLangSupported() { static $lang_supported = null; if(!$lang_supported) { $langs = file(_XE_PATH_.'common/lang/lang.info'); foreach($langs as $val) { list($lang_prefix, $lang_text) = explode(',',$val); $lang_text = trim($lang_text); $lang_supported[$lang_prefix] = $lang_text; } } return $lang_supported; } /** * Find selected languages to serve in the site * * @return array Selected languages */ function loadLangSelected() { static $lang_selected = null; if(!$lang_selected) { $orig_lang_file = _XE_PATH_.'common/lang/lang.info'; $selected_lang_file = _XE_PATH_.'files/config/lang_selected.info'; if(!FileHandler::hasContent($selected_lang_file)) { $old_selected_lang_file = _XE_PATH_.'files/cache/lang_selected.info'; FileHandler::moveFile($old_selected_lang_file, $selected_lang_file); } if(!FileHandler::hasContent($selected_lang_file)) { $buff = FileHandler::readFile($orig_lang_file); FileHandler::writeFile($selected_lang_file, $buff); $lang_selected = Context::loadLangSupported(); } else { $langs = file($selected_lang_file); foreach($langs as $val) { list($lang_prefix, $lang_text) = explode(',',$val); $lang_text = trim($lang_text); $lang_selected[$lang_prefix] = $lang_text; } } } return $lang_selected; } /** * Single Sign On (SSO) * * @return bool True : Module handling is necessary in the control path of current request , False : Otherwise */ function checkSSO() { // pass if it's not GET request or XE is not yet installed if($this->db_info->use_sso != 'Y' || isCrawler()) return true; $checkActList = array('rss'=>1, 'atom'=>1); if(Context::getRequestMethod()!='GET' || !Context::isInstalled() || isset($checkActList[Context::get('act')])) return true; // pass if default URL is not set $default_url = trim($this->db_info->default_url); if(!$default_url) return true; if(substr($default_url,-1)!='/') $default_url .= '/'; // for sites recieving SSO valdiation if($default_url == Context::getRequestUri()) { if(Context::get('default_url')) { $url = base64_decode(Context::get('default_url')); $url_info = parse_url($url); $url_info['query'].= ($url_info['query']?'&':'').'SSOID='.session_id(); $redirect_url = sprintf('%s://%s%s%s?%s',$url_info['scheme'],$url_info['host'],$url_info['port']?':'.$url_info['port']:'',$url_info['path'], $url_info['query']); header('location:'.$redirect_url); return false; } // for sites requesting SSO validation } else { // result handling : set session_name() if(Context::get('SSOID')) { $session_name = Context::get('SSOID'); setcookie(session_name(), $session_name); $url = preg_replace('/([\?\&])$/','',str_replace('SSOID='.$session_name,'',Context::getRequestUrl())); header('location:'.$url); return false; // send SSO request } else if($_COOKIE['sso']!=md5(Context::getRequestUri()) && !Context::get('SSOID')) { setcookie('sso',md5(Context::getRequestUri()),0,'/'); $url = sprintf("%s?default_url=%s", $default_url, base64_encode(Context::getRequestUrl())); header('location:'.$url); return false; } } return true; } /** * Check if FTP info is registered * * @return bool True: FTP information is registered, False: otherwise */ function isFTPRegisted() { $ftp_config_file = Context::getFTPConfigFile(); if(file_exists($ftp_config_file)) return true; return false; } /** * Get FTP information * * @return object FTP information */ function getFTPInfo() { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); if(!$self->isFTPRegisted()) return null; $ftp_config_file = $self->getFTPConfigFile(); @include($ftp_config_file); return $ftp_info; } /** * Add string to browser title * * @param string $site_title Browser title to be added * @return void */ function addBrowserTitle($site_title) { if(!$site_title) return; is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); if($self->site_title) $self->site_title .= ' - '.$site_title; else $self->site_title = $site_title; } /** * Set string to browser title * * @param string $site_title Browser title to be set * @return void */ function setBrowserTitle($site_title) { if(!$site_title) return; is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); $self->site_title = $site_title; } /** * Get browser title * * @return string Browser title(htmlspecialchars applied) */ function getBrowserTitle() { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); $oModuleController = &getController('module'); $oModuleController->replaceDefinedLangCode($self->site_title); return htmlspecialchars($self->site_title); } /** * Get browser title * @deprecated */ function _getBrowserTitle() { return $this->getBrowserTitle(); } /** * Load language file according to language type * * @param string $path Path of the language file * @return void */ function loadLang($path) { global $lang; is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); if(!is_object($lang)) $lang = new stdClass; if(!$self->lang_type) return; $filename = $self->_loadXmlLang($path); if(!$filename) $filename = $self->_loadPhpLang($path); if(!is_array($self->loaded_lang_files)) $self->loaded_lang_files = array(); if(in_array($filename, $self->loaded_lang_files)) return; if ($filename && is_readable($filename)) { $self->loaded_lang_files[] = $filename; @include($filename); } else { $self->_evalxmlLang($path); } } /** * Evaluation of xml language file * * @param string Path of the language file * @return void */ function _evalxmlLang($path) { global $lang; $_path = 'eval://'.$path; if(in_array($_path, $this->loaded_lang_files)) return; if(substr($path,-1)!='/') $path .= '/'; $file = $path.'lang.xml'; $oXmlLangParser = new XmlLangParser($file, $this->lang_type); $content = $oXmlLangParser->getCompileContent(); if ($content) { $this->loaded_lang_files[] = $_path; eval($content); } } /** * Load language file of xml type * * @param string $path Path of the language file * @return string file name */ function _loadXmlLang($path) { if(substr($path,-1)!='/') $path .= '/'; $file = $path.'lang.xml'; $oXmlLangParser = new XmlLangParser($file, $this->lang_type); $file = $oXmlLangParser->compile(); return $file; } /** * Load language file of php type * * @param string $path Path of the language file * @return string file name */ function _loadPhpLang($path) { if(substr($path,-1)!='/') $path .= '/'; $path_tpl = $path.'%s.lang.php'; $file = sprintf($path_tpl, $this->lang_type); $langs = array('ko','en'); // this will be configurable. while(!is_readable($file) && $langs[0]) { $file = sprintf($path_tpl, array_shift($langs)); } if(!is_readable($file)) return false; return $file; } /** * Set lang_type * * @param string $lang_type Language type. * @return void */ function setLangType($lang_type = 'ko') { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); $self->lang_type = $lang_type; $self->set('lang_type', $lang_type); $_SESSION['lang_type'] = $lang_type; } /** * Get lang_type * * @return string Language type */ function getLangType() { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); return $self->lang_type; } /** * Return string accoring to the inputed code * * @param string $code Language variable name * @return string If string for the code exists returns it, otherwise returns original code */ function getLang($code) { if(!$code) return; if($GLOBALS['lang']->{$code}) return $GLOBALS['lang']->{$code}; return $code; } /** * Set data to lang variable * * @param string $code Language variable name * @param string $val `$code`s value * @return void */ function setLang($code, $val) { $GLOBALS['lang']->{$code} = $val; } /** * Convert strings of variables in $source_object into UTF-8 * * @param object $source_obj Conatins strings to convert * @return object converted object */ function convertEncoding($source_obj) { $charset_list = array( 'UTF-8', 'EUC-KR', 'CP949', 'ISO8859-1', 'EUC-JP', 'SHIFT_JIS', 'CP932', 'EUC-CN', 'HZ', 'GBK', 'GB18030', 'EUC-TW', 'BIG5', 'CP950', 'BIG5-HKSCS', 'ISO2022-CN', 'ISO2022-CN-EXT', 'ISO2022-JP', 'ISO2022-JP-2', 'ISO2022-JP-1', 'ISO8859-6', 'ISO8859-8', 'JOHAB', 'ISO2022-KR', 'CP1255', 'CP1256', 'CP862', 'ASCII', 'ISO8859-1', 'ISO8850-2', 'ISO8850-3', 'ISO8850-4', 'ISO8850-5', 'ISO8850-7', 'ISO8850-9', 'ISO8850-10', 'ISO8850-13', 'ISO8850-14', 'ISO8850-15', 'ISO8850-16', 'CP1250', 'CP1251', 'CP1252', 'CP1253', 'CP1254', 'CP1257', 'CP850', 'CP866', ); $obj = clone($source_obj); foreach($charset_list as $charset) { $flag = true; foreach($obj as $key=>$val) { if(!$val) continue; if(!is_array($val) && iconv($charset,$charset,$val)!=$val) $flag = false; else if(is_array($val)) { $userdata = array('charset1'=>$charset,'charset2'=>$charset,'useFlag'=>true); Context::arrayConvWalkCallback($val,null,$userdata); if($userdata['returnFlag'] === false) $flag = false; } } if($flag) { if($charset == 'UTF-8') return $obj; foreach($obj as $key => $val) { if(!is_array($val)) $obj->{$key} = iconv($charset,'UTF-8',$val); else Context::arrayConvWalkCallback($val,null,array($charset,'UTF-8')); } return $obj; } } return $obj; } /** * Convert array type variables into UTF-8 * * @param mixed $val * @param string $key * @param mixed $userdata charset1 charset2 useFlag retrunFlag * @see arrayConvWalkCallback will replaced array_walk_recursive in >=PHP5 * @return object converted object */ function arrayConvWalkCallback(&$val, $key = null, &$userdata) { if (is_array($val)) array_walk($val,'Context::arrayConvWalkCallback', $userdata); else { if(!$userdata['useFlag']) $val = iconv($userdata['charset1'],$userdata['charset2'],$val); else { if(iconv($charset,$charset,$val)!=$val) $userdata['returnFlag'] = (bool)false; } } } /** * Convert strings into UTF-8 * * @param string $str String to convert * @return string converted string */ function convertEncodingStr($str) { $obj->str = $str; $obj = Context::convertEncoding($obj); return $obj->str; } /** * Force to set response method * * @param string $method Response method. [HTML|XMLRPC|JSON] * @return void */ function setResponseMethod($method='HTML') { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); $methods = array('HTML'=>1, 'XMLRPC'=>1, 'JSON'=>1, 'JS_CALLBACK' => 1); $self->response_method = isset($methods[$method]) ? $method : 'HTML'; } /** * Get reponse method * * @return string Response method. If it's not set, returns request method. */ function getResponseMethod() { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); if($self->response_method) return $self->response_method; $method = $self->getRequestMethod(); $methods = array('HTML'=>1, 'XMLRPC'=>1, 'JSON'=>1, 'JS_CALLBACK' => 1); return isset($methods[$method]) ? $method : 'HTML'; } /** * Determine request method * * @param string $type Request method. (Optional - GET|POST|XMLRPC|JSON) * @return void */ function setRequestMethod($type='') { is_a($this,'Context')?$self=&$this:$self=&Context::getInstance(); $self->js_callback_func = isset($_GET['xe_js_callback']) ? $_GET['xe_js_callback'] : $_POST['xe_js_callback']; ($type && $self->request_method=$type) or (strpos($_SERVER['CONTENT_TYPE'],'json') && $self->request_method='JSON') or ($GLOBALS['HTTP_RAW_POST_DATA'] && $self->request_method='XMLRPC') or ($self->js_callback_func && $self->request_method='JS_CALLBACK') or ($self->request_method = $_SERVER['REQUEST_METHOD']); } /** * handle request areguments for GET/POST * * @return void */ function _setRequestArgument() { if(!count($_REQUEST)) return; foreach($_REQUEST as $key => $val) { if($val === '' || Context::get($key)) continue; $val = $this->_filterRequestVar($key, $val); if($this->getRequestMethod()=='GET'&&isset($_GET[$key])) $set_to_vars = true; elseif($this->getRequestMethod()=='POST'&&isset($_POST[$key])) $set_to_vars = true; elseif($this->getRequestMethod()=='JS_CALLBACK' && (isset($_GET[$key]) || isset($_POST[$key]))) $set_to_vars = true; else $set_to_vars = false; if($set_to_vars) { $val = preg_replace('/<\?/i', '', $val); $val = preg_replace('/<\%/i', '', $val); $val = preg_replace('/