Viewing file: push.php (10.45 KB) -rw-rw-r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
/**
*
* @author Robert Johnston
*
* @package Forum Runner
* @version CVS/SVN: $Id: $
* @copyright (c) 2010 End of Time Studios, LLC
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
if (!defined('IN_PHPBB')) {
return;
}
require_once($phpbb_root_path . '/forumrunner/support/Snoopy.class.php');
require_once($phpbb_root_path . '/forumrunner/sitekey.php');
require_once($phpbb_root_path . '/forumrunner/version.php');
set_config('fr_last_push', time(), true);
// You must have your valid Forum Runner forum site key. This can be
// obtained from http://www.forumrunner.net in the Forum Manager.
if (!$mykey || $mykey == '') {
return;
}
// First of all, expire all users who have not logged in for 2 weeks, so
// we don't keep spamming the server with their entries.
$db->sql_query("
DELETE FROM {$table_prefix}forumrunner_push_users
WHERE last_login < DATE_SUB(NOW(), INTERVAL 14 DAY)
");
// Get list of users to check for updates to push
$sql = $db->sql_build_query('SELECT', array(
'SELECT' => 'u.user_id, fr.fr_username, fr.b, u.user_lastmark',
'FROM' => array(
$table_prefix . 'forumrunner_push_users' => 'fr',
),
'LEFT_JOIN' => array(
array(
'FROM' => array(USERS_TABLE => 'u'),
'ON' => 'u.user_id = fr.userid',
),
),
));
$result = $db->sql_query($sql);
$userids = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);
$out_msg = array();
foreach ($userids as $user) {
$pms = array();
$subs = array();
// Check for new PMs for this user
$sql = $db->sql_build_query('SELECT', array(
'SELECT' => 'u.username, pm.message_subject, pm.message_time, pm.msg_id',
'FROM' => array(
PRIVMSGS_TO_TABLE => 'pmt',
),
'LEFT_JOIN' => array(
array(
'FROM' => array(USERS_TABLE => 'u'),
'ON' => 'u.user_id = pmt.author_id',
),
array(
'FROM' => array(PRIVMSGS_TABLE => 'pm'),
'ON' => 'pm.msg_id = pmt.msg_id',
),
),
'WHERE' => 'pmt.pm_unread = 1 AND pmt.folder_id <> ' . PRIVMSGS_OUTBOX . ' AND pmt.user_id = ' . $user['user_id'],
'ORDER_BY' => 'pm.message_time ASC',
));
$result = $db->sql_query($sql);
$unreadpms = $db->sql_fetchrowset($result);
$db->sql_freeresult($result);
// Have some PMs. Check em out.
if (count($unreadpms)) {
$pmids = array();
foreach ($unreadpms as $pm) {
$pms[$pm['msg_id']] = $pm;
$pmids[] = $pm['msg_id'];
}
// We have our PM list. Now lets see which ones we've already sent
// and eliminate them.
$result = $db->sql_query("
SELECT pmid
FROM {$table_prefix}forumrunner_push_data
WHERE userid = " . $user['user_id'] . " AND pmid IN (" . implode(',', $pmids) . ")
");
while ($sentpm = $db->sql_fetchrow($result)) {
unset($pms[$sentpm['pmid']]);
}
$db->sql_freeresult($result);
// Save that we sent PM notices
foreach ($pms as $pm) {
$db->sql_query("
INSERT INTO {$table_prefix}forumrunner_push_data
(userid, pmid)
VALUES
({$user['user_id']}, {$pm['msg_id']})
");
}
}
// Subscriptions
$sql_array = array(
'SELECT' => 't.*, f.forum_name',
'FROM' => array(
TOPICS_WATCH_TABLE => 'tw',
TOPICS_TABLE => 't'
),
'WHERE' => 'tw.user_id = ' . $user['user_id'] . '
AND t.topic_id = tw.topic_id
AND ' . $db->sql_in_set('t.forum_id', $forbidden_forum_ary, true, true),
'ORDER_BY' => 't.topic_last_post_time DESC'
);
$sql_array['LEFT_JOIN'] = array();
$sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TABLE => 'f'), 'ON' => 't.forum_id = f.forum_id');
if ($config['load_db_lastread']) {
$sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TRACK_TABLE => 'ft'), 'ON' => 'ft.forum_id = t.forum_id AND ft.user_id = ' . $user['user_id']);
$sql_array['LEFT_JOIN'][] = array('FROM' => array(TOPICS_TRACK_TABLE => 'tt'), 'ON' => 'tt.topic_id = t.topic_id AND tt.user_id = ' . $user['user_id']);
$sql_array['SELECT'] .= ', tt.mark_time, ft.mark_time AS forum_mark_time';
}
$sql = $db->sql_build_query('SELECT', $sql_array);
$result = $db->sql_query($sql);
$topic_list = $topic_forum_list = $global_announce_list = $rowset = array();
while ($row = $db->sql_fetchrow($result)) {
$topic_id = (isset($row['b_topic_id'])) ? $row['b_topic_id'] : $row['topic_id'];
$topic_list[] = $topic_id;
$rowset[$topic_id] = $row;
$topic_forum_list[$row['forum_id']]['forum_mark_time'] = ($config['load_db_lastread']) ? $row['forum_mark_time'] : 0;
$topic_forum_list[$row['forum_id']]['topics'][] = $topic_id;
if ($row['topic_type'] == POST_GLOBAL) {
$global_announce_list[] = $topic_id;
}
}
$db->sql_freeresult($result);
$topic_tracking_info = array();
if ($config['load_db_lastread']) {
foreach ($topic_forum_list as $f_id => $topic_row) {
$topic_tracking_info += fr_get_topic_tracking($user['user_id'], $user['user_lastmark'], $f_id, $topic_row['topics'], $rowset, array($f_id => $topic_row['forum_mark_time']), ($f_id == 0) ? $global_announce_list : false);
}
} else {
foreach ($topic_forum_list as $f_id => $topic_row) {
$topic_tracking_info += fr_get_complete_topic_tracking($user['user_id'], $user['user_lastmark'], $f_id, $topic_row['topics'], $global_announce_list);
}
}
$subs = array();
foreach ($topic_list as $topic_id) {
$row = &$rowset[$topic_id];
$forum_id = $row['forum_id'];
$topic_id = (isset($row['b_topic_id'])) ? $row['b_topic_id'] : $row['topic_id'];
$unread_topic = (isset($topic_tracking_info[$topic_id]) && $row['topic_last_post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
if ($unread_topic) {
// This is an updated thread since last time they were on the forum
// Let's see if we sent this already
$result = $db->sql_query("
SELECT * FROM {$table_prefix}forumrunner_push_data
WHERE topicid = $topic_id AND userid = {$user['user_id']}
");
$push_threaddata = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if ($push_threaddata) {
// We have sent a notice about this thread at some point, lets see if
// our update is newer.
if ($push_threaddata['topicread'] < $row['topic_last_post_time']) {
// Yup. Send a notice and update table.
if ($push_threaddata['subsent']) {
continue;
}
$db->sql_query("
UPDATE {$table_prefix}forumrunner_push_data
SET topicread = {$row['topic_last_post_time']}, subsent = 1
WHERE id = {$push_threaddata['id']}
");
$subs[] = array(
'topicid' => $topic_id,
'title' => $row['topic_title'],
);
} // Already sent update
} else {
// Nope, send an update and insert new
$subs[] = array(
'topicid' => $topic_id,
'title' => $row['topic_title'],
);
$db->sql_query("
INSERT INTO {$table_prefix}forumrunner_push_data
(userid, topicid, topicread, subsent)
VALUES ({$user['user_id']}, $topic_id, {$row['topic_last_post_time']}, 1)
");
}
}
}
// Nothing to see here... move along....
$haspm = (count($pms) > 0);
$hassub = (count($subs) > 0);
if (!$haspm && !$hassub) {
continue;
}
// Forum name is always first arg.
$msgargs = array(base64_encode($config['sitename']));
$pmpart = 0;
if ($haspm) {
if (count($pms) > 1) {
$msgargs[] = base64_encode(count($pms));
$pmpart = 2;
} else {
$first_pm = array_shift($pms);
$msgargs[] = base64_encode($first_pm['username']);
$pmpart = 1;
}
}
$subpart = 0;
if ($hassub) {
if (count($subs) > 1) {
$msgargs[] = base64_encode(count($subs));
$subpart = 2;
} else {
$first_sub = array_shift($subs);
$msgargs[] = base64_encode($first_sub['title']);
$subpart = 1;
}
}
$out_msg[] = array(
'u' => $user['fr_username'],
'b' => $user['b'],
'pm' => $haspm,
'subs' => $hassub,
'm' => "__FR_PUSH_{$pmpart}PM_{$subpart}SUB",
'a' => $msgargs,
't' => count($pms) + count($subs),
);
}
// Send our update to Forum Runner central push server. Silently fail if
// necessary.
if (count($out_msg) > 0) {
$snoopy = new snoopy();
$snoopy->submit('http://push.forumrunner.net/push.php',
array(
'k' => $mykey,
'm' => serialize($out_msg),
'v' => $fr_version,
'p' => $fr_platform,
)
);
}
// End of Excecution. Support functions below.
function
fr_get_topic_tracking ($userid, $user_lastmark, $forum_id, $topic_ids, &$rowset, $forum_mark_time, $global_announce_list = false)
{
global $config;
$last_read = array();
if (!is_array($topic_ids)) {
$topic_ids = array($topic_ids);
}
foreach ($topic_ids as $topic_id) {
if (!empty($rowset[$topic_id]['mark_time'])) {
$last_read[$topic_id] = $rowset[$topic_id]['mark_time'];
}
}
$topic_ids = array_diff($topic_ids, array_keys($last_read));
if (sizeof($topic_ids)) {
$mark_time = array();
// Get global announcement info
if ($global_announce_list && sizeof($global_announce_list)) {
if (!isset($forum_mark_time[0])) {
global $db;
$sql = 'SELECT mark_time
FROM ' . FORUMS_TRACK_TABLE . "
WHERE user_id = $userid
AND forum_id = 0";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
if ($row) {
$mark_time[0] = $row['mark_time'];
}
} else {
if ($forum_mark_time[0] !== false) {
$mark_time[0] = $forum_mark_time[0];
}
}
}
if (!empty($forum_mark_time[$forum_id]) && $forum_mark_time[$forum_id] !== false) {
$mark_time[$forum_id] = $forum_mark_time[$forum_id];
}
$user_lastmark = (isset($mark_time[$forum_id])) ? $mark_time[$forum_id] : $user_lastmark;
foreach ($topic_ids as $topic_id) {
if ($global_announce_list && isset($global_announce_list[$topic_id])) {
$last_read[$topic_id] = (isset($mark_time[0])) ? $mark_time[0] : $user_lastmark;
} else {
$last_read[$topic_id] = $user_lastmark;
}
}
}
return $last_read;
}
function
fr_get_complete_topic_tracking ($userid, $user_lastmark, $forum_id, $topic_ids, $global_announce_list = false)
{
global $config;
$last_read = array();
if (!is_array($topic_ids)) {
$topic_ids = array($topic_ids);
}
if (sizeof($topic_ids)) {
foreach ($topic_ids as $topic_id) {
$last_read[$topic_id] = $user_lastmark;
}
}
return $last_read;
}
?>
|