PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/core/service/fanwe.service.php

https://github.com/alin40404/FanweShare
PHP | 522 lines | 433 code | 63 blank | 26 comment | 78 complexity | a00a211376b47078b2ae9bae4ce410e3 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 方维购物分享网站系统 (Build on ThinkPHP)
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. /**
  8. * fanwe.service
  9. *
  10. * 全局服务类
  11. *
  12. * @package service
  13. * @author awfigq <awfigq@qq.com>
  14. */
  15. define('IN_FANWE', true);
  16. error_reporting(E_ERROR);
  17. class FanweService
  18. {
  19. public $db = NULL;
  20. public $cache = NULL;
  21. public $session = NULL;
  22. public $memory = NULL;
  23. public $is_init = false;
  24. public $is_memory = true;
  25. public $is_session = true;
  26. public $is_admin = false;
  27. public $is_user = true;
  28. public $is_cron = true;
  29. public $is_setting = true;
  30. public $is_misc = true;
  31. public $is_group_city = false;
  32. public $config = array();
  33. public $var = array();
  34. public $cache_list = array('goods_category','image_servers','links','navs');
  35. public $allow_global = array(
  36. 'GLOBALS' => 1,
  37. '_GET' => 1,
  38. '_POST' => 1,
  39. '_REQUEST' => 1,
  40. '_COOKIE' => 1,
  41. '_SERVER' => 1,
  42. '_ENV' => 1,
  43. '_FILES' => 1,
  44. );
  45. public function &instance()
  46. {
  47. static $_instance = NULL;
  48. if($_instance === NULL)
  49. $_instance = new FanweService();
  50. return $_instance;
  51. }
  52. public function FanweService()
  53. {
  54. if(phpversion() < '5.3.0')
  55. set_magic_quotes_runtime(0);
  56. if(!defined('FANWE_ROOT'))
  57. define('FANWE_ROOT', str_replace('\\', '/',substr(dirname(__FILE__), 0, -12)));
  58. if(!file_exists(FANWE_ROOT.'./public/install.lock'))
  59. {
  60. header('Location: install/index.php');
  61. exit;
  62. }
  63. define('MAGIC_QUOTES_GPC', function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc());
  64. define('ICONV_ENABLE', function_exists('iconv'));
  65. define('MB_ENABLE', function_exists('mb_convert_encoding'));
  66. define('EXT_OBGZIP', function_exists('ob_gzhandler'));
  67. define('TIMESTAMP', time());
  68. if(!include(FANWE_ROOT.'./core/function/global.func.php'))
  69. {
  70. exit('not found global.func.php');
  71. }
  72. @require(FANWE_ROOT.'./public/constant.global.php');
  73. require fimport("function/time");
  74. define('IS_ROBOT', checkRobot());
  75. if(function_exists('ini_get'))
  76. {
  77. $memory_limit = @ini_get('memory_limit');
  78. if($memory_limit && getBytes($memory_limit) < 33554432 && function_exists('ini_set'))
  79. {
  80. ini_set('memory_limit', '128M');
  81. }
  82. }
  83. if(!$this->is_admin)
  84. {
  85. foreach ($GLOBALS as $key => $value)
  86. {
  87. if (!isset($this->allow_global[$key]))
  88. {
  89. $GLOBALS[$key] = NULL;
  90. unset($GLOBALS[$key]);
  91. }
  92. }
  93. }
  94. global $_FANWE;
  95. $_FANWE = array();
  96. $_FANWE['uid'] = 0;
  97. $_FANWE['user_name'] = '';
  98. $_FANWE['gid'] = 0;
  99. $_FANWE['sid'] = '';
  100. $_FANWE['form_hash'] = '';
  101. $_FANWE['client_ip'] = getFClientIp();
  102. $_FANWE['referer'] = '';
  103. $_FANWE['php_self'] = htmlspecialchars(getPhpSelf());
  104. if($_FANWE['php_self'] === false)
  105. systemError('request_tainting');
  106. $_FANWE['module_name'] = MODULE_NAME;
  107. $_FANWE['module_filename'] = basename($_FANWE['php_self']);
  108. $_FANWE['site_url'] = '';
  109. $_FANWE['site_root'] = '';
  110. $_FANWE['site_port'] = '';
  111. $_FANWE['config'] = array();
  112. $_FANWE['setting'] = array();
  113. $_FANWE['user'] = array();
  114. $_FANWE['group'] = array();
  115. $_FANWE['cookie'] = array();
  116. $_FANWE['cache'] = array();
  117. $_FANWE['session'] = array();
  118. $_FANWE['lang'] = array();
  119. $_FANWE['tpl_user_formats'] = array();
  120. $site_path = substr($_FANWE['php_self'], 0, strrpos($_FANWE['php_self'], '/'));
  121. $_FANWE['site_url'] = htmlspecialchars('http://'.$_SERVER['HTTP_HOST'].$site_path.'/');
  122. $url = parse_url($_FANWE['site_url']);
  123. $_FANWE['site_root'] = isset($url['path']) ? $url['path'] : '';
  124. $_FANWE['site_port'] = empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];
  125. if(defined('SUB_DIR'))
  126. {
  127. $_FANWE['site_url'] = str_replace(SUB_DIR, '', $_FANWE['site_url']);
  128. $_FANWE['site_root'] = str_replace(SUB_DIR, '', $_FANWE['site_root']);
  129. }
  130. define('PUBLIC_ROOT', FANWE_ROOT.'./public/');
  131. define('PUBLIC_PATH', $_FANWE['site_root'].'public/');
  132. define('SITE_URL', $_FANWE['site_root']);
  133. require fimport("class/cache");
  134. $this->cache = Cache::getInstance();
  135. $this->var = &$_FANWE;
  136. $this->buildConfig();
  137. $this->buildInput();
  138. $this->buildOutput();
  139. }
  140. public function initialize()
  141. {
  142. if(!$this->is_init)
  143. {
  144. $this->buildDb();
  145. $this->buildMemory();
  146. $this->buildSetting();
  147. $this->buildSession();
  148. //$this->buildCron();
  149. $this->buildCache();
  150. $this->buildUser();
  151. //$this->buildRewriteArgs();
  152. $this->buildMisc();
  153. }
  154. $this->is_init = true;
  155. define('TPL_PATH', $this->var['site_root'].'tpl/'.$this->var['setting']['site_tmpl'].'/');
  156. define('TMPL', $this->var['setting']['site_tmpl']);
  157. @include(FANWE_ROOT.'./tpl/'.$this->var['setting']['site_tmpl'].'/functions.php');
  158. if($this->var['setting']['shop_closed'] == 1 && !$this->is_admin)
  159. {
  160. showError(lang('common','site_close'),lang('common','site_close_content'),'',0,true);
  161. }
  162. }
  163. private function buildConfig()
  164. {
  165. $config = array();
  166. @include FANWE_ROOT.'./public/config.global.php';
  167. if(empty($config))
  168. {
  169. if(!file_exists(FANWE_ROOT.'./public/install.lock'))
  170. {
  171. header('Location: install');
  172. exit;
  173. }
  174. else
  175. {
  176. systemError('config_not_found');
  177. }
  178. }
  179. if(empty($config['security']['authkey']))
  180. {
  181. $config['security']['authkey'] = md5($config['cookie']['cookie_pre'].$config['db'][1]['dbname']);
  182. }
  183. if(empty($config['debug']) || !file_exists(fimport('function/debug')))
  184. {
  185. define('SYS_DEBUG', false);
  186. }
  187. elseif($config['debug'] === 1 || $config['debug'] === 2 || !empty($_REQUEST['debug']) && $_REQUEST['debug'] === $config['debug'])
  188. {
  189. define('SYS_DEBUG', true);
  190. if($config['debug'] == 2)
  191. error_reporting(E_ALL);
  192. }
  193. else
  194. {
  195. define('SYS_DEBUG', false);
  196. }
  197. timezoneSet($config['time_zone']);
  198. define('TIME_UTC', fGmtTime());
  199. $this->config = & $config;
  200. $this->var['config'] = & $config;
  201. if(substr($config['cookie']['cookie_path'], 0, 1) != '/')
  202. $this->var['config']['cookie']['cookie_path'] = '/'.$this->var['config']['cookie']['cookie_path'];
  203. $this->var['config']['cookie']['cookie_pre'] = $this->var['config']['cookie']['cookie_pre'].substr(md5($this->var['config']['cookie']['cookie_path'].'|'.$this->var['config']['cookie']['cookie_domain']), 0, 4).'_';
  204. }
  205. private function buildInput()
  206. {
  207. if (isset($_GET['GLOBALS']) || isset($_POST['GLOBALS']) || isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS']))
  208. {
  209. systemError('request_tainting');
  210. }
  211. if(!MAGIC_QUOTES_GPC && !$this->is_admin)
  212. {
  213. $_GET = fAddslashes($_GET);
  214. $_POST = fAddslashes($_POST);
  215. $_COOKIE = fAddslashes($_COOKIE);
  216. $_FILES = fAddslashes($_FILES);
  217. }
  218. $pre_length = strlen($this->config['cookie']['cookie_pre']);
  219. foreach($_COOKIE as $key => $val)
  220. {
  221. if(substr($key, 0, $pre_length) == $this->config['cookie']['cookie_pre'])
  222. {
  223. $this->var['cookie'][substr($key, $pre_length)] = $val;
  224. }
  225. }
  226. if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST))
  227. $_GET = array_merge($_GET, $_POST);
  228. foreach($_GET as $k => $v)
  229. {
  230. $this->var['request'][$k] = $v;
  231. }
  232. $this->var['isajax'] = empty($this->var['request']['isajax']) ? 0 : 1;
  233. $this->var['page'] = empty($this->var['request']['page']) ? 1 : max(1, intval($this->var['request']['page']));
  234. $this->var['sid'] = $this->var['cookie']['sid'] = isset($this->var['cookie']['sid']) ? htmlspecialchars($this->var['cookie']['sid']) : '';
  235. if(empty($this->var['cookie']['saltkey']))
  236. {
  237. $this->var['cookie']['saltkey'] = random(8);
  238. fSetCookie('saltkey', $this->var['cookie']['saltkey'], 86400 * 30, 1, 1);
  239. }
  240. $this->var['authkey'] = md5($this->var['config']['security']['authkey'].$this->var['cookie']['saltkey']);
  241. }
  242. private function buildOutput()
  243. {
  244. if($this->config['security']['url_xss_defend'] && $_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_SERVER['REQUEST_URI']))
  245. {
  246. $this->_xssCheck();
  247. }
  248. $attack_evasive = true;
  249. if(!empty($this->var['cookie']['from_header']))
  250. {
  251. $from_header_time = (int)authcode($this->var['cookie']['from_header'], 'DECODE');
  252. $attack_evasive = (TIME_UTC - $from_header_time < 10) ? false : true;
  253. fSetCookie('from_header','');
  254. }
  255. /*$module_action = strtolower(MODULE_NAME.'/'.ACTION_NAME);
  256. if($this->config['security']['attack_evasive'] && $attack_evasive && !in_array($module_action, $this->config['security']['attack_ignore']))
  257. {
  258. require_once fimport('include/security');
  259. }*/
  260. if(!empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false)
  261. {
  262. $this->config['output']['gzip'] = false;
  263. }
  264. $allow_gzip = $this->config['output']['gzip'] && empty($this->var['ajax']) && EXT_OBGZIP;
  265. $this->config['gzip_compress'] = $allow_gzip;
  266. ob_start($allow_gzip ? 'ob_gzhandler' : NULL);
  267. $this->config['charset'] = $this->config['output']['charset'];
  268. define('CHARSET', $this->config['output']['charset']);
  269. if($this->config['output']['forceheader'])
  270. @header('Content-Type: text/html; charset='.CHARSET);
  271. }
  272. private function buildDb()
  273. {
  274. require fimport('class/db');
  275. require fimport('class/mysql');
  276. $class = 'FDbMySql';
  277. if(count($this->var['config']['db']['slave']))
  278. {
  279. require fimport('class/mysqlslave');
  280. $class = 'FDbMysqlSlave';
  281. }
  282. $this->db = &FDB::object($class);
  283. $this->db->setConfig($this->config['db']);
  284. $this->db->connect();
  285. }
  286. private function buildMemory()
  287. {
  288. require fimport('class/memory');
  289. $this->memory = new Memory();
  290. if($this->is_memory)
  291. {
  292. $this->memory->init($this->config['memory']);
  293. }
  294. $this->var['memory'] = $this->memory->type;
  295. }
  296. private function buildSession()
  297. {
  298. if($this->is_session)
  299. {
  300. require fimport('class/session');
  301. $this->session = new Session();
  302. $this->session->init($this->var['cookie']['sid'], $this->var['client_ip'], $this->var['uid']);
  303. $this->var['sid'] = $this->session->sid;
  304. $this->var['session'] = $this->session->var;
  305. if($this->var['sid'] != $this->var['cookie']['sid'])
  306. {
  307. fSetCookie('sid', $this->var['sid'], 86400);
  308. }
  309. if($this->session->is_new)
  310. {
  311. if(ipBanned($this->var['client_ip']))
  312. $this->session->set('gid', 6);
  313. }
  314. /*if($this->session->get('gid') == 6)
  315. {
  316. $this->var['user']['gid'] = 6;
  317. systemError('user_banned');
  318. }*/
  319. if($this->var['uid'] && ($this->session->isnew || ($this->session->get('last_activity') + 600) < TIME_UTC))
  320. {
  321. $this->session->set('last_activity', TIME_UTC);
  322. if($this->session->is_new)
  323. {
  324. //FDB::update('user_status', array('last_ip' => $this->var['client_ip'], 'last_visit' => TIME_UTC), "uid='".$this->var['uid']."'");
  325. }
  326. }
  327. }
  328. }
  329. public function buildUser($uid)
  330. {
  331. if($this->is_user)
  332. {
  333. if($auth = $this->var['cookie']['auth'])
  334. {
  335. $auth = fAddslashes(explode("\t", authcode($auth, 'DECODE')));
  336. }
  337. list($password, $uid) = empty($auth) || count($auth) < 2 ? array('','') : $auth;
  338. if($uid)
  339. {
  340. $user = FS('user')->getUserById($uid);
  341. }
  342. if(!empty($user) && $user['password'] == $password)
  343. {
  344. $this->var['user'] = $user;
  345. $this->var['authoritys'] = FS('User')->getAuthoritys($uid);
  346. FS('User')->init($user);
  347. }
  348. else
  349. {
  350. $this->buildGuest();
  351. }
  352. }
  353. else
  354. {
  355. $this->buildGuest();
  356. }
  357. if(empty($this->var['cookie']['last_visit']))
  358. {
  359. $this->var['user']['last_visit'] = TIME_UTC - 3600;
  360. fSetCookie('last_visit', TIME_UTC - 3600, 86400 * 30);
  361. }
  362. else
  363. {
  364. $this->var['user']['last_visit'] = $this->var['cookie']['last_visit'];
  365. }
  366. $this->var['uid'] = $this->var['user']['uid'];
  367. $this->var['user_name'] = addslashes($this->var['user']['user_name']);
  368. $this->var['gid'] = $this->var['user']['gid'];
  369. FS('User')->setReferrals();
  370. }
  371. private function buildGuest()
  372. {
  373. $this->var['user'] = array( 'uid' => 0, 'user_name' => '', 'email' => '', 'gid' => 6);
  374. }
  375. private function buildCron()
  376. {
  377. if($this->is_cron)
  378. {
  379. require fimport("class/cron");
  380. Cron::run();
  381. }
  382. }
  383. private function buildMisc()
  384. {
  385. if(!$this->is_misc)
  386. return false;
  387. $this->var['form_hash'] = formHash();
  388. define('FORM_HASH', $this->var['form_hash']);
  389. if($this->init_user)
  390. {
  391. if($this->var['user']['status'] == -1)
  392. {
  393. systemError('user_banned',null);
  394. }
  395. }
  396. if($this->var['setting']['ip_access'] && !ipAccess($this->var['client_ip'], $this->var['setting']['ip_access']))
  397. {
  398. systemError('user_banned', null);
  399. }
  400. if($this->var['setting']['nocacheheaders'])
  401. {
  402. @header("Expires: -1");
  403. @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
  404. @header("Pragma: no-cache");
  405. }
  406. }
  407. private function buildSetting()
  408. {
  409. if($this->is_setting)
  410. $this->cache->loadCache('setting');
  411. if(!is_array($this->var['setting']))
  412. $this->var['setting'] = array();
  413. }
  414. private function buildCache()
  415. {
  416. !empty($this->cache_list) && $this->cache->loadCache($this->cache_list);
  417. }
  418. private function buildRewriteArgs()
  419. {
  420. if(intval($this->var['setting']['url_route']) > 0)
  421. {
  422. switch(MODULE_NAME.'/'.ACTION_NAME)
  423. {
  424. case 'index/index':
  425. case 'index/search':
  426. case 'index/today':
  427. case 'index/custom':
  428. getRewriteArgs(array('cat','city_py','sort','prices','keyword','page'));
  429. break;
  430. case 'goods/index':
  431. case 'goods/search':
  432. getRewriteArgs(array('site','cat','date','city_py','sort','prices','keyword','page'));
  433. break;
  434. }
  435. }
  436. }
  437. private function _xssCheck()
  438. {
  439. $temp = strtoupper(urldecode(urldecode($_SERVER['REQUEST_URI'])));
  440. if(strpos($temp, '<') !== false || strpos($temp, '"') !== false || strpos($temp, 'CONTENT-TRANSFER-ENCODING') !== false)
  441. {
  442. systemError('request_tainting');
  443. }
  444. return true;
  445. }
  446. }
  447. ?>