【discuzX2】/source/function/function_forum.php论坛模块通用函数集合分析

  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: function_forum.php 24723 2011-10-09 12:50:14Z yangli $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. /**
  12. *
  13. * @param $uid - 用户ID
  14. * @param $size - 头像大小 small/middle/big
  15. * @param $returnsrc - 是否返回头像地址
  16. */
  17. function discuz_uc_avatar($uid, $size = '', $returnsrc = FALSE) {
  18. global $_G;
  19. return avatar($uid, $size, $returnsrc, FALSE, $_G['setting']['avatarmethod'], $_G['setting']['ucenterurl']);
  20. }
  21. /**
  22. * 论坛附件删除
  23. * @param $attach - 单一附件数据
  24. */
  25. function dunlink($attach) {
  26. global $_G;
  27. $filename = $attach['attachment'];
  28. $havethumb = $attach['thumb'];
  29. $remote = $attach['remote'];
  30. if($remote) {
  31. ftpcmd('delete', $_G['setting']['ftp']['attachdir'].'/forum/'.$filename);
  32. $havethumb && ftpcmd('delete', $_G['setting']['ftp']['attachdir'].'/forum/'.getimgthumbname($filename));
  33. } else {
  34. @unlink($_G['setting']['attachdir'].'/forum/'.$filename);
  35. $havethumb && @unlink($_G['setting']['attachdir'].'/forum/'.getimgthumbname($filename));
  36. }
  37. if($attach['aid']) {
  38. @unlink($_G['setting']['attachdir'].'image/'.$attach['aid'].'_140_140.jpg');
  39. }
  40. }
  41. /**
  42. 权限表达式
  43. * @param $formula - 权限表达式
  44. */
  45. function formulaperm($formula) {
  46. global $_G;
  47. $formula = unserialize($formula);
  48. $medalperm = $formula['medal'];
  49. $permusers = $formula['users'];
  50. $permmessage = $formula['message'];
  51. if($_G['setting']['medalstatus'] && $medalperm) {
  52. $exists = 1;
  53. $_G['forum_formulamessage'] = '';
  54. $medalpermc = $medalperm;
  55. if($_G['uid']) {
  56. $medals = explode("\t", DB::result_first("SELECT medals FROM ".DB::table('common_member_field_forum')." WHERE u"));
  57. foreach($medalperm as $k => $medal) {
  58. foreach($medals as $r) {
  59. list($medalid) = explode("|", $r);
  60. if($medalid == $medal) {
  61. $exists = 0;
  62. unset($medalpermc[$k]);
  63. }
  64. }
  65. }
  66. } else {
  67. $exists = 0;
  68. }
  69. if($medalpermc) {
  70. loadcache('medals');
  71. foreach($medalpermc as $medal) {
  72. if($_G['cache']['medals'][$medal]) {
  73. $_G['forum_formulamessage'] .= '<img src="'.STATICURL.'image/common/'.$_G['cache']['medals'][$medal]['image'].'" /> '.$_G['cache']['medals'][$medal]['name'].' ';
  74. }
  75. }
  76. showmessage('forum_permforum_nomedal', NULL, array('forum_permforum_nomedal' => $_G['forum_formulamessage']), array('login' => 1));
  77. }
  78. }
  79. $formulatext = $formula[0];
  80. $formula = $formula[1];
  81. if($_G['adminid'] == 1 || $_G['forum']['ismoderator'] || in_array($_G['groupid'], explode("\t", $_G['forum']['spviewperm']))) {
  82. return FALSE;
  83. }
  84. if($permusers) {
  85. $permusers = str_replace(array("\r\n", "\r"), array("\n", "\n"), $permusers);
  86. $permusers = explode("\n", trim($permusers));
  87. if(!in_array($_G['member']['username'], $permusers)) {
  88. showmessage('forum_permforum_disallow', NULL, array(), array('login' => 1));
  89. }
  90. }
  91. if(!$formula) {
  92. return FALSE;
  93. }
  94. if(strexists($formula, '$memberformula[')) {
  95. preg_match_all("/\\\$memberformula

    /", $formula, $a);
  96. $fields = $profilefields = array();
  97. $mfadd = array();
  98. foreach($a[1] as $field) {
  99. switch($field) {
  100. case 'regdate':
  101. $formula = preg_replace("/\{(\d{4})\-(\d{1,2})\-(\d{1,2})\}/e", "'\'\\1-'.sprintf('%02d', '\\2').'-'.sprintf('%02d', '\\3').'\''", $formula);
  102. case 'regday':
  103. $fields[] = 'm.regdate';break;
  104. case 'regip':
  105. case 'lastip':
  106. $formula = preg_replace("/\{([\d\.]+?)\}/", "'\\1'", $formula);
  107. $formula = preg_replace('/(\$memberformula

    \'

    )\s*=+\s*\'([\d\.]+?)\'/', "strpos(\\1, '\\3')===0", $formula);
  108. case 'buyercredit':
  109. case 'sellercredit':
  110. $mfadd['ms'] = " LEFT JOIN ".DB::table('common_member_status')." ms ON m.u;
  111. $fields[] = 'ms.'.$field;break;
  112. case substr($field, 0, 5) == 'field':
  113. $mfadd['mp'] = " LEFT JOIN ".DB::table('common_member_profile')." mp ON m.u;
  114. $fields[] = 'mp.field'.intval(substr($field, 5));
  115. $profilefields[] = $field;break;
  116. }
  117. }
  118. $memberformula = array();
  119. if($_G['uid']) {
  120. $memberformula = DB::fetch_first("SELECT ".implode(',', $fields)." FROM ".DB::table('common_member')." m ".implode('', $mfadd)." WHERE m.u");
  121. if(in_array('regday', $a[1])) {
  122. $memberformula['regday'] = intval((TIMESTAMP - $memberformula['regdate']) / 86400);
  123. }
  124. if(in_array('regdate', $a[1])) {
  125. $memberformula['regdate'] = date('Y-m-d', $memberformula['regdate']);
  126. }
  127. $memberformula['lastip'] = $memberformula['lastip'] ? $memberformula['lastip'] : $_G['clientip'];
  128. } else {
  129. if(isset($memberformula['regip'])) {
  130. $memberformula['regip'] = $_G['clientip'];
  131. }
  132. if(isset($memberformula['lastip'])) {
  133. $memberformula['lastip'] = $_G['clientip'];
  134. }
  135. }
  136. }
  137. @eval("\$formulaperm = ($formula) ? TRUE : FALSE;");
  138. if(!$formulaperm) {
  139. if(!$permmessage) {
  140. $language = lang('forum/misc');
  141. $search = array('regdate', 'regday', 'regip', 'lastip', 'buyercredit', 'sellercredit', 'digestposts', 'posts', 'threads', 'oltime');
  142. $replace = array($language['formulaperm_regdate'], $language['formulaperm_regday'], $language['formulaperm_regip'], $language['formulaperm_lastip'], $language['formulaperm_buyercredit'], $language['formulaperm_sellercredit'], $language['formulaperm_digestposts'], $language['formulaperm_posts'], $language['formulaperm_threads'], $language['formulaperm_oltime']);
  143. for($i = 1; $i <= 8; $i++) {
  144. $search[] = 'extcredits'.$i;
  145. $replace[] = $_G['setting']['extcredits'][$i]['title'] ? $_G['setting']['extcredits'][$i]['title'] : $language['formulaperm_extcredits'].$i;
  146. }
  147. if($profilefields) {
  148. loadcache(array('fields_required', 'fields_optional'));
  149. foreach($profilefields as $profilefield) {
  150. $search[] = $profilefield;
  151. $replace[] = !empty($_G['cache']['fields_optional']['field_'.$profilefield]) ? $_G['cache']['fields_optional']['field_'.$profilefield]['title'] : $_G['cache']['fields_required']['field_'.$profilefield]['title'];
  152. }
  153. }
  154. $i = 0;$_G['forum_usermsg'] = '';
  155. foreach($search as $s) {
  156. if(in_array($s, array('digestposts', 'posts', 'threads', 'oltime', 'extcredits1', 'extcredits2', 'extcredits3', 'extcredits4', 'extcredits5', 'extcredits6', 'extcredits7', 'extcredits8'))) {
  157. $_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br /> '.$replace[$i].': '.(@eval('return intval(getuserprofile(\''.$s.'\'));')) : '';
  158. } elseif(in_array($s, array('regdate', 'regip'))) {
  159. $_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br /> '.$replace[$i].': '.(@eval('return $memberformula[\''.$s.'\'];')) : '';
  160. }
  161. $i++;
  162. }
  163. $search = array_merge($search, array('and', 'or', '>=', '<=', '=='));
  164. $replace = array_merge($replace, array(' <b>'.$language['formulaperm_and'].'</b> ', ' <b>'.$language['formulaperm_or'].'</b> ', '≥', '≤', '='));
  165. $_G['forum_formulamessage'] = str_replace($search, $replace, $formulatext);
  166. } else {
  167. $_G['forum_formulamessage'] = $permmessage;
  168. }
  169. if(!$permmessage) {
  170. showmessage('forum_permforum_nopermission', NULL, array('formulamessage' => $_G['forum_formulamessage'], 'usermsg' => $_G['forum_usermsg']), array('login' => 1));
  171. } else {
  172. showmessage('forum_permforum_nopermission_custommsg', NULL, array('formulamessage' => $_G['forum_formulamessage']), array('login' => 1));
  173. }
  174. }
  175. return TRUE;
  176. }
  177. /**
  178. 勋章权限表达式
  179. * @param $formula - 勋章权限表达式
  180. * @param $type - 1 权限验证 2 勋章字串
  181. */
  182. function medalformulaperm($formula, $type) {
  183. global $_G;
  184. $formula = unserialize($formula);
  185. $permmessage = $formula['message'];
  186. $formula = $formula['medal'];
  187. if(!empty($formula['usergroupallow']) && is_array($formula['usergroups']) && !in_array($_G['groupid'], $formula['usergroups'])) {
  188. loadcache('usergroups');
  189. $message = array();
  190. foreach($formula['usergroups'] as $groupid) {
  191. $message[] = $_G['cache']['usergroups'][$groupid]['grouptitle'].' ';
  192. }
  193. $_G['forum_formulamessage'] = implode(', ', $message);
  194. $_G['forum_usermsg'] = $_G['cache']['usergroups'][$_G['groupid']]['grouptitle'];
  195. return FALSE;
  196. }
  197. $formulatext = $formula[0];
  198. $formula = $formula[1];
  199. if(!$formula) {
  200. return FALSE;
  201. }
  202. if(strexists($formula, '$memberformula[')) {
  203. preg_match_all("/\\\$memberformula

    /", $formula, $a);
  204. $fields = $profilefields = array();
  205. $mfadd = array();
  206. foreach($a[1] as $field) {
  207. switch($field) {
  208. case 'regdate':
  209. $formula = preg_replace("/\{(\d{4})\-(\d{1,2})\-(\d{1,2})\}/e", "'\'\\1-'.sprintf('%02d', '\\2').'-'.sprintf('%02d', '\\3').'\''", $formula);
  210. case 'regday':
  211. $fields[] = 'm.regdate';break;
  212. case 'regip':
  213. case 'lastip':
  214. $formula = preg_replace("/\{([\d\.]+?)\}/", "'\\1'", $formula);
  215. $formula = preg_replace('/(\$memberformula

    \'

    )\s*=+\s*\'([\d\.]+?)\'/', "strpos(\\1, '\\3')===0", $formula);
  216. case 'buyercredit':
  217. case 'sellercredit':
  218. $mfadd['ms'] = " LEFT JOIN ".DB::table('common_member_status')." ms ON m.u;
  219. $fields[] = 'ms.'.$field;break;
  220. case substr($field, 0, 5) == 'field':
  221. $mfadd['mp'] = " LEFT JOIN ".DB::table('common_member_profile')." mp ON m.u;
  222. $fields[] = 'mp.field'.intval(substr($field, 5));
  223. $profilefields[] = $field;break;
  224. }
  225. }
  226. $memberformula = array();
  227. if($_G['uid']) {
  228. $memberformula = DB::fetch_first("SELECT ".implode(',', $fields)." FROM ".DB::table('common_member')." m ".implode('', $mfadd)." WHERE m.u");
  229. if(in_array('regday', $a[1])) {
  230. $memberformula['regday'] = intval((TIMESTAMP - $memberformula['regdate']) / 86400);
  231. }
  232. if(in_array('regdate', $a[1])) {
  233. $memberformula['regdate'] = date('Y-m-d', $memberformula['regdate']);
  234. }
  235. $memberformula['lastip'] = $memberformula['lastip'] ? $memberformula['lastip'] : $_G['clientip'];
  236. } else {
  237. if(isset($memberformula['regip'])) {
  238. $memberformula['regip'] = $_G['clientip'];
  239. }
  240. if(isset($memberformula['lastip'])) {
  241. $memberformula['lastip'] = $_G['clientip'];
  242. }
  243. }
  244. }
  245. @eval("\$formulaperm = ($formula) ? TRUE : FALSE;");
  246. if(!$formulaperm || $type == 2) {
  247. if(!$permmessage) {
  248. $language = lang('forum/misc');
  249. $search = array('regdate', 'regday', 'regip', 'lastip', 'buyercredit', 'sellercredit', 'digestposts', 'posts', 'threads', 'oltime');
  250. $replace = array($language['formulaperm_regdate'], $language['formulaperm_regday'], $language['formulaperm_regip'], $language['formulaperm_lastip'], $language['formulaperm_buyercredit'], $language['formulaperm_sellercredit'], $language['formulaperm_digestposts'], $language['formulaperm_posts'], $language['formulaperm_threads'], $language['formulaperm_oltime']);
  251. for($i = 1; $i <= 8; $i++) {
  252. $search[] = 'extcredits'.$i;
  253. $replace[] = $_G['setting']['extcredits'][$i]['title'] ? $_G['setting']['extcredits'][$i]['title'] : $language['formulaperm_extcredits'].$i;
  254. }
  255. if($profilefields) {
  256. loadcache(array('fields_required', 'fields_optional'));
  257. foreach($profilefields as $profilefield) {
  258. $search[] = $profilefield;
  259. $replace[] = !empty($_G['cache']['fields_optional']['field_'.$profilefield]) ? $_G['cache']['fields_optional']['field_'.$profilefield]['title'] : $_G['cache']['fields_required']['field_'.$profilefield]['title'];
  260. }
  261. }
  262. $i = 0;$_G['forum_usermsg'] = '';
  263. foreach($search as $s) {
  264. if(in_array($s, array('digestposts', 'posts', 'threads', 'oltime', 'extcredits1', 'extcredits2', 'extcredits3', 'extcredits4', 'extcredits5', 'extcredits6', 'extcredits7', 'extcredits8'))) {
  265. $_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br /> '.$replace[$i].': '.(@eval('return intval(getuserprofile(\''.$s.'\'));')) : '';
  266. } elseif(in_array($s, array('regdate', 'regip'))) {
  267. $_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br /> '.$replace[$i].': '.(@eval('return $memberformula[\''.$s.'\'];')) : '';
  268. }
  269. $i++;
  270. }
  271. $search = array_merge($search, array('and', 'or', '>=', '<=', '=='));
  272. $replace = array_merge($replace, array(' <b>'.$language['formulaperm_and'].'</b> ', ' <b>'.$language['formulaperm_or'].'</b> ', '≥', '≤', '='));
  273. $_G['forum_formulamessage'] = str_replace($search, $replace, $formulatext);
  274. } else {
  275. $_G['forum_formulamessage'] = $permmessage;
  276. }
  277. return $_G['forum_formulamessage'];
  278. } elseif($formulaperm && $type == 1) {
  279. return FALSE;
  280. }
  281. return TRUE;
  282. }
  283. /**
  284. * vip用户购买组权限是否到期
  285. * @param $terms 期限 来源于 memberfields 表的 groupterms 字段
  286. * @return 返回过期信息
  287. */
  288. function groupexpiry($terms) {
  289. $terms = is_array($terms) ? $terms : unserialize($terms);
  290. $groupexpiry = isset($terms['main']['time']) ? intval($terms['main']['time']) : 0;
  291. if(is_array($terms['ext'])) {
  292. foreach($terms['ext'] as $expiry) {
  293. if((!$groupexpiry && $expiry) || $expiry < $groupexpiry) {
  294. $groupexpiry = $expiry;
  295. }
  296. }
  297. }
  298. return $groupexpiry;
  299. }
  300. /**
  301. * 返回当前链接的域名
  302. * @return 返回域名
  303. */
  304. function site() {
  305. return $_SERVER['HTTP_HOST'];
  306. }
  307. /**
  308. * 显示主题分类
  309. * @param $curtypeid - 当前被选择的类型id
  310. * @return 返回的HTML数据
  311. */
  312. function typeselect($curtypeid = 0) {
  313. global $_G;
  314. if($threadtypes = $_G['forum']['threadtypes']) {
  315. $html = '<select name="typeid" > </option>';
  316. foreach($threadtypes['types'] as $typeid => $name) {
  317. $html .= '<option value="'.$typeid.'" '.($curtypeid == $typeid ? 'selected' : '').'>'.strip_tags($name).'</option>';
  318. }
  319. $html .= '</select>';
  320. return $html;
  321. } else {
  322. return '';
  323. }
  324. }
  325. /**
  326. * 更新管理者状态
  327. * @param $modacton - 动作
  328. * @param $smcols - 执行次数
  329. */
  330. function updatemodworks($modaction, $posts = 1) {
  331. global $_G;
  332. $today = dgmdate(TIMESTAMP, 'Y-m-d');
  333. if($_G['setting']['modworkstatus'] && $modaction && $posts) {
  334. DB::query("UPDATE ".DB::table('forum_modwork')." SET count=count+1, posts=posts+'$posts' WHERE u AND modaction='$modaction' AND dateline='$today'");
  335. if(!DB::affected_rows()) {
  336. DB::query("INSERT INTO ".DB::table('forum_modwork')." (uid, modaction, dateline, count, posts) VALUES ('$_G[uid]', '$modaction', '$today', 1, '$posts')");
  337. }
  338. }
  339. }
  340. /**
  341. * 格式化一个sql语句,通常用于update操作
  342. *
  343. * @param string $fieldname 字段名称
  344. * @param int $position 位置
  345. * @param int $value 数值 0|1
  346. * @return string
  347. */
  348. function buildbitsql($fieldname, $position, $value) {
  349. $t = " `$fieldname`=`$fieldname`";
  350. if($value) {
  351. $t .= ' | '.setstatus($position, 1);
  352. } else {
  353. $t .= ' & '.setstatus($position, 0);
  354. }
  355. return $t.' ';
  356. }
  357. function showmessagenoperm($type, $fid, $formula = '') {
  358. global $_G;
  359. loadcache('usergroups');
  360. if($formula) {
  361. $formula = unserialize($formula);
  362. $permmessage = stripslashes($formula['message']);
  363. }
  364. $usergroups = $nopermgroup = $forumnoperms = array();
  365. $nopermdefault = array(
  366. 'viewperm' => array(),
  367. 'getattachperm' => array(),
  368. 'postperm' => array(7),
  369. 'replyperm' => array(7),
  370. 'postattachperm' => array(7),
  371. );
  372. $perms = array('viewperm', 'postperm', 'replyperm', 'getattachperm', 'postattachperm');
  373. foreach($_G['cache']['usergroups'] as $gid => $usergroup) {
  374. $usergroups[$gid] = $usergroup['type'];
  375. $grouptype = $usergroup['type'] == 'member' ? 0 : 1;
  376. $nopermgroup[$grouptype][] = $gid;
  377. }
  378. if($fid == $_G['forum']['fid']) {
  379. $forum = $_G['forum'];
  380. } else {
  381. $forum = DB::fetch_first("SELECT * FROM ".DB::table('forum_forumfield')." WHERE f");
  382. }
  383. foreach($perms as $perm) {
  384. $permgroups = explode("\t", $forum[$perm]);
  385. $membertype = $forum[$perm] ? array_intersect($nopermgroup[0], $permgroups) : TRUE;
  386. $forumnoperm = $forum[$perm] ? array_diff(array_keys($usergroups), $permgroups) : $nopermdefault[$perm];
  387. foreach($forumnoperm as $groupid) {
  388. $nopermtype = $membertype && $groupid == 7 ? 'login' : ($usergroups[$groupid] == 'system' || $usergroups[$groupid] == 'special' ? 'none' : ($membertype ? 'upgrade' : 'none'));
  389. $forumnoperms[$fid][$perm][$groupid] = array($nopermtype, $permgroups);
  390. }
  391. }
  392. $v = $forumnoperms[$fid][$type][$_G['groupid']][0];
  393. $gids = $forumnoperms[$fid][$type][$_G['groupid']][1];
  394. $comma = $permgroups = '';
  395. if(is_array($gids)) {
  396. foreach($gids as $gid) {
  397. if($gid && $_G['cache']['usergroups'][$gid]) {
  398. $permgroups .= $comma.$_G['cache']['usergroups'][$gid]['grouptitle'];
  399. $comma = ', ';
  400. } elseif($_G['setting']['verify']['enabled'] && substr($gid, 0, 1) == 'v') {
  401. $vid = substr($gid, 1);
  402. $permgroups .= $comma.$_G['setting']['verify'][$vid]['title'];
  403. $comma = ', ';
  404. }
  405. }
  406. }
  407. $custom = 0;
  408. if($permmessage) {
  409. $message = $permmessage;
  410. $custom = 1;
  411. } else {
  412. if($v) {
  413. $message = $type.'_'.$v.'_nopermission';
  414. } else {
  415. $message = 'group_nopermission';
  416. }
  417. }
  418. showmessage($message, NULL, array('fid' => $fid, 'permgroups' => $permgroups, 'grouptitle' => $_G['group']['grouptitle']), array('login' => 1), $custom);
  419. }
  420. /**
  421. * 依据 tid 或者 fid ,自动取得 $_G['forum'] 或 $_G['thread'] 数据
  422. * @global <type>
  423. */
  424. function loadforum() {
  425. global $_G;
  426. $tid = intval(getgpc('tid'));
  427. $fid = getgpc('fid');
  428. if(!$fid && getgpc('gid')) {
  429. $fid = intval(getgpc('gid'));
  430. }
  431. if(!empty($_G['gp_archiver'])) {//X1.5的Archiver兼容
  432. if($fid) {
  433. dheader('location: archiver/?fid-'.$fid.'.html');
  434. } elseif($tid) {
  435. dheader('location: archiver/?tid-'.$tid.'.html');
  436. } else {
  437. dheader('location: archiver/');
  438. }
  439. }
  440. if(defined('IN_ARCHIVER') && $_G['setting']['archiverredirect'] && !IS_ROBOT) {
  441. dheader('location: ../forum.php'.($_G['mod'] ? '?mod='.$_G['mod'].(!empty($_GET['fid']) ? '&ffid'] : (!empty($_GET['tid']) ? '&ttid'] : '')) : ''));
  442. }
  443. if($_G['setting']['forumpicstyle']) {
  444. $_G['setting']['forumpicstyle'] = unserialize($_G['setting']['forumpicstyle']);
  445. empty($_G['setting']['forumpicstyle']['thumbwidth']) && $_G['setting']['forumpicstyle']['thumbwidth'] = 214;
  446. empty($_G['setting']['forumpicstyle']['thumbheight']) && $_G['setting']['forumpicstyle']['thumbheight'] = 160;
  447. } else {
  448. $_G['setting']['forumpicstyle'] = array('thumbwidth' => 214, 'thumbheight' => 160);
  449. }
  450. if($fid) {
  451. $fid = is_numeric($fid) ? intval($fid) : (!empty($_G['setting']['forumfids'][$fid]) ? $_G['setting']['forumfids'][$fid] : 0);
  452. }
  453. $modthreadkey = isset($_G['gp_modthreadkey']) && $_G['gp_modthreadkey'] == modauthkey($tid) ? $_G['gp_modthreadkey'] : '';
  454. $_G['forum_auditstatuson'] = $modthreadkey ? true : false;
  455. $accessadd1 = $accessadd2 = $modadd1 = $modadd2 = $metadescription = $hookscriptmessage = '';
  456. $adminid = $_G['adminid'];
  457. if($_G['uid']) {
  458. if($_G['member']['accessmasks']) {
  459. $accessadd1 = ', a.allowview, a.allowpost, a.allowreply, a.allowgetattach, a.allowgetimage, a.allowpostattach, a.allowpostimage';
  460. $accessadd2 = "LEFT JOIN ".DB::table('forum_access')." a ON a.u AND a.f;
  461. }
  462. if($adminid == 3) {
  463. $modadd1 = ', m.uid AS ismoderator';
  464. $modadd2 = "LEFT JOIN ".DB::table('forum_moderator')." m ON m.u AND m.f;
  465. }
  466. }
  467. if(!empty($tid) || !empty($fid)) {
  468. if(!empty ($tid)) {
  469. $archiveid = !empty($_G['gp_archiveid']) ? intval($_G['gp_archiveid']) : null;
  470. $_G['thread'] = get_thread_by_tid($tid, '*', '', $archiveid);
  471. if(!$_G['forum_auditstatuson'] && !empty($_G['thread'])
  472. && !($_G['thread']['displayorder'] >= 0 || (in_array($_G['thread']['displayorder'], array(-4,-3,-2)) && $_G['thread']['authorid'] == $_G['uid']))) {
  473. $_G['thread'] = null;
  474. }
  475. $_G['forum_thread'] = & $_G['thread'];
  476. if(empty($_G['thread'])) {
  477. $fid = $tid = 0;
  478. } else {
  479. $fid = $_G['thread']['fid'];
  480. $tid = $_G['thread']['tid'];
  481. }
  482. }
  483. if($fid) {
  484. $forum = DB::fetch_first("SELECT f.fid, f.*, ff.* $accessadd1 $modadd1, f.fid AS fid
  485. FROM ".DB::table('forum_forum')." f
  486. LEFT JOIN ".DB::table("forum_forumfield")." ff ON ff.fid=f.fid $accessadd2 $modadd2
  487. WHERE f.f");
  488. }
  489. if($forum) {
  490. $forum['ismoderator'] = !empty($forum['ismoderator']) || $adminid == 1 || $adminid == 2 ? 1 : 0;
  491. $fid = $forum['fid'];
  492. $gorup_admingroupids = $_G['setting']['group_admingroupids'] ? unserialize($_G['setting']['group_admingroupids']) : array('1' => '1');
  493. if($forum['status'] == 3) {
  494. if(!$_G['setting']['groupstatus']) {
  495. showmessage('group_status_off');
  496. }
  497. if(!empty($forum['moderators'])) {
  498. $forum['moderators'] = unserialize($forum['moderators']);
  499. } else {
  500. require_once libfile('function/group');
  501. $forum['moderators'] = update_groupmoderators($fid);
  502. }
  503. if($_G['uid'] && $_G['adminid'] != 1) {
  504. $forum['ismoderator'] = !empty($forum['moderators'][$_G['uid']]) ? 1 : 0;
  505. $_G['adminid'] = 0;
  506. if($forum['ismoderator'] || $gorup_admingroupids[$_G['groupid']]) {
  507. $_G['adminid'] = $_G['adminid'] ? $_G['adminid'] : 3;
  508. if(!empty($gorup_admingroupids[$_G['groupid']])) {
  509. $forum['ismoderator'] = 1;
  510. $_G['adminid'] = 2;
  511. }
  512. $group_userperm = unserialize($_G['setting']['group_userperm']);
  513. if(is_array($group_userperm)) {
  514. $_G['group'] = array_merge($_G['group'], $group_userperm);
  515. $_G['group']['allowmovethread'] = $_G['group']['allowcopythread'] = $_G['group']['allowedittypethread']= 0;
  516. }
  517. }
  518. }
  519. }
  520. foreach(array('threadtypes', 'threadsorts', 'creditspolicy', 'modrecommend') as $key) {
  521. $forum[$key] = !empty($forum[$key]) ? unserialize($forum[$key]) : array();
  522. if(!is_array($forum[$key])) {
  523. $forum[$key] = array();
  524. }
  525. }
  526. if($forum['status'] == 3) {
  527. $_G['isgroupuser'] = 0;
  528. $_G['basescript'] = 'group';
  529. if(empty($forum['level'])) {
  530. $levelid = DB::result_first("SELECT levelid FROM ".DB::table('forum_grouplevel')." WHERE creditshigher<='$forum[commoncredits]' AND '$forum[commoncredits]'<creditslower LIMIT 1");
  531. $forum['level'] = $levelid;
  532. DB::query("UPDATE ".DB::table('forum_forum')." SET level='$levelid' WHERE f");
  533. }
  534. loadcache('grouplevels');
  535. $grouplevel = $_G['grouplevels'][$forum['level']];
  536. if(!empty($grouplevel['icon'])) {
  537. $valueparse = parse_url($grouplevel['icon']);
  538. if(!isset($valueparse['host'])) {
  539. $grouplevel['icon'] = $_G['setting']['attachurl'].'common/'.$grouplevel['icon'];
  540. }
  541. }
  542. $group_postpolicy = $grouplevel['postpolicy'];
  543. if(is_array($group_postpolicy)) {
  544. $forum = array_merge($forum, $group_postpolicy);
  545. }
  546. $forum['allowfeed'] = $_G['setting']['group_allowfeed'];
  547. if($_G['uid']) {
  548. if(!empty($forum['moderators'][$_G['uid']])) {
  549. $_G['isgroupuser'] = 1;
  550. } else {
  551. $_G['isgroupuser'] = DB::result_first("SELECT level FROM ".DB::table('forum_groupuser')." WHERE f AND u LIMIT 1");
  552. if($_G['isgroupuser'] <= 0 && empty($forum['ismoderator'])) {
  553. $_G['group']['allowrecommend'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowrecommend'] = 0;
  554. $_G['group']['allowcommentpost'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowcommentpost'] = 0;
  555. $_G['group']['allowcommentitem'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowcommentitem'] = 0;
  556. $_G['group']['raterange'] = $_G['cache']['usergroup_'.$_G['groupid']]['raterange'] = array();
  557. $_G['group']['allowvote'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowvote'] = 0;
  558. } else {
  559. $_G['isgroupuser'] = 1;
  560. }
  561. }
  562. }
  563. }
  564. } else {
  565. $fid = 0;
  566. }
  567. }
  568. $_G['fid'] = $fid;
  569. $_G['tid'] = $tid;
  570. $_G['forum'] = &$forum;
  571. $_G['current_grouplevel'] = &$grouplevel;
  572. if(isset($_G['cookie']['widthauto']) && $_G['setting']['switchwidthauto'] && empty($_G['forum']['widthauto'])) {
  573. $_G['forum_widthauto'] = $_G['cookie']['widthauto'] > 0;
  574. } else {
  575. $_G['forum_widthauto'] = empty($_G['forum']['widthauto']) ? !$_G['setting']['allowwidthauto'] : $_G['forum']['widthauto'] > 0;
  576. if(!empty($_G['forum']['widthauto'])) {
  577. $_G['setting']['switchwidthauto'] = 0;
  578. }
  579. }
  580. }
  581. /**
  582. *
  583. * @param <type> $tid 主题id
  584. * @param <type> $fields 取用主题表的字段, 默认 "*"
  585. * @param <type> $addcondiction 除 t 以外的其他条件, 默认空
  586. * @param <type> $forcetableid 是否强制只从某个分表中取得 thread, 默认 null
  587. * @return array 当有合法数值返回的时候, 将自动追加2个key值threadtable, posttable 记录此主题使用的分表名称
  588. */
  589. function get_thread_by_tid($tid, $fields = '*', $addcondiction = '', $forcetableid = null) {
  590. global $_G;
  591. $ret = array();
  592. if(!is_numeric($tid)) {
  593. return $ret;
  594. }
  595. loadcache('threadtableids');
  596. $threadtableids = array(0);
  597. if(!empty($_G['cache']['threadtableids'])) {
  598. if($forcetableid === null || ($forcetableid > 0 && !in_array($forcetableid, $_G['cache']['threadtableids']))) {
  599. $threadtableids = array_merge($threadtableids, $_G['cache']['threadtableids']);
  600. } else {
  601. $threadtableids = array(intval($forcetableid));
  602. }
  603. }
  604. foreach($threadtableids as $tableid) {
  605. $table = $tableid > 0 ? "forum_thread_{$tableid}" : 'forum_thread';
  606. $ret = DB::fetch_first("SELECT $fields FROM ".DB::table($table)." WHERE t $addcondiction LIMIT 1");
  607. if($ret) {
  608. $ret['threadtable'] = $table;
  609. $ret['threadtableid'] = $tableid;
  610. $ret['posttable'] = 'forum_post'.($ret['posttableid'] ? '_'.$ret['posttableid'] : '');
  611. break;
  612. }
  613. }
  614. if(!is_array($ret)) {
  615. $ret = array();
  616. }
  617. return $ret;
  618. }
  619. /**
  620. * 依据 pid 和 其他条件,从某个post数据表中取回post信息
  621. * @global <type> $_G
  622. * @param <type> $pid 帖子id
  623. * @param <type> $fields 帖子表字段,默认 *
  624. * @param <type> $addcondiction 其他条件,默认 空
  625. * @param <type> $forcetable 是否强制使用某个分表, 支持: 使用id: 0 1 2 或者 p(主表), a(副表) 或者直接制定表名 forum_post_1
  626. * @return array
  627. */
  628. function get_post_by_pid($pid, $fields = '*', $addcondiction = '', $forcetable = null) {
  629. global $_G;
  630. $ret = array();
  631. if(!is_numeric($pid)) {
  632. return $ret;
  633. }
  634. loadcache('posttable_info');
  635. $posttableids = array(0);
  636. if($_G['cache']['posttable_info']) {
  637. if(isset($forcetable)) {
  638. if(is_numeric($forcetable) && array_key_exists($forcetable, $_G['cache']['posttable_info'])) {
  639. $posttableids[] = $forcetable;
  640. } elseif(substr($forcetable, 0, 10) == 'forum_post') {
  641. $posttableids[] = $forcetable;
  642. }
  643. } else {
  644. $posttableids = array_keys($_G['cache']['posttable_info']);
  645. }
  646. }
  647. foreach ($posttableids as $id) {
  648. $table = empty($id) ? 'forum_post' : (is_numeric($id) ? 'forum_post_'.$id : $id);
  649. $ret = DB::fetch_first("SELECT $fields FROM ".DB::table($table)." WHERE p $addcondiction LIMIT 1");
  650. if($ret) {
  651. $ret['posttable'] = $table;
  652. break;
  653. }
  654. }
  655. if(!is_array($ret)) {
  656. $ret = array();
  657. }
  658. return $ret;
  659. }
  660. function set_rssauth() {
  661. global $_G;
  662. if($_G['setting']['rssstatus'] && $_G['uid']) {
  663. $auth = authcode($_G['uid']."\t".($_G['fid'] ? $_G['fid'] : '').
  664. "\t".substr(md5($_G['member']['password']), 0, 8), 'ENCODE', md5($_G['config']['security']['authkey']));
  665. } else {
  666. $auth = '0';
  667. }
  668. $_G['rssauth'] = rawurlencode($auth);
  669. }
  670. function my_thread_log($opt, $data) {
  671. global $_G;
  672. $my_search_data = $_G['setting']['my_search_data'];
  673. if ($my_search_data && !is_array($my_search_data)) {
  674. $my_search_data = unserialize($my_search_data);
  675. }
  676. if(!$_G['setting']['my_search_data']) return;
  677. $data['action'] = $opt;
  678. $data['dateline'] = time();
  679. DB::insert('forum_threadlog', $data, false, true);
  680. }
  681. function my_post_log($opt, $data) {
  682. global $_G;
  683. $my_search_data = $_G['setting']['my_search_data'];
  684. if ($my_search_data && !is_array($my_search_data)) {
  685. $my_search_data = unserialize($my_search_data);
  686. }
  687. if(!$_G['setting']['my_search_data']) return;
  688. $data['action'] = $opt;
  689. $data['dateline'] = time();
  690. DB::insert('forum_postlog', $data, false, true);
  691. }
  692. function rssforumperm($forum) {
  693. $is_allowed = $forum['type'] != 'group' && (!$forum['viewperm'] || ($forum['viewperm'] && forumperm($forum['viewperm'], 7)));
  694. return $is_allowed;
  695. }
  696. /**
  697. * upload_icon_banner上传群组/版块图片
  698. * $forum中 fid和status 必须存在,status判断是群组还是版块图片,以存入不同目录
  699. * $file上传时的$_FILES
  700. * $type icon 或 banner
  701. */
  702. function upload_icon_banner(&$data, $file, $type) {
  703. global $_G;
  704. $data['extid'] = empty($data['extid']) ? $data['fid'] : $data['extid'];
  705. if(empty($data['extid'])) return '';
  706. if($data['status'] == 3 && $_G['setting']['group_imgsizelimit']) {
  707. $file['size'] > ($_G['setting']['group_imgsizelimit'] * 1024) && showmessage('file_size_overflow', '', array('size' => $_G['setting']['group_imgsizelimit'] * 1024));
  708. }
  709. require_once libfile('class/upload');
  710. $upload = new discuz_upload();
  711. $uploadtype = $data['status'] == 3 ? 'group' : 'common';
  712. if(!$upload->init($file, $uploadtype, $data['extid'], $type)) {
  713. return false;
  714. }
  715. if(!$upload->save()) {
  716. if(!defined('IN_ADMINCP')) {
  717. showmessage($upload->errormessage());
  718. } else {
  719. cpmsg($upload->errormessage(), '', 'error');
  720. }
  721. }
  722. if($data['status'] == 3 && $type == 'icon') {
  723. require_once libfile('class/image');
  724. $img = new image;
  725. $img->Thumb($upload->attach['target'], './'.$uploadtype.'/'.$upload->attach['attachment'], 48, 48, 'fixwr');
  726. }
  727. return $upload->attach['attachment'];
  728. }
  729. /**
  730. 用于 archiver 环境下的分页
  731. */
  732. function arch_multi($total, $perpage, $page, $link) {
  733. $pages = @ceil($total / $perpage) + 1;
  734. $pagelink = '';
  735. if($pages > 1) {
  736. $pagelink .= lang('forum/archiver', 'page') . ": \n";
  737. $pagestart = $page - 10 < 1 ? 1 : $page - 10;
  738. $pageend = $page + 10 >= $pages ? $pages : $page + 10;
  739. for($i = $pagestart; $i < $pageend; $i++) {
  740. $pagelink .= ($i == $page ? "<strong>[$i]</strong>" : "<a href=\"$link&page=$i\">$i</a>")." \n";
  741. }
  742. }
  743. return $pagelink;
  744. }
  745. /**
  746. archiver 模板路径
  747. */
  748. function loadarchiver($path) {
  749. global $_G;
  750. if(!$_G['setting']['archiver']) {
  751. require_once DISCUZ_ROOT . "./source/archiver/common/header.php";
  752. echo '<div >'</div>';
  753. require_once DISCUZ_ROOT . "./source/archiver/common/footer.php";
  754. exit;
  755. }
  756. $filename = $path . '.php';
  757. return DISCUZ_ROOT . "./source/archiver/$filename";
  758. }
  759. /**
  760. * update_threadpartake 按参与人次更新主题热度
  761. */
  762. function update_threadpartake($tid) {
  763. global $_G;
  764. if($_G['uid'] && $tid) {
  765. if($_G['setting']['heatthread']['period']) {
  766. $partaked = DB::result_first("SELECT uid FROM ".DB::table('forum_threadpartake')." WHERE t AND u");
  767. if(!$partaked) {
  768. DB::query("INSERT INTO ".DB::table('forum_threadpartake')." (tid, uid, dateline) VALUES ('$tid', '$_G[uid]', ".TIMESTAMP.")");
  769. DB::query("UPDATE ".DB::table('forum_thread')." SET heats=heats+1 WHERE t", 'UNBUFFERED');
  770. }
  771. } else {
  772. DB::query("UPDATE ".DB::table('forum_thread')." SET heats=heats+1 WHERE t", 'UNBUFFERED');
  773. }
  774. }
  775. }
  776. /**
  777. * 获取帖子封面地址
  778. *
  779. * @param int $tid
  780. * @param int $cover 负数时表示是远程,正数为本地 没有值表示只取filename
  781. */
  782. function getthreadcover($tid, $cover = 0, $getfilename = 0) {
  783. global $_G;
  784. if(empty($tid)) {
  785. return '';
  786. }
  787. $coverpath = '';
  788. $covername = 'threadcover/'.substr(md5($tid), 0, 2).'/'.substr(md5($tid), 2, 2).'/'.$tid.'.jpg';
  789. if($getfilename) {
  790. return $covername;
  791. }
  792. if($cover) {
  793. $coverpath = ($cover < 0 ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']).'forum/'.$covername;
  794. }
  795. return $coverpath;
  796. }
  797. /**
  798. *添加主题标签
  799. *
  800. * @param <type> $tags
  801. * @param <type> $itemid
  802. */
  803. function addthreadtag($tags, $itemid , $typeid = 'tid') {
  804. global $_G;
  805. if($tags == '') {
  806. return;
  807. }
  808. $tags = str_replace(array(chr(0xa3).chr(0xac), chr(0xa1).chr(0x41), chr(0xef).chr(0xbc).chr(0x8c)), ',', censor($tags));
  809. if(strexists($tags, ',')) {
  810. $tagarray = array_unique(explode(',', $tags));
  811. } else {
  812. $langcore = lang('core');
  813. $tags = str_replace($langcore['fullblankspace'], ' ', $tags);
  814. $tagarray = array_unique(explode(' ', $tags));
  815. }
  816. $tagcount = 0;
  817. foreach($tagarray as $tagname) {
  818. $tagname = trim($tagname);
  819. if(preg_match('/^([\x7f-\xff_-]|\w|\s){3,20}$/', $tagname)) {
  820. $result = DB::fetch_first("SELECT tagid, status FROM ".DB::table('common_tag')." WHERE tagname='$tagname'");
  821. if($result['tagid']) {
  822. if(!$result['status']) {
  823. $tagid = $result['tagid'];
  824. }
  825. } else {
  826. DB::query("INSERT INTO ".DB::table('common_tag')." (tagname, status) VALUES ('$tagname', '0')");
  827. $tagid = DB::insert_id();
  828. }
  829. if($tagid) {
  830. DB::query("INSERT INTO ".DB::table('common_tagitem')." (tagid, tagname, itemid, idtype) VALUES ('$tagid', '$tagname', '$itemid', '$typeid')");
  831. $tagcount++;
  832. $tagstr .= $tagid.','.$tagname.'\t';
  833. }
  834. if($tagcount > 4) {
  835. unset($tagarray);
  836. break;
  837. }
  838. }
  839. }
  840. return $tagstr;
  841. }
  842. /**
  843. *修改主题标签
  844. *
  845. * @param <type> $tags
  846. * @param <type> $itemid
  847. * @return <type>
  848. */
  849. function modthreadtag($tags, $itemid) {
  850. global $_G;
  851. $thread = & $_G['forum_thread'];
  852. $posttable = $thread['posttable'];
  853. $tagstr = DB::result_first("SELECT tags FROM ".DB::table($posttable)." WHERE t AND first=1");
  854. $threadtagarray = $threadtagidarray = $threadtagarraynew = array();
  855. $query = DB::query("SELECT tagid, tagname FROM ".DB::table('common_tagitem')." WHERE idtype='tid' AND item");
  856. while($result = DB::fetch($query)) {
  857. $threadtagarray[] = $result['tagname'];
  858. $threadtagidarray[] = $result['tagid'];
  859. }
  860. $tags = str_replace(array(chr(0xa3).chr(0xac), chr(0xa1).chr(0x41), chr(0xef).chr(0xbc).chr(0x8c)), ',', censor($tags));
  861. if(strexists($tags, ',')) {
  862. $tagarray = array_unique(explode(',', $tags));
  863. } else {
  864. $langcore = lang('core');
  865. $tags = str_replace($langcore['fullblankspace'], ' ', $tags);
  866. $tagarray = array_unique(explode(' ', $tags));
  867. }
  868. $tagcount = 0;
  869. foreach($tagarray as $tagname) {
  870. $tagname = trim($tagname);
  871. if(preg_match('/^([\x7f-\xff_-]|\w|\s){3,20}$/', $tagname)) {
  872. $threadtagarraynew[] = $tagname;
  873. if(!in_array($tagname, $threadtagarray)) {
  874. $result = DB::fetch_first("SELECT tagid, status FROM ".DB::table('common_tag')." WHERE tagname='$tagname'");
  875. if($result['tagid']) {
  876. if(!$result['status']) {
  877. $tagid = $result['tagid'];
  878. }
  879. } else {
  880. DB::query("INSERT INTO ".DB::table('common_tag')." (tagname, status) VALUES ('$tagname', '0')");
  881. $tagid = DB::insert_id();
  882. }
  883. if($tagid) {
  884. DB::query("INSERT INTO ".DB::table('common_tagitem')." (tagid, tagname, itemid, idtype) VALUES ('$tagid', '$tagname', '$itemid', 'tid')");
  885. $tagstr = $tagstr.$tagid.','.$tagname.'\t';
  886. }
  887. }
  888. }
  889. $tagcount++;
  890. if($tagcount > 4) {
  891. unset($tagarray);
  892. break;
  893. }
  894. }
  895. foreach($threadtagarray as $key => $tagname) {
  896. if(!in_array($tagname, $threadtagarraynew)) {
  897. DB::query("DELETE FROM ".DB::table('common_tagitem')." WHERE idtype='tid' AND itemid = '$itemid' AND tagname='$tagname'");
  898. $tagid = $threadtagidarray[$key];
  899. $tagstr = str_replace("$tagid,$tagname\t", '', $tagstr);
  900. }
  901. }
  902. return $tagstr;
  903. }
  904. /**
  905. * 将未使用的附件转为已使用状态
  906. * @param <int> $aid
  907. * @param <int> $tid
  908. * @param <pid> $pid
  909. */
  910. function convertunusedattach($aid, $tid, $pid) {
  911. if(!$aid) {
  912. return;
  913. }
  914. global $_G;
  915. $attach = DB::fetch_first("SELECT * FROM ".DB::table('forum_attachment_unused')." WHERE a AND u");
  916. if(!$attach) {
  917. return;
  918. }
  919. $attach = daddslashes($attach);
  920. $attach['tid'] = $tid;
  921. $attach['pid'] = $pid;
  922. DB::insert(getattachtablebytid($tid), $attach, false);
  923. DB::update('forum_attachment', array('tid' => $tid, 'pid' => $pid, 'tableid' => getattachtableid($tid)), "a");
  924. DB::delete('forum_attachment_unused', "a");
  925. }
  926. /**
  927. * 更新附件的 TID 值
  928. * @param <string> $where 条件
  929. * @param <int> $oldtid 旧 TID
  930. * @param <int> $newtid 新 TID
  931. */
  932. function updateattachtid($where, $oldtid, $newtid) {
  933. $oldattachtable = getattachtablebytid($oldtid);
  934. $newattachtable = getattachtablebytid($newtid);
  935. if($oldattachtable != $newattachtable) {
  936. $query = DB::query("SELECT * FROM ".DB::table($oldattachtable)." WHERE $where");
  937. while($attach = DB::fetch($query)) {
  938. $attach = daddslashes($attach);
  939. $attach['tid'] = $newtid;
  940. DB::insert($newattachtable, $attach);
  941. }
  942. DB::delete($oldattachtable, $where);
  943. }
  944. DB::query("UPDATE ".DB::table('forum_attachment')." SET t,table.getattachtableid($newtid)."' WHERE $where");
  945. }
  946. /**
  947. * 更新POST
  948. * @global $_G
  949. * @param array $data 更新的数据
  950. * @param string/array $condition 条件
  951. * @param bool $unbuffered 是否即时返回
  952. * @param int $posttableid post分表ID
  953. * @return int 返回的更新的记录数
  954. */
  955. function updatepost($data, $condition, $unbuffered = false, $posttableid = false) {
  956. global $_G;
  957. loadcache('posttableids');
  958. $affected_rows = 0;
  959. if(!empty($_G['cache']['posttableids'])) {
  960. $posttableids = $posttableid !== false && in_array($posttableid, $_G['cache']['posttableids']) ? array($posttableid) : $_G['cache']['posttableids'];
  961. } else {
  962. $posttableids = array('0');
  963. }
  964. foreach($posttableids as $id) {
  965. DB::update(getposttable($id), $data, $condition, $unbuffered);
  966. $affected_rows += DB::affected_rows();
  967. }
  968. return $affected_rows;
  969. }
  970. /**
  971. 插入一个帖子
  972. */
  973. function insertpost($data) {
  974. if(isset($data['tid'])) {
  975. $tableid = DB::result_first("SELECT posttableid FROM ".DB::table('forum_thread')." WHERE ttid']}'");
  976. } else {
  977. $tableid = $data['tid'] = 0;
  978. }
  979. $pid = DB::insert('forum_post_tableid', array('pid' => null), true);
  980. if(!$tableid) {
  981. $tablename = 'forum_post';
  982. } else {
  983. $tablename = "forum_post_$tableid";
  984. }
  985. $data = array_merge($data, array('pid' => $pid));
  986. DB::insert($tablename, $data);
  987. if($pid % 1024 == 0) {
  988. DB::delete('forum_post_tableid', "pid<$pid");
  989. }
  990. save_syscache('max_post_id', $pid);
  991. return $pid;
  992. }
  993. ?>