tipsypastels

Untitled

Dec 10th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.44 KB | None | 0 0
  1. <?php
  2. /**
  3. * PokéCommunity Notifications Script
  4. *
  5. * Outputs notifications as AJAX for use in the New UI.
  6. *
  7. * @package PokéCommunity
  8. * @subpackage Notifications System
  9. * @version 1.0
  10. */
  11.  
  12. // ####################### SET PHP ENVIRONMENT ###########################
  13. error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
  14.  
  15. // #################### DEFINE IMPORTANT CONSTANTS #######################
  16. define('THIS_SCRIPT', 'notifications');
  17. define('CSRF_PROTECTION', true);
  18. define('DISABLE_ADVERTISING', true);
  19.  
  20. $phrasegroups = array();
  21. $specialtemplates = array();
  22. $globaltemplates = array();
  23. $actiontemplates = array();
  24.  
  25. $dateline = "l, M jS, Y";
  26. $timeline = "g:ia";
  27.  
  28. require_once('./global.php');
  29.  
  30. if(!$vbulletin->userinfo['userid']) {
  31. print_no_permission();
  32. }
  33.  
  34. unset($notifications); // unset vBulletin's dumb notifications system
  35.  
  36. if (empty($_REQUEST['do']))
  37. {
  38. $_REQUEST['do'] = 'list';
  39. }
  40.  
  41. // ####################### List all notifications ########################
  42. if ($_REQUEST['do'] == 'list')
  43. {
  44.  
  45. /** GET params:
  46. cat category
  47. type type of notifications (must be part of the specified category)
  48. from range of notifications to start from
  49. preq number of notifications per request
  50. **/
  51.  
  52. $vbulletin->input->clean_array_gpc('g', array(
  53. 'cat' => TYPE_STR,
  54. 'type' => TYPE_STR,
  55. 'from' => TYPE_INT,
  56. 'preq' => TYPE_INT
  57. ));
  58.  
  59. $range_lower = ($vbulletin->GPC['from'] ?: "0");
  60. $max_notifications = ($vbulletin->GPC['preq'] ?: "20");
  61.  
  62. if($vbulletin->userinfo['userid']){
  63. $userid = $vbulletin->userinfo['userid'];
  64.  
  65. $notificationsQuery = get_user_notifications($userid,$vbulletin->GPC['cat'],$vbulletin->GPC['type'],$range_lower,$max_notifications);
  66.  
  67. while($notification = $db->fetch_array($notificationsQuery)){
  68. $notification['notificationdate'] = vbdate($vbulletin->options['dateformat'], $notification['dateline'], true);
  69. $notification['notificationtime'] = vbdate($vbulletin->options['timeformat'], $notification['dateline']);
  70.  
  71. $notifications['notifications'][] = $notification;
  72. }
  73. if (empty($notifications)) {
  74. $notifications['error'] = "no-notifications";
  75. exit(json_encode($notifications));
  76. }
  77. unset($notification);
  78. $userlookupids[] = $userid;
  79.  
  80. if($_REQUEST['output'] == 'json' && $_REQUEST['debug'] == '1') {
  81. print(json_encode($notifications));
  82. }
  83.  
  84. // iterate through notifications and get content IDs for relevant notifications
  85. if($notifications['notifications']) {
  86. foreach ($notifications['notifications'] as $notification) {
  87.  
  88. switch($notification['type']) {
  89. case 'friendrequest':
  90. $lookupdata['friendrequestids'][] = $notification['content_id'];
  91. break;
  92. case 'likedpost':
  93. $lookupdata['likedpostids'][] = $notification['content_id'];
  94. break;
  95. case 'quotedpost':
  96. $lookupdata['quotedpostids'][] = $notification['content_id'];
  97. break;
  98. case 'blogcomment':
  99. $lookupdata['blogcommentids'][] = $notification['content_id'];
  100. break;
  101. case 'visitormessage':
  102. $lookupdata['visitormessageids'][] = $notification['content_id'];
  103. break;
  104. case 'privatemessage':
  105. $lookupdata['privatemessageids'][] = $notification['content_id'];
  106. break;
  107. default:
  108. // we aren't looking up anything
  109. break;
  110. }
  111. // also get user IDs for every notification
  112. $userlookupids[] = $notification['from_userid'];
  113. }
  114. }
  115.  
  116. // look up basic user info (and avatars) for the user and anybody they're receiving notifications from
  117. $userlookupids = array_unique($userlookupids);
  118. $userqueryids = implode(",",$userlookupids); // filter out the riff-raff
  119.  
  120. $userlookup = $db->query_read("SELECT user.userid, user.username, user.avatarrevision
  121. " . ($vbulletin->options['avatarenabled'] ? ', avatar.avatarpath, NOT ISNULL(customavatar.userid) AS hascustomavatar, customavatar.dateline AS avatardateline, customavatar.width_thumb AS avwidth_thumb, customavatar.height_thumb AS avheight_thumb, customavatar.width as avwidth, customavatar.height as avheight, customavatar.filedata_thumb' : '') . "
  122. FROM user
  123. " . ($vbulletin->options['avatarenabled'] ? "LEFT JOIN " . TABLE_PREFIX . "avatar AS avatar ON (avatar.avatarid = user.avatarid) LEFT JOIN " . TABLE_PREFIX . "customavatar AS customavatar ON (customavatar.userid = user.userid) " : '') . "
  124. WHERE user.userid IN ($userqueryids)");
  125. while($userdata = $db->fetch_array($userlookup)) {
  126. $notifications['data']['userdata'][] = $userdata;
  127. }
  128.  
  129. // we now have the content we need to lookup and perform joined database queries with
  130. // coalescing this data is better than performing so many queries for each notification
  131. if($lookupdata) {
  132. foreach($lookupdata as $key => $value) {
  133. $value = array_unique($value); // filter out duplicate references (read: posts)
  134. $queryids = implode(",",$value);
  135. switch($key) {
  136. case 'friendrequestids':
  137. $extendeds = $db->query_read("SELECT * FROM userlist WHERE relationid = $userid AND userid IN ($queryids) AND friend = 'pending'");
  138. while($extended = $db->fetch_array($extendeds)){
  139. $notifications['data'][$key][] = $extended;
  140. }
  141. break;
  142. case 'likedpostids':
  143. $extendeds = $db->query_read("SELECT postid, post.threadid, post.parentid, post.username, post.userid, post.title, post.dateline, pagetext FROM post JOIN thread ON thread.threadid = post.threadid WHERE post.postid IN ($queryids)");
  144. while($extended = $db->fetch_array($extendeds)){
  145. $extended['pagetext'] = explode('[/QUOTE]', $extended['pagetext']);
  146. if($extended['pagetext'][1]){
  147. $extended['pagetext'] = $extended['pagetext'][1];
  148. } else {
  149. $extended['pagetext'] = $extended['pagetext'][0];
  150. }
  151. $extended['pagetext'] = strip_tags(strip_bbcode($extended['pagetext']));
  152. $extended['sample'] = mb_substr($extended['pagetext'], 0, 55, "utf-8") . '...';
  153. unset($extended['pagetext']); // no longer needed
  154. $notifications['data'][$key][] = $extended;
  155. }
  156. break;
  157. case 'quotedpostids':
  158. $extendeds = $db->query_read("SELECT postid, post.threadid, post.parentid, post.username, post.userid, post.title, post.dateline, pagetext FROM post JOIN thread ON thread.threadid = post.threadid WHERE post.postid IN ($queryids)");
  159. while($extended = $db->fetch_array($extendeds)){
  160. $extended['pagetext'] = strip_tags($extended['pagetext']);
  161. $stripped = explode('[/QUOTE]', $extended['pagetext']);
  162. $stripped = end($stripped);
  163. $extended['pagetext'] = strip_bbcode($extended['pagetext']);
  164. $extended['sample'] = mb_substr($stripped, 0, 55, "utf-8") . '...';
  165. unset($extended['pagetext']); // no longer needed
  166. $notifications['data'][$key][] = $extended;
  167. }
  168. break;
  169. case 'blogcommentids':
  170. $extendeds = $db->query_read("SELECT * FROM blog_text WHERE blogid IN ($queryids)");
  171. while($extended = $db->fetch_array($extendeds)){
  172. $extended['pagetext'] = strip_tags(strip_bbcode($extended['pagetext']));
  173. $extended['sample'] = mb_substr($extended['pagetext'], 0, 55, "utf-8") . '...';
  174. unset($extended['pagetext']); // no longer needed
  175. $notifications['data'][$key][] = $extended;
  176. }
  177. break;
  178. case 'visitormessageids':
  179. $extendeds = $db->query_read("SELECT * FROM visitormessage WHERE vmid IN ($queryids)");
  180. while($extended = $db->fetch_array($extendeds)){
  181. $extended['pagetext'] = strip_tags(strip_bbcode($extended['pagetext']));
  182. $extended['sample'] = mb_substr($extended['pagetext'], 0, 100, "utf-8");
  183. unset($extended['pagetext']); // no longer needed
  184. $notifications['data'][$key][] = $extended;
  185. }
  186. break;
  187. case 'privatemessageids':
  188. $extendeds = $db->query_read("SELECT * FROM pm JOIN pmtext ON pmtext.pmtextid = pm.pmtextid WHERE pm.pmid IN ($queryids)");
  189. while($extended = $db->fetch_array($extendeds)){
  190. $extended['message'] = strip_tags(strip_bbcode($extended['message'],true));
  191. $extended['sample'] = mb_substr($extended['message'], 0, 100, "utf-8");
  192. unset($extended['message']); // no longer needed
  193. $notifications['data'][$key][] = $extended;
  194. }
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. }
  201.  
  202. // data has all been grabbed and made usable, so let’s spit it back out.
  203. if($_REQUEST['output'] == 'json') {
  204. print(json_encode($notifications, JSON_PARTIAL_OUTPUT_ON_ERROR));
  205.  
  206. // spit out the JSON
  207. // Note: the notifications have not been linked with the extended data that we've looked up
  208. // this should be done client-side to minimize the JSON payload
  209. }
  210. else {
  211. require_once(DIR . '/includes/functions_user.php');
  212.  
  213. // link the notifications with their extended data
  214. // this part is a pain
  215.  
  216. foreach($notifications['notifications'] AS $notification) {
  217. $notificationUserData = $notifications['data']['userdata'][array_search($notification['from_userid'], array_column($notifications['data']['userdata'], 'userid'))];
  218.  
  219. switch($notification['type']) {
  220. case 'privatemessage':
  221. $notification['url'] = $vbulletin->options['bburl'] . "/private.php?do=showpm&pmid=" . $notification['content_id'];
  222. $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> sent you a message.";
  223. $notificationData = $notifications['data']['privatemessageids'][array_search($notification['content_id'], array_column($notifications['data']['privatemessageids'], 'pmid'))];
  224. break;
  225. case 'visitormessage':
  226. $notification['url'] = $vbulletin->options['bburl'] . "/converse.php?u=" . $notification['from_userid'] . "&u2=" . $notification['for_userid'] . "&vmid=" . $notification['content_id'] . "#vmessage" . $notification['content_id'];
  227. $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> posted on your profile.";
  228. $notificationData = $notifications['data']['visitormessageids'][array_search($notification['content_id'], array_column($notifications['data']['visitormessageids'], 'vmid'))];
  229. break;
  230. case 'friendrequest':
  231. $notification['url'] = $vbulletin->options['bburl'] . "/settings.php?do=friends#irc";
  232. $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> would like to be your friend.";
  233. break;
  234. case 'likedpost':
  235. $notification['url'] = $vbulletin->options['bburl'] . "/showthread.php?p=" . $notification['content_id'] . "#post" . $notification['content_id'];
  236. $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> liked your post.";
  237. $notificationData = $notifications['data']['likedpostids'][array_search($notification['content_id'], array_column($notifications['data']['likedpostids'], 'postid'))];
  238. break;
  239. case 'quotedpost':
  240. $notification['url'] = $vbulletin->options['bburl'] . "/showthread.php?p=" . $notification['content_id'] . "#post" . $notification['content_id'];
  241. $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> quoted your post.";
  242. $notificationData = $notifications['data']['quotedpostids'][array_search($notification['content_id'], array_column($notifications['data']['quotedpostids'], 'postid'))];
  243. break;
  244. case 'blogcomment':
  245. $notification['url'] = $vbulletin->options['bburl'] . "/blog.php?b=" . $notification['content_id'] . "#comments";
  246. $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> commented on your blog entry.";
  247. break;
  248. default:
  249. break;
  250. }
  251.  
  252. eval('$notificationsloop .= "' . fetch_template('dashboard_notificationbit') . '";');
  253. unset($notificationData, $notificationUserData);
  254. }
  255.  
  256. // draw cp nav bar
  257. construct_usercp_nav('notifications');
  258.  
  259. $frmjmpsel['usercp'] = 'class="fjsel" selected="selected"';
  260.  
  261. eval('$HTML = "' . fetch_template('dashboard_notifications') . '";');
  262.  
  263. // build navbar
  264. $navbits = construct_navbits(array('' => $vbphrase['user_control_panel']));
  265. eval('$navbar = "' . fetch_template('navbar') . '";');
  266. eval('print_output("' . fetch_template('dashboard_shell') . '");');
  267. }
  268. /*
  269. if($_REQUEST['debug'] == 1) {
  270. print_r($notifications);
  271. }
  272. */
  273. }
  274. }
  275.  
  276. // ###################### Mark all as seen #######################
  277. // generally should be invoked as an XHR only
  278. if ($_REQUEST['do'] == 'markallseen')
  279. {
  280. if($vbulletin->userinfo['userid']){
  281.  
  282. $vbulletin->input->clean_array_gpc('g', array(
  283. 'cat' => TYPE_STR
  284. ));
  285. $unreadtime = TIMENOW;
  286. $category = $vbulletin->GPC['cat'];
  287. $userid = $vbulletin->userinfo['userid'];
  288.  
  289. $vbulletin->db->query_write("UPDATE notifications SET seen = 1 WHERE for_userid = $userid AND dateline < $unreadtime ". ($category == 'all' ? "" : "AND category = '$category'"));
  290. }
  291. }
  292.  
  293. // ###################### Mark all as seen #######################
  294. // generally should be invoked as an XHR only
  295. if ($_REQUEST['do'] == 'markallread')
  296. {
  297. if($vbulletin->userinfo['userid']){
  298.  
  299. $vbulletin->input->clean_array_gpc('g', array(
  300. 'cat' => TYPE_STR
  301. ));
  302. $unreadtime = TIMENOW;
  303. $category = $vbulletin->GPC['cat'];
  304. $userid = $vbulletin->userinfo['userid'];
  305.  
  306. $vbulletin->db->query_write("UPDATE notifications SET read_notification = 1, seen = 1 WHERE for_userid = $userid AND dateline < $unreadtime ". ($category == 'all' ? "" : "AND category = '$category'"));
  307. }
  308. }
  309. ?>
Advertisement
Add Comment
Please, Sign In to add comment