View Full Version : help.. permission check for "last 5 posts.."
Hi
Could someone urgently amend this query for me to exclude posts the viewing user does not have permission to view?
$threads = $vbulletin->db->query_read("SELECT threadid, title, lastpost, lastposter
FROM " . TABLE_PREFIX . "thread
WHERE visible = '1' AND open = '1'
ORDER BY lastpost DESC LIMIT 5
");
Thanks
Simon
harmor19
01-29-2007, 03:21 AM
This gets the last 5 threads. I would've edit it to show the last 5 posts but I'll let you do that.
$count = 0;
$getposts = $db->query_read("
SELECT DISTINCT thread.title AS title, thread.threadid AS threadid, forum.forumid AS forumid
FROM " . TABLE_PREFIX . "post AS post
LEFT JOIN " . TABLE_PREFIX . "thread AS thread
ON post.threadid = thread.threadid
LEFT JOIN " . TABLE_PREFIX . "forum AS forum
ON thread.forumid = forum.forumid
ORDER BY postid DESC");
while($post = $db->fetch_array($getposts))
{
$forumperms = fetch_permissions($post['forumid']);
if (($forumperms & $vbulletin->bf_ugp_forumpermissions['canview']) OR ($forumperms & $vbulletin->bf_ugp_forumpermissions['canviewthreads']))
{
if($count <= 5)
{
$latestthreads2 .= "<b>Title</b>: <a href='showthread.php?t=$post[threadid]'>".$post['title']."</a><br />";
}
$count++;
}
}
Adrian Schneider
01-29-2007, 03:31 AM
2 tips:
-Do the forum permission checking prior to the query (generating a comma-delimited list of forumids they can or can't see): WHERE forumid NOT IN ($ids)
-Only show posts from the past few days, which would utilize the date index (greatly reducing the amount of juice it will take to bring up the records)
Thanks guys :)
I've changed it to the following (which I've taken from somewhere, can't remember where now!) ..
$blockforums = "";
foreach($vbulletin->forumcache AS $forum) {
$forumid = $forum['forumid'];
$forumperms =& $vbulletin->userinfo['forumpermissions']["$forumid"];
if (!isset($vbulletin->forumcache["$forumid"]) OR !($forumperms & $vbulletin->bf_ugp_forumpermissions['canview']) OR !($forumperms & $vbulletin->bf_ugp_forumpermissions['canviewothers']) OR !verify_forum_password($forumid, $vbulletin->forumcache["$forumid"]['password'], false))
{
$blockforums .= ','.$forum['forumid'];
}
}
$threads = $vbulletin->db->query_read("SELECT threadid, title, lastpost, lastposter
FROM " . TABLE_PREFIX . "thread
WHERE visible = '1' AND open = '1'
AND forumid NOT IN (0$blockforums)
ORDER BY lastpost DESC LIMIT 5
");
Does that look ok?
Thanks again
Simon
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.