discuz_application.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: discuz_application.php 36342 2017-01-09 01:15:30Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class discuz_application extends discuz_base{
  12. var $mem = null;
  13. var $session = null;
  14. var $config = array();
  15. var $var = array();
  16. var $cachelist = array();
  17. var $init_db = true;
  18. var $init_setting = true;
  19. var $init_user = true;
  20. var $init_session = true;
  21. var $init_cron = true;
  22. var $init_misc = true;
  23. var $init_mobile = true;
  24. var $initated = false;
  25. var $superglobal = array(
  26. 'GLOBALS' => 1,
  27. '_GET' => 1,
  28. '_POST' => 1,
  29. '_REQUEST' => 1,
  30. '_COOKIE' => 1,
  31. '_SERVER' => 1,
  32. '_ENV' => 1,
  33. '_FILES' => 1,
  34. );
  35. static function &instance() {
  36. static $object;
  37. if(empty($object)) {
  38. $object = new self();
  39. }
  40. return $object;
  41. }
  42. public function __construct() {
  43. $this->_init_env();
  44. $this->_init_config();
  45. $this->_init_input();
  46. $this->_init_output();
  47. }
  48. public function init() {
  49. if(!$this->initated) {
  50. $this->_init_db();
  51. $this->_init_setting();
  52. $this->_init_user();
  53. $this->_init_session();
  54. $this->_init_mobile();
  55. $this->_init_cron();
  56. $this->_init_misc();
  57. }
  58. $this->initated = true;
  59. }
  60. private function _init_env() {
  61. error_reporting(E_ERROR);
  62. if(PHP_VERSION < '5.3.0') {
  63. set_magic_quotes_runtime(0);
  64. }
  65. define('MAGIC_QUOTES_GPC', function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc());
  66. define('ICONV_ENABLE', function_exists('iconv'));
  67. define('MB_ENABLE', function_exists('mb_convert_encoding'));
  68. define('EXT_OBGZIP', function_exists('ob_gzhandler'));
  69. define('TIMESTAMP', time());
  70. $this->timezone_set();
  71. if(!defined('DISCUZ_CORE_FUNCTION') && !@include(DISCUZ_ROOT.'./source/function/function_core.php')) {
  72. exit('function_core.php is missing');
  73. }
  74. if(function_exists('ini_get')) {
  75. $memorylimit = @ini_get('memory_limit');
  76. if($memorylimit && return_bytes($memorylimit) < 33554432 && function_exists('ini_set')) {
  77. ini_set('memory_limit', '128m');
  78. }
  79. }
  80. define('IS_ROBOT', checkrobot());
  81. foreach ($GLOBALS as $key => $value) {
  82. if (!isset($this->superglobal[$key])) {
  83. $GLOBALS[$key] = null; unset($GLOBALS[$key]);
  84. }
  85. }
  86. global $_G;
  87. $_G = array(
  88. 'uid' => 0,
  89. 'username' => '',
  90. 'adminid' => 0,
  91. 'groupid' => 1,
  92. 'sid' => '',
  93. 'formhash' => '',
  94. 'connectguest' => 0,
  95. 'timestamp' => TIMESTAMP,
  96. 'starttime' => microtime(true),
  97. 'clientip' => $this->_get_client_ip(),
  98. 'remoteport' => $_SERVER['REMOTE_PORT'],
  99. 'referer' => '',
  100. 'charset' => '',
  101. 'gzipcompress' => '',
  102. 'authkey' => '',
  103. 'timenow' => array(),
  104. 'widthauto' => 0,
  105. 'disabledwidthauto' => 0,
  106. 'PHP_SELF' => '',
  107. 'siteurl' => '',
  108. 'siteroot' => '',
  109. 'siteport' => '',
  110. 'pluginrunlist' => !defined('PLUGINRUNLIST') ? array() : explode(',', PLUGINRUNLIST),
  111. 'config' => array(),
  112. 'setting' => array(),
  113. 'member' => array(),
  114. 'group' => array(),
  115. 'cookie' => array(),
  116. 'style' => array(),
  117. 'cache' => array(),
  118. 'session' => array(),
  119. 'lang' => array(),
  120. 'my_app' => array(),
  121. 'my_userapp' => array(),
  122. 'fid' => 0,
  123. 'tid' => 0,
  124. 'forum' => array(),
  125. 'thread' => array(),
  126. 'rssauth' => '',
  127. 'home' => array(),
  128. 'space' => array(),
  129. 'block' => array(),
  130. 'article' => array(),
  131. 'action' => array(
  132. 'action' => APPTYPEID,
  133. 'fid' => 0,
  134. 'tid' => 0,
  135. ),
  136. 'mobile' => '',
  137. 'notice_structure' => array(
  138. 'mypost' => array('post','pcomment','activity','reward','goods','at'),
  139. 'interactive' => array('poke','friend','wall','comment','click','sharenotice'),
  140. 'system' => array('system','myapp','credit','group','verify','magic','task','show','group','pusearticle','mod_member','blog','article'),
  141. 'manage' => array('mod_member','report','pmreport'),
  142. 'app' => array(),
  143. ),
  144. 'mobiletpl' => array('1' => 'mobile', '2' => 'touch', '3' => 'wml', 'yes' => 'mobile'),
  145. );
  146. $_G['PHP_SELF'] = dhtmlspecialchars($this->_get_script_url());
  147. $_G['basescript'] = CURSCRIPT;
  148. $_G['basefilename'] = basename($_G['PHP_SELF']);
  149. $sitepath = substr($_G['PHP_SELF'], 0, strrpos($_G['PHP_SELF'], '/'));
  150. if(defined('IN_API')) {
  151. $sitepath = preg_replace("/\/api\/?.*?$/i", '', $sitepath);
  152. } elseif(defined('IN_ARCHIVER')) {
  153. $sitepath = preg_replace("/\/archiver/i", '', $sitepath);
  154. }
  155. $_G['isHTTPS'] = ($_SERVER['HTTPS'] && strtolower($_SERVER['HTTPS']) != 'off') ? true : false;
  156. $_G['scheme'] = 'http'.($_G['isHTTPS'] ? 's' : '');
  157. $_G['siteurl'] = dhtmlspecialchars($_G['scheme'].'://'.$_SERVER['HTTP_HOST'].$sitepath.'/');
  158. $url = parse_url($_G['siteurl']);
  159. $_G['siteroot'] = isset($url['path']) ? $url['path'] : '';
  160. $_G['siteport'] = empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] == '80' || $_SERVER['SERVER_PORT'] == '443' ? '' : ':'.$_SERVER['SERVER_PORT'];
  161. if(defined('SUB_DIR')) {
  162. $_G['siteurl'] = str_replace(SUB_DIR, '/', $_G['siteurl']);
  163. $_G['siteroot'] = str_replace(SUB_DIR, '/', $_G['siteroot']);
  164. }
  165. $this->var = & $_G;
  166. }
  167. private function _get_script_url() {
  168. if(!isset($this->var['PHP_SELF'])){
  169. $scriptName = basename($_SERVER['SCRIPT_FILENAME']);
  170. if(basename($_SERVER['SCRIPT_NAME']) === $scriptName) {
  171. $this->var['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
  172. } else if(basename($_SERVER['PHP_SELF']) === $scriptName) {
  173. $this->var['PHP_SELF'] = $_SERVER['PHP_SELF'];
  174. } else if(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName) {
  175. $this->var['PHP_SELF'] = $_SERVER['ORIG_SCRIPT_NAME'];
  176. } else if(($pos = strpos($_SERVER['PHP_SELF'],'/'.$scriptName)) !== false) {
  177. $this->var['PHP_SELF'] = substr($_SERVER['SCRIPT_NAME'],0,$pos).'/'.$scriptName;
  178. } else if(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT']) === 0) {
  179. $this->var['PHP_SELF'] = str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));
  180. $this->var['PHP_SELF'][0] != '/' && $this->var['PHP_SELF'] = '/'.$this->var['PHP_SELF'];
  181. } else {
  182. system_error('request_tainting');
  183. }
  184. }
  185. return $this->var['PHP_SELF'];
  186. }
  187. private function _init_input() {
  188. if (isset($_GET['GLOBALS']) ||isset($_POST['GLOBALS']) || isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS'])) {
  189. system_error('request_tainting');
  190. }
  191. if(MAGIC_QUOTES_GPC) {
  192. $_GET = dstripslashes($_GET);
  193. $_POST = dstripslashes($_POST);
  194. $_COOKIE = dstripslashes($_COOKIE);
  195. }
  196. $prelength = strlen($this->config['cookie']['cookiepre']);
  197. foreach($_COOKIE as $key => $val) {
  198. if(substr($key, 0, $prelength) == $this->config['cookie']['cookiepre']) {
  199. $this->var['cookie'][substr($key, $prelength)] = $val;
  200. }
  201. }
  202. if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST)) {
  203. $_GET = array_merge($_GET, $_POST);
  204. }
  205. if(isset($_GET['page'])) {
  206. $_GET['page'] = rawurlencode($_GET['page']);
  207. }
  208. if(!(!empty($_GET['handlekey']) && preg_match('/^\w+$/', $_GET['handlekey']))) {
  209. unset($_GET['handlekey']);
  210. }
  211. if(!empty($this->var['config']['input']['compatible'])) {
  212. foreach($_GET as $k => $v) {
  213. $this->var['gp_'.$k] = daddslashes($v);
  214. }
  215. }
  216. $this->var['mod'] = empty($_GET['mod']) ? '' : dhtmlspecialchars($_GET['mod']);
  217. $this->var['inajax'] = empty($_GET['inajax']) ? 0 : (empty($this->var['config']['output']['ajaxvalidate']) ? 1 : ($_SERVER['REQUEST_METHOD'] == 'GET' && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' || $_SERVER['REQUEST_METHOD'] == 'POST' ? 1 : 0));
  218. $this->var['page'] = empty($_GET['page']) ? 1 : max(1, intval($_GET['page']));
  219. $this->var['sid'] = $this->var['cookie']['sid'] = isset($this->var['cookie']['sid']) ? dhtmlspecialchars($this->var['cookie']['sid']) : '';
  220. if(empty($this->var['cookie']['saltkey'])) {
  221. $this->var['cookie']['saltkey'] = random(8);
  222. dsetcookie('saltkey', $this->var['cookie']['saltkey'], 86400 * 30, 1, 1);
  223. }
  224. $this->var['authkey'] = md5($this->var['config']['security']['authkey'].$this->var['cookie']['saltkey']);
  225. }
  226. private function _init_config() {
  227. $_config = array();
  228. @include DISCUZ_ROOT.'./config/config_global.php';
  229. if(empty($_config)) {
  230. if(!file_exists(DISCUZ_ROOT.'./data/install.lock')) {
  231. header('location: install');
  232. exit;
  233. } else {
  234. system_error('config_notfound');
  235. }
  236. }
  237. if(empty($_config['security']['authkey'])) {
  238. $_config['security']['authkey'] = md5($_config['cookie']['cookiepre'].$_config['db'][1]['dbname']);
  239. }
  240. if(empty($_config['debug']) || !file_exists(libfile('function/debug'))) {
  241. define('DISCUZ_DEBUG', false);
  242. error_reporting(0);
  243. } elseif($_config['debug'] === 1 || $_config['debug'] === 2 || !empty($_REQUEST['debug']) && $_REQUEST['debug'] === $_config['debug']) {
  244. define('DISCUZ_DEBUG', true);
  245. error_reporting(E_ERROR);
  246. if($_config['debug'] === 2) {
  247. error_reporting(E_ALL);
  248. }
  249. } else {
  250. define('DISCUZ_DEBUG', false);
  251. error_reporting(0);
  252. }
  253. define('STATICURL', !empty($_config['output']['staticurl']) ? $_config['output']['staticurl'] : 'static/');
  254. $this->var['staticurl'] = STATICURL;
  255. $this->config = & $_config;
  256. $this->var['config'] = & $_config;
  257. if(substr($_config['cookie']['cookiepath'], 0, 1) != '/') {
  258. $this->var['config']['cookie']['cookiepath'] = '/'.$this->var['config']['cookie']['cookiepath'];
  259. }
  260. $this->var['config']['cookie']['cookiepre'] = $this->var['config']['cookie']['cookiepre'].substr(md5($this->var['config']['cookie']['cookiepath'].'|'.$this->var['config']['cookie']['cookiedomain']), 0, 4).'_';
  261. }
  262. private function _init_output() {
  263. if($this->config['security']['attackevasive'] && (!defined('CURSCRIPT') || !in_array($this->var['mod'], array('seccode', 'secqaa', 'swfupload')) && !defined('DISABLEDEFENSE'))) {
  264. require_once libfile('misc/security', 'include');
  265. }
  266. if(!empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) {
  267. $this->config['output']['gzip'] = false;
  268. }
  269. $allowgzip = $this->config['output']['gzip'] && empty($this->var['inajax']) && $this->var['mod'] != 'attachment' && EXT_OBGZIP;
  270. setglobal('gzipcompress', $allowgzip);
  271. if(!ob_start($allowgzip ? 'ob_gzhandler' : null)) {
  272. ob_start();
  273. }
  274. setglobal('charset', $this->config['output']['charset']);
  275. define('CHARSET', $this->config['output']['charset']);
  276. if($this->config['output']['forceheader']) {
  277. @header('Content-Type: text/html; charset='.CHARSET);
  278. }
  279. }
  280. public function reject_robot() {
  281. if(IS_ROBOT) {
  282. exit(header("HTTP/1.1 403 Forbidden"));
  283. }
  284. }
  285. private function _xss_check() {
  286. static $check = array('"', '>', '<', '\'', '(', ')', 'CONTENT-TRANSFER-ENCODING');
  287. if(isset($_GET['formhash']) && $_GET['formhash'] !== formhash()) {
  288. system_error('request_tainting');
  289. }
  290. if($_SERVER['REQUEST_METHOD'] == 'GET' ) {
  291. $temp = $_SERVER['REQUEST_URI'];
  292. } elseif(empty ($_GET['formhash'])) {
  293. $temp = $_SERVER['REQUEST_URI'].file_get_contents('php://input');
  294. } else {
  295. $temp = '';
  296. }
  297. if(!empty($temp)) {
  298. $temp = strtoupper(urldecode(urldecode($temp)));
  299. foreach ($check as $str) {
  300. if(strpos($temp, $str) !== false) {
  301. system_error('request_tainting');
  302. }
  303. }
  304. }
  305. return true;
  306. }
  307. private function _get_client_ip() {
  308. $ip = $_SERVER['REMOTE_ADDR'];
  309. if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
  310. $ip = $_SERVER['HTTP_CLIENT_IP'];
  311. } elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
  312. foreach ($matches[0] AS $xip) {
  313. if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
  314. $ip = $xip;
  315. break;
  316. }
  317. }
  318. }
  319. return $ip;
  320. }
  321. private function _init_db() {
  322. if($this->init_db) {
  323. $driver = function_exists('mysql_connect') ? 'db_driver_mysql' : 'db_driver_mysqli';
  324. if(getglobal('config/db/slave')) {
  325. $driver = function_exists('mysql_connect') ? 'db_driver_mysql_slave' : 'db_driver_mysqli_slave';
  326. }
  327. DB::init($driver, $this->config['db']);
  328. }
  329. }
  330. private function _init_session() {
  331. $sessionclose = !empty($this->var['setting']['sessionclose']);
  332. $this->session = $sessionclose ? new discuz_session_close() : new discuz_session();
  333. if($this->init_session) {
  334. $this->session->init($this->var['cookie']['sid'], $this->var['clientip'], $this->var['uid']);
  335. $this->var['sid'] = $this->session->sid;
  336. $this->var['session'] = $this->session->var;
  337. if(!empty($this->var['sid']) && $this->var['sid'] != $this->var['cookie']['sid']) {
  338. dsetcookie('sid', $this->var['sid'], 86400);
  339. }
  340. if($this->session->isnew) {
  341. if(ipbanned($this->var['clientip'])) {
  342. $this->session->set('groupid', 6);
  343. }
  344. }
  345. if($this->session->get('groupid') == 6) {
  346. $this->var['member']['groupid'] = 6;
  347. if(!defined('IN_MOBILE_API')) {
  348. sysmessage('user_banned');
  349. } else {
  350. mobile_core::result(array('error' => 'user_banned'));
  351. }
  352. }
  353. if($this->var['uid'] && !$sessionclose && ($this->session->isnew || ($this->session->get('lastactivity') + 600) < TIMESTAMP)) {
  354. $this->session->set('lastactivity', TIMESTAMP);
  355. if($this->session->isnew) {
  356. if($this->var['member']['lastip'] && $this->var['member']['lastvisit']) {
  357. dsetcookie('lip', $this->var['member']['lastip'].','.$this->var['member']['lastvisit']);
  358. }
  359. C::t('common_member_status')->update($this->var['uid'], array('lastip' => $this->var['clientip'], 'port' => $this->var['remoteport'], 'lastvisit' => TIMESTAMP));
  360. }
  361. }
  362. }
  363. }
  364. private function _init_user() {
  365. if($this->init_user) {
  366. if($auth = getglobal('auth', 'cookie')) {
  367. $auth = daddslashes(explode("\t", authcode($auth, 'DECODE')));
  368. }
  369. list($discuz_pw, $discuz_uid) = empty($auth) || count($auth) < 2 ? array('', '') : $auth;
  370. if($discuz_uid) {
  371. $user = getuserbyuid($discuz_uid, 1);
  372. }
  373. if(!empty($user) && $user['password'] == $discuz_pw) {
  374. if(isset($user['_inarchive'])) {
  375. C::t('common_member_archive')->move_to_master($discuz_uid);
  376. }
  377. $this->var['member'] = $user;
  378. } else {
  379. $user = array();
  380. $this->_init_guest();
  381. }
  382. if($user && $user['groupexpiry'] > 0 && $user['groupexpiry'] < TIMESTAMP) {
  383. $memberfieldforum = C::t('common_member_field_forum')->fetch($discuz_uid);
  384. $groupterms = dunserialize($memberfieldforum['groupterms']);
  385. if(!empty($groupterms['main'])) {
  386. C::t("common_member")->update($user['uid'], array('groupexpiry'=> 0, 'groupid' => $groupterms['main']['groupid'], 'adminid' => $groupterms['main']['adminid']));
  387. $user['groupid'] = $groupterms['main']['groupid'];
  388. $user['adminid'] = $groupterms['main']['adminid'];
  389. unset($groupterms['main'], $groupterms['ext'][$this->var['member']['groupid']]);
  390. $this->var['member'] = $user;
  391. C::t('common_member_field_forum')->update($discuz_uid, array('groupterms' => serialize($groupterms)));
  392. } elseif((getgpc('mod') != 'spacecp' || CURSCRIPT != 'home') && CURSCRIPT != 'member') {
  393. dheader('location: home.php?mod=spacecp&ac=usergroup&do=expiry');
  394. }
  395. }
  396. if($user && $user['freeze'] && (getgpc('mod') != 'spacecp' && getgpc('mod') != 'misc' || CURSCRIPT != 'home') && CURSCRIPT != 'member' && CURSCRIPT != 'misc') {
  397. dheader('location: home.php?mod=spacecp&ac=profile&op=password');
  398. }
  399. $this->cachelist[] = 'usergroup_'.$this->var['member']['groupid'];
  400. if($user && $user['adminid'] > 0 && $user['groupid'] != $user['adminid']) {
  401. $this->cachelist[] = 'admingroup_'.$this->var['member']['adminid'];
  402. }
  403. } else {
  404. $this->_init_guest();
  405. }
  406. setglobal('groupid', getglobal('groupid', 'member'));
  407. !empty($this->cachelist) && loadcache($this->cachelist);
  408. if($this->var['member'] && $this->var['group']['radminid'] == 0 && $this->var['member']['adminid'] > 0 && $this->var['member']['groupid'] != $this->var['member']['adminid'] && !empty($this->var['cache']['admingroup_'.$this->var['member']['adminid']])) {
  409. $this->var['group'] = array_merge($this->var['group'], $this->var['cache']['admingroup_'.$this->var['member']['adminid']]);
  410. }
  411. if($this->var['group']['allowmakehtml'] && isset($_GET['_makehtml'])) {
  412. $this->var['makehtml'] = 1;
  413. $this->_init_guest();
  414. loadcache(array('usergroup_7'));
  415. $this->var['group'] = $this->var['cache']['usergroup_7'];
  416. unset($this->var['inajax']);
  417. }
  418. if(empty($this->var['cookie']['lastvisit'])) {
  419. $this->var['member']['lastvisit'] = TIMESTAMP - 3600;
  420. dsetcookie('lastvisit', TIMESTAMP - 3600, 86400 * 30);
  421. } else {
  422. $this->var['member']['lastvisit'] = $this->var['cookie']['lastvisit'];
  423. }
  424. setglobal('uid', getglobal('uid', 'member'));
  425. setglobal('username', getglobal('username', 'member'));
  426. setglobal('adminid', getglobal('adminid', 'member'));
  427. setglobal('groupid', getglobal('groupid', 'member'));
  428. if($this->var['member']['newprompt']) {
  429. $this->var['member']['newprompt_num'] = C::t('common_member_newprompt')->fetch($this->var['member']['uid']);
  430. $this->var['member']['newprompt_num'] = unserialize($this->var['member']['newprompt_num']['data']);
  431. $this->var['member']['category_num'] = helper_notification::get_categorynum($this->var['member']['newprompt_num']);
  432. }
  433. }
  434. private function _init_guest() {
  435. $username = '';
  436. $groupid = 7;
  437. if(!empty($this->var['cookie']['con_auth_hash']) && ($openid = authcode($this->var['cookie']['con_auth_hash']))) {
  438. $this->var['connectguest'] = 1;
  439. $username = 'QQ_'.substr($openid, -6);
  440. $this->var['setting']['cacheindexlife'] = 0;
  441. $this->var['setting']['cachethreadlife'] = 0;
  442. $groupid = $this->var['setting']['connect']['guest_groupid'] ? $this->var['setting']['connect']['guest_groupid'] : $this->var['setting']['newusergroupid'];
  443. }
  444. setglobal('member', array( 'uid' => 0, 'username' => $username, 'adminid' => 0, 'groupid' => $groupid, 'credits' => 0, 'timeoffset' => 9999));
  445. }
  446. private function _init_cron() {
  447. $ext = empty($this->config['remote']['on']) || empty($this->config['remote']['cron']) || APPTYPEID == 200;
  448. if($this->init_cron && $this->init_setting && $ext) {
  449. if($this->var['cache']['cronnextrun'] <= TIMESTAMP) {
  450. discuz_cron::run();
  451. }
  452. }
  453. }
  454. private function _init_misc() {
  455. if($this->config['security']['urlxssdefend'] && !defined('DISABLEXSSCHECK')) {
  456. $this->_xss_check();
  457. }
  458. if(!$this->init_misc) {
  459. return false;
  460. }
  461. lang('core');
  462. if($this->init_setting && $this->init_user) {
  463. if(!isset($this->var['member']['timeoffset']) || $this->var['member']['timeoffset'] == 9999 || $this->var['member']['timeoffset'] === '') {
  464. $this->var['member']['timeoffset'] = $this->var['setting']['timeoffset'];
  465. }
  466. }
  467. $timeoffset = $this->init_setting ? $this->var['member']['timeoffset'] : $this->var['setting']['timeoffset'];
  468. $this->var['timenow'] = array(
  469. 'time' => dgmdate(TIMESTAMP),
  470. 'offset' => $timeoffset >= 0 ? ($timeoffset == 0 ? '' : '+'.$timeoffset) : $timeoffset
  471. );
  472. $this->timezone_set($timeoffset);
  473. $this->var['formhash'] = formhash();
  474. define('FORMHASH', $this->var['formhash']);
  475. if($this->init_user) {
  476. $allowvisitflag = in_array(CURSCRIPT, array('member')) || defined('ALLOWGUEST') && ALLOWGUEST;
  477. if($this->var['group'] && isset($this->var['group']['allowvisit']) && !$this->var['group']['allowvisit']) {
  478. if($this->var['uid'] && !$allowvisitflag) {
  479. if(!defined('IN_MOBILE_API')) {
  480. showmessage('user_banned');
  481. } else {
  482. mobile_core::result(array('error' => 'user_banned'));
  483. }
  484. } elseif((!defined('ALLOWGUEST') || !ALLOWGUEST) && !in_array(CURSCRIPT, array('member', 'api')) && !$this->var['inajax']) {
  485. if(!defined('IN_MOBILE_API')) {
  486. dheader('location: member.php?mod=logging&action=login&referer='.rawurlencode($this->var['siteurl'].$this->var['basefilename'].($_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : '')));
  487. } else {
  488. mobile_core::result(array('error' => 'to_login'));
  489. }
  490. }
  491. }
  492. if(isset($this->var['member']['status']) && $this->var['member']['status'] == -1 && !$allowvisitflag) {
  493. if(!defined('IN_MOBILE_API')) {
  494. showmessage('user_banned');
  495. } else {
  496. mobile_core::result(array('error' => 'user_banned'));
  497. }
  498. }
  499. }
  500. if($this->var['setting']['ipaccess'] && !ipaccess($this->var['clientip'], $this->var['setting']['ipaccess'])) {
  501. if(!defined('IN_MOBILE_API')) {
  502. showmessage('user_banned');
  503. } else {
  504. mobile_core::result(array('error' => 'user_banned'));
  505. }
  506. }
  507. if($this->var['setting']['bbclosed']) {
  508. if($this->var['uid'] && ($this->var['group']['allowvisit'] == 2 || $this->var['groupid'] == 1)) {
  509. } elseif(in_array(CURSCRIPT, array('admin', 'member', 'api')) || defined('ALLOWGUEST') && ALLOWGUEST) {
  510. } else {
  511. $closedreason = C::t('common_setting')->fetch('closedreason');
  512. $closedreason = str_replace(':', '&#58;', $closedreason);
  513. if(!defined('IN_MOBILE_API')) {
  514. showmessage($closedreason ? $closedreason : 'board_closed', NULL, array('adminemail' => $this->var['setting']['adminemail']), array('login' => 1));
  515. } else {
  516. mobile_core::result(array('error' => $closedreason ? $closedreason : 'board_closed'));
  517. }
  518. }
  519. }
  520. if(CURSCRIPT != 'admin' && !(in_array($this->var['mod'], array('logging', 'seccode')))) {
  521. periodscheck('visitbanperiods');
  522. }
  523. if(defined('IN_MOBILE')) {
  524. $this->var['tpp'] = $this->var['setting']['mobile']['mobiletopicperpage'] ? intval($this->var['setting']['mobile']['mobiletopicperpage']) : 20;
  525. $this->var['ppp'] = $this->var['setting']['mobile']['mobilepostperpage'] ? intval($this->var['setting']['mobile']['mobilepostperpage']) : 5;
  526. } else {
  527. $this->var['tpp'] = $this->var['setting']['topicperpage'] ? intval($this->var['setting']['topicperpage']) : 20;
  528. $this->var['ppp'] = $this->var['setting']['postperpage'] ? intval($this->var['setting']['postperpage']) : 10;
  529. }
  530. if($this->var['setting']['nocacheheaders']) {
  531. @header("Expires: -1");
  532. @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
  533. @header("Pragma: no-cache");
  534. }
  535. if($this->session->isnew && $this->var['uid']) {
  536. updatecreditbyaction('daylogin', $this->var['uid']);
  537. include_once libfile('function/stat');
  538. updatestat('login', 1);
  539. if(defined('IN_MOBILE')) {
  540. updatestat('mobilelogin', 1);
  541. }
  542. if($this->var['setting']['connect']['allow'] && $this->var['member']['conisbind']) {
  543. updatestat('connectlogin', 1);
  544. }
  545. }
  546. if(isset($this->var['member']['conisbind']) && $this->var['member']['conisbind'] && $this->var['setting'] && $this->var['setting']['connect']['newbiespan'] !== '') {
  547. $this->var['setting']['newbiespan'] = $this->var['setting']['connect']['newbiespan'];
  548. }
  549. $lastact = TIMESTAMP."\t".dhtmlspecialchars(basename($this->var['PHP_SELF']))."\t".dhtmlspecialchars($this->var['mod']);
  550. dsetcookie('lastact', $lastact, 86400);
  551. setglobal('currenturl_encode', base64_encode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));
  552. if((!empty($_GET['fromuid']) || !empty($_GET['fromuser'])) && ($this->var['setting']['creditspolicy']['promotion_visit'] || $this->var['setting']['creditspolicy']['promotion_register'])) {
  553. require_once libfile('misc/promotion', 'include');
  554. }
  555. $this->var['seokeywords'] = !empty($this->var['setting']['seokeywords'][CURSCRIPT]) ? $this->var['setting']['seokeywords'][CURSCRIPT] : '';
  556. $this->var['seodescription'] = !empty($this->var['setting']['seodescription'][CURSCRIPT]) ? $this->var['setting']['seodescription'][CURSCRIPT] : '';
  557. }
  558. private function _init_setting() {
  559. if($this->init_setting) {
  560. if(empty($this->var['setting'])) {
  561. $this->cachelist[] = 'setting';
  562. }
  563. if(empty($this->var['style'])) {
  564. $this->cachelist[] = 'style_default';
  565. }
  566. if(!isset($this->var['cache']['cronnextrun'])) {
  567. $this->cachelist[] = 'cronnextrun';
  568. }
  569. }
  570. !empty($this->cachelist) && loadcache($this->cachelist);
  571. if(!is_array($this->var['setting'])) {
  572. $this->var['setting'] = array();
  573. }
  574. }
  575. public function _init_style() {
  576. if(defined('IN_MOBILE')) {
  577. $mobile = max(1, intval(IN_MOBILE));
  578. if($mobile && $this->var['setting']['styleid'.$mobile]) {
  579. $styleid = $this->var['setting']['styleid'.$mobile];
  580. }
  581. } else {
  582. $styleid = !empty($this->var['cookie']['styleid']) ? $this->var['cookie']['styleid'] : 0;
  583. if(intval(!empty($this->var['forum']['styleid']))) {
  584. $this->var['cache']['style_default']['styleid'] = $styleid = $this->var['forum']['styleid'];
  585. } elseif(intval(!empty($this->var['category']['styleid']))) {
  586. $this->var['cache']['style_default']['styleid'] = $styleid = $this->var['category']['styleid'];
  587. }
  588. }
  589. $styleid = intval($styleid);
  590. if($styleid && $styleid != $this->var['setting']['styleid']) {
  591. loadcache('style_'.$styleid);
  592. if($this->var['cache']['style_'.$styleid]) {
  593. $this->var['style'] = $this->var['cache']['style_'.$styleid];
  594. }
  595. }
  596. define('IMGDIR', $this->var['style']['imgdir']);
  597. define('STYLEID', $this->var['style']['styleid']);
  598. define('VERHASH', $this->var['style']['verhash']);
  599. define('TPLDIR', $this->var['style']['tpldir']);
  600. define('TEMPLATEID', $this->var['style']['templateid']);
  601. }
  602. private function _init_mobile() {
  603. if(!$this->init_mobile) {
  604. return false;
  605. }
  606. if(!$this->var['setting'] || !$this->var['setting']['mobile']['allowmobile'] || !is_array($this->var['setting']['mobile']) || IS_ROBOT) {
  607. $nomobile = true;
  608. $unallowmobile = true;
  609. }
  610. if(getgpc('forcemobile')) {
  611. dsetcookie('dismobilemessage', '1', 3600);
  612. }
  613. $mobile = getgpc('mobile');
  614. $mobileflag = isset($this->var['mobiletpl'][$mobile]);
  615. if($mobile === 'no') {
  616. dsetcookie('mobile', 'no', 3600);
  617. $nomobile = true;
  618. } elseif($this->var['cookie']['mobile'] == 'no' && $mobileflag) {
  619. checkmobile();
  620. dsetcookie('mobile', '');
  621. } elseif($this->var['cookie']['mobile'] == 'no') {
  622. $nomobile = true;
  623. } elseif(!($mobile_ = checkmobile())) {
  624. $nomobile = true;
  625. }
  626. if(!$mobile || $mobile == 'yes') {
  627. $mobile = isset($mobile_) ? $mobile_ : 2;
  628. }
  629. if(!$this->var['mobile'] && !$unallowmobile) {
  630. if($mobileflag) {
  631. dheader("Location:misc.php?mod=mobile");
  632. }
  633. }
  634. if($nomobile || (!$this->var['setting']['mobile']['mobileforward'] && !$mobileflag)) {
  635. if($_SERVER['HTTP_HOST'] == $this->var['setting']['domain']['app']['mobile'] && $this->var['setting']['domain']['app']['default']) {
  636. dheader("Location:http://".$this->var['setting']['domain']['app']['default'].$_SERVER['REQUEST_URI']);
  637. return false;
  638. } else {
  639. return false;
  640. }
  641. }
  642. if(strpos($this->var['setting']['domain']['defaultindex'], CURSCRIPT) !== false && CURSCRIPT != 'forum' && !$_GET['mod']) {
  643. if($this->var['setting']['domain']['app']['mobile']) {
  644. $mobileurl = 'http://'.$this->var['setting']['domain']['app']['mobile'];
  645. } else {
  646. if($this->var['setting']['domain']['app']['forum']) {
  647. $mobileurl = 'http://'.$this->var['setting']['domain']['app']['forum'].'?mobile=yes';
  648. } else {
  649. $mobileurl = $this->var['siteurl'].'forum.php?mobile=yes';
  650. }
  651. }
  652. dheader("location:$mobileurl");
  653. }
  654. if($mobile === '3' && empty($this->var['setting']['mobile']['wml'])) {
  655. return false;
  656. }
  657. define('IN_MOBILE', isset($this->var['mobiletpl'][$mobile]) ? $mobile : '2');
  658. setglobal('gzipcompress', 0);
  659. $arr = array();
  660. foreach(array_keys($this->var['mobiletpl']) as $mobiletype) {
  661. $arr[] = '&mobile='.$mobiletype;
  662. $arr[] = 'mobile='.$mobiletype;
  663. }
  664. parse_str($_SERVER['QUERY_STRING'], $query);
  665. $query['mobile'] = 'no';
  666. unset($query['simpletype']);
  667. $query_sting_tmp = http_build_query($query);
  668. $this->var['setting']['mobile']['nomobileurl'] = ($this->var['setting']['domain']['app']['forum'] ? 'http://'.$this->var['setting']['domain']['app']['forum'].'/' : $this->var['siteurl']).$this->var['basefilename'].'?'.$query_sting_tmp;
  669. $this->var['setting']['lazyload'] = 0;
  670. if('utf-8' != CHARSET) {
  671. if(strtolower($_SERVER['REQUEST_METHOD']) === 'post') {
  672. foreach($_POST AS $pk => $pv) {
  673. if(!is_numeric($pv)) {
  674. $_GET[$pk] = $_POST[$pk] = $this->mobile_iconv_recurrence($pv);
  675. if(!empty($this->var['config']['input']['compatible'])) {
  676. $this->var['gp_'.$pk] = daddslashes($_GET[$pk]);
  677. }
  678. }
  679. }
  680. }
  681. }
  682. if(!$this->var['setting']['mobile']['mobilesimpletype']) {
  683. $this->var['setting']['imagemaxwidth'] = 224;
  684. }
  685. $this->var['setting']['regstatus'] = $this->var['setting']['mobile']['mobileregister'] ? $this->var['setting']['regstatus'] : 0 ;
  686. $this->var['setting']['thumbquality'] = 50;
  687. $this->var['setting']['avatarmethod'] = 0;
  688. $this->var['setting']['mobile']['simpletypeurl'] = array();
  689. $this->var['setting']['mobile']['simpletypeurl'][0] = $this->var['siteurl'].$this->var['basefilename'].($query_sting_tmp ? '?'.$query_sting_tmp.'&' : '?').'mobile=1&simpletype=no';
  690. $this->var['setting']['mobile']['simpletypeurl'][1] = $this->var['siteurl'].$this->var['basefilename'].($query_sting_tmp ? '?'.$query_sting_tmp.'&' : '?').'mobile=1&simpletype=yes';
  691. $this->var['setting']['mobile']['simpletypeurl'][2] = $this->var['siteurl'].$this->var['basefilename'].($query_sting_tmp ? '?'.$query_sting_tmp.'&' : '?').'mobile=2';
  692. unset($query_sting_tmp);
  693. ob_start();
  694. }
  695. public function timezone_set($timeoffset = 0) {
  696. if(function_exists('date_default_timezone_set')) {
  697. @date_default_timezone_set('Etc/GMT'.($timeoffset > 0 ? '-' : '+').(abs($timeoffset)));
  698. }
  699. }
  700. public function mobile_iconv_recurrence($value) {
  701. if(is_array($value)) {
  702. foreach($value AS $key => $val) {
  703. $value[$key] = $this->mobile_iconv_recurrence($val);
  704. }
  705. } else {
  706. $value = diconv($value, 'utf-8', CHARSET);
  707. }
  708. return $value;
  709. }
  710. }
  711. ?>