user_big_update(array('uid' => $uids), $update);
}
!empty($operate_create) && function_exists('operate_big_insert') and operate_big_insert($operate_create);
return TRUE;
}
// 大数据量容易超时 删除用户时使用,删除主题 回复 栏目统计 附件 全站统计
function well_thread_delete_all_by_uid($uid)
{
// 用户主题数
$user = user_read_cache($uid);
set_time_limit(0);
// 删除所有回复
if ($user['comments']) {
$postist = comment_pid_find_by_uid($uid, 1, $user['comments'], FALSE);
$pidarr = array();
foreach ($postist as $val) {
$pidarr[] = $val['pid'];
}
unset($postist);
!empty($pidarr) and comment_delete_by_pids($pidarr);
}
if ($user['articles']) {
// 如果主题、附件和回复数量太大会超时
$tidlist = thread_tid_find_by_uid($uid, 1, $user['articles'], FALSE, 'tid', array('tid'));
$tidarr = array();
foreach ($tidlist as $val) {
$tidarr[] = $val['tid'];
}
unset($tidlist);
!empty($tidarr) and well_thread_delete_all($tidarr);
}
return TRUE;
}
// 搜索标题
function well_thread_find_by_keyword($keyword, $d = NULL)
{
if (empty($keyword)) return NULL;
$db = $_SERVER['db'];
$d = $d ? $d : $db;
if (!$d) return FALSE;
$threadlist = db_sql_find("SELECT * FROM `{$d->tablepre}website_thread` WHERE subject LIKE '%$keyword%' LIMIT 60;", 'tid', $d);
if ($threadlist) {
$threadlist = arrlist_multisort($threadlist, 'tid', FALSE);
foreach ($threadlist as &$thread) {
well_thread_format($thread);
// 关键词标色
//$thread['subject'] = comment_highlight_keyword($thread['subject'], $keyword);
}
}
return $threadlist;
}
// 查找 最后评论 lastpid
function well_thread_find_lastpid($tid)
{
$arr = comment_pid_read(array('tid' => $tid), array('pid' => -1), array('pid'));
$lastpid = empty($arr) ? 0 : $arr['pid'];
return $lastpid;
}
// 更新最后的 uid
function well_thread_update_last($tid)
{
if (empty($tid)) return FALSE;
$lastpid = well_thread_find_lastpid($tid);
if (empty($lastpid)) return FALSE;
$lastpost = comment_read($lastpid);
if (empty($lastpost)) return FALSE;
$r = well_thread_update($tid, array('lastuid' => $lastpost['uid']));
return $r;
}
function well_thread_maxid()
{
$n = db_maxid('website_thread', 'tid');
return $n;
}
// 主题状态 0:通过 1~9 审核:1待审核 10~19:10退稿 11逻辑删除
function well_thread_format(&$thread)
{
global $gid, $uid, $forumlist;
$conf = _SERVER('conf');
if (empty($thread)) return;
$thread['create_date_fmt'] = humandate($thread['create_date']);
$thread['last_date_fmt'] = humandate($thread['last_date']);
$thread['create_date_fmt_ymd'] = date('Y-m-d', $thread['create_date']);
$thread['last_date_fmt_ymd'] = date('Y-m-d', $thread['last_date']);
$user = user_read_cache($thread['uid']);
$onlinelist = online_user_list_cache();
$user['online_status'] = isset($onlinelist[$user['uid']]) ? 1 : 0;
$thread['username'] = $user['username'];
$thread['user_avatar_url'] = array_value($user, 'avatar_url');
$thread['user'] = user_safe_info($user);
unset($user);
$forum = array_value($forumlist, $thread['fid']);
$thread['forum_name'] = array_value($forum, 'name');
$thread['forum_url'] = array_value($forum, 'url');
if ($thread['last_date'] == $thread['create_date']) {
$thread['last_date_fmt'] = '';
$thread['lastuid'] = 0;
$thread['lastusername'] = '';
} else {
$lastuser = $thread['lastuid'] ? user_read_cache($thread['lastuid']) : array();
$thread['lastusername'] = $thread['lastuid'] ? $lastuser['username'] : lang('guest');
$thread['lastuser'] = $thread['lastuid'] ? user_safe_info($lastuser) : array();
unset($lastuser);
}
$thread['url'] = url('read-' . $thread['tid'], '', FALSE);
if ($conf['url_rewrite_on'] > 1) {
$forum['well_alias'] AND $thread['url'] = url(urlencode($forum['well_alias']) . '-' . $thread['tid'], '', FALSE);
}
$thread['user_url'] = url('user-' . $thread['uid']);
$thread['sticky_class'] = '';
if ($thread['sticky'] > 0) {
if (1 == $thread['sticky']) {
$thread['sticky_class'] = 'success';
} elseif (2 == $thread['sticky']) {
$thread['sticky_class'] = 'warning';
} elseif (3 == $thread['sticky']) {
$thread['sticky_class'] = 'danger';
}
}
$nopic = view_path() . 'img/nopic.png';
if ($thread['icon']) {
$attach_dir_save_rule = array_value($conf, 'attach_dir_save_rule', 'Ym');
$day = date($attach_dir_save_rule, $thread['icon']);
$thread_format_icon_default = 1;
if (1 == $thread_format_icon_default) {
// 本地文件绝对路径
$destfile = $conf['upload_path'] . 'thumbnail/' . $day . '/' . $thread['uid'] . '_' . $thread['tid'] . '_' . $thread['icon'] . '.jpeg';
// 本地
$thread['icon_fmt'] = is_file($destfile) ? file_path($thread['attach_on']) . 'thumbnail/' . $day . '/' . $thread['uid'] . '_' . $thread['tid'] . '_' . $thread['icon'] . '.jpeg' : $nopic;
if (1 == $conf['attach_on'] && 1 == $thread['attach_on']) {
// 云储存
$thread['icon_fmt'] = file_path($thread['attach_on']) . 'thumbnail/' . $day . '/' . $thread['uid'] . '_' . $thread['tid'] . '_' . $thread['icon'] . '.jpeg';
} elseif (2 == $conf['attach_on'] && 2 == $thread['attach_on']) {
// 图床 未上传成功 本地图片在的话使用本地,不在则默认
$thread['icon_fmt'] = $thread['image_url'] ? $thread['image_url'] : $thread['icon_fmt'];
}
}
} else {
$thread['icon_fmt'] = $nopic;
}
if (empty($thread['icon'])) {
$thread['icon'] = 1;
$thread['icon_fmt'] = $conf['path'].'img/'.rand(1,92).'.jpg';
}
// 回复页面
$thread['pages'] = ceil($thread['posts'] / $conf['comment_pagesize']);
$thread['tag_fmt'] = $thread['tag'] ? xn_json_decode($thread['tag']) : '';
// 权限判断
$thread['allowupdate'] = ($uid == $thread['uid']) || forum_access_mod($thread['fid'], $gid, 'allowupdate');
$thread['allowdelete'] = (group_access($gid, 'allowuserdelete') and $uid == $thread['uid']) || forum_access_mod($thread['fid'], $gid, 'allowdelete');
$thread['allowtop'] = forum_access_mod($thread['fid'], $gid, 'allowtop');
if (isset($forum['model']) && 0 == $forum['model']) {
$thread['url_update'] = $thread['allowupdate'] ? url('home-content', array('op'=>'update','tid' => $thread['tid'])) : '';
}
$thread = well_thread_safe_info($thread);
}
function well_thread_format_last_date(&$thread)
{
if ($thread['last_date'] != $thread['create_date']) {
$thread['last_date_fmt'] = humandate($thread['last_date']);
} else {
$thread['create_date_fmt'] = humandate($thread['create_date']);
}
}
// 对 $threadlist 权限过滤
function well_thread_list_access_filter(&$threadlist, $gid)
{
global $forumlist;
if (empty($threadlist)) return NULL;
foreach ($threadlist as $tid => $thread) {
if (empty($forumlist[$thread['fid']]['accesson'])) continue;
if ($thread['sticky'] > 0) continue;
if (!forum_access_user($thread['fid'], $gid, 'allowread')) {
unset($threadlist[$tid]);
}
}
}
function well_thread_safe_info($thread)
{
unset($thread['userip'], $thread['user']['threads'], $thread['user']['posts'], $thread['user']['credits'], $thread['user']['golds'], $thread['user']['money']);
empty($thread['user']) || $thread['user'] = user_safe_info($thread['user']);
return $thread;
}
// 过滤安全数据
function well_thread_filter(&$val)
{
unset($val['userip'], $val['fid'], $val['flagid'], $val['type'], $val['user'], $val['create_date']);
}
//------------------------ 其他方法 ------------------------
// 集合主题tid,统一拉取,避免多次查询thread表
function thread_unified_pull($arr)
{
global $gid, $fid;
$stickylist = array_value($arr, 'stickylist', array());
$tidlist = array_value($arr, 'tidlist', array());
//$fid = array_value($arr, 'fid');
// 合并过滤空数组
//$tidlist = array_filter($stickylist + $tidlist);
$tidarrlist = $tidlist = $stickylist + $tidlist;
// 版块自定义
list($flaglist, $flagtids) = flag_thread_by_fid($fid);
empty($flagtids) || $tidarrlist += $flagtids;
unset($flagtids);
// 在这之前合并所有二维数组 tid值为键/array('tid值' => tid值)
$tidarr = empty($tidarrlist) ? array() : arrlist_values($tidarrlist, 'tid');
// 在这之前使用array_merge()前合并所有一维数组 tid/array(1,2,3)
if (empty($tidarr)) return NULL;
$tidarr = array_unique($tidarr);
// 主题相关统一遍历后再归类
$arrlist = well_thread_find($tidarr, count($tidarr));
// 过滤没有权限访问的主题 / filter no permission thread
well_thread_list_access_filter($arrlist, $gid);
$threadlist = array();
foreach ($arrlist as $_tid => &$_thread) {
$_thread = well_thread_safe_info($_thread);
// 归类列表数据
isset($tidlist[$_thread['tid']]) and $threadlist[$_tid] = $_thread;
// flag thread
if (!empty($flaglist)) {
foreach ($flaglist as $key => $val) {
if (isset($val['tids']) && in_array($_thread['tid'], $val['tids'])) {
$flaglist[$key]['list'][array_search($_thread['tid'], $val['tids'])] = $_thread;
ksort($flaglist[$key]['list']);
}
}
}
}
// 按之前tidlist排序
$threadlist = array2_sort_key($threadlist, $tidlist, 'tid');
unset($arrlist, $tidlist);
$arr = array('threadlist' => $threadlist, 'flaglist' => $flaglist);
return $arr;
}
// read.php 详情页其他主题调用,集合tid统一拉取数据,最后归类
function thread_other_pull($thread)
{
global $forumlist, $gid;
$fid = array_value($thread, 'fid');
$forum = array_value($forumlist, $fid);
if (empty($forum)) return NULL;
//$tid = array_value($thread, 'tid');
//$tag_fmt = array_value($thread, 'tag_fmt');
$arrlist = array();
$tidlist = array();
// 版块自定义
list($flaglist, $flagtids) = flag_thread_by_fid($fid);
empty($flagtids) || $tidlist += $flagtids;
unset($flagtids);
$randoms = thread_random_get($forum['randoms']);
// 在这之前合并所有二维数组 tid值为键/array('tid值' => tid值)
$tidarr = empty($tidlist) ? array() : arrlist_values($tidlist, 'tid');
// 在这之前使用array_merge()前合并所有一维数组 tid/array(1,2,3)
$randoms AND $tidarr = array_merge($tidarr, $randoms);
if (empty($tidarr)) return NULL;
$tidarr = array_unique($tidarr);
// 主题相关统一遍历后再归类
$threadlist = well_thread_find($tidarr, count($tidarr));
// 过滤没有权限访问的主题 / filter no permission thread
well_thread_list_access_filter($threadlist, $gid);
foreach ($threadlist as &$_thread) {
$_thread = well_thread_safe_info($_thread);
if ($randoms) {
in_array($_thread['tid'], $randoms) AND $arrlist['randomlist'][$_thread['tid']] = $_thread;
}
// flag thread
if (!empty($flaglist)) {
foreach ($flaglist as $key => $val) {
if (isset($val['tids']) && in_array($_thread['tid'], $val['tids'])) {
$flaglist[$key]['list'][array_search($_thread['tid'], $val['tids'])] = $_thread;
ksort($flaglist[$key]['list']);
}
}
}
}
unset($threadlist);
if (!empty($flaglist)) {
foreach ($flaglist as &$val) {
$i = 0;
if (!isset($val['list'])) continue;
foreach ($val['list'] as &$v) {
++$i;
$v['i'] = $i;
}
}
$arrlist['flaglist'] = $flaglist;
unset($flaglist);
}
return $arrlist;
}
//--------------------------cache--------------------------
// 已格式化 从缓存中读取,避免重复从数据库取数据
function well_thread_read_cache($tid)
{
global $conf;
$key = 'website_thread_' . $tid;
static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc
if (isset($cache[$key])) return $cache[$key];
if ('mysql' == $conf['cache']['type']) {
$r = well_thread_read($tid);
} else {
$r = cache_get($key);
if (NULL === $r) {
$r = well_thread_read($tid);
$r and cache_set($key, $r, 1800);
}
}
$cache[$key] = $r ? $r : NULL;
return $cache[$key];
}
?> // 频道和全局置顶主题
foreach ($arrlist as $val) {
if (in_array($val['fid'], $fids) && 2 == $val['sticky']) {
$sticky2[$val['tid']] = $val;
} elseif (3 == $val['sticky']) {
$sticky3[$val['tid']] = $val;
}
}
} else {
// 栏目/上级频道/全局置顶
foreach ($arrlist as $val) {
if ($forum['fid'] == $val['fid'] && 1 == $val['sticky']) {
$sticky1[$val['tid']] = $val;
} elseif (in_array($val['fid'], $fids) && 2 == $val['sticky']) {
$sticky2[$val['tid']] = $val;
} elseif (3 == $val['sticky']) {
$sticky3[$val['tid']] = $val;
}
}
}
$sticky3 = empty($sticky3) ? array() : array_multisort_key($sticky3, 'create_date', FALSE, 'tid');
$sticky2 = empty($sticky2) ? array() : array_multisort_key($sticky2, 'create_date', FALSE, 'tid');
$sticky1 = empty($sticky1) ? array() : array_multisort_key($sticky1, 'create_date', FALSE, 'tid');
$arr = $sticky3 + $sticky2 + $sticky1;
return $arr;
}
// 全部置顶缓存
function sticky_thread_find_cache()
{
global $conf;
$key = 'sticky_thread_list';
static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc
if (isset($cache[$key])) return $cache[$key];
if ('mysql' == $conf['cache']['type']) {
$arr = sticky_thread__find(array(), array('tid' => -1), 1, 2000);
} else {
$arr = cache_get($key);
if (NULL === $arr) {
$arr = sticky_thread__find(array(), array('tid' => -1), 1, 2000);
$arr AND cache_set($key, $arr, 1800);
}
}
$cache[$key] = $arr ? $arr : NULL;
return $cache[$key];
}
?>}
$post['classname'] = 'post';
}
function comment_format(&$post)
{
global $conf, $uid, $gid, $forumlist;
if (empty($post)) return;
$forum = $post['fid'] ? forum_read($post['fid']) : '';
$thread = well_thread_read_cache($post['tid']);
if ($thread) {
//$post['fid'] = $thread['fid'];
$post['closed'] = $thread['closed'];
$post['subject'] = $thread['subject'];
$post['url'] = $thread['url'];
} else {
$post['closed'] = 0;
$post['subject'] = lang('thread_not_exists');
$post['url'] = '';
}
$post['create_date_fmt'] = humandate($post['create_date']);
//$post['message'] = stripslashes(htmlspecialchars_decode($post['message']));
$user = user_read_cache($post['uid']);
$post['username'] = array_value($user, 'username');
$post['user_avatar_url'] = array_value($user, 'avatar_url');
$post['user'] = $user ? user_safe_info($user) : user_guest();
isset($post['floor']) || $post['floor'] = 0;
// 权限判断
$post['allowupdate'] = 2 == array_value($forum, 'comment', 0) && ($uid == $post['uid'] || forum_access_mod($post['fid'], $gid, 'allowupdate'));
$post['allowdelete'] = group_access($gid, 'allowuserdelete') && $uid == $post['uid'] || forum_access_mod($post['fid'], $gid, 'allowdelete');
$post['user_url'] = url('user-' . $post['uid'] . ($post['uid'] ? '' : '-' . $post['pid']));
if ($post['files'] > 0) {
list($attachlist, $imagelist, $filelist) = well_attach_find_by_pid($post['pid']);
// 使用图床 评论使用图床,mysql会过多,写死链接到内容是减轻mysql的过多的方法
if (2 == $conf['attach_on']) {
foreach ($imagelist as $key => $attach) {
$url = $conf['upload_url'] . 'website_attach/' . $attach['filename'];
// 替换成图床
$post['message'] = FALSE !== strpos($post['message'], $url) && $attach['image_url'] ? str_replace($url, $attach['image_url'], $post['message']) : $post['message'];
}
}
$post['filelist'] = $filelist;
} else {
$post['filelist'] = array();
}
$post['classname'] = 'post';
}
function comment_format_message(&$val)
{
global $conf;
if (empty($val)) return;
// 使用云储存
if (1 == $conf['attach_on'] && 1 == $val['attach_on']) {
$val['message'] = str_replace('="upload/', '="' . file_path($val['attach_on']), $val['message']);
} elseif (2 == $conf['attach_on'] && 2 == $val['attach_on']) {
// 使用图床
list($attachlist, $imagelist, $filelist) = well_attach_find_by_tid($val['tid']);
foreach ($imagelist as $key => $attach) {
$url = $conf['upload_url'] . 'website_attach/' . $attach['filename'];
// 替换成图床
$val['message'] = FALSE !== strpos($val['message'], $url) && $attach['image_url'] ? str_replace($url, $attach['image_url'], $val['message']) : $val['message'];
}
} else {
$val['message'] = str_replace('="upload/', '="' . file_path($val['attach_on']), $val['message']);
}
//$val['message'] = stripslashes(htmlspecialchars_decode($val['message']));
}
// 把内容中使用了云储存的附件链接替换掉
function comment_message_replace_url($pid, $message)
{
global $conf;
if (0 == $conf['attach_on']) {
$message = FALSE !== strpos($message, '="../upload/') ? str_replace('="../upload/', '="upload/', $message) : $message;
$message = FALSE !== strpos($message, '="/upload/') ? str_replace('="/upload/', '="upload/', $message) : $message;
} elseif (1 == $conf['attach_on']) {
// 使用云储存
$message = str_replace('="' . $conf['cloud_url'] . 'upload/', '="upload/', $message);
} elseif (2 == $conf['attach_on']) {
// 使用图床 评论使用图床,mysql会过多,写死链接到内容是减轻mysql的过多的方法
list($attachlist, $imagelist, $filelist) = well_attach_find_by_pid($pid);
foreach ($imagelist as $key => $attach) {
$url = $conf['upload_url'] . 'website_attach/' . $attach['filename'];
// 替换回相对链接
$message = $attach['image_url'] && FALSE !== strpos($message, $attach['image_url']) ? str_replace($attach['image_url'], $url, $message) : $message;
}
}
return $message;
}
function comment_filter($val)
{
unset($val['userip']);
return $val;
}
function comment_highlight_keyword($str, $k)
{
$r = str_ireplace($k, '' . $k . '', $str);
return $r;
}
//
//
function comment_message_format(&$s) { if (xn_strlen($s) < 100) return; $s = preg_replace('#.*?#is', '', $s); $s = str_ireplace(array('
', '
', '
', '', '', '', '', '' . ''), "\r\n", $s); $s = str_ireplace(array(' '), " ", $s); $s = strip_tags($s); $s = preg_replace('#[\r\n]+#', "\n", $s); $s = xn_substr(trim($s), 0, 100); $s = str_replace("\n", '
', $s); } // 对内容进行引用 function comment_quote($quotepid) { $quotepost = comment_read($quotepid); if (empty($quotepost)) return ''; $uid = $quotepost['uid']; $s = $quotepost['message']; $s = comment_brief($s, 100); $userhref = url('user-' . $uid); $user = user_read_cache($uid); $r = ' '; return $r; } // 获取内容的简介 0: html, 1: txt; 2: markdown; 3: ubb function comment_brief($s, $len = 100) { $s = strip_tags($s); $s = htmlspecialchars($s); $more = xn_strlen($s) > $len ? ' ... ' : ''; $s = xn_substr($s, 0, $len) . $more; return $s; } ?>