PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/forumrunner/include/general_vb.php

https://gitlab.com/elasa/vb-elasa.ir
PHP | 359 lines | 302 code | 39 blank | 18 comment | 61 complexity | e07ee86ef6c607d68c6656d4da7acb1d MD5 | raw file
  1. <?php
  2. /*
  3. * Forum Runner
  4. *
  5. * Copyright (c) 2010-2011 to End of Time Studios, LLC
  6. *
  7. * This file may not be redistributed in whole or significant part.
  8. *
  9. * http://www.forumrunner.com
  10. */
  11. function
  12. fr_exec_shut_down ($closedb = false)
  13. {
  14. global $vbulletin;
  15. if (!$closedb) {
  16. $vbulletin->db->unlock_tables();
  17. if (is_array($vbulletin->db->shutdownqueries))
  18. {
  19. $vbulletin->db->hide_errors();
  20. foreach($vbulletin->db->shutdownqueries AS $name => $query)
  21. {
  22. if (!empty($query) AND ($name !== 'pmpopup' OR !defined('NOPMPOPUP')))
  23. {
  24. $vbulletin->db->query_write($query);
  25. }
  26. }
  27. $vbulletin->db->show_errors();
  28. }
  29. } else {
  30. exec_mail_queue();
  31. if (defined('NOSHUTDOWNFUNC'))
  32. {
  33. $vbulletin->db->close();
  34. }
  35. $vbulletin->db->shutdownqueries = array();
  36. }
  37. }
  38. function
  39. get_sub_thread_updates ()
  40. {
  41. global $vbulletin, $db;
  42. $total = 0;
  43. if (!$vbulletin->options['threadmarking']) {
  44. if ($vbulletin->userinfo['userid'] AND in_coventry($vbulletin->userinfo['userid'], true)) {
  45. $lastpost_info = ", IF(tachythreadpost.userid IS NULL, thread.lastpost, tachythreadpost.lastpost) AS lastposts";
  46. $tachyjoin = "LEFT JOIN " . TABLE_PREFIX . "tachythreadpost AS tachythreadpost ON " .
  47. "(tachythreadpost.threadid = subscribethread.threadid AND tachythreadpost.userid = " . $vbulletin->userinfo['userid'] . ')';
  48. $lastpost_having = "HAVING lastposts > " . $vbulletin->userinfo['lastvisit'];
  49. } else {
  50. $lastpost_info = '';
  51. $tachyjoin = '';
  52. $lastpost_having = "AND lastpost > " . $vbulletin->userinfo['lastvisit'];
  53. }
  54. $getthreads = $db->query_read_slave("
  55. SELECT thread.threadid, thread.forumid, thread.postuserid, subscribethread.subscribethreadid
  56. $lastpost_info
  57. FROM " . TABLE_PREFIX . "subscribethread AS subscribethread
  58. INNER JOIN " . TABLE_PREFIX . "thread AS thread USING (threadid)
  59. $tachyjoin
  60. WHERE subscribethread.threadid = thread.threadid
  61. AND subscribethread.userid = " . $vbulletin->userinfo['userid'] . "
  62. AND thread.visible = 1
  63. AND subscribethread.canview = 1
  64. $lastpost_having
  65. ");
  66. } else {
  67. $readtimeout = TIMENOW - ($vbulletin->options['markinglimit'] * 86400);
  68. if ($vbulletin->userinfo['userid'] AND in_coventry($vbulletin->userinfo['userid'], true)) {
  69. $lastpost_info = ", IF(tachythreadpost.userid IS NULL, thread.lastpost, tachythreadpost.lastpost) AS lastposts";
  70. $tachyjoin = "LEFT JOIN " . TABLE_PREFIX . "tachythreadpost AS tachythreadpost ON " .
  71. "(tachythreadpost.threadid = subscribethread.threadid AND tachythreadpost.userid = " . $vbulletin->userinfo['userid'] . ')';
  72. } else {
  73. $lastpost_info = ', thread.lastpost AS lastposts';
  74. $tachyjoin = '';
  75. }
  76. $getthreads = $db->query_read_slave("
  77. SELECT thread.threadid, thread.forumid, thread.postuserid,
  78. IF(threadread.readtime IS NULL, $readtimeout, IF(threadread.readtime < $readtimeout, $readtimeout, threadread.readtime)) AS threadread,
  79. IF(forumread.readtime IS NULL, $readtimeout, IF(forumread.readtime < $readtimeout, $readtimeout, forumread.readtime)) AS forumread,
  80. subscribethread.subscribethreadid
  81. $lastpost_info
  82. FROM " . TABLE_PREFIX . "subscribethread AS subscribethread
  83. INNER JOIN " . TABLE_PREFIX . "thread AS thread ON (subscribethread.threadid = thread.threadid)
  84. LEFT JOIN " . TABLE_PREFIX . "threadread AS threadread ON (threadread.threadid = thread.threadid AND threadread.userid = " . $vbulletin->userinfo['userid'] . ")
  85. LEFT JOIN " . TABLE_PREFIX . "forumread AS forumread ON (forumread.forumid = thread.forumid AND forumread.userid = " . $vbulletin->userinfo['userid'] . ")
  86. $tachyjoin
  87. WHERE subscribethread.userid = " . $vbulletin->userinfo['userid'] . "
  88. AND thread.visible = 1
  89. AND subscribethread.canview = 1
  90. HAVING lastposts > IF(threadread > forumread, threadread, forumread)
  91. ");
  92. }
  93. if ($totalthreads = $db->num_rows($getthreads)) {
  94. $forumids = array();
  95. $threadids = array();
  96. $killthreads = array();
  97. while ($getthread = $db->fetch_array($getthreads)) {
  98. $forumperms = fetch_permissions($getthread['forumid']);
  99. if (!($forumperms & $vbulletin->bf_ugp_forumpermissions['canview']) OR !($forumperms & $vbulletin->bf_ugp_forumpermissions['canviewthreads']) OR ($getthread['postuserid'] != $vbulletin->userinfo['userid'] AND !($forumperms & $vbulletin->bf_ugp_forumpermissions['canviewothers']))) {
  100. $killthreads[] = $getthread['subscribethreadid'];
  101. continue;
  102. }
  103. $forumids["$getthread[forumid]"] = true;
  104. $threadids[] = $getthread['threadid'];
  105. }
  106. $threadids = implode(',', $threadids);
  107. }
  108. unset($getthread);
  109. $db->free_result($getthreads);
  110. // if there are some results to show, query the data
  111. if (!empty($threadids)) {
  112. // get last read info for each thread
  113. $lastread = array();
  114. foreach (array_keys($forumids) AS $forumid) {
  115. if ($vbulletin->options['threadmarking']) {
  116. $lastread["$forumid"] = max($vbulletin->forumcache["$forumid"]['forumread'], TIMENOW - ($vbulletin->options['markinglimit'] * 86400));
  117. } else {
  118. $lastread["$forumid"] = max(intval(fetch_bbarray_cookie('forum_view', $forumid)), $vbulletin->userinfo['lastvisit']);
  119. }
  120. }
  121. if ($vbulletin->userinfo['userid'] AND in_coventry($vbulletin->userinfo['userid'], true)) {
  122. $lastpost_info = "IF(tachythreadpost.userid IS NULL, thread.lastpost, tachythreadpost.lastpost) AS lastpost, " .
  123. "IF(tachythreadpost.userid IS NULL, thread.lastposter, tachythreadpost.lastposter) AS lastposter, " .
  124. "IF(tachythreadpost.userid IS NULL, thread.lastpostid, tachythreadpost.lastpostid) AS lastpostid";
  125. $tachyjoin = "LEFT JOIN " . TABLE_PREFIX . "tachythreadpost AS tachythreadpost ON " .
  126. "(tachythreadpost.threadid = thread.threadid AND tachythreadpost.userid = " . $vbulletin->userinfo['userid'] . ')';
  127. } else {
  128. $lastpost_info = 'thread.lastpost, thread.lastposter, thread.lastpostid';
  129. $tachyjoin = '';
  130. }
  131. $hook_query_fields = $hook_query_joins = $hook_query_where = '';
  132. $getthreads = $db->query_read_slave("
  133. SELECT $previewfield
  134. thread.threadid, thread.title AS threadtitle, thread.forumid, thread.pollid,
  135. thread.open, thread.replycount, thread.postusername, thread.postuserid,
  136. thread.dateline, thread.views, thread.iconid AS threadiconid,
  137. thread.notes, thread.visible,
  138. $lastpost_info
  139. " . ($vbulletin->options['threadmarking'] ? ", threadread.readtime AS threadread" : '') . "
  140. $hook_query_fields
  141. FROM " . TABLE_PREFIX . "thread AS thread
  142. " . ($vbulletin->options['threadmarking'] ? " LEFT JOIN " . TABLE_PREFIX . "threadread AS threadread ON (threadread.threadid = thread.threadid AND threadread.userid = " . $vbulletin->userinfo['userid'] . ")" : '') . "
  143. $previewjoin
  144. $tachyjoin
  145. $hook_query_joins
  146. WHERE thread.threadid IN($threadids)
  147. $hook_query_where
  148. ORDER BY lastpost DESC
  149. ");
  150. // check to see if there are any threads to display. If there are, do so, otherwise, show message
  151. if ($totalthreads = $db->num_rows($getthreads)) {
  152. $threads = array();
  153. while ($getthread = $db->fetch_array($getthreads)) {
  154. // unset the thread preview if it can't be seen
  155. $forumperms = fetch_permissions($getthread['forumid']);
  156. if ($vbulletin->options['threadpreview'] > 0 AND !($forumperms & $vbulletin->bf_ugp_forumpermissions['canviewthreads'])) {
  157. $getthread['preview'] = '';
  158. }
  159. if ($vbulletin->forumcache[$getthread['forumid']]['options'] & $vbulletin->bf_misc_forumoptions['allowicons']) {
  160. $show['threadicons'] = true;
  161. $subscribedthreadscolspan = 6;
  162. }
  163. $threads["$getthread[threadid]"] = $getthread;
  164. }
  165. }
  166. unset($getthread);
  167. $db->free_result($getthreads);
  168. if ($totalthreads) {
  169. foreach ($threads AS $threadid => $thread) {
  170. $last = $lastread["$thread[forumid]"];
  171. if ($last == -1) {
  172. $last = $vbulletin->userinfo['lastvisit'];
  173. }
  174. if ($thread['lastpost'] > $last) {
  175. if ($vbulletin->options['threadmarking'] AND $thread['threadread']) {
  176. $threadview = $thread['threadread'];
  177. } else {
  178. $threadview = intval(fetch_bbarray_cookie('thread_lastview', $thread['threadid']));
  179. }
  180. if ($thread['lastpost'] > $threadview) {
  181. $total++;
  182. }
  183. }
  184. }
  185. }
  186. }
  187. return $total;
  188. }
  189. function
  190. get_pm_unread ()
  191. {
  192. global $vbulletin, $db;
  193. $pmurc = $db->query_first_slave("
  194. SELECT SUM(IF(messageread = 0 AND folderid >= 0, 1, 0)) AS pmunread
  195. FROM " . TABLE_PREFIX . "pm AS pm
  196. WHERE pm.userid = " . $vbulletin->userinfo['userid'] . "
  197. ");
  198. $total = 0;
  199. if ($pmurc['pmunread']) {
  200. $total = intval($pmurc['pmunread']);
  201. }
  202. return $total;
  203. }
  204. function
  205. fr_update_push_user ($username, $fr_b = false)
  206. {
  207. global $vbulletin;
  208. $tableinfo = $vbulletin->db->query_first("
  209. SHOW TABLES LIKE '" . TABLE_PREFIX . "forumrunner_push_users'
  210. ");
  211. if ($tableinfo && $vbulletin->userinfo['userid']) {
  212. if ($username) {
  213. // There can be only one FR user associated with this vb_userid and fr_username
  214. $vb_user = $vbulletin->db->query_read_slave("
  215. SELECT id FROM " . TABLE_PREFIX . "forumrunner_push_users
  216. WHERE vb_userid = {$vbulletin->userinfo['userid']}
  217. ");
  218. if ($vbulletin->db->num_rows($vb_user) > 1) {
  219. // Multiple vb_userids. Nuke em.
  220. $vbulletin->db->query_write("
  221. DELETE FROM " . TABLE_PREFIX . "forumrunner_push_users
  222. WHERE vb_userid = {$vbulletin->userinfo['userid']}
  223. ");
  224. }
  225. $fr_user = $vbulletin->db->query_first("
  226. SELECT id FROM " . TABLE_PREFIX . "forumrunner_push_users
  227. WHERE fr_username = '" . $vbulletin->db->escape_string($username) . "'
  228. ");
  229. if ($fr_user) {
  230. $vbulletin->db->query_write("
  231. UPDATE " . TABLE_PREFIX . "forumrunner_push_users
  232. SET vb_userid = {$vbulletin->userinfo['userid']}, last_login = NOW(), b = " . ($fr_b ? 1 : 0) . "
  233. WHERE id = {$fr_user['id']}
  234. ");
  235. } else {
  236. $vbulletin->db->query_write("
  237. INSERT INTO " . TABLE_PREFIX . "forumrunner_push_users
  238. (vb_userid, fr_username, b, last_login)
  239. VALUES ({$vbulletin->userinfo['userid']}, '" . $vbulletin->db->escape_string($username) . "', " . ($fr_b ? 1 : 0) . ", NOW())
  240. ");
  241. }
  242. } else {
  243. // Nuke any old entries of them being logged in
  244. $vbulletin->db->query_write("
  245. DELETE FROM " . TABLE_PREFIX . "forumrunner_push_users
  246. WHERE vb_userid = {$vbulletin->userinfo['userid']}
  247. ");
  248. }
  249. }
  250. }
  251. function
  252. fr_update_subsent ($threadid, $threadread)
  253. {
  254. global $vbulletin;
  255. $tableinfo = $vbulletin->db->query_first("
  256. SHOW TABLES LIKE '" . TABLE_PREFIX . "forumrunner_push_data'
  257. ");
  258. if ($tableinfo) {
  259. $vbulletin->db->query_write("
  260. UPDATE " . TABLE_PREFIX . "forumrunner_push_data
  261. SET vb_subsent = 0, vb_threadread = $threadread
  262. WHERE vb_userid = {$vbulletin->userinfo['userid']} AND vb_threadid = $threadid
  263. ");
  264. }
  265. }
  266. function
  267. fr_show_ad ()
  268. {
  269. global $vbulletin;
  270. if (!$vbulletin ||
  271. ($vbulletin->options['forumrunner_googleads_onoff'] == 0) ||
  272. (trim($vbulletin->options['forumrunner_googleads_usergroups'] == '')) ||
  273. (trim($vbulletin->options['forumrunner_admob_publisherid_iphone']) == '' &&
  274. trim($vbulletin->options['forumrunner_admob_publisherid_android']) == '' &&
  275. trim($vbulletin->options['forumrunner_googleads_javascript'] == '')))
  276. {
  277. return 0;
  278. }
  279. $adgids = explode(',', $vbulletin->options['forumrunner_googleads_usergroups']);
  280. $exclude_adgids = explode(',', $vbulletin->options['forumrunner_googleads_exclude_usergroups']);
  281. $mgids[] = $vbulletin->userinfo['usergroupid'];
  282. if ($vbulletin->userinfo['membergroupids'] && $vbulletin->userinfo['membergroupids'] != '') {
  283. $mgids = array_merge($mgids, explode(',', $vbulletin->userinfo['membergroupids']));
  284. }
  285. if (is_array($adgids)) {
  286. for ($i = 0; $i < count($adgids); $i++) {
  287. $adgids[$i] = trim($adgids[$i]);
  288. }
  289. }
  290. if (is_array($exclude_adgids)) {
  291. for ($i = 0; $i < count($exclude_adgids); $i++) {
  292. $exclude_adgids[$i] = trim($exclude_adgids[$i]);
  293. }
  294. }
  295. // See if they are included
  296. if (count(array_intersect($adgids, $mgids)) == 0) {
  297. return 0;
  298. }
  299. // See if they are excluded
  300. if (count(array_intersect($exclude_adgids, $mgids))) {
  301. return 0;
  302. }
  303. $ad = 0;
  304. if ($vbulletin->options['forumrunner_googleads_threadlist']) {
  305. $ad += FR_AD_THREADLIST;
  306. }
  307. if ($vbulletin->options['forumrunner_googleads_topthread']) {
  308. $ad += FR_AD_TOPTHREAD;
  309. }
  310. if ($vbulletin->options['forumrunner_googleads_bottomthread']) {
  311. $ad += FR_AD_BOTTOMTHREAD;
  312. }
  313. return $ad;
  314. }
  315. function
  316. fr_standard_error ($error = '')
  317. {
  318. json_error(prepare_utf8_string(strip_tags($error)));
  319. }
  320. ?>