PageRenderTime 32ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/changleqi/core/kernel.php

http://phpfor.googlecode.com/
PHP | 608 lines | 464 code | 78 blank | 66 comment | 84 complexity | 61bed1d6b7b34fb5854490b4045fdf84 MD5 | raw file
  1. <?php
  2. /**
  3. * kernel
  4. *
  5. * @package
  6. * @version $Id: kernel.php 1948 2008-04-25 09:36:32Z flaboy $
  7. * @copyright 2003-2007 ShopEx
  8. * @license Commercial
  9. */
  10. class kernel{
  11. var $__setting;
  12. var $_funcPkg;
  13. var $models;
  14. var $_app_version = '4.8.5';
  15. var $__call_libs;
  16. var $_co_depth=0;
  17. var $memcache = false;
  18. var $_base_link = null;
  19. var $model_dir = 'model';
  20. function kernel(){
  21. error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
  22. $GLOBALS['system'] = &$this;
  23. if (get_magic_quotes_gpc()){
  24. unsafeVar($_GET);
  25. unsafeVar($_POST);
  26. unsafeVar($_COOKIE);
  27. }
  28. if(!defined('CORE_INCLUDE_DIR')){
  29. define('CORE_INCLUDE_DIR',CORE_DIR.
  30. ((!defined('SHOP_DEVELOPER') || !constant('SHOP_DEVELOPER')) && version_compare(PHP_VERSION,'5.0','>=')?'/include_v5':'/include'));
  31. }
  32. set_include_path(CORE_INCLUDE_DIR.PATH_SEPARATOR.'.'.PATH_SEPARATOR.CORE_DIR.'/lib/pear');
  33. require('defined.php');
  34. $this->model_dir = ((!defined('SHOP_DEVELOPER') || !constant('SHOP_DEVELOPER')) && version_compare(PHP_VERSION,'5.0','>=')?'model_v5':'model');
  35. if(constant('WITH_MEMCACHE')){
  36. $this->init_memcache(); //review: ????
  37. }
  38. if(defined('IN_INSTALLER')){
  39. $this->cache = new nocache();
  40. }else{
  41. $this->__metadata = unserialize(file_get_contents(HOME_DIR.'/fdata.php'));
  42. if(isset($this->__metadata['GTASK_REMINDER']) && $this->__metadata['GTASK_REMINDER']>0 && time()>$this->__metadata['GTASK_REMINDER']){
  43. $goods = &$this->loadModel('trading/goods');
  44. $goods->flush_gtask();
  45. }
  46. if(constant('WITHOUT_CACHE')){
  47. $this->cache = new nocache();
  48. }else{
  49. require('cachemgr.php');
  50. if(constant('WITH_MEMCACHE')){
  51. require(PLUGIN_DIR.'/functions/cache_memcache.php');
  52. $this->cache = new cache_memcache;
  53. }elseif(defined('CACHE_METHOD')){
  54. require(PLUGIN_DIR.'/functions/'.CACHE_METHOD.'.php');
  55. $cache_method = CACHE_METHOD;
  56. $this->cache = new $cache_method;
  57. }elseif(php_sapi_name()=='isapi'){
  58. require('secache.php');
  59. require('secache_no_flock.php');
  60. $this->cache = new secache_no_flock;
  61. }else{
  62. require('secache.php');
  63. $this->cache = new secache;
  64. }
  65. }
  66. }
  67. require('setmgr.php');
  68. $this->__setting = new setmgr;
  69. $this->set_timezone(SERVER_TIMEZONE);
  70. }
  71. function apply_modifiers(&$output,$type){
  72. if(!constant('SAFE_MODE')){
  73. $output_modifiers = $this->getConf('system.output_modifiers');
  74. $modifiers = array_merge(
  75. (array)$output_modifiers['*']
  76. ,(array)$output_modifiers[$type.':*']
  77. , (array)$output_modifiers[$type.':'.strtolower($this->request['action']['controller']).':*']
  78. , (array)$output_modifiers[$type.':'.strtolower($this->request['action']['controller'].':'.$this->request['action']['method'])]);
  79. if($modifiers){
  80. $appmgr = &$this->loadModel('system/appmgr');
  81. foreach($modifiers as $modifier){
  82. if($a_func = $appmgr->get_func($modifier)){
  83. list($obj,$func) = $a_func;
  84. $output = $obj->$func($output);
  85. }
  86. }
  87. }
  88. }
  89. }
  90. function call(){
  91. $args = func_get_args();
  92. $func = array_shift($args);
  93. if(!function_exists('dapi')){
  94. require_once('dapi.php');
  95. }
  96. return dapi_call($func,$args,$this);
  97. }
  98. function getmeta($key,&$ret){
  99. if(isset($this->__metadata[$key])){
  100. $ret = $this->__metadata[$key];
  101. return true;
  102. }else{
  103. return false;
  104. }
  105. }
  106. function savemeta($key,$value){
  107. $this->__metadata[$key] = $value;
  108. $this->__metachanged[$key] = &$this->__metadata[$key];
  109. register_shutdown_function_once(array(&$this,'_final_save_meta'),__FUNCTION__.__LINE__);
  110. }
  111. function _final_save_meta(){
  112. $data = unserialize(file_get_contents(HOME_DIR.'/fdata.php'));
  113. $data = array_merge((array)$data,$this->__metachanged);
  114. file_put_contents(HOME_DIR.'/fdata.php',serialize($data));
  115. }
  116. function log($message,$key='message'){
  117. error_log();
  118. }
  119. function init_memcache(){
  120. if(!$this->memcache){
  121. $this->memcache=new Memcache;
  122. $ports = explode(',',MEMCACHED_PORT);
  123. foreach(explode(',',MEMCACHED_HOST) as $i=>$h){
  124. $this->memcache->addServer($h,$ports[$i]);
  125. }
  126. $this->memcache->pconnect();
  127. }
  128. }
  129. function set_timezone($tz){
  130. if(function_exists('date_default_timezone_set')){
  131. $tz = 0-$tz;
  132. if($tz>12 || $tz<-12){
  133. $tz = 0;
  134. }
  135. date_default_timezone_set('Etc/GMT'.($tz>0?('+'.$tz):$tz));
  136. }
  137. }
  138. function base_url(){
  139. if(!isset($this->_base_url)){
  140. $this->_base_url='http://'.$_SERVER['HTTP_HOST'].substr(PHP_SELF, 0, strrpos(PHP_SELF, '/') + 1);
  141. }
  142. return $this->_base_url;
  143. }
  144. /**
  145. *
  146. * This is the short Description for the Function
  147. *
  148. * This is the long description for the Class
  149. *
  150. * @return mixed Description
  151. * @access private
  152. * @see ??
  153. */
  154. function &parse($query){
  155. if($pos = strpos($query,'.')){
  156. $type = substr($query,$pos+1);
  157. if($position = strpos($type,'&')){
  158. $type = substr($type,0,$position);
  159. }
  160. if($position = strpos($type,'?')){
  161. $type = substr($type,0,$position);
  162. }
  163. $query = substr($query,0,$pos);
  164. }
  165. $query=str_replace('~','-',$query);
  166. $args = explode('-',$query);
  167. $act = 'index';
  168. if(($ctl = array_shift($args)) && $ctl!='index'){
  169. $c = count($args);
  170. if($c>0 && !is_numeric($args[$c-1])){
  171. $act = array_pop($args);
  172. }
  173. }
  174. foreach($args as $k=>$v){
  175. $args[$k] = str_replace(array(';jh;',';dian;',';xie;'),array('-','.','/'),$v);
  176. }
  177. return array('controller'=>$ctl,'method'=>$act,'args'=>$args,'type'=>$type);
  178. }
  179. function &getLink($controller,$method,$args=null,$extname=null){
  180. if($controller=='index') return '';
  181. $array = array($controller);
  182. $use_arg = 0;
  183. if(is_array($args) && (isset($args[1]) || (isset($args[0]) && $args[0]))){
  184. $use_arg = 1;
  185. foreach($args as $k=>$v){
  186. $args[$k] = str_replace(array('-','.','/','%2F'),array(';jh;',';dian;',';xie;',';xie;'),$v);
  187. }
  188. $array = array_merge(array($controller),$args);
  189. }
  190. if($method!='index' || ($use_arg && !is_numeric(array_pop($args)))){
  191. $array[] = urlencode($method);
  192. }
  193. return implode('-',$array).'.'.($extname?$extname:$this->seoEmuFile);
  194. }
  195. function microtime(){
  196. list($usec, $sec) = explode(" ", microtime());
  197. return ((float)$usec + (float)$sec);
  198. }
  199. function realUrl($ctl,$act='index',$args=null,$extName = 'html',$base_url=null){
  200. if(!$base_url){
  201. $base_url = $this->base_url();
  202. }
  203. if(!$extName){
  204. $extName='html';
  205. }
  206. if(!isset($this->__emu_static)){
  207. $this->__emu_static = (!$this->getConf('system.seo.emuStatic'));
  208. $this->__link_builder = $this->getConf('system.seo.mklink');
  209. }
  210. if($this->__emu_static){
  211. $base_url.=APP_ROOT_PHP.'?';
  212. }
  213. if($ctl=='page' && $act=='index'){
  214. return $base_url;
  215. }else{
  216. return $base_url.$this->getLink($ctl?$ctl:$this->request['action']['controller'],$act?$act:$this->request['action']['method'],$args,$extName);
  217. }
  218. }
  219. function shutdown(){ }
  220. function errorHandler($errno, $errstr, $errfile, $errline){
  221. return ($errno== ($this->_halt_err_level & $errno))?false:true;
  222. }
  223. function co_start(){
  224. $this->_co_depth++;
  225. }
  226. function co_end(){
  227. return array_keys($this->_cacheObjects[$this->_co_depth--]);
  228. }
  229. function checkExpries($cname){
  230. if(is_array($cname)){
  231. for($i=$this->_co_depth;$i>0;$i--){
  232. foreach($cname as $obj){
  233. $this->_cacheObjects[$i][strtoupper($obj)]=1;
  234. }
  235. }
  236. }else{
  237. for($i=$this->_co_depth;$i>0;$i--){
  238. $this->_cacheObjects[$i][strtoupper($cname)]=1;
  239. }
  240. }
  241. }
  242. /**
  243. * &database
  244. *
  245. * @access public
  246. * @return void
  247. */
  248. function &database(){
  249. if(!isset($this->__db)){
  250. if(!class_exists('AloneDB')){
  251. require 'AloneDB.php';
  252. }
  253. $this->__db = new AloneDB($this);
  254. $this->__db->prefix = DB_PREFIX;
  255. }
  256. return $this->__db;
  257. }
  258. /**
  259. * error
  260. *
  261. * @param int $errcode
  262. * @access public
  263. * @return void
  264. */
  265. function error($errcode=404,$errmsg=null){
  266. if($errcode==404){
  267. $this->responseCode(404);
  268. }
  269. header('X-JSON: '.json_encode(array('code'=>$errcode,'id'=>time())));
  270. die('<h1>Error:'.$errcode.'</h1><p>'.$errmsg.'</p>');
  271. }
  272. function api_call($instance,$host,$file,$port=80,$tolken){
  273. require_once(API_DIR.'/include/api_utility.php');
  274. if(!$this->intance_api[$instance]){
  275. $this->intance_api[$instance]=new api_utility($host,$file,$port,$tolken);
  276. }
  277. return $this->intance_api[$instance];
  278. }
  279. /**
  280. * loadModel
  281. *
  282. * @param mixed $className
  283. * @param mixed $single
  284. * @access public
  285. * @return void
  286. */
  287. function &loadModel($modelName,$single=true){
  288. if($single && isset($this->models[strtolower($modelName)]) ){
  289. return $this->models[strtolower($modelName)];
  290. }
  291. $className='mdl_'.basename($modelName);
  292. class_exists('modelFactory') or require(CORE_INCLUDE_DIR.'/modelFactory.php');
  293. $plugin_path = explode("/",$modelName);
  294. if($plugin_path[0]=='plugins'){
  295. if(file_exists($modleFile=PLUGIN_DIR.'/app/'.$plugin_path[1].'/mdl.'.basename($modelName).'.php')){
  296. require($modleFile);
  297. }
  298. }
  299. if (!class_exists($className))
  300. if (file_exists($modelFile=CORE_DIR.'/'.$this->model_dir.'/'.dirname($modelName).'/mdl.'.basename($modelName).'.php'))
  301. require($modelFile);
  302. if (defined('CUSTOM_CORE_DIR')){
  303. $CusclassName='cmd_'.basename($modelName);
  304. if (!class_exists($CusclassName))
  305. if(file_exists($cusinc = CUSTOM_CORE_DIR.'/model/'.dirname($modelName).'/cmd.'.basename($modelName).'.php')){
  306. require($cusinc);
  307. $className=$CusclassName;
  308. }
  309. }
  310. $object= new $className();
  311. $object->modelName = $modelName;
  312. if($single){
  313. $this->models[strtolower($modelName)] = &$object;
  314. }
  315. return $object;
  316. }
  317. /**
  318. * callAction
  319. *
  320. * @param mixed $objMod
  321. * @param mixed $act_method
  322. * @access public
  323. * @return void
  324. */
  325. function callAction(&$objCtl,$act_method,$args=null){
  326. $protected = array_flip(get_class_methods('pagefactory'));
  327. $ctlmap = $this->getConf('system.ctlmap');
  328. if(!$this->request['action']['ident']){
  329. $this->request['action']['ident'] = strtolower('shop:'.
  330. $this->request['action']['controller'].
  331. ':'.$this->request['action']['method']);
  332. }
  333. if(!constant('SAFE_MODE') && isset($ctlmap[$this->request['action']['ident']])){
  334. $appmgr = $this->loadModel('system/appmgr');
  335. list($objCtl,$act_method) = $appmgr->get_func($ctlmap[$this->request['action']['ident']]);
  336. }
  337. $tmp_ident = $this->request['action']['ident'];
  338. foreach($ctlmap as $key=>$value){
  339. $appmgr = $this->loadModel('system/appmgr');
  340. if($key[strlen($key)-1]=='*'&&strstr($key,substr($tmp_ident,0,strrpos($tmp_ident,':')))){
  341. list($objCtl,$act_method) = $appmgr->get_func(substr($value,0,strrpos($value,':')).':'.$act_method);
  342. }
  343. }
  344. if(isset($objCtl->_call)){
  345. array_unshift($args,$act_method);
  346. $act_method = $objCtl->_call;
  347. }
  348. if($act_method{0}!=='_' && method_exists($objCtl,$act_method)){
  349. if(isset($args[0])){
  350. call_user_func_array(array(&$objCtl,$act_method),$args);
  351. }else{
  352. $objCtl->$act_method();
  353. }
  354. return true;
  355. }else{
  356. return false;
  357. }
  358. }
  359. /**
  360. * output
  361. *
  362. * @param mixed $content
  363. * @param int $expired_time
  364. * @param mixed $mime_type
  365. * @param mixed $headers
  366. * @param mixed $filename
  367. * @access public
  368. * @return void
  369. */
  370. function output($content,$expired_time=0,$mime_type=MIME_HTML,$headers=false,$filename=null){
  371. $lastmodified = gmdate("D, d M Y H:i:s");
  372. $expires = gmdate ("D, d M Y H:i:s", time() + 20);
  373. header("Last-Modified: " . $lastmodified . " GMT");
  374. header("Expires: " .$expires. " GMT");
  375. if(is_array($headers)){
  376. foreach($headers as $theheader){
  377. header($theheader);
  378. }
  379. }
  380. if($mime_type==MIME_HTML){
  381. header('Content-Type: text/html; charset=utf-8');
  382. echo($content);
  383. }else{
  384. header('Content-Type: '.$mime_type.'; charset=utf-8');
  385. if($filename){
  386. header('Content-Disposition: inline; filename="'.$filename.'"');
  387. }
  388. flush();
  389. echo($content);
  390. }
  391. }
  392. function getConf($key){
  393. return $this->__setting->get($key,$var);
  394. }
  395. function setConf($key,$data,$immediately=false){
  396. return $this->__setting->set($key,$data,$immediately);
  397. }
  398. function sprintf(){
  399. $args = func_get_args();
  400. $str = $args[0];
  401. unset($args[0]);
  402. $str = preg_replace_callback('/\\$([a-z\\.\\_0-9]+)\\$/is',array(&$this,'_rep_conf'),$str);
  403. foreach($args as $k=>$v){
  404. $str = str_replace('%'.$k,$v,$str);
  405. }
  406. return $str;
  407. }
  408. function _rep_conf($matches){
  409. return $this->getConf($matches[1]);
  410. }
  411. /**
  412. * sfile
  413. *
  414. * @param mixed $file
  415. * @access public
  416. * @return void
  417. */
  418. function sfile($file,$file_bak=null,$head_redect=false){
  419. if(!file_exists($file)){
  420. $file = $file_bak;
  421. }
  422. $etag = md5_file($file);
  423. header('Etag: '.$etag);
  424. if(isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag){
  425. header('HTTP/1.1 304 Not Modified',true,304);
  426. exit(0);
  427. }else{
  428. set_time_limit(0);
  429. header("Expires: " .$expires. " GMT");
  430. header("Cache-Control: public");
  431. session_cache_limiter('public');
  432. sendfile($file);
  433. }
  434. }
  435. function responseCode($code){
  436. $codeArr = array(
  437. 100=>'Continue',
  438. 101=>'Switching Protocols',
  439. 200=>'OK',
  440. 201=>'Created',
  441. 202=>'Accepted',
  442. 203=>'Non-Authoritative Information',
  443. 204=>'No Content',
  444. 205=>'Reset Content',
  445. 206=>'Partial Content',
  446. 300=>'Multiple Choices',
  447. 301=>'Moved Permanently',
  448. 302=>'Found',
  449. 303=>'See Other',
  450. 304=>'Not Modified',
  451. 305=>'Use Proxy',
  452. 307=>'Temporary Redirect',
  453. 400=>'Bad Request',
  454. 401=>'Unauthorized',
  455. 402=>'Payment Required',
  456. 403=>'Forbidden',
  457. 404=>'Not Found',
  458. 405=>'Method Not Allowed',
  459. 406=>'Not Acceptable',
  460. 407=>'Proxy Authentication Required',
  461. 408=>'Request Timeout',
  462. 409=>'Conflict',
  463. 410=>'Gone',
  464. 411=>'Length Required',
  465. 412=>'Precondition Failed',
  466. 413=>'Request Entity Too Large',
  467. 414=>'Request-URI Too Long',
  468. 415=>'Unsupported Media Type',
  469. 416=>'Requested Range Not Satisfiable',
  470. 417=>'Expectation Failed',
  471. 500=>'Internal Server Error',
  472. 501=>'Not Implemented',
  473. 502=>'Bad Gateway',
  474. 503=>'Service Unavailable',
  475. 504=>'Gateway Timeout',
  476. 505=>'HTTP Version Not Supported',
  477. );
  478. header('HTTP/1.1 '.$code.' '.$codeArr[$code],true,$code);
  479. }
  480. function version(){
  481. if(!file_exists(CORE_DIR.'/version.txt')){
  482. $return = array();
  483. }else{
  484. $return = parse_ini_file(CORE_DIR.'/version.txt');
  485. }
  486. $return['ver'] = $this->_app_version;
  487. return $return;
  488. }
  489. // ???UCAPP???? by yangbing 2010-8-16
  490. function toUcApp($appAct,$forward = ''){
  491. if(in_array($appAct,array('register','login','logout'))){
  492. if($appAct == 'register' && $_COOKIE['R_ID']){
  493. $rid = 'rid='.intval($_COOKIE['R_ID']).'&';
  494. }
  495. $forward = empty($forward)?$_SERVER["HTTP_REFERER"]:$forward;
  496. if(preg_match ("/member|cart|login|signup/i", $_SERVER["HTTP_REFERER"])){
  497. $addUrl = 'app=2';
  498. }else{
  499. $addUrl = 'forward='.$forward;
  500. }
  501. if(constant('UC_APP')){
  502. header('Location: '.UC_APP.'/'.$appAct.'?'.$rid.$addUrl);
  503. exit();
  504. }
  505. }
  506. return false;
  507. }
  508. }
  509. class nocache{
  510. function set($key,$value){return true;}
  511. function get($key,$value){return false;}
  512. function setModified(){;}
  513. function status(){;}
  514. function clear(){;}
  515. function exec(){;}
  516. function fetch(){return false;}
  517. function store(){return false;}
  518. }
  519. function unSafeVar(&$data)
  520. {
  521. if (is_array($data))
  522. {
  523. foreach ($data as $key => $value)
  524. {
  525. unSafeVar($data[$key]);
  526. }
  527. }else{
  528. $data = stripslashes($data);
  529. }
  530. }