PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/changleqi/core/include/shopCore.php

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