PageRenderTime 62ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/b2b/core/include/shopCore.php

http://phpfor.googlecode.com/
PHP | 487 lines | 408 code | 66 blank | 13 comment | 78 complexity | b4eeb8f6a9f3bf21bb7a73c4e1c3ef11 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. }
  18. if(defined('MODE_SWITCHER')){
  19. $mode_switcher = MODE_SWITCHER;
  20. require_once(PLUGIN_DIR.'/functions/'.$switcher.'.php');
  21. $switcher = new $mode_switcher;
  22. if(!$switcher->test()){
  23. header('Content-type: text/html;charset=utf-8',true,503);
  24. readfile(HOME_DIR.'/notice.html');
  25. }
  26. }elseif(file_exists(HOME_DIR.'/notice.html')){
  27. header('Content-type: text/html;charset=utf-8',true,503);
  28. readfile(HOME_DIR.'/notice.html');
  29. exit;
  30. }
  31. if(file_exists(BASE_DIR.'/upgrade.php')){
  32. header('HTTP/1.1 503 Service Unavailable',true,503);
  33. require(CORE_DIR.'/func_ext.php');
  34. $smarty = &$this->loadModel('system/frontend');
  35. $smarty->display('shop:common/upgrade.html');
  36. }else{
  37. $this->run();
  38. }
  39. }
  40. function compactUrl($newurl){
  41. $this->_succ=true;
  42. header('Location: '.$newurl,true,301);
  43. exit;
  44. }
  45. /**
  46. * shop
  47. *
  48. * @access public
  49. * @return void
  50. */
  51. function run(){
  52. if(isset($_GET['gOo'])){
  53. $urlTools = &$this->loadModel('utility/url');
  54. if($url=$urlTools->oldVersionShopEx($_GET)){
  55. $this->compactUrl($url);
  56. }
  57. }
  58. ob_start();
  59. define('IN_SHOP',true);
  60. $_COOKIE = $_COOKIE[COOKIE_PFIX];
  61. $request = $this->parseRequest();
  62. $this->lang = $request['lang']?$request['lang']:DEFAULT_LANG;
  63. $request['money'] = $request['member_lv'].$request['cur'];
  64. $this->request = &$request;
  65. $GLOBALS['runtime'] = $request;
  66. if(isset($request['member'])){
  67. foreach($request['member'] as $k=>$v){
  68. $GLOBALS['runtime'][$k] = $v;
  69. }
  70. }
  71. $cacheAble = !(count($_POST)>0);
  72. if(defined('BLACKLIST')){
  73. $blackList = preg_split('/[\s,]+/',BLACKLIST);
  74. require_once(CORE_DIR.'/func_ext.php');
  75. if($this->match_network($blackList,remote_addr())){
  76. $this->_succ = true;
  77. header('Connection: close',true,401);
  78. echo '<h1>Access Denied</h1>';
  79. exit();
  80. }
  81. }
  82. if(isset($_GET['ctl'])){
  83. $page = &$this->_frontend($request,array(
  84. 'controller'=>$_GET['ctl'],
  85. 'method'=>isset($_GET['act'])?$_GET['act']:'index',
  86. 'args'=>isset($_GET['p'])?$_GET['p']:null));
  87. }elseif(!$cacheAble || !$this->cache->get($ident = implode('|',$request),$page)){
  88. register_shutdown_function(array(&$this,'shutdown'));
  89. $this->co_start();
  90. $page = &$this->_frontend($request);
  91. if($cacheAble && $page['cache']){
  92. $this->cache->set($ident,$page,$this->co_end());
  93. }
  94. }
  95. $this->display($page);
  96. exit();
  97. }
  98. function match_network ($nets, $ip, $first=false) {
  99. $return = false;
  100. if (!is_array ($nets)) $nets = array ($nets);
  101. foreach ($nets as $net) {
  102. $rev = (preg_match ("/^\!/", $net)) ? true : false;
  103. $net = preg_replace ("/^\!/", "", $net);
  104. $ip_arr = explode('/', $net);
  105. $net_long = ip2long($ip_arr[0]);
  106. $x = ip2long($ip_arr[1]);
  107. $mask = long2ip($x) == $ip_arr[1] ? $x : 0xffffffff << (32 - $ip_arr[1]);
  108. $ip_long = ip2long($ip);
  109. if ($rev) {
  110. if (($ip_long & $mask) == ($net_long & $mask)) return false;
  111. } else {
  112. if (($ip_long & $mask) == ($net_long & $mask)) $return = true;
  113. if ($first && $return) return true;
  114. }
  115. }
  116. return $return;
  117. }
  118. function errorHandler($errno, $errstr, $errfile, $errline){
  119. $this->_errArr[] = array('no'=>$errno,'msg'=>$errstr,'file'=>$errfile,'line'=>$errline);
  120. if($errno == ((E_ERROR | E_USER_ERROR) & $errno)){
  121. $this->shutdown(true);
  122. }
  123. return true;
  124. }
  125. function shutdown($halt=false){
  126. if($halt || !isset($this->_succ)){
  127. $this->_succ=true;
  128. $data = array('html'=>'','body'=>'','date'=>date("Y-m-d H:i:s (T)"),'fatal'=>null);
  129. $errorlevels = array(
  130. 2048 => 'Error',
  131. 1024 => 'Notice',
  132. 512 => 'Warning',
  133. 256 => 'Error',
  134. 128 => 'Warning',
  135. 64 => 'Error',
  136. 32 => 'Warning',
  137. 16 => 'Error',
  138. 8 => 'Notice',
  139. 4 => 'Error',
  140. 2 => 'Warning',
  141. 1 => 'Error');
  142. while(ob_get_level()>0){
  143. $data['body'] .= ob_get_contents();
  144. ob_end_clean();
  145. }
  146. if(!$halt){
  147. if($pos = strrpos($data['body'],'Fatal error')){
  148. $data['fatal'].=substr($data['body'],$pos);
  149. }elseif($pos = strrpos($data['body'],'Parse error')){
  150. $data['fatal'].=substr($data['body'],$pos);
  151. }
  152. }else{
  153. $err = array_pop($this->_errArr);
  154. $data['fatal'].= "<li class=\"err_{$err['no']}\"><b class=\"no\">{$errorlevels[$err['no']]}:</b> <span class=\"body\">{$err['msg']}<span>{$err['file']}:{$err['line']}</li>";
  155. }
  156. foreach($this->_errArr as $err){
  157. $data['html'].= "<li class=\"err_{$err['no']}\"><b class=\"no\">{$errorlevels[$err['no']]}:</b> <span class=\"body\">{$err['msg']}<span>{$err['file']}:{$err['line']}</li>";
  158. }
  159. if($data['fatal']){
  160. $this->responseCode(500);
  161. $data['msg'] = is_file(HOME_DIR.'/upload/error500.html')?file_get_contents(HOME_DIR.'/upload/error500.html'):'??????????????????';
  162. $html = file_get_contents(CORE_DIR.'/shop/view/page/system-error.html');
  163. foreach($data as $k=>$v){
  164. $html = str_replace('%'.$k.'%',$v,$html);
  165. }
  166. echo $html;
  167. }else{
  168. echo $data['html'].$data['body'];
  169. }
  170. if(function_exists('debug_backtrace')){
  171. echo '<ol>';
  172. $lastfile = null;
  173. foreach(debug_backtrace() as $trace){
  174. if(isset($trace['file']) && $trace['file']!=$lastfile){
  175. $lastfile = $trace['file'];
  176. echo '<div style="padding-top:10px;color:#999">'.$trace['file'].':'.$trace['line'].'</div>';
  177. }
  178. echo '<li style="font-weight:bold">'.(isset($trace['class'])?$trace['class']:'php').'::'.$trace['function'].'()</li>';
  179. }
  180. echo '</ol>';
  181. }
  182. exit();
  183. }
  184. }
  185. function setCookie($name,$value,$expire=false,$path=null){
  186. if(!isset($this->_cookiePath) || !$this->_cookiePath){
  187. $cookieLife = $this->getConf('system.cookie.life');
  188. $this->_cookiePath = substr(PHP_SELF, 0, strrpos(PHP_SELF, '/')).'/';
  189. $this->_cookieLife = $cookieLife;
  190. }
  191. $this->_cookieLife = ($this->_cookieLife>0)?$this->_cookieLife:315360000;
  192. setCookie(COOKIE_PFIX.'['.$name.']',$value,($expire===false)?(time()+$this->_cookieLife):$expire,$this->_cookiePath);
  193. $_COOKIE[$name] = $value;
  194. }
  195. function display(&$pageObj){
  196. $this->_succ = true;
  197. $header_sent = headers_sent();
  198. // header('Runtime: '.($this->microtime() - $this->_start));
  199. header('Connection: close');
  200. if($pageObj['cache']){
  201. header("Cache-Control: private");
  202. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  203. if(isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $pageObj['header']['Etag']){
  204. header('Etag: '.$pageObj['header']['Etag'],true,304);
  205. exit(0);
  206. }
  207. // header("Expires: " .gmdate("D, d M Y H:i:s", time() + (isset($pageObj['cachettl'])?$pageObj['cachettl']:1)). " GMT");
  208. }else{
  209. if(strpos($_SERVER["HTTP_USER_AGENT"],'MSIE 6.0') && !defined('ORDER_DOWNLOAD')){
  210. header("Cache-Control: no-cache"); // ????
  211. }else{
  212. header("Cache-Control: no-cache, no-store, must-revalidate"); // ????
  213. }
  214. // header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  215. header("Expires: -1");
  216. header("Pragma: no-cache");
  217. unset($pageObj['header']['Last-Modified']);
  218. unset($pageObj['header']['Etag']);
  219. }
  220. foreach($pageObj['header'] as $k=>$v){
  221. header($k.': '.$v);
  222. }
  223. if($pageObj['gziped'] && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && !$header_sent){
  224. if(strpos(" ".$_SERVER['HTTP_ACCEPT_ENCODING'],"gzip")){
  225. header('Content-Encoding: gzip');
  226. header('Content-Length: '.$pageObj['gziped-size']);
  227. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'HEAD') exit(0);
  228. echo $pageObj['gziped'];
  229. }elseif(strpos(" ".$_SERVER['HTTP_ACCEPT_ENCODING'],"x-gzip")){
  230. header('Content-Encoding: x-gzip');
  231. header('Content-Length: '.$pageObj['gziped-size']);
  232. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'HEAD') exit(0);
  233. echo $pageObj['gziped'];
  234. }else{
  235. header('Content-Length: '.$pageObj['size']);
  236. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'HEAD') exit(0);
  237. echo $pageObj['body'];
  238. }
  239. }else{
  240. header('Content-Length: '.$pageObj['size']);
  241. if (strtoupper($_SERVER['REQUEST_METHOD']) == 'HEAD') exit(0);
  242. echo $pageObj['body'];
  243. }
  244. exit();
  245. }
  246. function mkUrl($ctl,$act='index',$args=null,$extName = 'html'){
  247. return $this->realUrl($ctl,$act,$args,$extName,$this->request['base_url']);
  248. }
  249. function parseUrl($query){
  250. return ($query=='index.html')
  251. ?array('controller'=>'page','method'=>'index','args'=>array(),'type'=>'html')
  252. :$this->call($this->getConf('system.seo.parselink'),array($query));
  253. }
  254. function &_frontend($request,$action=null){
  255. require_once(CORE_DIR.'/func_ext.php');
  256. ob_start();
  257. if(!$action)$action = $this->parseUrl($request['query']);
  258. $this->request['action'] = &$action;
  259. require_once('shopPage.php');
  260. $controller = &$this->getController($action['controller']);
  261. $controller->action = &$action;
  262. $this->ctl = &$controller;
  263. if(!is_object($controller))$this->error(404);
  264. $this->use_gzip = (function_exists('gzencode') && ($this->getConf('system.use_gzip') === true || $this->getConf('system.use_gzip')!='false'));
  265. $controller->_header = &$page['header'];
  266. if(!$this->callAction($controller,$action['method'],$action['args'])){
  267. $urlTools = &$this->loadModel('utility/url');
  268. if($newurl = $urlTools->map($_SERVER['QUERY_STRING'])){
  269. $this->compactUrl($newurl);
  270. }else{
  271. $this->error(404);
  272. }
  273. }
  274. $page = array(
  275. 'header'=>array('Content-Language'=>'utf-8'),
  276. 'cache'=>!$controller->noCache,
  277. 'body'=>'',
  278. 'size'=>0,
  279. );
  280. $this->_succ = true;
  281. $ob_length=ob_get_level()-1;
  282. while(ob_get_level()>0){
  283. if($ob_length==ob_get_level()){
  284. break;
  285. }else{
  286. $ob_length=ob_get_level();
  287. }
  288. $page['size'] += ob_get_length();
  289. $page['body'] .= ob_get_contents();
  290. ob_end_clean();
  291. }
  292. if(isset($controller->cachettl)) $page['cachettl'] = $controller->cachettl; //
  293. if(isset($this->_expiresTime)) $page['expires'] = $this->_expiresTime;
  294. $page['header']['Etag'] = crc32($page['body']);
  295. $page['header']['Last-Modified'] = gmdate('D, d M Y H:i:s').' GMT';
  296. $this->_debugger['runtime'] = $this->microtime() - $this->_start;
  297. $this->_debugger['gzip'] = $this->use_gzip;
  298. $page['header']['Content-type'] = $controller->contentType;
  299. if($this->use_gzip){
  300. if($page['gziped'] = @gzencode($page['body'], 3)){
  301. $page['gziped-size'] = strlen($page['gziped']);
  302. }
  303. }
  304. return $page;
  305. }
  306. function setExpries($time){
  307. if($time>time()){
  308. $this->_expiresTime = isset($this->_expiresTime)?min($time,$this->_expiresTime):$time;
  309. }
  310. return true;
  311. }
  312. function &getController($mod){
  313. $object = false;
  314. $fname = CORE_DIR.'/shop/controller/'.dirname($mod).'/ctl.'.basename($mod).'.php';
  315. if (defined('CUSTOM_CORE_DIR')){
  316. $cusfname = CUSTOM_CORE_DIR.'/shop/controller/'.dirname($mod).'/cct.'.basename($mod).'.php';
  317. if (file_exists($fname))
  318. require($fname);
  319. if (file_exists($cusfname)){
  320. require($cusfname);
  321. $mod_name='cct_'.basename($mod);
  322. }
  323. else{
  324. $mod_name = 'ctl_'.basename($mod);
  325. }
  326. $object = new $mod_name($this);
  327. return $object;
  328. }
  329. else{
  330. if(!is_file($fname)){
  331. $this->error(404);
  332. }else{
  333. require($fname);
  334. $mod_name = 'ctl_'.basename($mod);
  335. $object = new $mod_name($this);
  336. return $object;
  337. }
  338. }
  339. }
  340. function error($code){
  341. if($code==404){
  342. $this->responseCode(404);
  343. $this->_succ=true;
  344. header("Content-Type: text/html; charset=utf-8");
  345. echo $this->getConf('errorpage.p404');
  346. }else{
  347. $this->responseCode(500);
  348. header("Content-Type: text/html; charset=utf-8");
  349. echo $this->getConf('errorpage.p500');
  350. }
  351. die();
  352. }
  353. function _build_post($d,$path=null){
  354. $m='';
  355. foreach($d as $k=>$v){
  356. $p = $path?$path.'['.$k.']':$k;
  357. if(is_array($v)){
  358. $m .= $this->_build_post($v,$p);
  359. }else{
  360. $m .='<input type="hidden" name="'.$p.'" value="'.$v.'" />';
  361. }
  362. }
  363. return $m;
  364. }
  365. /*???????
  366. ????:
  367. ??_GET
  368. */
  369. function parseRequest($query = null){
  370. if(!$query){
  371. $query = $_SERVER["QUERY_STRING"];
  372. if(!($REQUEST_URI = getenv('REQUEST_URI'))){
  373. if(isset($_SERVER['HTTP_X_REWRITE_URL'])){
  374. $REQUEST_URI = $_SERVER['HTTP_X_REWRITE_URL']?$_SERVER['HTTP_X_REWRITE_URL']:$_SERVER['REQUEST_URI'];
  375. }else{
  376. $REQUEST_URI = $_SERVER['REQUEST_URI'];
  377. }
  378. }
  379. }
  380. $get = null;
  381. if($p = strpos($query,'?')){
  382. $get = substr($query,$p+1);
  383. $query = substr($query,0,$p);
  384. }else{
  385. $p = parse_url($REQUEST_URI);
  386. if(isset($p['query']))$get = $p['query'];
  387. }
  388. if($get!=$query){
  389. parse_str($get,$get);
  390. $_GET = array_merge($_GET,$get);
  391. }
  392. $url_prefix = $this->base_url();
  393. return array(
  394. 'base_url'=>$url_prefix,
  395. 'member_lv'=>isset($_COOKIE['MLV'])?$_COOKIE['MLV']:-1,
  396. 'query'=>$query?$query:'index.html',
  397. 'cur'=>isset($_COOKIE['CUR'])?$_COOKIE['CUR']:null,
  398. 'lang'=>isset($_COOKIE['LANG'])?$_COOKIE['LANG']:null
  399. );
  400. }
  401. function location($url){
  402. if($_POST){
  403. $html="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  404. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  405. <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en-US\" lang=\"en-US\" dir=\"ltr\">
  406. <head></header><body>Redirecting...";
  407. $html .= '<form id="splash" action="'.$url.'" method="post">'.$this->_build_post($_POST);
  408. $html.=<<<EOF
  409. </form><script language="javascript">
  410. document.getElementById('splash').submit();
  411. </script></html>
  412. EOF;
  413. echo $html;
  414. exit();
  415. }else{
  416. header('Location: '.$url);
  417. exit();
  418. }
  419. }
  420. }
  421. ?>