最原生的 CURD,无关联其他数据。
// 只用传 message, message_fmt 自动生成
function post__create($arr, $gid) {
post_message_fmt($arr, $gid);
$r = db_insert('post', $arr);
return $r;
}
function post__update($pid, $arr) {
$r = db_update('post', array('pid'=>$pid), $arr);
return $r;
}
function post__read($pid) {
$post = db_find_one('post', array('pid'=>$pid));
return $post;
}
function post__delete($pid) {
$r = db_delete('post', array('pid'=>$pid));
haya_post_like_delete_by_pid($pid);
return $r;
}
function post__find($cond = array(), $orderby = array(), $page = 1, $pagesize = 20) {
$postlist = db_find('post', $cond, $orderby, $page, $pagesize, 'pid');
return $postlist;
}
// ------------> 关联 CURD,主要是强相关的数据,比如缓存。弱相关的大量数据需要另外处理。
// 回帖
function post_create($arr, $fid, $gid) {
global $conf, $time;
$pid = post__create($arr, $gid);
if(!$pid) return $pid;
$tid = $arr['tid'];
$uid = $arr['uid'];
// 回帖
if($tid > 0) {
// todo: 如果是老帖,不更新 lastpid
thread__update($tid, array('posts+'=>1, 'lastpid'=>$pid, 'lastuid'=>$uid, 'last_date'=>$time));
$uid AND user__update($uid, array('posts+'=>1));
runtime_set('posts+', 1);
runtime_set('todayposts+', 1);
forum__update($fid, array('todayposts+'=>1));
}
//post_list_cache_delete($tid);
// 更新板块信息。
forum_list_cache_delete();
// 关联附件
$message = $arr['message'];
attach_assoc_post($pid);
// 更新用户的用户组
user_update_group($uid);
$post = post__read($pid);
if (!$post) return;
if ($post['isfirst']) return;
$thread = thread__read($post['tid']);
$fid = $thread ? $thread['fid'] : 0;
$config = setting_get('xn_moderate');
if (!empty($config['post_moderate']) && moderate_check_post($uid, $fid)) {
db_update('post', array('pid' => $pid), array('is_approved' => 0));
moderate_undo_stats_post($pid);
}
@include_once APP_PATH.'plugin/xn_adguard/xnadguard_load.php';
if (function_exists('xnadguard_plugin_load')) {
xnadguard_plugin_load();
}
if (function_exists('xnadguard_process_post')) {
global $gid;
xnadguard_process_post($pid, $arr, $fid, $gid);
}
return $pid;
}
// 编辑回帖
function post_update($pid, $arr, $tid = 0) {
global $conf, $user, $gid;
$post = post__read($pid);
if(empty($post)) return FALSE;
$tid = $post['tid'];
$uid = $post['uid'];
$isfirst = $post['isfirst'];
post_message_fmt($arr, $gid);
$r = post__update($pid, $arr);
attach_assoc_post($pid);
@include_once APP_PATH.'plugin/xn_adguard/xnadguard_load.php';
if (function_exists('xnadguard_plugin_load')) {
xnadguard_plugin_load();
}
if (function_exists('xnadguard_process_post')) {
global $gid, $fid;
$post = post__read($pid);
if (!empty($post)) {
$scan_fid = $fid;
if (empty($scan_fid) && $post['tid'] > 0) {
$thread = thread__read($post['tid']);
if (!empty($thread['fid'])) $scan_fid = $thread['fid'];
}
xnadguard_process_post($pid, $arr, $scan_fid, $gid);
}
}
if (!function_exists('xnseo_plugin_load')) {
@include_once APP_PATH . 'plugin/xn_seo_longtail/xnseo_load.php';
}
if (function_exists('xnseo_plugin_load') && xnseo_plugin_load() && !empty($isfirst) && !empty($tid) && function_exists('xnseo_process_thread')) {
xnseo_process_thread($tid);
}if ($isfirst) {
xn_seo_pushmap_after_thread_change($tid, 'update');
}
return $r;
}
function post_read($pid) {
$post = post__read($pid);
post_format($post);
return $post;
}
// 从缓存中读取,避免重复从数据库取数据,主要用来前端显示,可能有延迟。重要业务逻辑不要调用此函数,数据可能不准确,因为并没有清理缓存,针对 request 生命周期有效。
function post_read_cache($pid) {
static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,要跨进程,可以再加一层缓存: memcached/xcache/apc/
if(isset($cache[$pid])) return $cache[$pid];
$cache[$pid] = post_read($pid);
return $cache[$pid];
}
// $tid 用来清理缓存
function post_delete($pid) {
global $conf;
$post = post_read_cache($pid);
if(empty($post)) return TRUE; // 已经不存在了。
$tid = $post['tid'];
$uid = $post['uid'];
$thread = thread_read_cache($tid);
$fid = $thread['fid'];
if(!$post['isfirst']) {
$set = setting_get('tt_credits');
$changes = array(
'credits' => 0 - intval($set['post_exp']),
'golds' => 0 - intval($set['post_gold']),
);
// Prevent delete rollbacks from pushing balances below zero after rewards were already spent.
$uid AND credits_apply_user_delta($uid, $changes) AND user_update_group($uid);
}
$post = post__read($pid);
if ($post && $post['is_approved'] != 1 && !$post['isfirst']) {
$tid = $post['tid'];
$uid = $post['uid'];
thread__update($tid, array('posts+' => 1));
if ($uid) {
user__update($uid, array('posts+' => 1));
}
runtime_set('posts+', 1);
}
if(!$post['isfirst']) {
thread__update($tid, array('posts-'=>1));
$uid AND irst_thread);
}
if (!empty($postlist)) {
$floor = $thread['posts'] - ($page - 1) * $pagesize + 1;
foreach ($postlist as & $post) {
$post['floor'] = $floor--;
post_format($post);
}
}
return $postlist;
} elseif (!empty($haya_post_info_see_user)) {
$haya_post_info_cond = array('tid' => $tid, 'uid' => intval($haya_post_info_see_user));
$postlist = post__find($haya_post_info_cond, array('pid' => 1), $page, $pagesize);
if ($page == 1) {
$first_thread = post__read($thread['firstpid']);
$postlist += array($thread['firstpid'] => $first_thread);
}
if (!empty($postlist)) {
$floor = ($page - 1) * $pagesize + 1;
foreach ($postlist as & $post) {
$post['floor'] = $floor++;
post_format($post);
}
}
return $postlist;
}
}
}
$postlist = post__find(array('tid'=>$tid), array('pid'=>1), $page, $pagesize);
if($postlist) {
$floor = ($page - 1)* $pagesize + 1;
foreach($postlist as &$post) {
$post['floor'] = $floor++;
post_format($post);
}
}
return $postlist;
}
//
//
function user_post_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 post_list_cache_delete($tid) { global $conf; $r = cache_delete("postlist_$tid"); return $r; }*/ // ------------> 其他方法 function post_count($cond = array()) { $n = db_count('post', $cond); return $n; } function post_maxid() { $n = db_maxid('post', 'pid'); return $n; } function post_safe_info($post) { unset($post['userip']); if(!empty($post['user'])) { $post['user'] = user_safe_info($post['user']); } return $post; } function post_find_by_pids($pids, $order = array('pid'=>-1)) { if(!$pids) return array(); $postlist = db_find('post', array('pid'=>$pids), $order, 1, 1000, 'pid'); if($postlist) foreach($postlist as &$post) post_format($post); return $postlist; } function post_highlight_keyword($str, $k) { $r = str_ireplace($k, ''.$k.'', $str); return $r; } // 公用的附件模板,采用函数,效率比 include 高。 function post_file_list_html($filelist, $include_delete = FALSE) { if(empty($filelist)) return ''; $s = ''."\r\n"; return $s; } function post_format(&$post) { global $conf, $uid, $sid, $gid, $longip; if(empty($post)) return; $post['create_date_fmt'] = humandate($post['create_date']); $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 : user_guest(); !isset($post['floor']) AND $post['floor'] = ''; $thread = thread_read_cache($post['tid']); // 权限判断 $post['allowupdate'] = ($uid == $post['uid']) || forum_access_mod($thread['fid'], $gid, 'allowupdate'); $post['allowdelete'] = ($uid == $post['uid']) || forum_access_mod($thread['fid'], $gid, 'allowdelete'); $post['user_url'] = url("user-$post[uid]".($post['uid'] ? '' : "-$post[pid]")); if($post['files'] > 0) { list($attachlist, $imagelist, $filelist) = attach_find_by_pid($post['pid']); $post['filelist'] = $filelist; } else { $post['filelist'] = array(); } $post['classname'] = 'post'; @include_once APP_PATH . 'plugin/tcy_cfr2/model/r2.func.php'; r2_on_model_post_format_end($post);if (!empty($post['user']['gid']) && intval($post['user']['gid']) == 7) { $post['message_fmt_fox'] = ''."\xe8\xaf\xa5\xe7\x94\xa8\xe6\x88\xb7\xe8\xa2\xab\xe5\x85\xb3\xe7\xa6\x81\xe9\x97\xad\xef\xbc\x8c\xe5\x86\x85\xe5\xae\xb9\xe8\xa2\xab\xe9\x9a\x90\xe8\x97\x8f".'
'.$post['message_fmt'].''; $post['message_fmt'] = ''."\xe8\xaf\xa5\xe7\x94\xa8\xe6\x88\xb7\xe8\xa2\xab\xe5\x85\xb3\xe7\xa6\x81\xe9\x97\xad\xef\xbc\x8c\xe5\x86\x85\xe5\xae\xb9\xe8\xa2\xab\xe9\x9a\x90\xe8\x97\x8f".''; } $post['floormanage'] = forum_access_mod($thread['fid'], $gid, 'allowdelete'); if (isset($post['is_approved']) && $post['is_approved'] != 1) { global $uid, $gid; $is_owner = !empty($uid) && $post['uid'] == $uid; $_fid = isset($post['fid']) ? $post['fid'] : 0; if (empty($_fid)) { $_t = thread__read($post['tid']); $_fid = $_t ? $_t['fid'] : 0; } $is_mod = function_exists('moderate_can_moderate') && moderate_can_moderate($gid, $_fid); if ($is_owner || $is_mod) { if ($post['isfirst']) { } elseif ($post['is_approved'] == 0) { $post['message_fmt'] = '回复审核中,仅自己和管理员可见' . $post['message_fmt']; } elseif ($post['is_approved'] == 2) { $post['message_fmt'] = '回复审核未通过,仅自己和管理员可见' . $post['message_fmt']; } } else { if ($post['isfirst']) { } else { $post['message_fmt'] = ''; } } } } // 写入时格式化 function post_message_fmt(&$arr, $gid) { // 超长内容截取 $arr['message'] = xn_substr($arr['message'], 0, 2028000); // 格式转换: 类型,0: html, 1: txt; 2: markdown; 3: ubb $arr['message_fmt'] = htmlspecialchars($arr['message']); // 入库的时候进行转换,编辑的时候,自行调取 message, 或者 message_fmt $arr['doctype'] == 0 && $arr['message_fmt'] = ($gid == 1 ? $arr['message'] : xn_html_safe($arr['message'])); $arr['doctype'] == 1 && $arr['message_fmt'] = xn_txt_to_html($arr['message']); // 付费块入库前去掉 ,避免前台显示字面量 if (!empty($arr['message_fmt']) && function_exists('tt_pay_clean_message_fmt_blocks')) { $arr['message_fmt'] = tt_pay_clean_message_fmt_blocks($arr['message_fmt']); } // 对引用进行处理 !empty($arr['quotepid']) && $arr['quotepid'] > 0 && $arr['message_fmt'] = post_quote($arr['quotepid']).$arr['message_fmt']; } // 获取内容的简介 0: html, 1: txt; 2: markdown; 3: ubb function post_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; } // 对内容进行引用 function post_quote($quotepid) { $quotepost = post__read($quotepid); if(empty($quotepost)) return ''; $uid = $quotepost['uid']; $s = $quotepost['message']; $user = user_read_cache($uid); $user['gid'] == 7 AND $s = '【该用户被关禁闭,内容被隐藏'; $s = post_brief($s, 100); $userhref = url("user-$uid"); $user = user_read_cache($uid); $r = ''; return $r; } // 对 $threadlist 权限过滤 function post_list_access_filter(&$postlist, $gid) { global $conf, $forumlist; if(empty($postlist)) return; foreach($postlist as $pid=>$post) { $thread = thread__read($post['tid']); $fid = $thread['fid']; if(empty($forumlist[$fid]['accesson'])) continue; if($thread['top'] > 0) continue; if(!forum_access_user($fid, $gid, 'allowread')) { unset($postlist[$pid]); } } // 勿用 return:会中断 post_list_access_filter(),导致审核插件等后续 hook 失效 @include_once APP_PATH.'plugin/xn_adguard/xnadguard_load.php'; if (function_exists('xnadguard_plugin_load')) { xnadguard_plugin_load(); } if (function_exists('xnadguard_config')) { $config = xnadguard_config(); if (!empty($config['enable']) && !empty($config['audit_hide']) && $config['mode'] === 'audit') { global $uid, $gid; if (!xnadguard_is_whitelist($gid, $uid) && $gid != 1) { foreach ($postlist as $pk => $post) { if (isset($post['OK']) && $post['OK'] != '1' && $post['uid'] != $uid) { unset($postlist[$pk]); } } } } } global $uid, $thread; if (empty($thread) && !empty($postlist)) { $first = reset($postlist); if ($first) { $thread = thread__read($first['tid']); } } if (!empty($postlist) && $thread && isset($thread['is_approved']) && $thread['is_approved'] == 0) { foreach ($postlist as $pkey => $post) { if ($post['is_approved'] != 1) { if ($uid && $post['uid'] == $uid) continue; if (moderate_can_moderate($gid, $thread['fid'])) continue; unset($postlist[$pkey]); } } } } ?>'.$user['username'].' '.$s.'
喷水三通相关主题 - 1篇 - 性息网-全国性息-免费楼凤论坛