Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * PokéCommunity Notifications Script
- *
- * Outputs notifications as AJAX for use in the New UI.
- *
- * @package PokéCommunity
- * @subpackage Notifications System
- * @version 1.0
- */
- // ####################### SET PHP ENVIRONMENT ###########################
- error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
- // #################### DEFINE IMPORTANT CONSTANTS #######################
- define('THIS_SCRIPT', 'notifications');
- define('CSRF_PROTECTION', true);
- define('DISABLE_ADVERTISING', true);
- $phrasegroups = array();
- $specialtemplates = array();
- $globaltemplates = array();
- $actiontemplates = array();
- $dateline = "l, M jS, Y";
- $timeline = "g:ia";
- require_once('./global.php');
- if(!$vbulletin->userinfo['userid']) {
- print_no_permission();
- }
- unset($notifications); // unset vBulletin's dumb notifications system
- if (empty($_REQUEST['do']))
- {
- $_REQUEST['do'] = 'list';
- }
- // ####################### List all notifications ########################
- if ($_REQUEST['do'] == 'list')
- {
- /** GET params:
- cat category
- type type of notifications (must be part of the specified category)
- from range of notifications to start from
- preq number of notifications per request
- **/
- $vbulletin->input->clean_array_gpc('g', array(
- 'cat' => TYPE_STR,
- 'type' => TYPE_STR,
- 'from' => TYPE_INT,
- 'preq' => TYPE_INT
- ));
- $range_lower = ($vbulletin->GPC['from'] ?: "0");
- $max_notifications = ($vbulletin->GPC['preq'] ?: "20");
- if($vbulletin->userinfo['userid']){
- $userid = $vbulletin->userinfo['userid'];
- $notificationsQuery = get_user_notifications($userid,$vbulletin->GPC['cat'],$vbulletin->GPC['type'],$range_lower,$max_notifications);
- while($notification = $db->fetch_array($notificationsQuery)){
- $notification['notificationdate'] = vbdate($vbulletin->options['dateformat'], $notification['dateline'], true);
- $notification['notificationtime'] = vbdate($vbulletin->options['timeformat'], $notification['dateline']);
- $notifications['notifications'][] = $notification;
- }
- if (empty($notifications)) {
- $notifications['error'] = "no-notifications";
- exit(json_encode($notifications));
- }
- unset($notification);
- $userlookupids[] = $userid;
- if($_REQUEST['output'] == 'json' && $_REQUEST['debug'] == '1') {
- print(json_encode($notifications));
- }
- // iterate through notifications and get content IDs for relevant notifications
- if($notifications['notifications']) {
- foreach ($notifications['notifications'] as $notification) {
- switch($notification['type']) {
- case 'friendrequest':
- $lookupdata['friendrequestids'][] = $notification['content_id'];
- break;
- case 'likedpost':
- $lookupdata['likedpostids'][] = $notification['content_id'];
- break;
- case 'quotedpost':
- $lookupdata['quotedpostids'][] = $notification['content_id'];
- break;
- case 'blogcomment':
- $lookupdata['blogcommentids'][] = $notification['content_id'];
- break;
- case 'visitormessage':
- $lookupdata['visitormessageids'][] = $notification['content_id'];
- break;
- case 'privatemessage':
- $lookupdata['privatemessageids'][] = $notification['content_id'];
- break;
- default:
- // we aren't looking up anything
- break;
- }
- // also get user IDs for every notification
- $userlookupids[] = $notification['from_userid'];
- }
- }
- // look up basic user info (and avatars) for the user and anybody they're receiving notifications from
- $userlookupids = array_unique($userlookupids);
- $userqueryids = implode(",",$userlookupids); // filter out the riff-raff
- $userlookup = $db->query_read("SELECT user.userid, user.username, user.avatarrevision
- " . ($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' : '') . "
- FROM user
- " . ($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) " : '') . "
- WHERE user.userid IN ($userqueryids)");
- while($userdata = $db->fetch_array($userlookup)) {
- $notifications['data']['userdata'][] = $userdata;
- }
- // we now have the content we need to lookup and perform joined database queries with
- // coalescing this data is better than performing so many queries for each notification
- if($lookupdata) {
- foreach($lookupdata as $key => $value) {
- $value = array_unique($value); // filter out duplicate references (read: posts)
- $queryids = implode(",",$value);
- switch($key) {
- case 'friendrequestids':
- $extendeds = $db->query_read("SELECT * FROM userlist WHERE relationid = $userid AND userid IN ($queryids) AND friend = 'pending'");
- while($extended = $db->fetch_array($extendeds)){
- $notifications['data'][$key][] = $extended;
- }
- break;
- case 'likedpostids':
- $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)");
- while($extended = $db->fetch_array($extendeds)){
- $extended['pagetext'] = explode('[/QUOTE]', $extended['pagetext']);
- if($extended['pagetext'][1]){
- $extended['pagetext'] = $extended['pagetext'][1];
- } else {
- $extended['pagetext'] = $extended['pagetext'][0];
- }
- $extended['pagetext'] = strip_tags(strip_bbcode($extended['pagetext']));
- $extended['sample'] = mb_substr($extended['pagetext'], 0, 55, "utf-8") . '...';
- unset($extended['pagetext']); // no longer needed
- $notifications['data'][$key][] = $extended;
- }
- break;
- case 'quotedpostids':
- $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)");
- while($extended = $db->fetch_array($extendeds)){
- $extended['pagetext'] = strip_tags($extended['pagetext']);
- $stripped = explode('[/QUOTE]', $extended['pagetext']);
- $stripped = end($stripped);
- $extended['pagetext'] = strip_bbcode($extended['pagetext']);
- $extended['sample'] = mb_substr($stripped, 0, 55, "utf-8") . '...';
- unset($extended['pagetext']); // no longer needed
- $notifications['data'][$key][] = $extended;
- }
- break;
- case 'blogcommentids':
- $extendeds = $db->query_read("SELECT * FROM blog_text WHERE blogid IN ($queryids)");
- while($extended = $db->fetch_array($extendeds)){
- $extended['pagetext'] = strip_tags(strip_bbcode($extended['pagetext']));
- $extended['sample'] = mb_substr($extended['pagetext'], 0, 55, "utf-8") . '...';
- unset($extended['pagetext']); // no longer needed
- $notifications['data'][$key][] = $extended;
- }
- break;
- case 'visitormessageids':
- $extendeds = $db->query_read("SELECT * FROM visitormessage WHERE vmid IN ($queryids)");
- while($extended = $db->fetch_array($extendeds)){
- $extended['pagetext'] = strip_tags(strip_bbcode($extended['pagetext']));
- $extended['sample'] = mb_substr($extended['pagetext'], 0, 100, "utf-8");
- unset($extended['pagetext']); // no longer needed
- $notifications['data'][$key][] = $extended;
- }
- break;
- case 'privatemessageids':
- $extendeds = $db->query_read("SELECT * FROM pm JOIN pmtext ON pmtext.pmtextid = pm.pmtextid WHERE pm.pmid IN ($queryids)");
- while($extended = $db->fetch_array($extendeds)){
- $extended['message'] = strip_tags(strip_bbcode($extended['message'],true));
- $extended['sample'] = mb_substr($extended['message'], 0, 100, "utf-8");
- unset($extended['message']); // no longer needed
- $notifications['data'][$key][] = $extended;
- }
- break;
- default:
- break;
- }
- }
- }
- // data has all been grabbed and made usable, so let’s spit it back out.
- if($_REQUEST['output'] == 'json') {
- print(json_encode($notifications, JSON_PARTIAL_OUTPUT_ON_ERROR));
- // spit out the JSON
- // Note: the notifications have not been linked with the extended data that we've looked up
- // this should be done client-side to minimize the JSON payload
- }
- else {
- require_once(DIR . '/includes/functions_user.php');
- // link the notifications with their extended data
- // this part is a pain
- foreach($notifications['notifications'] AS $notification) {
- $notificationUserData = $notifications['data']['userdata'][array_search($notification['from_userid'], array_column($notifications['data']['userdata'], 'userid'))];
- switch($notification['type']) {
- case 'privatemessage':
- $notification['url'] = $vbulletin->options['bburl'] . "/private.php?do=showpm&pmid=" . $notification['content_id'];
- $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> sent you a message.";
- $notificationData = $notifications['data']['privatemessageids'][array_search($notification['content_id'], array_column($notifications['data']['privatemessageids'], 'pmid'))];
- break;
- case 'visitormessage':
- $notification['url'] = $vbulletin->options['bburl'] . "/converse.php?u=" . $notification['from_userid'] . "&u2=" . $notification['for_userid'] . "&vmid=" . $notification['content_id'] . "#vmessage" . $notification['content_id'];
- $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> posted on your profile.";
- $notificationData = $notifications['data']['visitormessageids'][array_search($notification['content_id'], array_column($notifications['data']['visitormessageids'], 'vmid'))];
- break;
- case 'friendrequest':
- $notification['url'] = $vbulletin->options['bburl'] . "/settings.php?do=friends#irc";
- $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> would like to be your friend.";
- break;
- case 'likedpost':
- $notification['url'] = $vbulletin->options['bburl'] . "/showthread.php?p=" . $notification['content_id'] . "#post" . $notification['content_id'];
- $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> liked your post.";
- $notificationData = $notifications['data']['likedpostids'][array_search($notification['content_id'], array_column($notifications['data']['likedpostids'], 'postid'))];
- break;
- case 'quotedpost':
- $notification['url'] = $vbulletin->options['bburl'] . "/showthread.php?p=" . $notification['content_id'] . "#post" . $notification['content_id'];
- $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> quoted your post.";
- $notificationData = $notifications['data']['quotedpostids'][array_search($notification['content_id'], array_column($notifications['data']['quotedpostids'], 'postid'))];
- break;
- case 'blogcomment':
- $notification['url'] = $vbulletin->options['bburl'] . "/blog.php?b=" . $notification['content_id'] . "#comments";
- $notification['message'] = "<strong>" . $notification['from_username'] . "</strong> commented on your blog entry.";
- break;
- default:
- break;
- }
- eval('$notificationsloop .= "' . fetch_template('dashboard_notificationbit') . '";');
- unset($notificationData, $notificationUserData);
- }
- // draw cp nav bar
- construct_usercp_nav('notifications');
- $frmjmpsel['usercp'] = 'class="fjsel" selected="selected"';
- eval('$HTML = "' . fetch_template('dashboard_notifications') . '";');
- // build navbar
- $navbits = construct_navbits(array('' => $vbphrase['user_control_panel']));
- eval('$navbar = "' . fetch_template('navbar') . '";');
- eval('print_output("' . fetch_template('dashboard_shell') . '");');
- }
- /*
- if($_REQUEST['debug'] == 1) {
- print_r($notifications);
- }
- */
- }
- }
- // ###################### Mark all as seen #######################
- // generally should be invoked as an XHR only
- if ($_REQUEST['do'] == 'markallseen')
- {
- if($vbulletin->userinfo['userid']){
- $vbulletin->input->clean_array_gpc('g', array(
- 'cat' => TYPE_STR
- ));
- $unreadtime = TIMENOW;
- $category = $vbulletin->GPC['cat'];
- $userid = $vbulletin->userinfo['userid'];
- $vbulletin->db->query_write("UPDATE notifications SET seen = 1 WHERE for_userid = $userid AND dateline < $unreadtime ". ($category == 'all' ? "" : "AND category = '$category'"));
- }
- }
- // ###################### Mark all as seen #######################
- // generally should be invoked as an XHR only
- if ($_REQUEST['do'] == 'markallread')
- {
- if($vbulletin->userinfo['userid']){
- $vbulletin->input->clean_array_gpc('g', array(
- 'cat' => TYPE_STR
- ));
- $unreadtime = TIMENOW;
- $category = $vbulletin->GPC['cat'];
- $userid = $vbulletin->userinfo['userid'];
- $vbulletin->db->query_write("UPDATE notifications SET read_notification = 1, seen = 1 WHERE for_userid = $userid AND dateline < $unreadtime ". ($category == 'all' ? "" : "AND category = '$category'"));
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment