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

/aoliz/core/include/shopCore.php

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