/code/core/sociax/View.class.php
PHP | 476 lines | 229 code | 19 blank | 228 comment | 38 complexity | ed77e0e5fe63c5f5b7f509fe753e5189 MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // $Id$
-
- /**
- +------------------------------------------------------------------------------
- * ThinkPHP ????
- * ?????????
- +------------------------------------------------------------------------------
- * @category Think
- * @package Think
- * @subpackage Core
- * @author liu21st <liu21st@gmail.com>
- * @version $Id$
- +------------------------------------------------------------------------------
- */
- class View extends Think
- {
- protected $tVar = array(); // ??????
- protected $trace = array(); // ??trace??
- protected $templateFile = ''; // ?????
-
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param mixed $name
- * @param mixed $value
- +----------------------------------------------------------
- */
- public function assign($name,$value=''){
- if(is_array($name)) {
- $this->tVar = array_merge($this->tVar,$name);
- }elseif(is_object($name)){
- foreach($name as $key =>$val)
- $this->tVar[$key] = $val;
- }else {
- $this->tVar[$name] = $value;
- }
- }
-
- /**
- +----------------------------------------------------------
- * Trace????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param mixed $name
- * @param mixed $value
- +----------------------------------------------------------
- */
- public function trace($title,$value='') {
- if(is_array($title))
- $this->trace = array_merge($this->trace,$title);
- else
- $this->trace[$title] = $value;
- }
-
- /**
- +----------------------------------------------------------
- * ????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $name
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- */
- public function get($name){
- if(isset($this->tVar[$name]))
- return $this->tVar[$name];
- else
- return false;
- }
-
- /**
- +----------------------------------------------------------
- * ????????? ????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $templateFile ????? ???????
- * @param string $charset ???????
- * @param string $contentType ????
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- */
- public function display($templateFile='',$charset='',$contentType='text/html')
- {
- $this->fetch($templateFile,$charset,$contentType,true);
- }
-
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $charset ????
- * @param string $contentType ????
- * @param string $display ??????
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- */
- protected function layout($content,$charset='',$contentType='text/html')
- {
- if(false !== strpos($content,'<!-- layout')) {
- // ?????????
- $find = preg_match_all('/<!-- layout::(.+?)::(.+?) -->/is',$content,$matches);
- if($find) {
- for ($i=0; $i< $find; $i++) {
- // ???????????????
- if(0===strpos($matches[1][$i],'$'))
- // ????
- $matches[1][$i] = $this->get(substr($matches[1][$i],1));
- if(0 != $matches[2][$i] ) {
- // ???????
- // ??????????
- $guid = md5($matches[1][$i]);
- $cache = S($guid);
- if($cache) {
- $layoutContent = $cache;
- }else{
- $layoutContent = $this->fetch($matches[1][$i],$charset,$contentType);
- S($guid,$layoutContent,$matches[2][$i]);
- }
- }else{
- $layoutContent = $this->fetch($matches[1][$i],$charset,$contentType);
- }
- $content = str_replace($matches[0][$i],$layoutContent,$content);
- }
- }
- }
- return $content;
- }
-
- /**
- +----------------------------------------------------------
- * ?????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $templateFile ????? ???????
- * @param string $charset ???????
- * @param string $contentType ????
- * @param string $display ??????
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- */
- public function fetch($templateFile='',$charset='',$contentType='text/html',$display=false)
- {
- //??????ts
- global $ts;
- $this->tVar['ts'] = $ts;
- $GLOBALS['_viewStartTime'] = microtime(TRUE);
- if(null===$templateFile)
- // ??null?????????????????
- return ;
- if(empty($charset)) $charset = C('DEFAULT_CHARSET');
- // ??????
- header("Content-Type:".$contentType."; charset=".$charset);
- header("Cache-control: private"); //??????
- //????
- ob_start();
- ob_implicit_flush(0);
-
- if(!file_exists_case($templateFile))
- // ????????
- $templateFile = $this->parseTemplateFile($templateFile);
-
- $engine = strtolower(C('TMPL_ENGINE_TYPE'));
- if('php'==$engine) {
- // ??????????????
- extract($this->tVar, EXTR_OVERWRITE);
- // ????PHP??
- include $templateFile;
- }elseif('think'==$engine && $this->checkCache($templateFile)) {
- // ???Think?????????? ???????????
- extract($this->tVar, EXTR_OVERWRITE);
- //????????
- include C('CACHE_PATH').md5($templateFile).C('TMPL_CACHFILE_SUFFIX');
- }else{
- // ?????????? ?????????
- // ???????????
- $className = 'Template'.ucwords($engine);
- require_cache(THINK_PATH.'/Util/Template/'.$className.'.class.php');
- $tpl = new $className;
- $tpl->fetch($templateFile,$this->tVar,$charset);
- }
- $this->templateFile = $templateFile;
- // ???????
- $content = ob_get_clean();
- // ??????
- $content = $this->templateContentReplace($content);
- // ??????
- $content = $this->layout($content,$charset,$contentType);
- // ??????
- return $this->output($content,$display);
- }
-
- /**
- +----------------------------------------------------------
- * ??????????
- * ???????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $tmplTemplateFile ?????
- +----------------------------------------------------------
- * @return boolen
- +----------------------------------------------------------
- */
- protected function checkCache($tmplTemplateFile)
- {
- if (!C('TMPL_CACHE_ON')) // ?????????
- return false;
- $tmplCacheFile = C('CACHE_PATH').md5($tmplTemplateFile).C('TMPL_CACHFILE_SUFFIX');
- if(!is_file($tmplCacheFile)){
- return false;
- }elseif (filemtime($tmplTemplateFile) > filemtime($tmplCacheFile)) {
- // ????????????????
- return false;
- }elseif (C('TMPL_CACHE_TIME') != -1 && time() > filemtime($tmplCacheFile)+C('TMPL_CACHE_TIME')) {
- // ????????
- return false;
- }
- //????
- return true;
- }
-
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @htmlfile ?????????
- * @htmlpath ?????????
- * @param string $templateFile ??????????
- * ???? ???????????
- * @param string $charset ????
- * @param string $contentType ????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- public function buildHtml($htmlfile,$htmlpath='',$templateFile='',$charset='',$contentType='text/html') {
- $content = $this->fetch($templateFile,$charset,$contentType);
- $htmlpath = !empty($htmlpath)?$htmlpath:HTML_PATH;
- $htmlfile = $htmlpath.$htmlfile.C('HTML_FILE_SUFFIX');
- if(!is_dir(dirname($htmlfile)))
- // ????????? ???
- mk_dir(dirname($htmlfile));
- if(false === file_put_contents($htmlfile,$content))
- throw_exception(L('_CACHE_WRITE_ERROR_'));
- return $content;
- }
-
- /**
- +----------------------------------------------------------
- * ????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $content ????
- * @param boolean $display ??????
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- */
- protected function output($content,$display) {
- if(C('HTML_CACHE_ON')) HtmlCache::writeHTMLCache($content);
- if($display) {
- if(C('SHOW_RUN_TIME')){
- $runtime = '<div id="think_run_time" class="think_run_time">'.$this->showTime().'</div>';
- if(strpos($content,'{__RUNTIME__}'))
- $content = str_replace('{__RUNTIME__}',$runtime,$content);
- else
- $content .= $runtime;
- }
- echo $content;
- if(C('SHOW_PAGE_TRACE')) $this->showTrace();
- return null;
- }else {
- return $content;
- }
- }
-
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $content ????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- protected function templateContentReplace($content) {
- // ???????????
- $replace = array(
- '../Public' => APP_PUBLIC_PATH,// ??????
- '__PUBLIC__' => WEB_PUBLIC_PATH,// ??????
- '__TMPL__' => APP_TMPL_PATH, // ??????
- '__ROOT__' => __ROOT__, // ??????
- '__APP__' => __APP__, // ??????
- '__URL__' => __URL__, // ??????
- '__ACTION__' => __ACTION__, // ??????
- '__SELF__' => __SELF__, // ??????
- '__THEME__' => __THEME__, // ??????
- '__UPLOAD__' => __UPLOAD__, // ??????
- );
- if(C('TOKEN_ON')) {
- if(strpos($content,'{__TOKEN__}')) {
- // ???????????
- $replace['{__TOKEN__}'] = $this->buildFormToken();
- }elseif(strpos($content,'{__NOTOKEN__}')){
- // ??????????
- $replace['{__NOTOKEN__}'] = '';
- }elseif(preg_match('/<\/form(\s*)>/is',$content,$match)) {
- // ???????????
- $replace[$match[0]] = $this->buildFormToken().$match[0];
- }
- }
- // ???????????????
- if(is_array(C('TMPL_PARSE_STRING')) )
- $replace = array_merge($replace,C('TMPL_PARSE_STRING'));
- $content = str_replace(array_keys($replace),array_values($replace),$content);
- return $content;
- }
-
- /**
- +----------------------------------------------------------
- * ?????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- private function buildFormToken() {
- // ??????????????
- $tokenName = C('TOKEN_NAME');
- $tokenType = C('TOKEN_TYPE');
- $tokenValue = $tokenType(microtime(TRUE));
- $token = '<input type="hidden" name="'.$tokenName.'" value="'.$tokenValue.'" />';
- $_SESSION[$tokenName] = $tokenValue;
- return $token;
- }
-
- /**
- +----------------------------------------------------------
- * ????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @param string $templateFile ???
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- * @throws ThinkExecption
- +----------------------------------------------------------
- */
- private function parseTemplateFile($templateFile) {
- if(''==$templateFile) {
- // ????????? ????????
- $templateFile = C('TMPL_FILE_NAME');
- //2009-06-02??
- }elseif(strpos($templateFile,'&')){
- // ???????????
- $templateFile = str_replace('&','/',$templateFile).C('TMPL_TEMPLATE_SUFFIX');
- //????
- }elseif(strpos($templateFile,'@')){
- // ??????????? ???????? ?? blue@User:add
- $templateFile = TMPL_PATH.str_replace(array('@',':'),'/',$templateFile).C('TMPL_TEMPLATE_SUFFIX');
- }elseif(strpos($templateFile,':')){
- // ???????????
- $templateFile = TEMPLATE_PATH.'/'.str_replace(':','/',$templateFile).C('TMPL_TEMPLATE_SUFFIX');
-
- }elseif(!is_file($templateFile)) {
- // ?????????????
- $templateFile = dirname(C('TMPL_FILE_NAME')).'/'.$templateFile.C('TMPL_TEMPLATE_SUFFIX');
- }
- if(!file_exists_case($templateFile))
- throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
- return $templateFile;
- }
-
- /**
- +----------------------------------------------------------
- * ????????????????????????
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- private function showTime() {
- // ??????
- $startTime = $GLOBALS['_viewStartTime'];
- $endTime = microtime(TRUE);
- $total_run_time = number_format(($endTime - $GLOBALS['_beginTime']), 3);
- $showTime = 'Process: '.$total_run_time.'s ';
- if(C('SHOW_ADV_TIME')) {
- // ????????
- $_load_time = number_format(($GLOBALS['_loadTime'] -$GLOBALS['_beginTime'] ), 3);
- $_init_time = number_format(($GLOBALS['_initTime'] -$GLOBALS['_loadTime'] ), 3);
- $_exec_time = number_format(($startTime -$GLOBALS['_initTime'] ), 3);
- $_parse_time = number_format(($endTime - $startTime), 3);
- $showTime .= '( Load:'.$_load_time.'s Init:'.$_init_time.'s Exec:'.$_exec_time.'s Template:'.$_parse_time.'s )';
- }
- if(C('SHOW_DB_TIMES') && class_exists('Db',false) ) {
- // ?????????
- $db = Db::getInstance();
- $showTime .= ' | DB :'.$db->Q().' queries '.$db->W().' writes ';
- }
- if(C('SHOW_CACHE_TIMES') && class_exists('Cache',false)) {
- // ????????
- $cache = Cache::getInstance();
- $showTime .= ' | Cache :'.$cache->Q().' gets '.$cache->W().' writes ';
- }
- if(MEMORY_LIMIT_ON && C('SHOW_USE_MEM')) {
- // ??????
- $startMem = array_sum(explode(' ', $GLOBALS['_startUseMems']));
- $endMem = array_sum(explode(' ', memory_get_usage()));
- $showTime .= ' | UseMem:'. number_format(($endMem - $startMem)/1024).' kb';
- }
- return $showTime;
- }
-
- /**
- +----------------------------------------------------------
- * ????Trace??
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- */
- private function showTrace(){
- // ????Trace?? ??Trace????
- // ???? return array('????'=>$_SERVER['PHP_SELF'],'????'=>$_SERVER['SERVER_PROTOCOL'],...);
- $traceFile = CONFIG_PATH.'trace.php';
- $_trace = is_file($traceFile)? include $traceFile : array();
- // ????????
- $this->trace('????', $_SERVER['REQUEST_URI']);
- $this->trace('????', C('CACHE_PATH').md5($this->templateFile).C('TMPL_CACHFILE_SUFFIX'));
- $this->trace('????', $_SERVER['REQUEST_METHOD']);
- $this->trace('????', $_SERVER['SERVER_PROTOCOL']);
- $this->trace('????', date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']));
- $this->trace('????', $_SERVER['HTTP_USER_AGENT']);
- $this->trace('??ID' , session_id());
- $log = Log::$log;
- $this->trace('????',count($log)?count($log).'???<br/>'.implode('<br/>',$log):'?????');
- $files = get_included_files();
- $this->trace('????', count($files).str_replace("\n",'<br/>',substr(substr(print_r($files,true),7),0,-2)));
- $_trace = array_merge($_trace,$this->trace);
- // ??Trace????
- include C('TMPL_TRACE_FILE');
- }
-
- }//
- ?>