PageRenderTime 201ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/code/core/sociax/View.class.php

http://thinksns-2.googlecode.com/
PHP | 476 lines | 229 code | 19 blank | 228 comment | 38 complexity | ed77e0e5fe63c5f5b7f509fe753e5189 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP ????
  15. * ?????????
  16. +------------------------------------------------------------------------------
  17. * @category Think
  18. * @package Think
  19. * @subpackage Core
  20. * @author liu21st <liu21st@gmail.com>
  21. * @version $Id$
  22. +------------------------------------------------------------------------------
  23. */
  24. class View extends Think
  25. {
  26. protected $tVar = array(); // ??????
  27. protected $trace = array(); // ??trace??
  28. protected $templateFile = ''; // ?????
  29. /**
  30. +----------------------------------------------------------
  31. * ??????
  32. +----------------------------------------------------------
  33. * @access public
  34. +----------------------------------------------------------
  35. * @param mixed $name
  36. * @param mixed $value
  37. +----------------------------------------------------------
  38. */
  39. public function assign($name,$value=''){
  40. if(is_array($name)) {
  41. $this->tVar = array_merge($this->tVar,$name);
  42. }elseif(is_object($name)){
  43. foreach($name as $key =>$val)
  44. $this->tVar[$key] = $val;
  45. }else {
  46. $this->tVar[$name] = $value;
  47. }
  48. }
  49. /**
  50. +----------------------------------------------------------
  51. * Trace????
  52. +----------------------------------------------------------
  53. * @access public
  54. +----------------------------------------------------------
  55. * @param mixed $name
  56. * @param mixed $value
  57. +----------------------------------------------------------
  58. */
  59. public function trace($title,$value='') {
  60. if(is_array($title))
  61. $this->trace = array_merge($this->trace,$title);
  62. else
  63. $this->trace[$title] = $value;
  64. }
  65. /**
  66. +----------------------------------------------------------
  67. * ????????
  68. +----------------------------------------------------------
  69. * @access public
  70. +----------------------------------------------------------
  71. * @param string $name
  72. +----------------------------------------------------------
  73. * @return mixed
  74. +----------------------------------------------------------
  75. */
  76. public function get($name){
  77. if(isset($this->tVar[$name]))
  78. return $this->tVar[$name];
  79. else
  80. return false;
  81. }
  82. /**
  83. +----------------------------------------------------------
  84. * ????????? ????????
  85. +----------------------------------------------------------
  86. * @access public
  87. +----------------------------------------------------------
  88. * @param string $templateFile ????? ???????
  89. * @param string $charset ???????
  90. * @param string $contentType ????
  91. +----------------------------------------------------------
  92. * @return mixed
  93. +----------------------------------------------------------
  94. */
  95. public function display($templateFile='',$charset='',$contentType='text/html')
  96. {
  97. $this->fetch($templateFile,$charset,$contentType,true);
  98. }
  99. /**
  100. +----------------------------------------------------------
  101. * ??????
  102. +----------------------------------------------------------
  103. * @access protected
  104. +----------------------------------------------------------
  105. * @param string $charset ????
  106. * @param string $contentType ????
  107. * @param string $display ??????
  108. +----------------------------------------------------------
  109. * @return mixed
  110. +----------------------------------------------------------
  111. */
  112. protected function layout($content,$charset='',$contentType='text/html')
  113. {
  114. if(false !== strpos($content,'<!-- layout')) {
  115. // ?????????
  116. $find = preg_match_all('/<!-- layout::(.+?)::(.+?) -->/is',$content,$matches);
  117. if($find) {
  118. for ($i=0; $i< $find; $i++) {
  119. // ???????????????
  120. if(0===strpos($matches[1][$i],'$'))
  121. // ????
  122. $matches[1][$i] = $this->get(substr($matches[1][$i],1));
  123. if(0 != $matches[2][$i] ) {
  124. // ???????
  125. // ??????????
  126. $guid = md5($matches[1][$i]);
  127. $cache = S($guid);
  128. if($cache) {
  129. $layoutContent = $cache;
  130. }else{
  131. $layoutContent = $this->fetch($matches[1][$i],$charset,$contentType);
  132. S($guid,$layoutContent,$matches[2][$i]);
  133. }
  134. }else{
  135. $layoutContent = $this->fetch($matches[1][$i],$charset,$contentType);
  136. }
  137. $content = str_replace($matches[0][$i],$layoutContent,$content);
  138. }
  139. }
  140. }
  141. return $content;
  142. }
  143. /**
  144. +----------------------------------------------------------
  145. * ?????????
  146. +----------------------------------------------------------
  147. * @access public
  148. +----------------------------------------------------------
  149. * @param string $templateFile ????? ???????
  150. * @param string $charset ???????
  151. * @param string $contentType ????
  152. * @param string $display ??????
  153. +----------------------------------------------------------
  154. * @return mixed
  155. +----------------------------------------------------------
  156. */
  157. public function fetch($templateFile='',$charset='',$contentType='text/html',$display=false)
  158. {
  159. //??????ts
  160. global $ts;
  161. $this->tVar['ts'] = $ts;
  162. $GLOBALS['_viewStartTime'] = microtime(TRUE);
  163. if(null===$templateFile)
  164. // ??null?????????????????
  165. return ;
  166. if(empty($charset)) $charset = C('DEFAULT_CHARSET');
  167. // ??????
  168. header("Content-Type:".$contentType."; charset=".$charset);
  169. header("Cache-control: private"); //??????
  170. //????
  171. ob_start();
  172. ob_implicit_flush(0);
  173. if(!file_exists_case($templateFile))
  174. // ????????
  175. $templateFile = $this->parseTemplateFile($templateFile);
  176. $engine = strtolower(C('TMPL_ENGINE_TYPE'));
  177. if('php'==$engine) {
  178. // ??????????????
  179. extract($this->tVar, EXTR_OVERWRITE);
  180. // ????PHP??
  181. include $templateFile;
  182. }elseif('think'==$engine && $this->checkCache($templateFile)) {
  183. // ???Think?????????? ???????????
  184. extract($this->tVar, EXTR_OVERWRITE);
  185. //????????
  186. include C('CACHE_PATH').md5($templateFile).C('TMPL_CACHFILE_SUFFIX');
  187. }else{
  188. // ?????????? ?????????
  189. // ???????????
  190. $className = 'Template'.ucwords($engine);
  191. require_cache(THINK_PATH.'/Util/Template/'.$className.'.class.php');
  192. $tpl = new $className;
  193. $tpl->fetch($templateFile,$this->tVar,$charset);
  194. }
  195. $this->templateFile = $templateFile;
  196. // ???????
  197. $content = ob_get_clean();
  198. // ??????
  199. $content = $this->templateContentReplace($content);
  200. // ??????
  201. $content = $this->layout($content,$charset,$contentType);
  202. // ??????
  203. return $this->output($content,$display);
  204. }
  205. /**
  206. +----------------------------------------------------------
  207. * ??????????
  208. * ???????????
  209. +----------------------------------------------------------
  210. * @access public
  211. +----------------------------------------------------------
  212. * @param string $tmplTemplateFile ?????
  213. +----------------------------------------------------------
  214. * @return boolen
  215. +----------------------------------------------------------
  216. */
  217. protected function checkCache($tmplTemplateFile)
  218. {
  219. if (!C('TMPL_CACHE_ON')) // ?????????
  220. return false;
  221. $tmplCacheFile = C('CACHE_PATH').md5($tmplTemplateFile).C('TMPL_CACHFILE_SUFFIX');
  222. if(!is_file($tmplCacheFile)){
  223. return false;
  224. }elseif (filemtime($tmplTemplateFile) > filemtime($tmplCacheFile)) {
  225. // ????????????????
  226. return false;
  227. }elseif (C('TMPL_CACHE_TIME') != -1 && time() > filemtime($tmplCacheFile)+C('TMPL_CACHE_TIME')) {
  228. // ????????
  229. return false;
  230. }
  231. //????
  232. return true;
  233. }
  234. /**
  235. +----------------------------------------------------------
  236. * ??????
  237. +----------------------------------------------------------
  238. * @access public
  239. +----------------------------------------------------------
  240. * @htmlfile ?????????
  241. * @htmlpath ?????????
  242. * @param string $templateFile ??????????
  243. * ???? ???????????
  244. * @param string $charset ????
  245. * @param string $contentType ????
  246. +----------------------------------------------------------
  247. * @return string
  248. +----------------------------------------------------------
  249. */
  250. public function buildHtml($htmlfile,$htmlpath='',$templateFile='',$charset='',$contentType='text/html') {
  251. $content = $this->fetch($templateFile,$charset,$contentType);
  252. $htmlpath = !empty($htmlpath)?$htmlpath:HTML_PATH;
  253. $htmlfile = $htmlpath.$htmlfile.C('HTML_FILE_SUFFIX');
  254. if(!is_dir(dirname($htmlfile)))
  255. // ????????? ???
  256. mk_dir(dirname($htmlfile));
  257. if(false === file_put_contents($htmlfile,$content))
  258. throw_exception(L('_CACHE_WRITE_ERROR_'));
  259. return $content;
  260. }
  261. /**
  262. +----------------------------------------------------------
  263. * ????
  264. +----------------------------------------------------------
  265. * @access protected
  266. +----------------------------------------------------------
  267. * @param string $content ????
  268. * @param boolean $display ??????
  269. +----------------------------------------------------------
  270. * @return mixed
  271. +----------------------------------------------------------
  272. */
  273. protected function output($content,$display) {
  274. if(C('HTML_CACHE_ON')) HtmlCache::writeHTMLCache($content);
  275. if($display) {
  276. if(C('SHOW_RUN_TIME')){
  277. $runtime = '<div id="think_run_time" class="think_run_time">'.$this->showTime().'</div>';
  278. if(strpos($content,'{__RUNTIME__}'))
  279. $content = str_replace('{__RUNTIME__}',$runtime,$content);
  280. else
  281. $content .= $runtime;
  282. }
  283. echo $content;
  284. if(C('SHOW_PAGE_TRACE')) $this->showTrace();
  285. return null;
  286. }else {
  287. return $content;
  288. }
  289. }
  290. /**
  291. +----------------------------------------------------------
  292. * ??????
  293. +----------------------------------------------------------
  294. * @access protected
  295. +----------------------------------------------------------
  296. * @param string $content ????
  297. +----------------------------------------------------------
  298. * @return string
  299. +----------------------------------------------------------
  300. */
  301. protected function templateContentReplace($content) {
  302. // ???????????
  303. $replace = array(
  304. '../Public' => APP_PUBLIC_PATH,// ??????
  305. '__PUBLIC__' => WEB_PUBLIC_PATH,// ??????
  306. '__TMPL__' => APP_TMPL_PATH, // ??????
  307. '__ROOT__' => __ROOT__, // ??????
  308. '__APP__' => __APP__, // ??????
  309. '__URL__' => __URL__, // ??????
  310. '__ACTION__' => __ACTION__, // ??????
  311. '__SELF__' => __SELF__, // ??????
  312. '__THEME__' => __THEME__, // ??????
  313. '__UPLOAD__' => __UPLOAD__, // ??????
  314. );
  315. if(C('TOKEN_ON')) {
  316. if(strpos($content,'{__TOKEN__}')) {
  317. // ???????????
  318. $replace['{__TOKEN__}'] = $this->buildFormToken();
  319. }elseif(strpos($content,'{__NOTOKEN__}')){
  320. // ??????????
  321. $replace['{__NOTOKEN__}'] = '';
  322. }elseif(preg_match('/<\/form(\s*)>/is',$content,$match)) {
  323. // ???????????
  324. $replace[$match[0]] = $this->buildFormToken().$match[0];
  325. }
  326. }
  327. // ???????????????
  328. if(is_array(C('TMPL_PARSE_STRING')) )
  329. $replace = array_merge($replace,C('TMPL_PARSE_STRING'));
  330. $content = str_replace(array_keys($replace),array_values($replace),$content);
  331. return $content;
  332. }
  333. /**
  334. +----------------------------------------------------------
  335. * ?????????
  336. +----------------------------------------------------------
  337. * @access private
  338. +----------------------------------------------------------
  339. * @return string
  340. +----------------------------------------------------------
  341. */
  342. private function buildFormToken() {
  343. // ??????????????
  344. $tokenName = C('TOKEN_NAME');
  345. $tokenType = C('TOKEN_TYPE');
  346. $tokenValue = $tokenType(microtime(TRUE));
  347. $token = '<input type="hidden" name="'.$tokenName.'" value="'.$tokenValue.'" />';
  348. $_SESSION[$tokenName] = $tokenValue;
  349. return $token;
  350. }
  351. /**
  352. +----------------------------------------------------------
  353. * ????????
  354. +----------------------------------------------------------
  355. * @access private
  356. +----------------------------------------------------------
  357. * @param string $templateFile ???
  358. +----------------------------------------------------------
  359. * @return string
  360. +----------------------------------------------------------
  361. * @throws ThinkExecption
  362. +----------------------------------------------------------
  363. */
  364. private function parseTemplateFile($templateFile) {
  365. if(''==$templateFile) {
  366. // ????????? ????????
  367. $templateFile = C('TMPL_FILE_NAME');
  368. //2009-06-02??
  369. }elseif(strpos($templateFile,'&')){
  370. // ???????????
  371. $templateFile = str_replace('&','/',$templateFile).C('TMPL_TEMPLATE_SUFFIX');
  372. //????
  373. }elseif(strpos($templateFile,'@')){
  374. // ??????????? ???????? ?? blue@User:add
  375. $templateFile = TMPL_PATH.str_replace(array('@',':'),'/',$templateFile).C('TMPL_TEMPLATE_SUFFIX');
  376. }elseif(strpos($templateFile,':')){
  377. // ???????????
  378. $templateFile = TEMPLATE_PATH.'/'.str_replace(':','/',$templateFile).C('TMPL_TEMPLATE_SUFFIX');
  379. }elseif(!is_file($templateFile)) {
  380. // ?????????????
  381. $templateFile = dirname(C('TMPL_FILE_NAME')).'/'.$templateFile.C('TMPL_TEMPLATE_SUFFIX');
  382. }
  383. if(!file_exists_case($templateFile))
  384. throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
  385. return $templateFile;
  386. }
  387. /**
  388. +----------------------------------------------------------
  389. * ????????????????????????
  390. +----------------------------------------------------------
  391. * @access private
  392. +----------------------------------------------------------
  393. * @return string
  394. +----------------------------------------------------------
  395. */
  396. private function showTime() {
  397. // ??????
  398. $startTime = $GLOBALS['_viewStartTime'];
  399. $endTime = microtime(TRUE);
  400. $total_run_time = number_format(($endTime - $GLOBALS['_beginTime']), 3);
  401. $showTime = 'Process: '.$total_run_time.'s ';
  402. if(C('SHOW_ADV_TIME')) {
  403. // ????????
  404. $_load_time = number_format(($GLOBALS['_loadTime'] -$GLOBALS['_beginTime'] ), 3);
  405. $_init_time = number_format(($GLOBALS['_initTime'] -$GLOBALS['_loadTime'] ), 3);
  406. $_exec_time = number_format(($startTime -$GLOBALS['_initTime'] ), 3);
  407. $_parse_time = number_format(($endTime - $startTime), 3);
  408. $showTime .= '( Load:'.$_load_time.'s Init:'.$_init_time.'s Exec:'.$_exec_time.'s Template:'.$_parse_time.'s )';
  409. }
  410. if(C('SHOW_DB_TIMES') && class_exists('Db',false) ) {
  411. // ?????????
  412. $db = Db::getInstance();
  413. $showTime .= ' | DB :'.$db->Q().' queries '.$db->W().' writes ';
  414. }
  415. if(C('SHOW_CACHE_TIMES') && class_exists('Cache',false)) {
  416. // ????????
  417. $cache = Cache::getInstance();
  418. $showTime .= ' | Cache :'.$cache->Q().' gets '.$cache->W().' writes ';
  419. }
  420. if(MEMORY_LIMIT_ON && C('SHOW_USE_MEM')) {
  421. // ??????
  422. $startMem = array_sum(explode(' ', $GLOBALS['_startUseMems']));
  423. $endMem = array_sum(explode(' ', memory_get_usage()));
  424. $showTime .= ' | UseMem:'. number_format(($endMem - $startMem)/1024).' kb';
  425. }
  426. return $showTime;
  427. }
  428. /**
  429. +----------------------------------------------------------
  430. * ????Trace??
  431. +----------------------------------------------------------
  432. * @access private
  433. +----------------------------------------------------------
  434. */
  435. private function showTrace(){
  436. // ????Trace?? ??Trace????
  437. // ???? return array('????'=>$_SERVER['PHP_SELF'],'????'=>$_SERVER['SERVER_PROTOCOL'],...);
  438. $traceFile = CONFIG_PATH.'trace.php';
  439. $_trace = is_file($traceFile)? include $traceFile : array();
  440. // ????????
  441. $this->trace('????', $_SERVER['REQUEST_URI']);
  442. $this->trace('????', C('CACHE_PATH').md5($this->templateFile).C('TMPL_CACHFILE_SUFFIX'));
  443. $this->trace('????', $_SERVER['REQUEST_METHOD']);
  444. $this->trace('????', $_SERVER['SERVER_PROTOCOL']);
  445. $this->trace('????', date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']));
  446. $this->trace('????', $_SERVER['HTTP_USER_AGENT']);
  447. $this->trace('??ID' , session_id());
  448. $log = Log::$log;
  449. $this->trace('????',count($log)?count($log).'???<br/>'.implode('<br/>',$log):'?????');
  450. $files = get_included_files();
  451. $this->trace('????', count($files).str_replace("\n",'<br/>',substr(substr(print_r($files,true),7),0,-2)));
  452. $_trace = array_merge($_trace,$this->trace);
  453. // ??Trace????
  454. include C('TMPL_TRACE_FILE');
  455. }
  456. }//
  457. ?>