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

  1. <?php  
  2.   
  3. /** 
  4.  *      [Discuz!] (C)2001-2099 Comsenz Inc. 
  5.  *      This is NOT a freeware, use is subject to license terms 
  6.  *      论坛模块通用函数集合 
  7.  *      $Id: function_forum.php 24723 2011-10-09 12:50:14Z yangli $ 
  8.  */  
  9.   
  10. if(!defined('IN_DISCUZ')) {  
  11.     exit('Access Denied');  
  12. }  
  13.   
  14. /** 
  15. *  
  16. * @param $uid - 用户ID 
  17. * @param $size - 头像大小 small/middle/big 
  18. * @param $returnsrc - 是否返回头像地址 
  19. */  
  20. function discuz_uc_avatar($uid, $size = '', $returnsrc = FALSE) {  
  21.     global $_G;  
  22.     return avatar($uid, $size, $returnsrc, FALSE, $_G['setting']['avatarmethod'], $_G['setting']['ucenterurl']);  
  23. }  
  24.   
  25. /** 
  26. * 论坛附件删除 
  27. * @param $attach - 单一附件数据 
  28. */  
  29. function dunlink($attach) {  
  30.     global $_G;  
  31.     $filename = $attach['attachment'];  
  32.     $havethumb = $attach['thumb'];  
  33.     $remote = $attach['remote'];  
  34.     if($remote) {  
  35.         ftpcmd('delete', $_G['setting']['ftp']['attachdir'].'/forum/'.$filename);  
  36.         $havethumb && ftpcmd('delete', $_G['setting']['ftp']['attachdir'].'/forum/'.getimgthumbname($filename));  
  37.     } else {  
  38.         @unlink($_G['setting']['attachdir'].'/forum/'.$filename);  
  39.         $havethumb && @unlink($_G['setting']['attachdir'].'/forum/'.getimgthumbname($filename));  
  40.     }  
  41.     if($attach['aid']) {  
  42.         @unlink($_G['setting']['attachdir'].'image/'.$attach['aid'].'_140_140.jpg');  
  43.     }  
  44. }  
  45.   
  46. /** 
  47. 权限表达式 
  48. * @param $formula - 权限表达式 
  49. */  
  50. function formulaperm($formula) {  
  51.     global $_G;  
  52.   
  53.     $formula = unserialize($formula);  
  54.     $medalperm = $formula['medal'];  
  55.     $permusers = $formula['users'];  
  56.     $permmessage = $formula['message'];  
  57.     if($_G['setting']['medalstatus'] && $medalperm) {  
  58.         $exists = 1;  
  59.         $_G['forum_formulamessage'] = '';  
  60.         $medalpermc = $medalperm;  
  61.         if($_G['uid']) {  
  62.             $medals = explode("\t", DB::result_first("SELECT medals FROM ".DB::table('common_member_field_forum')." WHERE uid='$_G[uid]'"));  
  63.             foreach($medalperm as $k => $medal) {  
  64.                 foreach($medals as $r) {  
  65.                     list($medalid) = explode("|", $r);  
  66.                     if($medalid == $medal) {  
  67.                         $exists = 0;  
  68.                         unset($medalpermc[$k]);  
  69.                     }  
  70.                 }  
  71.             }  
  72.         } else {  
  73.             $exists = 0;  
  74.         }  
  75.         if($medalpermc) {  
  76.             loadcache('medals');  
  77.             foreach($medalpermc as $medal) {  
  78.                 if($_G['cache']['medals'][$medal]) {  
  79.                     $_G['forum_formulamessage'] .= '<img src="'.STATICURL.'image/common/'.$_G['cache']['medals'][$medal]['image'].'" style="vertical-align:middle;" /> '.$_G['cache']['medals'][$medal]['name'].'  ';  
  80.                 }  
  81.             }  
  82.             showmessage('forum_permforum_nomedal', NULL, array('forum_permforum_nomedal' => $_G['forum_formulamessage']), array('login' => 1));  
  83.         }  
  84.     }  
  85.     $formulatext = $formula[0];  
  86.     $formula = $formula[1];  
  87.     if($_G['adminid'] == 1 || $_G['forum']['ismoderator'] || in_array($_G['groupid'], explode("\t", $_G['forum']['spviewperm']))) {  
  88.         return FALSE;  
  89.     }  
  90.     if($permusers) {  
  91.         $permusers = str_replace(array("\r\n", "\r"), array("\n", "\n"), $permusers);  
  92.         $permusers = explode("\n", trim($permusers));  
  93.         if(!in_array($_G['member']['username'], $permusers)) {  
  94.             showmessage('forum_permforum_disallow', NULL, array(), array('login' => 1));  
  95.         }  
  96.     }  
  97.     if(!$formula) {  
  98.         return FALSE;  
  99.     }  
  100.     if(strexists($formula, '$memberformula[')) {  
  101.         preg_match_all("/\\\$memberformula
    (\w+?)
    /", $formula, $a);  
  102.         $fields = $profilefields = array();  
  103.         $mfadd = array();  
  104.         foreach($a[1] as $field) {  
  105.             switch($field) {  
  106.                 case 'regdate':  
  107.                     $formula = preg_replace("/\{(\d{4})\-(\d{1,2})\-(\d{1,2})\}/e", "'\'\\1-'.sprintf('%02d', '\\2').'-'.sprintf('%02d', '\\3').'\''", $formula);  
  108.                 case 'regday':  
  109.                     $fields[] = 'm.regdate';break;  
  110.                 case 'regip':  
  111.                 case 'lastip':  
  112.                     $formula = preg_replace("/\{([\d\.]+?)\}/", "'\\1'", $formula);  
  113.                     $formula = preg_replace('/(\$memberformula
    \'(regip|lastip)\'
    )\s*=+\s*\'([\d\.]+?)\'/', "strpos(\\1, '\\3')===0", $formula);  
  114.                 case 'buyercredit':  
  115.                 case 'sellercredit':  
  116.                     $mfadd['ms'] = " LEFT JOIN ".DB::table('common_member_status')." ms ON m.uid=ms.uid";  
  117.                     $fields[] = 'ms.'.$field;break;  
  118.                 case substr($field, 0, 5) == 'field':  
  119.                     $mfadd['mp'] = " LEFT JOIN ".DB::table('common_member_profile')." mp ON m.uid=mp.uid";  
  120.                     $fields[] = 'mp.field'.intval(substr($field, 5));  
  121.                     $profilefields[] = $field;break;  
  122.             }  
  123.         }  
  124.         $memberformula = array();  
  125.         if($_G['uid']) {  
  126.             $memberformula = DB::fetch_first("SELECT ".implode(',', $fields)." FROM ".DB::table('common_member')." m ".implode('', $mfadd)." WHERE m.uid='$_G[uid]'");  
  127.             if(in_array('regday', $a[1])) {  
  128.                 $memberformula['regday'] = intval((TIMESTAMP - $memberformula['regdate']) / 86400);  
  129.             }  
  130.             if(in_array('regdate', $a[1])) {  
  131.                 $memberformula['regdate'] = date('Y-m-d', $memberformula['regdate']);  
  132.             }  
  133.             $memberformula['lastip'] = $memberformula['lastip'] ? $memberformula['lastip'] : $_G['clientip'];  
  134.         } else {  
  135.             if(isset($memberformula['regip'])) {  
  136.                 $memberformula['regip'] = $_G['clientip'];  
  137.             }  
  138.             if(isset($memberformula['lastip'])) {  
  139.                 $memberformula['lastip'] = $_G['clientip'];  
  140.             }  
  141.         }  
  142.     }  
  143.     @eval("\$formulaperm = ($formula) ? TRUE : FALSE;");  
  144.     if(!$formulaperm) {  
  145.         if(!$permmessage) {  
  146.             $language = lang('forum/misc');  
  147.             $search = array('regdate', 'regday', 'regip', 'lastip', 'buyercredit', 'sellercredit', 'digestposts', 'posts', 'threads', 'oltime');  
  148.             $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']);  
  149.             for($i = 1; $i <= 8; $i++) {  
  150.                 $search[] = 'extcredits'.$i;  
  151.                 $replace[] = $_G['setting']['extcredits'][$i]['title'] ? $_G['setting']['extcredits'][$i]['title'] : $language['formulaperm_extcredits'].$i;  
  152.             }  
  153.             if($profilefields) {  
  154.                 loadcache(array('fields_required', 'fields_optional'));  
  155.                 foreach($profilefields as $profilefield) {  
  156.                     $search[] = $profilefield;  
  157.                     $replace[] = !empty($_G['cache']['fields_optional']['field_'.$profilefield]) ? $_G['cache']['fields_optional']['field_'.$profilefield]['title'] : $_G['cache']['fields_required']['field_'.$profilefield]['title'];  
  158.                 }  
  159.             }  
  160.             $i = 0;$_G['forum_usermsg'] = '';  
  161.             foreach($search as $s) {  
  162.                 if(in_array($s, array('digestposts', 'posts', 'threads', 'oltime', 'extcredits1', 'extcredits2', 'extcredits3', 'extcredits4', 'extcredits5', 'extcredits6', 'extcredits7', 'extcredits8'))) {  
  163.                     $_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br />   '.$replace[$i].': '.(@eval('return intval(getuserprofile(\''.$s.'\'));')) : '';  
  164.                 } elseif(in_array($s, array('regdate', 'regip'))) {  
  165.                     $_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br />   '.$replace[$i].': '.(@eval('return $memberformula[\''.$s.'\'];')) : '';  
  166.                 }  
  167.                 $i++;  
  168.             }  
  169.             $search = array_merge($search, array('and', 'or', '>=', '<=', '=='));  
  170.             $replace = array_merge($replace, array('  <b>'.$language['formulaperm_and'].'</b>  ', '  <b>'.$language['formulaperm_or'].'</b>  ', '≥', '≤', '='));  
  171.             $_G['forum_formulamessage'] = str_replace($search, $replace, $formulatext);  
  172.         } else {  
  173.             $_G['forum_formulamessage'] = $permmessage;  
  174.         }  
  175.   
  176.         if(!$permmessage) {  
  177.             showmessage('forum_permforum_nopermission', NULL, array('formulamessage' => $_G['forum_formulamessage'], 'usermsg' => $_G['forum_usermsg']), array('login' => 1));  
  178.         } else {  
  179.             showmessage('forum_permforum_nopermission_custommsg', NULL, array('formulamessage' => $_G['forum_formulamessage']), array('login' => 1));  
  180.         }  
  181.     }  
  182.     return TRUE;  
  183. }  
  184.   
  185. /** 
  186. 勋章权限表达式 
  187. * @param $formula - 勋章权限表达式 
  188. * @param $type - 1 权限验证 2 勋章字串 
  189. */  
  190. function medalformulaperm($formula, $type) {  
  191.     global $_G;  
  192.   
  193.     $formula = unserialize($formula);  
  194.     $permmessage = $formula['message'];  
  195.     $formula = $formula['medal'];  
  196.     if(!empty($formula['usergroupallow']) && is_array($formula['usergroups']) && !in_array($_G['groupid'], $formula['usergroups'])) {  
  197.         loadcache('usergroups');  
  198.         $message = array();  
  199.         foreach($formula['usergroups'] as $groupid) {  
  200.             $message[] = $_G['cache']['usergroups'][$groupid]['grouptitle'].' ';  
  201.         }  
  202.         $_G['forum_formulamessage'] = implode(', ', $message);  
  203.         $_G['forum_usermsg'] = $_G['cache']['usergroups'][$_G['groupid']]['grouptitle'];  
  204.         return FALSE;  
  205.     }  
  206.     $formulatext = $formula[0];  
  207.     $formula = $formula[1];  
  208.     if(!$formula) {  
  209.         return FALSE;  
  210.     }  
  211.     if(strexists($formula, '$memberformula[')) {  
  212.         preg_match_all("/\\\$memberformula
    (\w+?)
    /", $formula, $a);  
  213.         $fields = $profilefields = array();  
  214.         $mfadd = array();  
  215.         foreach($a[1] as $field) {  
  216.             switch($field) {  
  217.                 case 'regdate':  
  218.                     $formula = preg_replace("/\{(\d{4})\-(\d{1,2})\-(\d{1,2})\}/e", "'\'\\1-'.sprintf('%02d', '\\2').'-'.sprintf('%02d', '\\3').'\''", $formula);  
  219.                 case 'regday':  
  220.                     $fields[] = 'm.regdate';break;  
  221.                 case 'regip':  
  222.                 case 'lastip':  
  223.                     $formula = preg_replace("/\{([\d\.]+?)\}/", "'\\1'", $formula);  
  224.                     $formula = preg_replace('/(\$memberformula
    \'(regip|lastip)\'
    )\s*=+\s*\'([\d\.]+?)\'/', "strpos(\\1, '\\3')===0", $formula);  
  225.                 case 'buyercredit':  
  226.                 case 'sellercredit':  
  227.                     $mfadd['ms'] = " LEFT JOIN ".DB::table('common_member_status')." ms ON m.uid=ms.uid";  
  228.                     $fields[] = 'ms.'.$field;break;  
  229.                 case substr($field, 0, 5) == 'field':  
  230.                     $mfadd['mp'] = " LEFT JOIN ".DB::table('common_member_profile')." mp ON m.uid=mp.uid";  
  231.                     $fields[] = 'mp.field'.intval(substr($field, 5));  
  232.                     $profilefields[] = $field;break;  
  233.             }  
  234.         }  
  235.         $memberformula = array();  
  236.         if($_G['uid']) {  
  237.             $memberformula = DB::fetch_first("SELECT ".implode(',', $fields)." FROM ".DB::table('common_member')." m ".implode('', $mfadd)." WHERE m.uid='$_G[uid]'");  
  238.             if(in_array('regday', $a[1])) {  
  239.                 $memberformula['regday'] = intval((TIMESTAMP - $memberformula['regdate']) / 86400);  
  240.             }  
  241.             if(in_array('regdate', $a[1])) {  
  242.                 $memberformula['regdate'] = date('Y-m-d', $memberformula['regdate']);  
  243.             }  
  244.             $memberformula['lastip'] = $memberformula['lastip'] ? $memberformula['lastip'] : $_G['clientip'];  
  245.         } else {  
  246.             if(isset($memberformula['regip'])) {  
  247.                 $memberformula['regip'] = $_G['clientip'];  
  248.             }  
  249.             if(isset($memberformula['lastip'])) {  
  250.                 $memberformula['lastip'] = $_G['clientip'];  
  251.             }  
  252.         }  
  253.     }  
  254.     @eval("\$formulaperm = ($formula) ? TRUE : FALSE;");  
  255.     if(!$formulaperm || $type == 2) {  
  256.         if(!$permmessage) {  
  257.             $language = lang('forum/misc');  
  258.             $search = array('regdate', 'regday', 'regip', 'lastip', 'buyercredit', 'sellercredit', 'digestposts', 'posts', 'threads', 'oltime');  
  259.             $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']);  
  260.             for($i = 1; $i <= 8; $i++) {  
  261.                 $search[] = 'extcredits'.$i;  
  262.                 $replace[] = $_G['setting']['extcredits'][$i]['title'] ? $_G['setting']['extcredits'][$i]['title'] : $language['formulaperm_extcredits'].$i;  
  263.             }  
  264.             if($profilefields) {  
  265.                 loadcache(array('fields_required', 'fields_optional'));  
  266.                 foreach($profilefields as $profilefield) {  
  267.                     $search[] = $profilefield;  
  268.                     $replace[] = !empty($_G['cache']['fields_optional']['field_'.$profilefield]) ? $_G['cache']['fields_optional']['field_'.$profilefield]['title'] : $_G['cache']['fields_required']['field_'.$profilefield]['title'];  
  269.                 }  
  270.             }  
  271.             $i = 0;$_G['forum_usermsg'] = '';  
  272.             foreach($search as $s) {  
  273.                 if(in_array($s, array('digestposts', 'posts', 'threads', 'oltime', 'extcredits1', 'extcredits2', 'extcredits3', 'extcredits4', 'extcredits5', 'extcredits6', 'extcredits7', 'extcredits8'))) {  
  274.                     $_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br />   '.$replace[$i].': '.(@eval('return intval(getuserprofile(\''.$s.'\'));')) : '';  
  275.                 } elseif(in_array($s, array('regdate', 'regip'))) {  
  276.                     $_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br />   '.$replace[$i].': '.(@eval('return $memberformula[\''.$s.'\'];')) : '';  
  277.                 }  
  278.                 $i++;  
  279.             }  
  280.             $search = array_merge($search, array('and', 'or', '>=', '<=', '=='));  
  281.             $replace = array_merge($replace, array('  <b>'.$language['formulaperm_and'].'</b>  ', '  <b>'.$language['formulaperm_or'].'</b>  ', '≥', '≤', '='));  
  282.             $_G['forum_formulamessage'] = str_replace($search, $replace, $formulatext);  
  283.         } else {  
  284.             $_G['forum_formulamessage'] = $permmessage;  
  285.         }  
  286.   
  287.         return $_G['forum_formulamessage'];  
  288.     } elseif($formulaperm && $type == 1) {  
  289.         return FALSE;  
  290.     }  
  291.     return TRUE;  
  292. }  
  293.   
  294. /** 
  295. * vip用户购买组权限是否到期 
  296. * @param $terms 期限 来源于 memberfields 表的 groupterms 字段 
  297. * @return 返回过期信息 
  298. */  
  299. function groupexpiry($terms) {  
  300.     $terms = is_array($terms) ? $terms : unserialize($terms);  
  301.     $groupexpiry = isset($terms['main']['time']) ? intval($terms['main']['time']) : 0;  
  302.     if(is_array($terms['ext'])) {  
  303.         foreach($terms['ext'] as $expiry) {  
  304.             if((!$groupexpiry && $expiry) || $expiry < $groupexpiry) {  
  305.                 $groupexpiry = $expiry;  
  306.             }  
  307.         }  
  308.     }  
  309.     return $groupexpiry;  
  310. }  
  311.   
  312. /** 
  313. * 返回当前链接的域名 
  314. * @return 返回域名 
  315. */  
  316. function site() {  
  317.     return $_SERVER['HTTP_HOST'];  
  318. }  
  319.   
  320. /** 
  321. * 显示主题分类 
  322. * @param $curtypeid - 当前被选择的类型id 
  323. * @return 返回的HTML数据 
  324. */  
  325. function typeselect($curtypeid = 0) {  
  326.     global $_G;  
  327.     if($threadtypes = $_G['forum']['threadtypes']) {  
  328.         $html = '<select name="typeid" id="typeid"><option value="0"> </option>';  
  329.         foreach($threadtypes['types'] as $typeid => $name) {  
  330.             $html .= '<option value="'.$typeid.'" '.($curtypeid == $typeid ? 'selected' : '').'>'.strip_tags($name).'</option>';  
  331.         }  
  332.         $html .= '</select>';  
  333.         return $html;  
  334.     } else {  
  335.         return '';  
  336.     }  
  337. }  
  338.   
  339. /** 
  340. * 更新管理者状态 
  341. * @param $modacton - 动作 
  342. * @param $smcols - 执行次数 
  343. */  
  344. function updatemodworks($modaction, $posts = 1) {  
  345.     global $_G;  
  346.     $today = dgmdate(TIMESTAMP, 'Y-m-d');  
  347.     if($_G['setting']['modworkstatus'] && $modaction && $posts) {  
  348.         DB::query("UPDATE ".DB::table('forum_modwork')." SET count=count+1, posts=posts+'$posts' WHERE uid='$_G[uid]' AND modaction='$modaction' AND dateline='$today'");  
  349.         if(!DB::affected_rows()) {  
  350.             DB::query("INSERT INTO ".DB::table('forum_modwork')." (uid, modaction, dateline, count, posts) VALUES ('$_G[uid]', '$modaction', '$today', 1, '$posts')");  
  351.         }  
  352.     }  
  353. }  
  354.   
  355. /** 
  356.  * 格式化一个sql语句,通常用于update操作 
  357.  * 
  358.  * @param string $fieldname 字段名称 
  359.  * @param int $position 位置 
  360.  * @param int $value 数值 0|1 
  361.  * @return string 
  362.  */  
  363. function buildbitsql($fieldname, $position, $value) {  
  364.     $t = " `$fieldname`=`$fieldname`";  
  365.     if($value) {  
  366.         $t .= ' | '.setstatus($position, 1);  
  367.     } else {  
  368.         $t .= ' & '.setstatus($position, 0);  
  369.     }  
  370.     return $t.' ';  
  371. }  
  372.   
  373. function showmessagenoperm($type, $fid, $formula = '') {  
  374.     global $_G;  
  375.     loadcache('usergroups');  
  376.     if($formula) {  
  377.         $formula = unserialize($formula);  
  378.         $permmessage = stripslashes($formula['message']);  
  379.     }  
  380.   
  381.     $usergroups = $nopermgroup = $forumnoperms = array();  
  382.     $nopermdefault = array(  
  383.         'viewperm' => array(),  
  384.         'getattachperm' => array(),  
  385.         'postperm' => array(7),  
  386.         'replyperm' => array(7),  
  387.         'postattachperm' => array(7),  
  388.     );  
  389.     $perms = array('viewperm', 'postperm', 'replyperm', 'getattachperm', 'postattachperm');  
  390.   
  391.     foreach($_G['cache']['usergroups'] as $gid => $usergroup) {  
  392.         $usergroups[$gid] = $usergroup['type'];  
  393.         $grouptype = $usergroup['type'] == 'member' ? 0 : 1;  
  394.         $nopermgroup[$grouptype][] = $gid;  
  395.     }  
  396.     if($fid == $_G['forum']['fid']) {  
  397.         $forum = $_G['forum'];  
  398.     } else {  
  399.         $forum = DB::fetch_first("SELECT * FROM ".DB::table('forum_forumfield')." WHERE fid='$fid'");  
  400.     }  
  401.   
  402.     foreach($perms as $perm) {  
  403.         $permgroups = explode("\t", $forum[$perm]);  
  404.         $membertype = $forum[$perm] ? array_intersect($nopermgroup[0], $permgroups) : TRUE;  
  405.         $forumnoperm = $forum[$perm] ? array_diff(array_keys($usergroups), $permgroups) : $nopermdefault[$perm];  
  406.         foreach($forumnoperm as $groupid) {  
  407.             $nopermtype = $membertype && $groupid == 7 ? 'login' : ($usergroups[$groupid] == 'system' || $usergroups[$groupid] == 'special' ? 'none' : ($membertype ? 'upgrade' : 'none'));  
  408.             $forumnoperms[$fid][$perm][$groupid] = array($nopermtype, $permgroups);  
  409.         }  
  410.     }  
  411.   
  412.     $v = $forumnoperms[$fid][$type][$_G['groupid']][0];  
  413.     $gids = $forumnoperms[$fid][$type][$_G['groupid']][1];  
  414.     $comma = $permgroups = '';  
  415.     if(is_array($gids)) {  
  416.         foreach($gids as $gid) {  
  417.             if($gid && $_G['cache']['usergroups'][$gid]) {  
  418.                 $permgroups .= $comma.$_G['cache']['usergroups'][$gid]['grouptitle'];  
  419.                 $comma = ', ';  
  420.             } elseif($_G['setting']['verify']['enabled'] && substr($gid, 0, 1) == 'v') {  
  421.                 $vid = substr($gid, 1);  
  422.                 $permgroups .= $comma.$_G['setting']['verify'][$vid]['title'];  
  423.                 $comma = ', ';  
  424.             }  
  425.   
  426.         }  
  427.     }  
  428.   
  429.     $custom = 0;  
  430.     if($permmessage) {  
  431.         $message = $permmessage;  
  432.         $custom = 1;  
  433.     } else {  
  434.         if($v) {  
  435.             $message = $type.'_'.$v.'_nopermission';  
  436.         } else {  
  437.             $message = 'group_nopermission';  
  438.         }  
  439.     }  
  440.   
  441.     showmessage($message, NULL, array('fid' => $fid, 'permgroups' => $permgroups, 'grouptitle' => $_G['group']['grouptitle']), array('login' => 1), $custom);  
  442. }  
  443.   
  444. /** 
  445.  * 依据 tid 或者 fid ,自动取得 $_G['forum'] 或 $_G['thread'] 数据 
  446.  * @global <type> 
  447.  */  
  448. function loadforum() {  
  449.     global $_G;  
  450.     $tid = intval(getgpc('tid'));  
  451.     $fid = getgpc('fid');  
  452.     if(!$fid && getgpc('gid')) {  
  453.         $fid = intval(getgpc('gid'));  
  454.     }  
  455.     if(!empty($_G['gp_archiver'])) {//X1.5的Archiver兼容  
  456.         if($fid) {  
  457.             dheader('location: archiver/?fid-'.$fid.'.html');  
  458.         } elseif($tid) {  
  459.             dheader('location: archiver/?tid-'.$tid.'.html');  
  460.         } else {  
  461.             dheader('location: archiver/');  
  462.         }  
  463.     }  
  464.     if(defined('IN_ARCHIVER') && $_G['setting']['archiverredirect'] && !IS_ROBOT) {  
  465.         dheader('location: ../forum.php'.($_G['mod'] ? '?mod='.$_G['mod'].(!empty($_GET['fid']) ? '&fid='.$_GET['fid'] : (!empty($_GET['tid']) ? '&tid='.$_GET['tid'] : '')) : ''));  
  466.     }  
  467.     if($_G['setting']['forumpicstyle']) {  
  468.         $_G['setting']['forumpicstyle'] = unserialize($_G['setting']['forumpicstyle']);  
  469.         empty($_G['setting']['forumpicstyle']['thumbwidth']) && $_G['setting']['forumpicstyle']['thumbwidth'] = 214;  
  470.         empty($_G['setting']['forumpicstyle']['thumbheight']) && $_G['setting']['forumpicstyle']['thumbheight'] = 160;  
  471.     } else {  
  472.         $_G['setting']['forumpicstyle'] = array('thumbwidth' => 214, 'thumbheight' => 160);  
  473.     }  
  474.     if($fid) {  
  475.         $fid = is_numeric($fid) ? intval($fid) : (!empty($_G['setting']['forumfids'][$fid]) ? $_G['setting']['forumfids'][$fid] : 0);  
  476.     }  
  477.   
  478.     $modthreadkey = isset($_G['gp_modthreadkey']) && $_G['gp_modthreadkey'] == modauthkey($tid) ? $_G['gp_modthreadkey'] : '';  
  479.     $_G['forum_auditstatuson'] = $modthreadkey ? true : false;  
  480.   
  481.     $accessadd1 = $accessadd2 = $modadd1 = $modadd2 = $metadescription = $hookscriptmessage = '';  
  482.     $adminid = $_G['adminid'];  
  483.     if($_G['uid']) {  
  484.         if($_G['member']['accessmasks']) {  
  485.             $accessadd1 = ', a.allowview, a.allowpost, a.allowreply, a.allowgetattach, a.allowgetimage, a.allowpostattach, a.allowpostimage';  
  486.             $accessadd2 = "LEFT JOIN ".DB::table('forum_access')." a ON a.uid='$_G[uid]' AND a.fid=f.fid";  
  487.         }  
  488.   
  489.         if($adminid == 3) {  
  490.             $modadd1 = ', m.uid AS ismoderator';  
  491.             $modadd2 = "LEFT JOIN ".DB::table('forum_moderator')." m ON m.uid='$_G[uid]' AND m.fid=f.fid";  
  492.         }  
  493.     }  
  494.   
  495.     if(!empty($tid) || !empty($fid)) {  
  496.   
  497.         if(!empty ($tid)) {  
  498.             $archiveid = !empty($_G['gp_archiveid']) ? intval($_G['gp_archiveid']) : null;  
  499.             $_G['thread'] = get_thread_by_tid($tid, '*', '', $archiveid);  
  500.             if(!$_G['forum_auditstatuson'] && !empty($_G['thread'])  
  501.                     && !($_G['thread']['displayorder'] >= 0 || (in_array($_G['thread']['displayorder'], array(-4,-3,-2)) && $_G['thread']['authorid'] == $_G['uid']))) {  
  502.                 $_G['thread'] = null;  
  503.             }  
  504.   
  505.             $_G['forum_thread'] = & $_G['thread'];  
  506.   
  507.             if(empty($_G['thread'])) {  
  508.                 $fid = $tid = 0;  
  509.             } else {  
  510.                 $fid = $_G['thread']['fid'];  
  511.                 $tid = $_G['thread']['tid'];  
  512.             }  
  513.         }  
  514.   
  515.         if($fid) {  
  516.             $forum = DB::fetch_first("SELECT f.fid, f.*, ff.* $accessadd1 $modadd1, f.fid AS fid  
  517.             FROM ".DB::table('forum_forum')." f  
  518.             LEFT JOIN ".DB::table("forum_forumfield")." ff ON ff.fid=f.fid $accessadd2 $modadd2  
  519.             WHERE f.fid='$fid'");  
  520.         }  
  521.   
  522.         if($forum) {  
  523.             $forum['ismoderator'] = !empty($forum['ismoderator']) || $adminid == 1 || $adminid == 2 ? 1 : 0;  
  524.             $fid = $forum['fid'];  
  525.             $gorup_admingroupids = $_G['setting']['group_admingroupids'] ? unserialize($_G['setting']['group_admingroupids']) : array('1' => '1');  
  526.   
  527.             if($forum['status'] == 3) {  
  528.                 if(!$_G['setting']['groupstatus']) {  
  529.                     showmessage('group_status_off');  
  530.                 }  
  531.                 if(!empty($forum['moderators'])) {  
  532.                     $forum['moderators'] = unserialize($forum['moderators']);  
  533.                 } else {  
  534.                     require_once libfile('function/group');  
  535.                     $forum['moderators'] = update_groupmoderators($fid);  
  536.                 }  
  537.                 if($_G['uid'] && $_G['adminid'] != 1) {  
  538.                     $forum['ismoderator'] = !empty($forum['moderators'][$_G['uid']]) ? 1 : 0;  
  539.                     $_G['adminid'] = 0;  
  540.                     if($forum['ismoderator'] || $gorup_admingroupids[$_G['groupid']]) {  
  541.                         $_G['adminid'] = $_G['adminid'] ? $_G['adminid'] : 3;  
  542.                         if(!empty($gorup_admingroupids[$_G['groupid']])) {  
  543.                             $forum['ismoderator'] = 1;  
  544.                             $_G['adminid'] = 2;  
  545.                         }  
  546.   
  547.                         $group_userperm = unserialize($_G['setting']['group_userperm']);  
  548.                         if(is_array($group_userperm)) {  
  549.                             $_G['group'] = array_merge($_G['group'], $group_userperm);  
  550.                             $_G['group']['allowmovethread'] = $_G['group']['allowcopythread'] = $_G['group']['allowedittypethread']= 0;  
  551.                         }  
  552.                     }  
  553.                 }  
  554.             }  
  555.             foreach(array('threadtypes', 'threadsorts', 'creditspolicy', 'modrecommend') as $key) {  
  556.                 $forum[$key] = !empty($forum[$key]) ? unserialize($forum[$key]) : array();  
  557.                 if(!is_array($forum[$key])) {  
  558.                     $forum[$key] = array();  
  559.                 }  
  560.             }  
  561.   
  562.             if($forum['status'] == 3) {  
  563.                 $_G['isgroupuser'] = 0;  
  564.                 $_G['basescript'] = 'group';  
  565.                 if(empty($forum['level'])) {  
  566.                     $levelid = DB::result_first("SELECT levelid FROM ".DB::table('forum_grouplevel')." WHERE creditshigher<='$forum[commoncredits]' AND '$forum[commoncredits]'<creditslower LIMIT 1");  
  567.                     $forum['level'] = $levelid;  
  568.                     DB::query("UPDATE ".DB::table('forum_forum')." SET level='$levelid' WHERE fid='$fid'");  
  569.                 }  
  570.                 loadcache('grouplevels');  
  571.                 $grouplevel = $_G['grouplevels'][$forum['level']];  
  572.                 if(!empty($grouplevel['icon'])) {  
  573.                     $valueparse = parse_url($grouplevel['icon']);  
  574.                     if(!isset($valueparse['host'])) {  
  575.                         $grouplevel['icon'] = $_G['setting']['attachurl'].'common/'.$grouplevel['icon'];  
  576.                     }  
  577.                 }  
  578.   
  579.                 $group_postpolicy = $grouplevel['postpolicy'];  
  580.                 if(is_array($group_postpolicy)) {  
  581.                     $forum = array_merge($forum, $group_postpolicy);  
  582.                 }  
  583.                 $forum['allowfeed'] = $_G['setting']['group_allowfeed'];  
  584.                 if($_G['uid']) {  
  585.                     if(!empty($forum['moderators'][$_G['uid']])) {  
  586.                         $_G['isgroupuser'] = 1;  
  587.                     } else {  
  588.                         $_G['isgroupuser'] = DB::result_first("SELECT level FROM ".DB::table('forum_groupuser')." WHERE fid='$fid' AND uid='$_G[uid]' LIMIT 1");  
  589.                         if($_G['isgroupuser'] <= 0 && empty($forum['ismoderator'])) {  
  590.                             $_G['group']['allowrecommend'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowrecommend'] = 0;  
  591.                             $_G['group']['allowcommentpost'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowcommentpost'] = 0;  
  592.                             $_G['group']['allowcommentitem'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowcommentitem'] = 0;  
  593.                             $_G['group']['raterange'] = $_G['cache']['usergroup_'.$_G['groupid']]['raterange'] = array();  
  594.                             $_G['group']['allowvote'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowvote'] = 0;  
  595.                         } else {  
  596.                             $_G['isgroupuser'] = 1;  
  597.                         }  
  598.                     }  
  599.                 }  
  600.             }  
  601.         } else {  
  602.             $fid = 0;  
  603.         }  
  604.     }  
  605.   
  606.     $_G['fid'] = $fid;  
  607.     $_G['tid'] = $tid;  
  608.     $_G['forum'] = &$forum;  
  609.     $_G['current_grouplevel'] = &$grouplevel;  
  610.   
  611.     if(isset($_G['cookie']['widthauto']) && $_G['setting']['switchwidthauto'] && empty($_G['forum']['widthauto'])) {  
  612.         $_G['forum_widthauto'] = $_G['cookie']['widthauto'] > 0;  
  613.     } else {  
  614.         $_G['forum_widthauto'] = empty($_G['forum']['widthauto']) ? !$_G['setting']['allowwidthauto'] : $_G['forum']['widthauto'] > 0;  
  615.         if(!empty($_G['forum']['widthauto'])) {  
  616.             $_G['setting']['switchwidthauto'] = 0;  
  617.         }  
  618.     }  
  619. }  
  620.   
  621. /** 
  622.  * 
  623.  * @param <type> $tid 主题id 
  624.  * @param <type> $fields 取用主题表的字段, 默认 "*" 
  625.  * @param <type> $addcondiction 除 tid='$tid' 以外的其他条件, 默认空 
  626.  * @param <type> $forcetableid 是否强制只从某个分表中取得 thread, 默认 null 
  627.  * @return array 当有合法数值返回的时候, 将自动追加2个key值threadtable, posttable 记录此主题使用的分表名称 
  628.  */  
  629. function get_thread_by_tid($tid, $fields = '*', $addcondiction = '', $forcetableid = null) {  
  630.     global $_G;  
  631.   
  632.     $ret = array();  
  633.     if(!is_numeric($tid)) {  
  634.         return $ret;  
  635.     }  
  636.     loadcache('threadtableids');  
  637.     $threadtableids = array(0);  
  638.     if(!empty($_G['cache']['threadtableids'])) {  
  639.         if($forcetableid === null || ($forcetableid > 0 && !in_array($forcetableid, $_G['cache']['threadtableids']))) {  
  640.             $threadtableids = array_merge($threadtableids, $_G['cache']['threadtableids']);  
  641.         } else {  
  642.             $threadtableids = array(intval($forcetableid));  
  643.         }  
  644.     }  
  645.   
  646.     foreach($threadtableids as $tableid) {  
  647.         $table = $tableid > 0 ? "forum_thread_{$tableid}" : 'forum_thread';  
  648.         $ret = DB::fetch_first("SELECT $fields FROM ".DB::table($table)." WHERE tid='$tid' $addcondiction LIMIT 1");  
  649.         if($ret) {  
  650.             $ret['threadtable'] = $table;  
  651.             $ret['threadtableid'] = $tableid;  
  652.             $ret['posttable'] = 'forum_post'.($ret['posttableid'] ? '_'.$ret['posttableid'] : '');  
  653.             break;  
  654.         }  
  655.     }  
  656.   
  657.     if(!is_array($ret)) {  
  658.         $ret = array();  
  659.     }  
  660.   
  661.     return $ret;  
  662. }  
  663.   
  664. /** 
  665.  * 依据 pid 和 其他条件,从某个post数据表中取回post信息 
  666.  * @global <type> $_G 
  667.  * @param <type> $pid 帖子id 
  668.  * @param <type> $fields 帖子表字段,默认 * 
  669.  * @param <type> $addcondiction 其他条件,默认 空 
  670.  * @param <type> $forcetable 是否强制使用某个分表, 支持: 使用id: 0 1 2 或者 p(主表), a(副表) 或者直接制定表名 forum_post_1 
  671.  * @return array 
  672.  */  
  673. function get_post_by_pid($pid, $fields = '*', $addcondiction = '', $forcetable = null) {  
  674.     global $_G;  
  675.   
  676.     $ret = array();  
  677.     if(!is_numeric($pid)) {  
  678.         return $ret;  
  679.     }  
  680.   
  681.     loadcache('posttable_info');  
  682.   
  683.     $posttableids = array(0);  
  684.     if($_G['cache']['posttable_info']) {  
  685.         if(isset($forcetable)) {  
  686.             if(is_numeric($forcetable) && array_key_exists($forcetable, $_G['cache']['posttable_info'])) {  
  687.                 $posttableids[] = $forcetable;  
  688.             } elseif(substr($forcetable, 0, 10) == 'forum_post') {  
  689.                 $posttableids[] = $forcetable;  
  690.             }  
  691.         } else {  
  692.             $posttableids = array_keys($_G['cache']['posttable_info']);  
  693.         }  
  694.     }  
  695.   
  696.     foreach ($posttableids as $id) {  
  697.         $table = empty($id) ? 'forum_post' : (is_numeric($id) ? 'forum_post_'.$id : $id);  
  698.         $ret = DB::fetch_first("SELECT $fields FROM ".DB::table($table)." WHERE pid='$pid' $addcondiction LIMIT 1");  
  699.         if($ret) {  
  700.             $ret['posttable'] = $table;  
  701.             break;  
  702.         }  
  703.     }  
  704.   
  705.     if(!is_array($ret)) {  
  706.         $ret = array();  
  707.     }  
  708.   
  709.     return $ret;  
  710. }  
  711.   
  712. function set_rssauth() {  
  713.     global $_G;  
  714.     if($_G['setting']['rssstatus'] && $_G['uid']) {  
  715.         $auth = authcode($_G['uid']."\t".($_G['fid'] ? $_G['fid'] : '').  
  716.         "\t".substr(md5($_G['member']['password']), 0, 8), 'ENCODE', md5($_G['config']['security']['authkey']));  
  717.     } else {  
  718.         $auth = '0';  
  719.     }  
  720.     $_G['rssauth'] = rawurlencode($auth);  
  721. }  
  722.   
  723. function my_thread_log($opt, $data) {  
  724.     global $_G;  
  725.     $my_search_data = $_G['setting']['my_search_data'];  
  726.     if ($my_search_data && !is_array($my_search_data)) {  
  727.         $my_search_data = unserialize($my_search_data);  
  728.     }  
  729.     if(!$_G['setting']['my_search_data']) return;  
  730.     $data['action'] = $opt;  
  731.     $data['dateline'] = time();  
  732.     DB::insert('forum_threadlog', $data, false, true);  
  733. }  
  734.   
  735. function my_post_log($opt, $data) {  
  736.     global $_G;  
  737.     $my_search_data = $_G['setting']['my_search_data'];  
  738.     if ($my_search_data && !is_array($my_search_data)) {  
  739.         $my_search_data = unserialize($my_search_data);  
  740.     }  
  741.     if(!$_G['setting']['my_search_data']) return;  
  742.     $data['action'] = $opt;  
  743.     $data['dateline'] = time();  
  744.     DB::insert('forum_postlog', $data, false, true);  
  745. }  
  746.   
  747. function rssforumperm($forum) {  
  748.     $is_allowed = $forum['type'] != 'group' && (!$forum['viewperm'] || ($forum['viewperm'] && forumperm($forum['viewperm'], 7)));  
  749.     return $is_allowed;  
  750. }  
  751.   
  752. /** 
  753.  *  upload_icon_banner上传群组/版块图片 
  754.  *  $forum中 fid和status 必须存在,status判断是群组还是版块图片,以存入不同目录 
  755.  *  $file上传时的$_FILES 
  756.  *  $type icon 或 banner 
  757.  */  
  758. function upload_icon_banner(&$data, $file, $type) {  
  759.     global $_G;  
  760.     $data['extid'] = empty($data['extid']) ? $data['fid'] : $data['extid'];  
  761.     if(empty($data['extid'])) return '';  
  762.   
  763.     if($data['status'] == 3 && $_G['setting']['group_imgsizelimit']) {  
  764.         $file['size'] > ($_G['setting']['group_imgsizelimit'] * 1024) && showmessage('file_size_overflow', '', array('size' => $_G['setting']['group_imgsizelimit'] * 1024));  
  765.     }  
  766.     require_once libfile('class/upload');  
  767.     $upload = new discuz_upload();  
  768.     $uploadtype = $data['status'] == 3 ? 'group' : 'common';  
  769.   
  770.     if(!$upload->init($file, $uploadtype, $data['extid'], $type)) {  
  771.         return false;  
  772.     }  
  773.   
  774.     if(!$upload->save()) {  
  775.         if(!defined('IN_ADMINCP')) {  
  776.             showmessage($upload->errormessage());  
  777.         } else {  
  778.             cpmsg($upload->errormessage(), '', 'error');  
  779.         }  
  780.     }  
  781.     if($data['status'] == 3 && $type == 'icon') {  
  782.         require_once libfile('class/image');  
  783.         $img = new image;  
  784.         $img->Thumb($upload->attach['target'], './'.$uploadtype.'/'.$upload->attach['attachment'], 48, 48, 'fixwr');  
  785.     }  
  786.     return $upload->attach['attachment'];  
  787. }  
  788.   
  789. /** 
  790.     用于 archiver 环境下的分页 
  791. */  
  792. function arch_multi($total, $perpage, $page, $link) {  
  793.     $pages = @ceil($total / $perpage) + 1;  
  794.     $pagelink = '';  
  795.     if($pages > 1) {  
  796.         $pagelink .= lang('forum/archiver', 'page') . ": \n";  
  797.         $pagestart = $page - 10 < 1 ? 1 : $page - 10;  
  798.         $pageend = $page + 10 >= $pages ? $pages : $page + 10;  
  799.         for($i = $pagestart; $i < $pageend; $i++) {  
  800.             $pagelink .= ($i == $page ? "<strong>[$i]</strong>" : "<a href=\"$link&page=$i\">$i</a>")." \n";  
  801.         }  
  802.     }  
  803.     return $pagelink;  
  804. }  
  805.   
  806. /** 
  807.     archiver 模板路径 
  808. */  
  809. function loadarchiver($path) {  
  810.     global $_G;  
  811.     if(!$_G['setting']['archiver']) {  
  812.         require_once DISCUZ_ROOT . "./source/archiver/common/header.php";  
  813.         echo '<div id="content">'.lang('message', 'forum_archiver_disabled').'</div>';  
  814.         require_once DISCUZ_ROOT . "./source/archiver/common/footer.php";  
  815.         exit;  
  816.     }  
  817.     $filename = $path . '.php';  
  818.     return DISCUZ_ROOT . "./source/archiver/$filename";  
  819. }  
  820. /** 
  821.  *  update_threadpartake 按参与人次更新主题热度 
  822.  */  
  823. function update_threadpartake($tid) {  
  824.     global $_G;  
  825.     if($_G['uid'] && $tid) {  
  826.         if($_G['setting']['heatthread']['period']) {  
  827.             $partaked = DB::result_first("SELECT uid FROM ".DB::table('forum_threadpartake')." WHERE tid='$tid' AND uid='$_G[uid]'");  
  828.             if(!$partaked) {  
  829.                 DB::query("INSERT INTO ".DB::table('forum_threadpartake')." (tid, uid, dateline) VALUES ('$tid', '$_G[uid]', ".TIMESTAMP.")");  
  830.                 DB::query("UPDATE ".DB::table('forum_thread')." SET heats=heats+1 WHERE tid='$tid'", 'UNBUFFERED');  
  831.             }  
  832.         } else {  
  833.             DB::query("UPDATE ".DB::table('forum_thread')." SET heats=heats+1 WHERE tid='$tid'", 'UNBUFFERED');  
  834.   
  835.         }  
  836.     }  
  837. }  
  838.   
  839. /** 
  840.  * 获取帖子封面地址 
  841.  * 
  842.  * @param int $tid 
  843.  * @param int $cover 负数时表示是远程,正数为本地 没有值表示只取filename 
  844.  */  
  845. function getthreadcover($tid, $cover = 0, $getfilename = 0) {  
  846.     global $_G;  
  847.     if(empty($tid)) {  
  848.         return '';  
  849.     }  
  850.     $coverpath = '';  
  851.     $covername = 'threadcover/'.substr(md5($tid), 0, 2).'/'.substr(md5($tid), 2, 2).'/'.$tid.'.jpg';  
  852.     if($getfilename) {  
  853.         return $covername;  
  854.     }  
  855.     if($cover) {  
  856.         $coverpath = ($cover < 0 ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']).'forum/'.$covername;  
  857.     }  
  858.     return $coverpath;  
  859. }  
  860.   
  861. /** 
  862.  *添加主题标签 
  863.  * 
  864.  * @param <type> $tags 
  865.  * @param <type> $itemid 
  866.  */  
  867. function addthreadtag($tags, $itemid , $typeid = 'tid') {  
  868.     global $_G;  
  869.   
  870.     if($tags == '') {  
  871.         return;  
  872.     }  
  873.   
  874.     $tags = str_replace(array(chr(0xa3).chr(0xac), chr(0xa1).chr(0x41), chr(0xef).chr(0xbc).chr(0x8c)), ',', censor($tags));  
  875.     if(strexists($tags, ',')) {  
  876.         $tagarray = array_unique(explode(',', $tags));  
  877.     } else {  
  878.         $langcore = lang('core');  
  879.         $tags = str_replace($langcore['fullblankspace'], ' ', $tags);  
  880.         $tagarray = array_unique(explode(' ', $tags));  
  881.     }  
  882.     $tagcount = 0;  
  883.     foreach($tagarray as $tagname) {  
  884.         $tagname = trim($tagname);  
  885.         if(preg_match('/^([\x7f-\xff_-]|\w|\s){3,20}$/', $tagname)) {  
  886.             $result = DB::fetch_first("SELECT tagid, status FROM ".DB::table('common_tag')." WHERE tagname='$tagname'");  
  887.             if($result['tagid']) {  
  888.                 if(!$result['status']) {  
  889.                     $tagid = $result['tagid'];  
  890.                 }  
  891.             } else {  
  892.                 DB::query("INSERT INTO ".DB::table('common_tag')." (tagname, status) VALUES ('$tagname', '0')");  
  893.                 $tagid = DB::insert_id();  
  894.             }  
  895.             if($tagid) {  
  896.                 DB::query("INSERT INTO ".DB::table('common_tagitem')." (tagid, tagname, itemid, idtype) VALUES ('$tagid', '$tagname', '$itemid', '$typeid')");  
  897.                 $tagcount++;  
  898.                 $tagstr .= $tagid.','.$tagname.'\t';  
  899.             }  
  900.             if($tagcount > 4) {  
  901.                 unset($tagarray);  
  902.                 break;  
  903.             }  
  904.         }  
  905.     }  
  906.     return $tagstr;  
  907. }  
  908.   
  909. /** 
  910.  *修改主题标签 
  911.  * 
  912.  * @param <type> $tags 
  913.  * @param <type> $itemid 
  914.  * @return <type> 
  915.  */  
  916. function modthreadtag($tags, $itemid) {  
  917.     global $_G;  
  918.   
  919.     $thread = & $_G['forum_thread'];  
  920.     $posttable = $thread['posttable'];  
  921.     $tagstr = DB::result_first("SELECT tags FROM ".DB::table($posttable)." WHERE tid='$itemid' AND first=1");  
  922.   
  923.     $threadtagarray = $threadtagidarray = $threadtagarraynew = array();  
  924.     $query = DB::query("SELECT tagid, tagname FROM ".DB::table('common_tagitem')." WHERE idtype='tid' AND itemid='$itemid'");  
  925.     while($result = DB::fetch($query)) {  
  926.         $threadtagarray[] = $result['tagname'];  
  927.         $threadtagidarray[] = $result['tagid'];  
  928.     }  
  929.   
  930.     $tags = str_replace(array(chr(0xa3).chr(0xac), chr(0xa1).chr(0x41), chr(0xef).chr(0xbc).chr(0x8c)), ',', censor($tags));  
  931.     if(strexists($tags, ',')) {  
  932.         $tagarray = array_unique(explode(',', $tags));  
  933.     } else {  
  934.         $langcore = lang('core');  
  935.         $tags = str_replace($langcore['fullblankspace'], ' ', $tags);  
  936.         $tagarray = array_unique(explode(' ', $tags));  
  937.     }  
  938.   
  939.     $tagcount = 0;  
  940.     foreach($tagarray as $tagname) {  
  941.         $tagname = trim($tagname);  
  942.         if(preg_match('/^([\x7f-\xff_-]|\w|\s){3,20}$/', $tagname)) {  
  943.             $threadtagarraynew[] = $tagname;  
  944.             if(!in_array($tagname, $threadtagarray)) {  
  945.                 $result = DB::fetch_first("SELECT tagid, status FROM ".DB::table('common_tag')." WHERE tagname='$tagname'");  
  946.                 if($result['tagid']) {  
  947.                     if(!$result['status']) {  
  948.                         $tagid = $result['tagid'];  
  949.                     }  
  950.                 } else {  
  951.                     DB::query("INSERT INTO ".DB::table('common_tag')." (tagname, status) VALUES ('$tagname', '0')");  
  952.                     $tagid = DB::insert_id();  
  953.                 }  
  954.                 if($tagid) {  
  955.                     DB::query("INSERT INTO ".DB::table('common_tagitem')." (tagid, tagname, itemid, idtype) VALUES ('$tagid', '$tagname', '$itemid', 'tid')");  
  956.                     $tagstr = $tagstr.$tagid.','.$tagname.'\t';  
  957.                 }  
  958.             }  
  959.         }  
  960.         $tagcount++;  
  961.         if($tagcount > 4) {  
  962.             unset($tagarray);  
  963.             break;  
  964.         }  
  965.     }  
  966.     foreach($threadtagarray as $key => $tagname) {  
  967.         if(!in_array($tagname, $threadtagarraynew)) {  
  968.             DB::query("DELETE FROM  ".DB::table('common_tagitem')." WHERE idtype='tid' AND itemid = '$itemid' AND tagname='$tagname'");  
  969.             $tagid = $threadtagidarray[$key];  
  970.             $tagstr = str_replace("$tagid,$tagname\t", '', $tagstr);  
  971.         }  
  972.     }  
  973.     return $tagstr;  
  974. }  
  975.   
  976. /** 
  977.  * 将未使用的附件转为已使用状态 
  978.  * @param <int> $aid 
  979.  * @param <int> $tid 
  980.  * @param <pid> $pid 
  981.  */  
  982. function convertunusedattach($aid, $tid, $pid) {  
  983.     if(!$aid) {  
  984.         return;  
  985.     }  
  986.     global $_G;  
  987.     $attach = DB::fetch_first("SELECT * FROM ".DB::table('forum_attachment_unused')." WHERE aid='$aid' AND uid='$_G[uid]'");  
  988.     if(!$attach) {  
  989.         return;  
  990.     }  
  991.     $attach = daddslashes($attach);  
  992.     $attach['tid'] = $tid;  
  993.     $attach['pid'] = $pid;  
  994.     DB::insert(getattachtablebytid($tid), $attach, false);  
  995.     DB::update('forum_attachment', array('tid' => $tid, 'pid' => $pid, 'tableid' => getattachtableid($tid)), "aid='$attach[aid]'");  
  996.     DB::delete('forum_attachment_unused', "aid='$attach[aid]'");  
  997. }  
  998.   
  999. /** 
  1000.  * 更新附件的 TID 值 
  1001.  * @param <string> $where 条件 
  1002.  * @param <int> $oldtid 旧 TID 
  1003.  * @param <int> $newtid  新 TID 
  1004.  */  
  1005. function updateattachtid($where, $oldtid, $newtid) {  
  1006.     $oldattachtable = getattachtablebytid($oldtid);  
  1007.     $newattachtable = getattachtablebytid($newtid);  
  1008.     if($oldattachtable != $newattachtable) {  
  1009.         $query = DB::query("SELECT * FROM ".DB::table($oldattachtable)." WHERE $where");  
  1010.         while($attach = DB::fetch($query)) {  
  1011.             $attach = daddslashes($attach);  
  1012.             $attach['tid'] = $newtid;  
  1013.             DB::insert($newattachtable, $attach);  
  1014.         }  
  1015.         DB::delete($oldattachtable, $where);  
  1016.     }  
  1017.     DB::query("UPDATE ".DB::table('forum_attachment')." SET tid='$newtid',tableid='".getattachtableid($newtid)."' WHERE $where");  
  1018. }  
  1019.   
  1020. /** 
  1021.  * 更新POST 
  1022.  * @global  $_G 
  1023.  * @param array $data 更新的数据 
  1024.  * @param string/array $condition 条件 
  1025.  * @param bool $unbuffered 是否即时返回 
  1026.  * @param int $posttableid post分表ID 
  1027.  * @return int 返回的更新的记录数 
  1028.  */  
  1029. function updatepost($data, $condition, $unbuffered = false, $posttableid = false) {  
  1030.     global $_G;  
  1031.     loadcache('posttableids');  
  1032.     $affected_rows = 0;  
  1033.     if(!empty($_G['cache']['posttableids'])) {  
  1034.         $posttableids = $posttableid !== false && in_array($posttableid, $_G['cache']['posttableids']) ? array($posttableid) : $_G['cache']['posttableids'];  
  1035.     } else {  
  1036.         $posttableids = array('0');  
  1037.     }  
  1038.     foreach($posttableids as $id) {  
  1039.         DB::update(getposttable($id), $data, $condition, $unbuffered);  
  1040.         $affected_rows += DB::affected_rows();  
  1041.     }  
  1042.     return $affected_rows;  
  1043. }  
  1044.   
  1045. /** 
  1046.     插入一个帖子 
  1047. */  
  1048. function insertpost($data) {  
  1049.     if(isset($data['tid'])) {  
  1050.         $tableid = DB::result_first("SELECT posttableid FROM ".DB::table('forum_thread')." WHERE tid='{$data['tid']}'");  
  1051.     } else {  
  1052.         $tableid = $data['tid'] = 0;  
  1053.     }  
  1054.     $pid = DB::insert('forum_post_tableid', array('pid' => null), true);  
  1055.   
  1056.     if(!$tableid) {  
  1057.         $tablename = 'forum_post';  
  1058.     } else {  
  1059.         $tablename = "forum_post_$tableid";  
  1060.     }  
  1061.   
  1062.     $data = array_merge($data, array('pid' => $pid));  
  1063.   
  1064.     DB::insert($tablename, $data);  
  1065.     if($pid % 1024 == 0) {  
  1066.         DB::delete('forum_post_tableid', "pid<$pid");  
  1067.     }  
  1068.     save_syscache('max_post_id', $pid);  
  1069.     return $pid;  
  1070. }  
  1071.   
  1072. ?>  

posted on 2017-09-09 15:55  alleyonine  阅读(1198)  评论(0编辑  收藏  举报

导航