MyBB Community Forums

Full Version: Don't fetch from inactive or private forums?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to make this code not to fetch threads from forums that are locked, invalid or users can view it also threads that are on hold to 

	$querysearch = $db->simple_select(TAGS, "*", TAGS." LIKE '%".$db->escape_string_like($search)."%'", array("order_by" => "tid", "order_dir" => $mybb->settings['systemtag_ascdesclist'], 'limit' => "{$start}, {$perpage}"));
	


How to incorporate this please:

// get forums user cannot view
        $unviewable = get_unviewable_forums(true);
        if($unviewable)
        {
            $wheres = "fid NOT IN ($unviewable)";
        }
        // get inactive forums
        $inactive = get_inactive_forums();
        if($inactive)
        {
            $wheres = "fid NOT IN ($inactive)";
        }

AND threads.tid != '0' AND threads.visible = '1' AND threads.closed NOT LIKE 'moved|%' AND {$unviewable}"
A simple idea:
Create this function:
function unviewabletid()
{
   global $db;
   $badtids = array();
   $unviewable = explode(',', get_unviewable_forums(true));
   $inactive = explode(',', get_inactive_forums());
   $badfids = array_unique(array_merge($unviewable, $inactive));
   if (count($badfids)>0)
   {
      $where = " AND fid NOT IN (".implode(',', $badfids).")";
   }
   else
   {
      $were = "";
   }
   $query = $db->simple_select('threads', "tid", "threads.tid != '0' AND threads.visible = '1' AND threads.closed NOT LIKE 'moved|%'".$where);
   while($row = $db->fetch_array($query))
   {
      $badtids[] = $row['tid'];
   }
   if (count($badtids)>0)
   {
      return " AND tid NOT IN (".implode(',', $badtids).")";
   }
   return ""
}

And modify your queries, like this:
$querysearch = $db->simple_select(TAGS, "*", TAGS." LIKE '%".$db->escape_string_like($search)."%'".unviewabletid(), array("order_by" => "tid", "order_dir" => $mybb->settings['systemtag_ascdesclist'], 'limit' => "{$start}, {$perpage}"));
Man you are awesome can I borrow your brain for a while Wink

Thanks buddy.

Buddy not sure why I get this error:


SQL Error:
    1054 - Unknown column 'threads.tid' in 'where clause'
Query:
    SELECT tid FROM mybb_threads WHERE threads.tid != '0' AND threads.visible = '1' AND threads.closed NOT LIKE 'moved|%' 

The table also stores each table TID.

Maybe I can create a query to fetch all threads then check which have some restrictions ans then use that data to show tags?

$querysearch = $db->query("
        SELECT *
        FROM ".TABLE_PREFIX."tags t
        WHERE t.tags LIKE '%".$db->escape_string_like($search)."%' 
		order by t.tid ".$mybb->settings['systemtag_ascdesclist']."
		LIMIT {$start}, {$perpage}
    ");

I need to join threads and forums tables then check if the tags tid has some limitations like its hidden or the forum can't permissions!
$noperms = explode(',', get_unviewable_forums(true));
$inactive = explode(',', get_inactive_forums());
$unviewable = implode(',', array_unique(array_merge($noperms, $inactive)));

$querysearch = $db->query("
        SELECT *
        FROM {$db->table_prefix}tags t
        LEFT JOIN {$db->table_prefix}threads th ON(th.tid = t.tid)
        WHERE t.tags LIKE '%{$db->escape_string_like($search)}%' AND th.fid NOT IN({$unviewable}) 
        ORDER BY t.tid {$mybb->settings['systemtag_ascdesclist']}
        LIMIT {$start}, {$perpage}
    "); 
Sorry, I did a mistake, forget to remove your table prefixes.
So my query must start with:
$query = $db->simple_select('threads', "tid", "tid != '0' AND visible = '1' AND closed NOT LIKE 'moved|%'".$where);

btw, Destroy666 gave the code you need Smile
Guys thanks very much for such an amazing support.

Yesterday before you posted your patches I came up with this fix that seem to work please take a look!

I am currently running on mybb 1.6 version!


$querysearch = $db->query("
        SELECT *
        FROM ".TABLE_PREFIX."tags t
		LEFT JOIN ".TABLE_PREFIX."threads a ON (a.tid=t.tid)
		LEFT JOIN ".TABLE_PREFIX."forums f ON (a.fid=f.fid)
        WHERE t.tags LIKE '%".$db->escape_string_like($search)."%' AND a.visible='1' AND a.closed NOT LIKE 'moved|%' AND 1=1 $unviewwhere
		order by t.tid ".$mybb->settings['systemtag_ascdesclist']."
		LIMIT {$start}, {$perpage}
    ");

Please fix any possible security issues!

This like casing the issue as it adds comma to the last value in the array!
$unviewable = implode(',', array_unique(array_merge($noperms, $inactive)));

SELECT * FROM mybb_tags t LEFT JOIN mybb_threads a ON (a.tid=t.tid) WHERE t.tags LIKE '%blog%' AND a.visible='1' AND a.closed NOT LIKE 'moved|%' AND a.fid NOT IN ('201','12',) order by t.tid DESC LIMIT 0, 20

The other issue is that for some reason these are not working on admin only on guests and regular users!:

$noperms = explode(',', get_unviewable_forums(true));

$inactive = explode(',', get_inactive_forums());

$unviewable = implode(',', array_unique(array_merge($noperms, $inactive)));

I am not sure why it's empty returns no values for admin!

Maybe I should use:

if ($unviewable)
   {
       $where = "AND a.fid NOT IN ($unviewable)";
   }
else
{
$where = "";
}



This seems to work but is it the correct way???


$noperms = explode(',', get_unviewable_forums(true));
$inactive = explode(',', get_inactive_forums());
$unviewable = implode('', array_unique(array_merge($noperms, $inactive)));

if ($unviewable)
    {
        $where = "AND a.fid NOT IN ($unviewable)";
    } 
else
{
$where = "";
}

$querysearch = $db->query("
        SELECT *
        FROM ".TABLE_PREFIX."tags t
		LEFT JOIN ".TABLE_PREFIX."threads a ON (a.tid=t.tid)
        WHERE t.tags LIKE '%".$db->escape_string_like($search)."%' AND a.visible='1' AND a.closed NOT LIKE 'moved|%' $where
		order by t.tid ".$mybb->settings['systemtag_ascdesclist']."
		LIMIT {$start}, {$perpage}
    ");
You didn't use my code above so I can't tell what's wrong except that LEFT JOIN to the forums table is redudant (and probably causes the error) in the 1st code you posted and in the 2nd this is wrong:
$unviewable = implode('', array_unique(array_merge($noperms, $inactive)));

if ($unviewable)
    {
        $where = "AND a.fid NOT IN ($unviewable)";
    } 
else
{
$where = "";
}
It will check only one (in some cases non-existent) forum because for some reason you changed the comma implosion to en empty implosion.

Additionally, since you're using 1.6 and get_uniewable_forums() returns weird values before 1.8.4, you need to strip ' from it first:
$noperms = explode(',', str_replace("'", '', get_unviewable_forums(true)));

Also empty value may need to be removed in this line with array_filter():
$unviewable = implode(',', array_filter(array_unique(array_merge($noperms, $inactive))));
D666 buddy thanks very much! I am using your code and it works great BUT for some reason when I log in as ADMIN get this error:

"SELECT * FROM mybb_tags t LEFT JOIN mybb_threads a ON (a.tid=t.tid) WHERE t.tags LIKE '%affiliate%' AND a.visible='1' AND a.closed NOT LIKE 'moved|%' AND a.fid NOT IN () order by t.tid DESC LIMIT 0, 20 "

I doesn't return:
$noperms
$inactive
Ah yes, forgot to include empty case in my post. Add:
$qunview = '';
if($unviewable)
{
    $qunview = " AND th.fid NOT IN($unviewable)";
}
before the query.

And change AND th.fid NOT IN({$unviewable}) to {$qunview} in the query.
Baddy you are nailed it thanks so much works great.


Do you know what I will have to use for 1.8?

1.6 code:


$noperms = explode(',', str_replace("'", '', get_unviewable_forums(true)));
$inactive = explode(',', get_inactive_forums());
$unviewable = implode(',', array_filter(array_unique(array_merge($noperms, $inactive))));

$qunview = '';
if($unviewable)
{
    $where = " AND a.fid NOT IN($unviewable)";
} 

$querysearch = $db->query("
        SELECT *
        FROM ".TABLE_PREFIX."tags t
		LEFT JOIN ".TABLE_PREFIX."threads a ON (a.tid=t.tid)
        WHERE t.tags LIKE '%".$db->escape_string_like($search)."%' AND a.visible='1' AND a.closed NOT LIKE 'moved|%' {$where}
		order by t.tid ".$mybb->settings['systemtag_ascdesclist']."
		LIMIT {$start}, {$perpage}
    ");