aboutsummaryrefslogtreecommitdiffstats
path: root/mod/ping.php
diff options
context:
space:
mode:
Diffstat (limited to 'mod/ping.php')
-rw-r--r--mod/ping.php122
1 files changed, 77 insertions, 45 deletions
diff --git a/mod/ping.php b/mod/ping.php
index ac12e2fc0..8a498d543 100644
--- a/mod/ping.php
+++ b/mod/ping.php
@@ -1,9 +1,22 @@
<?php
-
+/**
+ * @file mod/ping.php
+ *
+ */
require_once('include/bbcode.php');
require_once('include/notify.php');
+/**
+ * @brief do several updates when pinged.
+ *
+ * This function does several tasks. Whenever called it checks for new messages,
+ * introductions, notifications, etc. and returns a json with the results.
+ *
+ * @param App &$a
+ * @result JSON
+ */
+
function ping_init(&$a) {
$result = array();
@@ -28,15 +41,31 @@ function ping_init(&$a) {
header("content-type: application/json");
+ /**
+ * If you have several windows open to this site and switch to a different channel
+ * in one of them, the others may get into a confused state showing you a page or options
+ * on that page which were only valid under the old identity. You session has changed.
+ * Therefore we send a notification of this fact back to the browser where it is picked up
+ * in javascript and which reloads the page it is on so that it is valid under the context
+ * of the now current channel.
+ */
+
$result['invalid'] = ((intval($_GET['uid'])) && (intval($_GET['uid']) != local_user()) ? 1 : 0);
- if(x($_SESSION,'sysmsg')){
+ /**
+ * Send all system messages (alerts) to the browser.
+ * Some are marked as informational and some represent
+ * errors or serious notifications. These typically
+ * will popup on the current page (no matter what page it is)
+ */
+
+ if(x($_SESSION, 'sysmsg')){
foreach ($_SESSION['sysmsg'] as $m){
$result['notice'][] = array('message' => $m);
}
unset($_SESSION['sysmsg']);
}
- if(x($_SESSION,'sysmsg_info')){
+ if(x($_SESSION, 'sysmsg_info')){
foreach ($_SESSION['sysmsg_info'] as $m){
$result['info'][] = array('message' => $m);
}
@@ -48,6 +77,9 @@ function ping_init(&$a) {
killme();
}
+ /**
+ * Update chat presence indication (if applicable)
+ */
if(get_observer_hash() && (! $result['invalid'])) {
$r = q("select cp_id, cp_room from chatpresence where cp_xchan = '%s' and cp_client = '%s' and cp_room = 0 limit 1",
@@ -73,6 +105,11 @@ function ping_init(&$a) {
}
}
+ /**
+ * Chatpresence continued... if somebody hasn't pinged recently, they've most likely left the page
+ * and shouldn't count as online anymore. We allow an expection for bots.
+ */
+
q("delete from chatpresence where cp_last < UTC_TIMESTAMP() - INTERVAL 3 MINUTE and cp_client != 'auto' ");
if((! local_user()) || ($result['invalid'])) {
@@ -80,8 +117,17 @@ function ping_init(&$a) {
killme();
}
- if(x($_REQUEST,'markRead') && local_user()) {
+ /**
+ * Everything following is only permitted under the context of a locally authenticated site member.
+ */
+
+ /**
+ * Handle "mark all xyz notifications read" requests.
+ */
+
+ // mark all items read
+ if(x($_REQUEST, 'markRead') && local_user()) {
switch($_REQUEST['markRead']) {
case 'network':
$r = q("update item set item_flags = ( item_flags ^ %d ) where (item_flags & %d) and uid = %d",
@@ -90,7 +136,6 @@ function ping_init(&$a) {
intval(local_user())
);
break;
-
case 'home':
$r = q("update item set item_flags = ( item_flags ^ %d ) where (item_flags & %d) and (item_flags & %d) and uid = %d",
intval(ITEM_UNSEEN),
@@ -111,19 +156,23 @@ function ping_init(&$a) {
intval(local_user())
);
break;
-
case 'notify':
$r = q("update notify set seen = 1 where uid = %d",
intval(local_user())
);
break;
-
default:
break;
}
}
+
+ /**
+ * URL ping/something will return detail for "something", e.g. a json list with which to populate a notification
+ * dropdown menu.
+ */
+
if(argc() > 1 && argv(1) === 'notify') {
$t = q("select count(*) as total from notify where uid = %d and seen = 0",
intval(local_user())
@@ -163,12 +212,9 @@ function ping_init(&$a) {
echo json_encode(array('notify' => $notifs));
killme();
-
}
-
if(argc() > 1 && argv(1) === 'messages') {
-
$channel = $a->get_channel();
$t = q("select mail.*, xchan.* from mail left join xchan on xchan_hash = from_xchan
where channel_id = %d and not ( mail_flags & %d ) and not (mail_flags & %d )
@@ -181,7 +227,6 @@ function ping_init(&$a) {
if($t) {
foreach($t as $zz) {
-// $msg = sprintf( t('sent you a private message.'), $zz['xchan_name']);
$notifs[] = array(
'notify_link' => $a->get_baseurl() . '/mail/' . $zz['id'],
'name' => $zz['xchan_name'],
@@ -196,14 +241,9 @@ function ping_init(&$a) {
echo json_encode(array('notify' => $notifs));
killme();
-
}
-
-
-
if(argc() > 1 && (argv(1) === 'network' || argv(1) === 'home')) {
-
$result = array();
$r = q("SELECT * FROM item
@@ -220,15 +260,13 @@ function ping_init(&$a) {
continue;
$result[] = format_notification($item);
}
- }
- logger('ping: ' . print_r($result,true));
+ }
+ logger('ping (network||home): ' . print_r($result, true), LOGGER_DATA);
echo json_encode(array('notify' => $result));
killme();
-
}
if(argc() > 1 && (argv(1) === 'intros')) {
-
$result = array();
$r = q("SELECT abook.*, xchan.* FROM abook left join xchan on abook_xchan = xchan_hash
@@ -250,15 +288,13 @@ function ping_init(&$a) {
'message' => t('added your channel')
);
}
- }
- logger('ping: ' . print_r($result,true));
+ }
+ logger('ping (intros): ' . print_r($result, true), LOGGER_DATA);
echo json_encode(array('notify' => $result));
killme();
-
}
if(argc() > 1 && (argv(1) === 'all_events')) {
-
$bd_format = t('g A l F d') ; // 8 AM Friday January 18
$result = array();
@@ -267,22 +303,21 @@ function ping_init(&$a) {
WHERE `event`.`uid` = %d AND start < '%s' AND start > '%s' and `ignore` = 0
ORDER BY `start` DESC ",
intval(local_user()),
- dbesc(datetime_convert('UTC',date_default_timezone_get(),'now + 7 days')),
- dbesc(datetime_convert('UTC',date_default_timezone_get(),'now - 1 days'))
+ dbesc(datetime_convert('UTC', date_default_timezone_get(), 'now + 7 days')),
+ dbesc(datetime_convert('UTC', date_default_timezone_get(), 'now - 1 days'))
);
if($r) {
foreach($r as $rr) {
if($rr['adjust'])
- $md = datetime_convert('UTC',date_default_timezone_get(),$rr['start'],'Y/m');
+ $md = datetime_convert('UTC', date_default_timezone_get(), $rr['start'], 'Y/m');
else
- $md = datetime_convert('UTC','UTC',$rr['start'],'Y/m');
+ $md = datetime_convert('UTC', 'UTC', $rr['start'], 'Y/m');
- $strt = datetime_convert('UTC',$rr['convert'] ? date_default_timezone_get() : 'UTC',$rr['start']);
- $today = ((substr($strt,0,10) === datetime_convert('UTC',date_default_timezone_get(),'now','Y-m-d')) ? true : false);
+ $strt = datetime_convert('UTC', (($rr['adjust']) ? date_default_timezone_get() : 'UTC'), $rr['start']);
+ $today = ((substr($strt, 0, 10) === datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y-m-d')) ? true : false);
- $when = day_translate(datetime_convert('UTC', $rr['adjust'] ? date_default_timezone_get() : 'UTC', $rr['start'], $bd_format)) . (($today) ? ' ' . t('[today]') : '');
-
+ $when = day_translate(datetime_convert('UTC', (($rr['adjust']) ? date_default_timezone_get() : 'UTC'), $rr['start'], $bd_format)) . (($today) ? ' ' . t('[today]') : '');
$result[] = array(
'notify_link' => $a->get_baseurl() . '/events', // FIXME this takes you to an edit page and it may not be yours, we really want to just view the single event --> '/events/event/' . $rr['event_hash'],
@@ -294,15 +329,17 @@ function ping_init(&$a) {
'message' => t('posted an event')
);
}
- }
- logger('ping: ' . print_r($result,true));
+ }
+ logger('ping (all_events): ' . print_r($result, true), LOGGER_DATA);
echo json_encode(array('notify' => $result));
killme();
-
}
- // Normal ping - just the counts
+
+ /**
+ * Normal ping - just the counts, no detail
+ */
$t = q("select count(*) as total from notify where uid = %d and seen = 0",
intval(local_user())
@@ -310,7 +347,6 @@ function ping_init(&$a) {
if($t)
$result['notify'] = intval($t[0]['total']);
-
$t1 = dba_timer();
$r = q("SELECT id, item_restrict, item_flags FROM item
@@ -321,7 +357,6 @@ function ping_init(&$a) {
);
if(count($r)) {
-
$arr = array('items' => $r);
call_hooks('network_ping', $arr);
@@ -333,7 +368,6 @@ function ping_init(&$a) {
}
}
-
$t2 = dba_timer();
$intr = q("select count(abook_id) as total from abook where (abook_flags & %d) and abook_channel = %d",
@@ -372,15 +406,15 @@ function ping_init(&$a) {
WHERE `event`.`uid` = %d AND start < '%s' AND start > '%s' and `ignore` = 0
ORDER BY `start` ASC ",
intval(local_user()),
- dbesc(datetime_convert('UTC',date_default_timezone_get(),'now + 7 days')),
- dbesc(datetime_convert('UTC',date_default_timezone_get(),'now - 1 days'))
+ dbesc(datetime_convert('UTC', date_default_timezone_get(), 'now + 7 days')),
+ dbesc(datetime_convert('UTC', date_default_timezone_get(), 'now - 1 days'))
);
if($events) {
$result['all_events'] = count($events);
if($result['all_events']) {
- $str_now = datetime_convert('UTC',$a->timezone,'now','Y-m-d');
+ $str_now = datetime_convert('UTC', date_default_timezone_get(), 'now', 'Y-m-d');
foreach($events as $x) {
$bd = false;
if($x['type'] === 'birthday') {
@@ -390,7 +424,7 @@ function ping_init(&$a) {
else {
$result['events'] ++;
}
- if(datetime_convert('UTC',((intval($x['adjust'])) ? $a->timezone : 'UTC'), $x['start'],'Y-m-d') === $str_now) {
+ if(datetime_convert('UTC', ((intval($x['adjust'])) ? date_default_timezone_get() : 'UTC'), $x['start'], 'Y-m-d') === $str_now) {
$result['all_events_today'] ++;
if($bd)
$result['birthdays_today'] ++;
@@ -409,6 +443,4 @@ function ping_init(&$a) {
echo $x;
killme();
-
}
-