PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/aoliz/core/kernel.php

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