PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/new88li/core/include/shopCore.php

http://phpfor.googlecode.com/
PHP | 406 lines | 301 code | 53 blank | 52 comment | 54 complexity | 00984263fc7f94854c203313e69dcbe4 MD5 | raw file
  1. <?php
  2. require_once(CORE_DIR.'/kernel.php');
  3. define('COOKIE_PFIX','S');
  4. define('MAKE_DIR',true);
  5. class shopCore extends kernel{
  6. var $member = null;
  7. var $is_shop = true;
  8. var $_err = array();
  9. var $ErrorSet = array();
  10. var $use_gzip = true;
  11. var $page;
  12. function shopCore(){
  13. parent::kernel();
  14. /*if(isset($_GET['_test_rewrite'])){
  15. echo '[*['.md5($_GET['s']).']*]';
  16. exit;
  17. }elseif(defined('MODE_SWITCHER')){
  18. $mode_switcher = MODE_SWITCHER;
  19. require_once(PLUGIN_DIR.'/functions/'.$switcher.'.php');
  20. $switcher = new $mode_switcher;
  21. if(!$switcher->test()){
  22. header('Content-type: text/html;charset=utf-8',true,503);
  23. readfile(HOME_DIR.'/notice.html');
  24. }
  25. }elseif(file_exists(HOME_DIR.'/notice.html')){ //review: ????
  26. header('Content-type: text/html;charset=utf-8',true,503);
  27. readfile(HOME_DIR.'/notice.html');
  28. exit;
  29. }
  30. if(file_exists(BASE_DIR.'/upgrade.php')){ //review: ????
  31. header('HTTP/1.1 503 Service Unavailable',true,503);
  32. require(CORE_DIR.'/func_ext.php');
  33. $smarty = &$this->loadModel('system/frontend');
  34. $smarty->display('shop:common/upgrade.html');
  35. }elseif($_POST['api_url'] == 'time_auth'){
  36. require(CORE_INCLUDE_DIR.'/shop/core.time_auth.php');
  37. core_time_auth($this);
  38. }else{*/
  39. $this->run();
  40. //}
  41. }
  42. function compactUrl($newurl){
  43. $this->_succ=true;
  44. header('Location: '.$newurl,true,301);
  45. exit;
  46. }
  47. /**
  48. * shop
  49. *
  50. * @access public
  51. * @return void
  52. */
  53. function run(){
  54. ob_start();
  55. define('IN_SHOP',true);
  56. $_COOKIE = $_COOKIE[COOKIE_PFIX]; //???????????, ????????
  57. $request = $this->parseRequest();
  58. $this->lang = isset($request['lang'])?$request['lang']:DEFAULT_LANG;
  59. $request['money'] = $request['member_lv'].$request['cur'];
  60. $this->request = &$request;
  61. $GLOBALS['runtime'] = &$request;
  62. if(isset($request['member'])){
  63. foreach($request['member'] as $k=>$v){
  64. $GLOBALS['runtime'][$k] = $v;
  65. }
  66. }
  67. $cacheAble = !(count($_POST)>0);
  68. /*if(constant('BLACKLIST')){
  69. $blackList = preg_split('/[\s,]+/',BLACKLIST);
  70. if(!function_exists('shop_match_network')){
  71. require(CORE_INCLUDE_DIR.'/shop/core.match_network.php');
  72. }
  73. if(!function_exists('remote_addr')){
  74. require(CORE_DIR.'/func_ext.php');
  75. }
  76. if(shop_match_network($blackList,remote_addr())){
  77. $this->_succ = true;
  78. header('Connection: close',true,401);
  79. echo '<h1>Access Denied</h1>';
  80. exit();
  81. }
  82. }*/
  83. $page = null;
  84. if(isset($_GET['ctl'])){
  85. $page = &$this->_frontend($request,array(
  86. 'controller'=>$_GET['ctl'],
  87. 'method'=>isset($_GET['act'])?$_GET['act']:'index',
  88. 'args'=>isset($_GET['p'])?$_GET['p']:null));
  89. }elseif(!$cacheAble || !$this->cache->get($ident = implode('|',$request),$page)){
  90. register_shutdown_function(array(&$this,'shutdown'));
  91. $this->co_start();
  92. $page = &$this->_frontend($request);
  93. if($cacheAble && $page['cache']){
  94. $this->cache->set($ident,$page,$this->co_end());
  95. }
  96. }
  97. $this->display($page);
  98. exit();
  99. }
  100. function errorHandler($errno, $errstr, $errfile, $errline){
  101. $this->_errArr[] = array('no'=>$errno,'msg'=>$errstr,'file'=>$errfile,'line'=>$errline);
  102. if($errno == ((E_ERROR | E_USER_ERROR) & $errno)){
  103. $this->shutdown(true);
  104. }
  105. return true;
  106. }
  107. function shutdown($halt=false){
  108. if($halt || !$this->_succ){
  109. if(!function_exists('shop_core_debugger')){
  110. require(CORE_INCLUDE_DIR.'/shop/core.debugger.php');
  111. shop_core_debugger($this);
  112. }
  113. }
  114. }
  115. function setCookie($name,$value,$expire=false,$path=null){
  116. if(!$this->_cookiePath){
  117. $cookieLife = $this->getConf('system.cookie.life');
  118. $this->_cookiePath = substr(PHP_SELF, 0, strrpos(PHP_SELF, '/')).'/';
  119. $this->_cookieLife = $cookieLife;
  120. }
  121. $this->_cookieLife = ($this->_cookieLife>0)?$this->_cookieLife:315360000;
  122. setCookie(COOKIE_PFIX.'['.$name.']',$value,($expire===false)?(time()+$this->_cookieLife):$expire,$this->_cookiePath);
  123. $_COOKIE[$name] = $value;
  124. }
  125. function display(&$pageObj){
  126. $this->_succ = true;
  127. $header_sent = headers_sent();
  128. header('Connection: close');
  129. if($pageObj['cache']){
  130. header("Cache-Control: private");
  131. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  132. }else{
  133. header("Cache-Control: no-cache, no-store, must-revalidate"); // ????
  134. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  135. header("Pragma: no-cache");
  136. }
  137. if(isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $pageObj['header']['Etag']){
  138. header('Etag: '.$pageObj['header']['Etag'],true,304);
  139. exit(0);
  140. }
  141. foreach($pageObj['header'] as $k=>$v){
  142. header($k.': '.$v);
  143. }
  144. if($pageObj['gziped'] && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && !$header_sent){
  145. if(strpos(" ".$_SERVER['HTTP_ACCEPT_ENCODING'],"gzip")){
  146. header('Content-Encoding: gzip');
  147. header('Content-Length: '.$pageObj['gziped-size']);
  148. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'HEAD') exit(0);
  149. echo $pageObj['gziped'];
  150. }elseif(strpos(" ".$_SERVER['HTTP_ACCEPT_ENCODING'],"x-gzip")){
  151. header('Content-Encoding: x-gzip');
  152. header('Content-Length: '.$pageObj['gziped-size']);
  153. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'HEAD') exit(0);
  154. echo $pageObj['gziped'];
  155. }else{
  156. header('Content-Length: '.$pageObj['size']);
  157. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'HEAD') exit(0);
  158. echo $pageObj['body'];
  159. }
  160. }else{
  161. header('Content-Length: '.$pageObj['size']);
  162. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'HEAD') exit(0);
  163. echo $pageObj['body'];
  164. }
  165. exit();
  166. }
  167. function mkUrl($ctl,$act='index',$args=null,$extName = 'html'){
  168. return $this->realUrl($ctl,$act,$args,$extName,$this->request['base_url']);
  169. }
  170. function &_frontend($request,$action=null){
  171. if(!function_exists('mkdir_p')){
  172. require(CORE_DIR.'/func_ext.php');
  173. }
  174. ob_start();
  175. if(!$action){
  176. $action = ($request['query']=='index.html')
  177. ?array('controller'=>'page','method'=>'index','args'=>array(),'type'=>'html')
  178. :$this->parse($request['query']);
  179. }
  180. $this->request['action'] = &$action;
  181. require_once('shopPage.php');
  182. $controller = &$this->getController($action['controller']);
  183. $controller->action = &$action;
  184. $this->ctl = &$controller;
  185. if(!is_object($controller))$this->error(404);
  186. $this->use_gzip = (function_exists('gzencode') && !constant('WITHOUT_GZIP'));
  187. $controller->_header = &$page['header'];
  188. if(!$this->callAction($controller,$action['method'],$action['args'])){
  189. $urlTools = &$this->loadModel('utility/url');
  190. if($newurl = $urlTools->map($_SERVER['QUERY_STRING'])){
  191. $this->compactUrl($newurl);
  192. }else{
  193. $this->error(404);
  194. }
  195. }
  196. $page = array(
  197. 'header'=>array('Content-Language'=>'utf-8'),
  198. 'cache'=>!$controller->noCache,
  199. 'body'=>'',
  200. 'size'=>0,
  201. );
  202. $this->_succ = true;
  203. $ob_length=ob_get_level()-1;
  204. $_tmpi=10;
  205. while(ob_get_level()>0 && $_tmpi>0){
  206. $_tmpi--;
  207. if($ob_length==ob_get_level()){
  208. break;
  209. }else{
  210. $ob_length=ob_get_level();
  211. }
  212. $page['size'] += ob_get_length();
  213. $page['body'] .= ob_get_contents();
  214. ob_end_clean();
  215. }
  216. if(isset($controller->cachettl)) $page['cachettl'] = $controller->cachettl; //
  217. if(isset($this->_expiresTime)) $page['expires'] = $this->_expiresTime;
  218. $page['header']['Etag'] = md5($page['body']);
  219. $page['header']['Last-Modified'] = gmdate('D, d M Y H:i:s').' GMT';
  220. $page['header']['Content-type'] = $controller->contentType;
  221. if($this->use_gzip){
  222. if($page['gziped'] = @gzencode($page['body'], 3)){
  223. $page['gziped-size'] = strlen($page['gziped']);
  224. }
  225. }
  226. return $page;
  227. }
  228. function setExpries($time){
  229. if($time>time()){
  230. $this->_expiresTime = isset($this->_expiresTime)?min($time,$this->_expiresTime):$time;
  231. }
  232. return true;
  233. }
  234. function &getController($mod){
  235. $object = false;
  236. $fname = CORE_DIR.'/shop/controller/'.dirname($mod).'/ctl.'.basename($mod).'.php';
  237. if(substr($mod,0,7)=='action_'){
  238. $addon = &$this->loadModel('system/addons');
  239. $object = &$addon->load(substr($mod,7),'shop');
  240. $object->template_dir = dirname($object->plugin_path).'/';
  241. $object->db = &$this->database();
  242. return $object;
  243. }elseif (defined('CUSTOM_CORE_DIR')){
  244. $cusfname = CUSTOM_CORE_DIR.'/shop/controller/'.dirname($mod).'/cct.'.basename($mod).'.php';
  245. if (file_exists($fname))
  246. require($fname);
  247. if (file_exists($cusfname)){
  248. require($cusfname);
  249. $mod_name='cct_'.basename($mod);
  250. }
  251. else{
  252. $mod_name = 'ctl_'.basename($mod);
  253. }
  254. if(class_exists($mod_name)){
  255. $object = new $mod_name($this);
  256. return $object;
  257. }else{
  258. $this->error(404);
  259. }
  260. }else{
  261. if(!file_exists($fname)){
  262. $this->error(404);
  263. }else{
  264. require($fname);
  265. $mod_name = 'ctl_'.basename($mod);
  266. $object = new $mod_name($this);
  267. return $object;
  268. }
  269. }
  270. }
  271. function error($code){
  272. if($code==404){
  273. $this->responseCode(404);
  274. $this->_succ=true;
  275. header("Content-Type: text/html; charset=utf-8");
  276. echo $this->getConf('errorpage.p404').str_repeat(' ',512);
  277. }else{
  278. $this->responseCode(500);
  279. header("Content-Type: text/html; charset=utf-8");
  280. echo $this->getConf('errorpage.p500');
  281. }
  282. die();
  283. }
  284. function _build_post($d,$path=null){
  285. $m='';
  286. foreach($d as $k=>$v){
  287. $p = $path?$path.'['.$k.']':$k;
  288. if(is_array($v)){
  289. $m .= $this->_build_post($v,$p);
  290. }else{
  291. $m .='<input type="hidden" name="'.$p.'" value="'.$v.'" />';
  292. }
  293. }
  294. return $m;
  295. }
  296. /*???????
  297. ????:
  298. ??_GET
  299. */
  300. function parseRequest(){
  301. $query = $_SERVER["QUERY_STRING"];
  302. if(!($REQUEST_URI = getenv('REQUEST_URI'))){
  303. if(isset($_SERVER['HTTP_X_REWRITE_URL'])){
  304. $REQUEST_URI = $_SERVER['HTTP_X_REWRITE_URL']?$_SERVER['HTTP_X_REWRITE_URL']:$_SERVER['REQUEST_URI'];
  305. }else{
  306. $REQUEST_URI = $_SERVER['REQUEST_URI'];
  307. }
  308. }
  309. $get = null;
  310. if($p = strpos($query,'?')){
  311. $get = substr($query,$p+1);
  312. $query = substr($query,0,$p);
  313. }else{
  314. $p = parse_url($REQUEST_URI);
  315. if(isset($p['query']))$get = $p['query'];
  316. }
  317. if($get!=$query){ //review: ???
  318. parse_str($get,$get);
  319. $_GET = array_merge($_GET,(array)$get);
  320. if (get_magic_quotes_gpc()){
  321. unSafeVar($_GET);
  322. }
  323. }
  324. return array(
  325. 'base_url'=>$this->base_url(),
  326. 'member_lv'=>isset($_COOKIE['MLV'])?$_COOKIE['MLV']:-1,
  327. 'query'=>$query?$query:'index.html',
  328. 'cur'=>isset($_COOKIE['CUR'])?$_COOKIE['CUR']:null,
  329. 'lang'=>isset($_COOKIE['LANG'])?$_COOKIE['LANG']:null
  330. );
  331. }
  332. function location($url){
  333. if($_POST){
  334. $html="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  335. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  336. <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\" lang=\"en-US\" dir=\"ltr\">
  337. <head></header><body>Redirecting...";
  338. $html .= '<form id="splash" action="'.$url.'" method="post">'.$this->_build_post($_POST);
  339. $html.=<<<EOF
  340. </form><script language="javascript">
  341. document.getElementById('splash').submit();
  342. </script></html>
  343. EOF;
  344. echo $html;
  345. exit();
  346. }else{
  347. header('Location: '.$url);
  348. exit();
  349. }
  350. }
  351. }
  352. ?>