[ Index ] |
PHP Cross Reference of MyBB 1.8.38 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * MyBB 1.8 4 * Copyright 2014 MyBB Group, All Rights Reserved 5 * 6 * Website: http://www.mybb.com 7 * License: http://www.mybb.com/about/license 8 * 9 */ 10 11 /** 12 * Mark a particular thread as read for the current user. 13 * 14 * @param int $tid The thread ID 15 * @param int $fid The forum ID of the thread 16 */ 17 function mark_thread_read($tid, $fid) 18 { 19 global $mybb, $db; 20 21 // Can only do "true" tracking for registered users 22 if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid']) 23 { 24 // For registered users, store the information in the database. 25 switch($db->type) 26 { 27 case "pgsql": 28 case "sqlite": 29 $db->replace_query("threadsread", array('tid' => $tid, 'uid' => $mybb->user['uid'], 'dateline' => TIME_NOW), array("tid", "uid")); 30 break; 31 default: 32 $db->write_query(" 33 REPLACE INTO ".TABLE_PREFIX."threadsread (tid, uid, dateline) 34 VALUES('$tid', '{$mybb->user['uid']}', '".TIME_NOW."') 35 "); 36 } 37 } 38 // Default back to cookie marking 39 else 40 { 41 my_set_array_cookie("threadread", $tid, TIME_NOW, -1); 42 } 43 44 $unread_count = fetch_unread_count($fid); 45 if($unread_count == 0) 46 { 47 mark_forum_read($fid); 48 } 49 } 50 51 /** 52 * Fetches the number of unread threads for the current user in a particular forum. 53 * 54 * @param string $fid The forums (CSV list) 55 * @return int The number of unread threads 56 */ 57 function fetch_unread_count($fid) 58 { 59 global $cache, $db, $mybb; 60 61 $forums_all = $forums_own = array(); 62 $forums = explode(',', $fid); 63 foreach($forums as $forum) 64 { 65 $permissions = forum_permissions($forum); 66 if(!empty($permissions['canonlyviewownthreads'])) 67 { 68 $forums_own[] = $forum; 69 } 70 else 71 { 72 $forums_all[] = $forum; 73 } 74 } 75 if(!empty($forums_own)) 76 { 77 $where = "(fid IN (".implode(',', $forums_own).") AND uid = {$mybb->user['uid']})"; 78 $where2 = "(t.fid IN (".implode(',', $forums_own).") AND t.uid = {$mybb->user['uid']})"; 79 } 80 if(!empty($forums_all)) 81 { 82 if(isset($where)) 83 { 84 $where = "({$where} OR fid IN (".implode(',', $forums_all)."))"; 85 $where2 = "({$where2} OR t.fid IN (".implode(',', $forums_all)."))"; 86 } 87 else 88 { 89 $where = 'fid IN ('.implode(',', $forums_all).')'; 90 $where2 = 't.fid IN ('.implode(',', $forums_all).')'; 91 } 92 } 93 $cutoff = TIME_NOW-$mybb->settings['threadreadcut']*60*60*24; 94 95 if(!empty($permissions['canonlyviewownthreads'])) 96 { 97 $onlyview = " AND uid = '{$mybb->user['uid']}'"; 98 $onlyview2 = " AND t.uid = '{$mybb->user['uid']}'"; 99 } 100 101 if($mybb->user['uid'] == 0) 102 { 103 $comma = ''; 104 $tids = ''; 105 $threadsread = $forumsread = array(); 106 107 if(isset($mybb->cookies['mybb']['threadread'])) 108 { 109 $threadsread = my_unserialize($mybb->cookies['mybb']['threadread'], false); 110 } 111 if(isset($mybb->cookies['mybb']['forumread'])) 112 { 113 $forumsread = my_unserialize($mybb->cookies['mybb']['forumread'], false); 114 } 115 116 if(!empty($threadsread)) 117 { 118 foreach($threadsread as $key => $value) 119 { 120 $tids .= $comma.(int)$key; 121 $comma = ','; 122 } 123 } 124 125 if(!empty($tids)) 126 { 127 $count = 0; 128 129 // We've read at least some threads, are they here? 130 $query = $db->simple_select("threads", "lastpost, tid, fid", "visible=1 AND closed NOT LIKE 'moved|%' AND {$where} AND lastpost > '{$cutoff}'", array("limit" => 100)); 131 132 while($thread = $db->fetch_array($query)) 133 { 134 if((!isset($threadsread[$thread['tid']]) || $thread['lastpost'] > (int)$threadsread[$thread['tid']]) && (!isset($forumsread[$thread['fid']]) || $thread['lastpost'] > (int)$forumsread[$thread['fid']])) 135 { 136 ++$count; 137 } 138 } 139 140 return $count; 141 } 142 143 // Not read any threads? 144 return false; 145 } 146 else 147 { 148 switch($db->type) 149 { 150 case "pgsql": 151 $query = $db->query(" 152 SELECT COUNT(t.tid) AS unread_count 153 FROM ".TABLE_PREFIX."threads t 154 LEFT JOIN ".TABLE_PREFIX."threadsread tr ON (tr.tid=t.tid AND tr.uid='{$mybb->user['uid']}') 155 LEFT JOIN ".TABLE_PREFIX."forumsread fr ON (fr.fid=t.fid AND fr.uid='{$mybb->user['uid']}') 156 WHERE t.visible=1 AND t.closed NOT LIKE 'moved|%' AND {$where2} AND t.lastpost > COALESCE(tr.dateline,$cutoff) AND t.lastpost > COALESCE(fr.dateline,$cutoff) AND t.lastpost>$cutoff 157 "); 158 break; 159 default: 160 $query = $db->query(" 161 SELECT COUNT(t.tid) AS unread_count 162 FROM ".TABLE_PREFIX."threads t 163 LEFT JOIN ".TABLE_PREFIX."threadsread tr ON (tr.tid=t.tid AND tr.uid='{$mybb->user['uid']}') 164 LEFT JOIN ".TABLE_PREFIX."forumsread fr ON (fr.fid=t.fid AND fr.uid='{$mybb->user['uid']}') 165 WHERE t.visible=1 AND t.closed NOT LIKE 'moved|%' AND {$where2} AND t.lastpost > IFNULL(tr.dateline,$cutoff) AND t.lastpost > IFNULL(fr.dateline,$cutoff) AND t.lastpost>$cutoff 166 "); 167 } 168 return $db->fetch_field($query, "unread_count"); 169 } 170 } 171 172 /** 173 * Mark a particular forum as read. 174 * 175 * @param int $fid The forum ID 176 */ 177 function mark_forum_read($fid) 178 { 179 global $mybb, $db; 180 181 // Can only do "true" tracking for registered users 182 if($mybb->settings['threadreadcut'] > 0 && $mybb->user['uid']) 183 { 184 // Experimental setting to mark parent forums as read 185 $forums_to_read = array(); 186 187 if($mybb->settings['readparentforums']) 188 { 189 $ignored_forums = array(); 190 $forums = array_reverse(explode(",", get_parent_list($fid))); 191 192 unset($forums[0]); 193 if(!empty($forums)) 194 { 195 $ignored_forums[] = $fid; 196 197 foreach($forums as $forum) 198 { 199 $fids = array($forum); 200 $ignored_forums[] = $forum; 201 202 $children = explode(",", get_parent_list($forum)); 203 foreach($children as $child) 204 { 205 if(in_array($child, $ignored_forums)) 206 { 207 continue; 208 } 209 210 $fids[] = $child; 211 $ignored_forums[] = $child; 212 } 213 214 if(fetch_unread_count(implode(",", $fids)) == 0) 215 { 216 $forums_to_read[] = $forum; 217 } 218 } 219 } 220 } 221 222 switch($db->type) 223 { 224 case "pgsql": 225 case "sqlite": 226 add_shutdown(array($db, "replace_query"), array("forumsread", array('fid' => $fid, 'uid' => $mybb->user['uid'], 'dateline' => TIME_NOW), array("fid", "uid"))); 227 228 if(!empty($forums_to_read)) 229 { 230 foreach($forums_to_read as $forum) 231 { 232 add_shutdown(array($db, "replace_query"), array("forumsread", array('fid' => $forum, 'uid' => $mybb->user['uid'], 'dateline' => TIME_NOW), array('fid', 'uid'))); 233 } 234 } 235 break; 236 default: 237 $child_sql = ''; 238 if(!empty($forums_to_read)) 239 { 240 foreach($forums_to_read as $forum) 241 { 242 $child_sql .= ", ('{$forum}', '{$mybb->user['uid']}', '".TIME_NOW."')"; 243 } 244 } 245 246 $db->shutdown_query(" 247 REPLACE INTO ".TABLE_PREFIX."forumsread (fid, uid, dateline) 248 VALUES('{$fid}', '{$mybb->user['uid']}', '".TIME_NOW."'){$child_sql} 249 "); 250 } 251 } 252 // Mark in a cookie 253 else 254 { 255 my_set_array_cookie("forumread", $fid, TIME_NOW, -1); 256 } 257 } 258 259 /** 260 * Marks all forums as read. 261 * 262 */ 263 function mark_all_forums_read() 264 { 265 global $mybb, $db, $cache; 266 267 // Can only do "true" tracking for registered users 268 if($mybb->user['uid'] > 0) 269 { 270 $db->update_query("users", array('lastvisit' => TIME_NOW), "uid='".$mybb->user['uid']."'"); 271 require_once MYBB_ROOT."inc/functions_user.php"; 272 update_pm_count('', 2); 273 274 if($mybb->settings['threadreadcut'] > 0) 275 { 276 // Need to loop through all forums and mark them as read 277 $forums = $cache->read('forums'); 278 279 $update_count = ceil(count($forums)/20); 280 281 if($update_count < 15) 282 { 283 $update_count = 15; 284 } 285 286 switch($db->type) 287 { 288 case "pgsql": 289 case "sqlite": 290 $mark_query = array(); 291 break; 292 default: 293 $mark_query = ''; 294 } 295 296 $done = 0; 297 foreach(array_keys($forums) as $fid) 298 { 299 switch($db->type) 300 { 301 case "pgsql": 302 case "sqlite": 303 $mark_query[] = array('fid' => $fid, 'uid' => $mybb->user['uid'], 'dateline' => TIME_NOW); 304 break; 305 default: 306 if($mark_query != '') 307 { 308 $mark_query .= ','; 309 } 310 $mark_query .= "('{$fid}', '{$mybb->user['uid']}', '".TIME_NOW."')"; 311 } 312 ++$done; 313 314 // Only do this in loops of $update_count, save query time 315 if($done % $update_count) 316 { 317 switch($db->type) 318 { 319 case "pgsql": 320 case "sqlite": 321 foreach($mark_query as $replace_query) 322 { 323 add_shutdown(array($db, "replace_query"), array("forumsread", $replace_query, array("fid", "uid"))); 324 } 325 $mark_query = array(); 326 break; 327 default: 328 $db->shutdown_query(" 329 REPLACE INTO ".TABLE_PREFIX."forumsread (fid, uid, dateline) 330 VALUES {$mark_query} 331 "); 332 $mark_query = ''; 333 } 334 } 335 } 336 337 if(!empty($mark_query)) 338 { 339 switch($db->type) 340 { 341 case "pgsql": 342 case "sqlite": 343 foreach($mark_query as $replace_query) 344 { 345 add_shutdown(array($db, "replace_query"), array("forumsread", $replace_query, array("fid", "uid"))); 346 } 347 break; 348 default: 349 $db->shutdown_query(" 350 REPLACE INTO ".TABLE_PREFIX."forumsread (fid, uid, dateline) 351 VALUES {$mark_query} 352 "); 353 } 354 } 355 } 356 } 357 else 358 { 359 my_setcookie("mybb[readallforums]", 1); 360 my_setcookie("mybb[lastvisit]", TIME_NOW); 361 362 my_unsetcookie("mybb[threadread]"); 363 my_unsetcookie("mybb[forumread]"); 364 } 365 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup | Cross-referenced by PHPXref |