2016-04-19 03:38:38 +00:00
< ? php
2016-05-24 08:25:13 +00:00
2016-10-01 22:41:25 +00:00
namespace Zotlabs\Module ;
2016-04-19 03:38:38 +00:00
2019-04-16 00:34:29 +00:00
use App ;
use Zotlabs\Web\Controller ;
use Zotlabs\Lib\Enotify ;
2019-09-22 06:18:20 +00:00
use Zotlabs\Lib\Apps ;
2019-04-16 00:34:29 +00:00
2016-04-19 03:38:38 +00:00
require_once ( 'include/bbcode.php' );
2016-05-24 08:25:13 +00:00
2016-04-19 03:38:38 +00:00
/**
2016-10-01 22:41:25 +00:00
* @ brief Ping Controller .
2019-04-16 00:34:29 +00:00
* Called from the client at regular intervals to check for updates from the server
2016-04-19 03:38:38 +00:00
*
*/
2019-04-16 00:34:29 +00:00
class Ping extends Controller {
2016-04-19 03:38:38 +00:00
2016-10-01 22:41:25 +00:00
/**
* @ 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 .
*
* @ result JSON
*/
2019-04-16 00:34:29 +00:00
2016-04-19 03:38:38 +00:00
function init () {
2017-10-06 14:46:27 +00:00
2019-04-16 00:34:29 +00:00
$result = [];
$notifs = [];
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
$result [ 'notify' ] = 0 ;
$result [ 'home' ] = 0 ;
2020-02-13 05:36:21 +00:00
$result [ 'stream' ] = 0 ;
2016-04-19 03:38:38 +00:00
$result [ 'intros' ] = 0 ;
$result [ 'mail' ] = 0 ;
$result [ 'register' ] = 0 ;
2019-03-28 02:20:35 +00:00
$result [ 'moderate' ] = 0 ;
2016-04-19 03:38:38 +00:00
$result [ 'events' ] = 0 ;
$result [ 'events_today' ] = 0 ;
$result [ 'birthdays' ] = 0 ;
$result [ 'birthdays_today' ] = 0 ;
$result [ 'all_events' ] = 0 ;
$result [ 'all_events_today' ] = 0 ;
2018-06-02 20:11:11 +00:00
$result [ 'notice' ] = [];
$result [ 'info' ] = [];
2017-07-28 20:42:02 +00:00
$result [ 'pubs' ] = 0 ;
$result [ 'files' ] = 0 ;
2018-06-01 21:22:52 +00:00
$result [ 'forums' ] = 0 ;
2018-06-02 20:11:11 +00:00
$result [ 'forums_sub' ] = [];
2019-04-16 00:34:29 +00:00
$result [ 'reports' ] = 0 ;
if ( ! $_SESSION [ 'static_loadtime' ]) {
2017-07-29 10:24:03 +00:00
$_SESSION [ 'static_loadtime' ] = datetime_convert ();
2019-04-16 00:34:29 +00:00
}
2016-04-19 03:38:38 +00:00
$vnotify = false ;
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
$item_normal = item_normal ();
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( local_channel ()) {
2016-04-19 03:38:38 +00:00
$vnotify = get_pconfig ( local_channel (), 'system' , 'vnotify' );
$evdays = intval ( get_pconfig ( local_channel (), 'system' , 'evdays' ));
$ob_hash = get_observer_hash ();
}
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
// if unset show all visual notification types
2019-04-16 00:34:29 +00:00
if ( $vnotify === false ) {
2016-04-19 03:38:38 +00:00
$vnotify = ( - 1 );
2019-04-16 00:34:29 +00:00
}
if ( $evdays < 1 ) {
2016-04-19 03:38:38 +00:00
$evdays = 3 ;
2019-04-16 00:34:29 +00:00
}
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
/**
* If you have several windows open to this site and switch to a different channel
2016-10-01 22:41:25 +00:00
* in one of them , the others may get into a confused state showing you a page or options
2016-04-19 03:38:38 +00:00
* 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
2016-10-01 22:41:25 +00:00
* of the now current channel .
2016-04-19 03:38:38 +00:00
*/
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
$result [ 'invalid' ] = (( intval ( $_GET [ 'uid' ])) && ( intval ( $_GET [ 'uid' ]) != local_channel ()) ? 1 : 0 );
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
/**
* 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 )
*/
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( x ( $_SESSION , 'sysmsg' )) {
foreach ( $_SESSION [ 'sysmsg' ] as $m ) {
2016-04-19 03:38:38 +00:00
$result [ 'notice' ][] = array ( 'message' => $m );
}
unset ( $_SESSION [ 'sysmsg' ]);
}
2019-04-16 00:34:29 +00:00
if ( x ( $_SESSION , 'sysmsg_info' )) {
foreach ( $_SESSION [ 'sysmsg_info' ] as $m ) {
2016-04-19 03:38:38 +00:00
$result [ 'info' ][] = array ( 'message' => $m );
}
unset ( $_SESSION [ 'sysmsg_info' ]);
}
2019-04-16 00:34:29 +00:00
if ( ! ( $vnotify & VNOTIFY_INFO )) {
2016-04-19 03:38:38 +00:00
$result [ 'info' ] = array ();
2019-04-16 00:34:29 +00:00
}
if ( ! ( $vnotify & VNOTIFY_ALERT )) {
2016-04-19 03:38:38 +00:00
$result [ 'notice' ] = array ();
2019-04-16 00:34:29 +00:00
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
// If we're currently installing, there won't be a populated database.
// So just send back what we have and stop here.
if ( App :: $install ) {
json_return_and_die ( $result );
2016-04-19 03:38:38 +00:00
}
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
/**
* Update chat presence indication ( if applicable )
*/
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( get_observer_hash () && ( ! $result [ 'invalid' ])) {
2016-04-19 03:38:38 +00:00
$r = q ( " select cp_id, cp_room from chatpresence where cp_xchan = '%s' and cp_client = '%s' and cp_room = 0 limit 1 " ,
dbesc ( get_observer_hash ()),
dbesc ( $_SERVER [ 'REMOTE_ADDR' ])
);
$basic_presence = false ;
2019-04-16 00:34:29 +00:00
if ( $r ) {
2016-10-01 22:41:25 +00:00
$basic_presence = true ;
2016-04-19 03:38:38 +00:00
q ( " update chatpresence set cp_last = '%s' where cp_id = %d " ,
dbesc ( datetime_convert ()),
intval ( $r [ 0 ][ 'cp_id' ])
);
}
2019-04-16 00:34:29 +00:00
if ( ! $basic_presence ) {
2016-04-19 03:38:38 +00:00
q ( " insert into chatpresence ( cp_xchan, cp_last, cp_status, cp_client)
values ( '%s' , '%s' , '%s' , '%s' ) " ,
dbesc ( get_observer_hash ()),
dbesc ( datetime_convert ()),
dbesc ( 'online' ),
dbesc ( $_SERVER [ 'REMOTE_ADDR' ])
);
}
}
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
/**
* 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 .
*/
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
q ( " delete from chatpresence where cp_last < %s - INTERVAL %s and cp_client != 'auto' " ,
db_utcnow (), db_quoteinterval ( '3 MINUTE' )
2016-10-01 22:41:25 +00:00
);
2018-04-11 09:49:33 +00:00
$sql_extra = '' ;
2019-04-16 00:34:29 +00:00
if ( ! ( $vnotify & VNOTIFY_LIKE )) {
2018-04-23 19:12:31 +00:00
$sql_extra = " AND verb NOT IN (' " . dbesc ( ACTIVITY_LIKE ) . " ', ' " . dbesc ( ACTIVITY_DISLIKE ) . " ') " ;
2019-04-16 00:34:29 +00:00
}
2018-04-11 09:49:33 +00:00
2018-04-08 23:51:11 +00:00
$discover_tab_on = can_view_public_stream ();
2017-10-06 14:46:27 +00:00
$notify_pubs = (( local_channel ()) ? ( $vnotify & VNOTIFY_PUBS ) && $discover_tab_on : $discover_tab_on );
2017-07-28 20:42:02 +00:00
2019-09-22 06:18:20 +00:00
if ( $notify_pubs && local_channel () && ! Apps :: system_app_installed ( local_channel (), 'Public Stream' )) {
$notify_pubs = false ;
}
2019-04-16 00:34:29 +00:00
$sys = get_sys_channel ();
2017-07-28 20:42:02 +00:00
2019-04-16 00:34:29 +00:00
if ( $notify_pubs ) {
2017-07-28 20:42:02 +00:00
$pubs = q ( " SELECT count(id) as total from item
WHERE uid = % d
AND item_unseen = 1
2018-02-08 08:47:49 +00:00
AND author_xchan != '%s'
2017-07-28 20:42:02 +00:00
AND created > '" . datetime_convert(' UTC ',' UTC ',$_SESSION[' static_loadtime ']) . "'
2018-04-11 09:49:33 +00:00
$item_normal
$sql_extra " ,
2017-07-29 10:24:03 +00:00
intval ( $sys [ 'channel_id' ]),
2018-02-04 19:42:40 +00:00
dbesc ( get_observer_hash ())
2017-07-28 20:42:02 +00:00
);
2019-04-16 00:34:29 +00:00
if ( $pubs ) {
2017-07-28 20:42:02 +00:00
$result [ 'pubs' ] = intval ( $pubs [ 0 ][ 'total' ]);
2019-04-16 00:34:29 +00:00
}
2017-07-28 20:42:02 +00:00
}
2019-04-16 00:34:29 +00:00
if (( argc () > 1 ) && ( argv ( 1 ) === 'pubs' ) && ( $notify_pubs )) {
2017-07-28 20:42:02 +00:00
2019-04-16 00:34:29 +00:00
$local_result = [];
2017-10-06 14:46:27 +00:00
$r = q ( " SELECT * FROM item
WHERE uid = % d
AND item_unseen = 1
2018-02-08 08:47:49 +00:00
AND author_xchan != '%s'
2017-10-06 14:46:27 +00:00
AND created > '" . datetime_convert(' UTC ',' UTC ',$_SESSION[' static_loadtime ']) . "'
$item_normal
2018-04-11 09:49:33 +00:00
$sql_extra
2017-10-06 14:46:27 +00:00
ORDER BY created DESC
LIMIT 300 " ,
intval ( $sys [ 'channel_id' ]),
2017-11-03 12:49:58 +00:00
dbesc ( get_observer_hash ())
2017-10-06 14:46:27 +00:00
);
2019-04-16 00:34:29 +00:00
if ( $r ) {
2017-10-06 14:46:27 +00:00
xchan_query ( $r );
2019-04-16 00:34:29 +00:00
foreach ( $r as $rr ) {
2017-10-07 13:40:34 +00:00
$rr [ 'llink' ] = str_replace ( 'display/' , 'pubstream/?f=&mid=' , $rr [ 'llink' ]);
2019-04-16 00:34:29 +00:00
$z = Enotify :: format ( $rr );
if ( $z ) {
$local_result [] = $z ;
2018-10-26 05:12:47 +00:00
}
2017-10-06 14:46:27 +00:00
}
}
2019-04-16 00:34:29 +00:00
json_return_and_die ( [ 'notify' => $local_result ] );
2017-10-06 14:46:27 +00:00
}
2019-04-16 00:34:29 +00:00
if (( ! local_channel ()) || ( $result [ 'invalid' ])) {
json_return_and_die ( $result );
2016-04-19 03:38:38 +00:00
}
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
/**
* Everything following is only permitted under the context of a locally authenticated site member .
*/
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
/**
* Handle " mark all xyz notifications read " requests .
*/
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
// mark all items read
2019-04-16 00:34:29 +00:00
if ( x ( $_REQUEST , 'markRead' ) && local_channel ()) {
switch ( $_REQUEST [ 'markRead' ]) {
2020-02-13 05:36:21 +00:00
case 'stream' :
2018-02-08 08:47:49 +00:00
$r = q ( " UPDATE item SET item_unseen = 0 WHERE uid = %d AND item_unseen = 1 " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ())
);
break ;
case 'home' :
2018-02-08 08:47:49 +00:00
$r = q ( " UPDATE item SET item_unseen = 0 WHERE uid = %d AND item_unseen = 1 AND item_wall = 1 " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ())
);
break ;
2017-11-06 08:34:20 +00:00
case 'mail' :
2018-02-08 08:47:49 +00:00
$r = q ( " UPDATE mail SET mail_seen = 1 WHERE channel_id = %d AND mail_seen = 0 " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ())
);
break ;
case 'all_events' :
2018-02-08 08:47:49 +00:00
$r = q ( " UPDATE event SET dismissed = 1 WHERE uid = %d AND dismissed = 0 AND dtstart < '%s' AND dtstart > '%s' " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ()),
dbesc ( datetime_convert ( 'UTC' , date_default_timezone_get (), 'now + ' . intval ( $evdays ) . ' days' )),
dbesc ( datetime_convert ( 'UTC' , date_default_timezone_get (), 'now - 1 days' ))
);
break ;
case 'notify' :
$r = q ( " update notify set seen = 1 where uid = %d " ,
intval ( local_channel ())
);
break ;
2017-10-06 14:46:27 +00:00
case 'pubs' :
unset ( $_SESSION [ 'static_loadtime' ]);
break ;
2016-04-19 03:38:38 +00:00
default :
break ;
}
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( x ( $_REQUEST , 'markItemRead' ) && local_channel ()) {
2018-02-08 08:47:49 +00:00
$r = q ( " UPDATE item SET item_unseen = 0 WHERE uid = %d AND parent = %d " ,
intval ( local_channel ()),
intval ( $_REQUEST [ 'markItemRead' ])
2016-04-19 03:38:38 +00:00
);
}
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
/**
* URL ping / something will return detail for " something " , e . g . a json list with which to populate a notification
* dropdown menu .
*/
2019-04-16 00:34:29 +00:00
if ( argc () > 1 && argv ( 1 ) === 'notify' ) {
2018-02-08 08:47:49 +00:00
$t = q ( " SELECT * FROM notify WHERE uid = %d AND seen = 0 ORDER BY CREATED DESC " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ())
);
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $t ) {
foreach ( $t as $tt ) {
2017-11-17 09:40:34 +00:00
$message = trim ( strip_tags ( bbcode ( $tt [ 'msg' ])));
2019-04-16 00:34:29 +00:00
if ( strpos ( $message , $tt [ 'xname' ]) === 0 )
2017-11-17 20:25:18 +00:00
$message = substr ( $message , strlen ( $tt [ 'xname' ]) + 1 );
2017-11-17 09:40:34 +00:00
2017-11-19 19:37:58 +00:00
$mid = basename ( $tt [ 'link' ]);
2019-07-07 23:32:48 +00:00
$mid = (( strpos ( $mid , 'b64.' ) === 0 ) ? @ base64url_decode ( substr ( $mid , 4 )) : $mid );
2017-11-19 19:37:58 +00:00
2019-04-16 00:34:29 +00:00
if ( in_array ( $tt [ 'verb' ], [ ACTIVITY_LIKE , ACTIVITY_DISLIKE ])) {
2018-02-13 10:01:58 +00:00
// we need the thread parent
$r = q ( " select thr_parent from item where mid = '%s' and uid = %d limit 1 " ,
dbesc ( $mid ),
intval ( local_channel ())
);
2018-04-17 16:56:33 +00:00
$b64mid = (( strpos ( $r [ 0 ][ 'thr_parent' ], 'b64.' ) === 0 ) ? $r [ 0 ][ 'thr_parent' ] : 'b64.' . base64url_encode ( $r [ 0 ][ 'thr_parent' ]));
2018-02-13 10:01:58 +00:00
}
else {
2018-04-17 16:56:33 +00:00
$b64mid = (( strpos ( $mid , 'b64.' ) === 0 ) ? $mid : 'b64.' . base64url_encode ( $mid ));
2018-02-13 10:01:58 +00:00
}
2017-11-19 19:37:58 +00:00
2016-04-19 03:38:38 +00:00
$notifs [] = array (
2017-10-22 19:01:58 +00:00
'notify_link' => z_root () . '/notify/view/' . $tt [ 'id' ],
'name' => $tt [ 'xname' ],
'url' => $tt [ 'url' ],
'photo' => $tt [ 'photo' ],
'when' => relative_date ( $tt [ 'created' ]),
'hclass' => (( $tt [ 'seen' ]) ? 'notify-seen' : 'notify-unseen' ),
2017-12-18 11:46:37 +00:00
'b64mid' => (( $tt [ 'otype' ] == 'item' ) ? $b64mid : 'undefined' ),
'notify_id' => (( $tt [ 'otype' ] == 'item' ) ? $tt [ 'id' ] : 'undefined' ),
2017-11-17 09:40:34 +00:00
'message' => $message
2016-04-19 03:38:38 +00:00
);
}
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
json_return_and_die ( [ 'notify' => $notifs ] );
2016-04-19 03:38:38 +00:00
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( argc () > 1 && argv ( 1 ) === 'mail' ) {
$channel = App :: get_channel ();
2016-10-01 22:41:25 +00:00
$t = q ( " select mail.*, xchan.* from mail left join xchan on xchan_hash = from_xchan
where channel_id = % d and mail_seen = 0 and mail_deleted = 0
2016-04-19 03:38:38 +00:00
and from_xchan != '%s' order by created desc limit 50 " ,
intval ( local_channel ()),
dbesc ( $channel [ 'channel_hash' ])
);
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $t ) {
foreach ( $t as $zz ) {
2016-04-19 03:38:38 +00:00
$notifs [] = array (
2016-10-01 22:41:25 +00:00
'notify_link' => z_root () . '/mail/' . $zz [ 'id' ],
2016-04-19 03:38:38 +00:00
'name' => $zz [ 'xchan_name' ],
2018-12-01 03:01:10 +00:00
'addr' => $zz [ 'xchan_addr' ],
2016-04-19 03:38:38 +00:00
'url' => $zz [ 'xchan_url' ],
'photo' => $zz [ 'xchan_photo_s' ],
2016-10-01 22:41:25 +00:00
'when' => relative_date ( $zz [ 'created' ]),
'hclass' => ( intval ( $zz [ 'mail_seen' ]) ? 'notify-seen' : 'notify-unseen' ),
2016-04-19 03:38:38 +00:00
'message' => t ( 'sent you a private message' ),
);
}
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
json_return_and_die ( [ 'notify' => $notifs ] );
2016-04-19 03:38:38 +00:00
}
2016-10-01 22:41:25 +00:00
2020-02-13 05:36:21 +00:00
if ( argc () > 1 && ( argv ( 1 ) === 'stream' || argv ( 1 ) === 'home' )) {
2019-04-16 00:34:29 +00:00
$local_result = array ();
2019-03-28 02:20:35 +00:00
$item_normal_moderate = $item_normal ;
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( argv ( 1 ) === 'home' ) {
2018-12-09 22:15:35 +00:00
$sql_extra .= " and item_wall = 1 " ;
2019-03-28 02:20:35 +00:00
$item_normal_moderate = item_normal_moderate ();
2018-12-09 22:15:35 +00:00
}
2018-02-04 19:42:40 +00:00
$r = q ( " SELECT * FROM item
WHERE uid = % d
AND item_unseen = 1
2018-02-08 08:47:49 +00:00
AND author_xchan != '%s'
2019-03-28 02:20:35 +00:00
$item_normal_moderate
2018-04-11 09:49:33 +00:00
$sql_extra
2018-02-08 08:47:49 +00:00
ORDER BY created DESC
2018-02-04 19:42:40 +00:00
LIMIT 300 " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ()),
2017-11-03 12:49:58 +00:00
dbesc ( $ob_hash )
2016-04-19 03:38:38 +00:00
);
2019-04-16 00:34:29 +00:00
if ( $r ) {
2016-04-19 03:38:38 +00:00
xchan_query ( $r );
2019-04-16 00:34:29 +00:00
foreach ( $r as $item ) {
$z = Enotify :: format ( $item );
2018-12-09 22:15:35 +00:00
2018-10-26 05:12:47 +00:00
if ( $z ) {
2019-04-16 00:34:29 +00:00
$local_result [] = $z ;
2018-10-26 05:12:47 +00:00
}
2016-04-19 03:38:38 +00:00
}
}
2019-04-16 00:34:29 +00:00
json_return_and_die ( [ 'notify' => $local_result ] );
2016-04-19 03:38:38 +00:00
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( argc () > 1 && ( argv ( 1 ) === 'intros' )) {
$local_result = array ();
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
$r = q ( " SELECT * FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash where abook_channel = %d and abook_pending = 1 and abook_self = 0 and abook_ignored = 0 and xchan_deleted = 0 and xchan_orphan = 0 ORDER BY abook_created DESC LIMIT 50 " ,
intval ( local_channel ())
);
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $r ) {
foreach ( $r as $rr ) {
$local_result [] = [
2019-08-28 05:25:06 +00:00
'notify_link' => z_root () . '/connections/' . $rr [ 'abook_id' ],
2019-04-16 00:34:29 +00:00
'name' => $rr [ 'xchan_name' ],
'addr' => $rr [ 'xchan_addr' ],
'url' => $rr [ 'xchan_url' ],
'photo' => $rr [ 'xchan_photo_s' ],
'when' => relative_date ( $rr [ 'abook_created' ]),
'hclass' => ( 'notify-unseen' ),
'message' => t ( 'added your channel' )
];
2016-04-19 03:38:38 +00:00
}
}
2019-04-16 00:34:29 +00:00
json_return_and_die ( [ 'notify' => $local_result ] );
2016-04-19 03:38:38 +00:00
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( ( argc () > 1 && ( argv ( 1 ) === 'register' )) && is_site_admin ()) {
2017-10-04 11:37:17 +00:00
$result = array ();
$r = q ( " SELECT account_email, account_created from account where (account_flags & %d) > 0 " ,
intval ( ACCOUNT_PENDING )
);
2019-04-16 00:34:29 +00:00
if ( $r ) {
foreach ( $r as $rr ) {
2017-10-04 11:37:17 +00:00
$result [] = array (
'notify_link' => z_root () . '/admin/accounts' ,
'name' => $rr [ 'account_email' ],
2018-12-01 03:01:10 +00:00
'addr' => $rr [ 'account_email' ],
2017-10-04 11:37:17 +00:00
'url' => '' ,
2018-05-15 08:20:20 +00:00
'photo' => z_root () . '/' . get_default_profile_photo ( 48 ),
2017-10-04 11:37:17 +00:00
'when' => relative_date ( $rr [ 'account_created' ]),
'hclass' => ( 'notify-unseen' ),
'message' => t ( 'requires approval' )
);
}
}
2019-04-16 00:34:29 +00:00
json_return_and_die ( [ 'notify' => $result ] );
2017-10-04 11:37:17 +00:00
}
2019-04-16 00:34:29 +00:00
if ( argc () > 1 && ( argv ( 1 ) === 'all_events' )) {
2016-04-19 03:38:38 +00:00
$bd_format = t ( 'g A l F d' ) ; // 8 AM Friday January 18
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
$result = array ();
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
$r = q ( " SELECT * FROM event left join xchan on event_xchan = xchan_hash
2016-10-04 04:48:53 +00:00
WHERE event . uid = % d AND dtstart < '%s' AND dtstart > '%s' and dismissed = 0
2016-06-02 04:48:54 +00:00
and etype in ( 'event' , 'birthday' )
2016-10-04 04:48:53 +00:00
ORDER BY dtstart DESC LIMIT 1000 " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ()),
dbesc ( datetime_convert ( 'UTC' , date_default_timezone_get (), 'now + ' . intval ( $evdays ) . ' days' )),
dbesc ( datetime_convert ( 'UTC' , date_default_timezone_get (), 'now - 1 days' ))
);
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $r ) {
foreach ( $r as $rr ) {
2016-10-01 22:41:25 +00:00
2016-06-02 04:48:54 +00:00
$strt = datetime_convert ( 'UTC' , (( $rr [ 'adjust' ]) ? date_default_timezone_get () : 'UTC' ), $rr [ 'dtstart' ]);
2016-04-19 03:38:38 +00:00
$today = (( substr ( $strt , 0 , 10 ) === datetime_convert ( 'UTC' , date_default_timezone_get (), 'now' , 'Y-m-d' )) ? true : false );
2016-06-02 04:48:54 +00:00
$when = day_translate ( datetime_convert ( 'UTC' , (( $rr [ 'adjust' ]) ? date_default_timezone_get () : 'UTC' ), $rr [ 'dtstart' ], $bd_format )) . (( $today ) ? ' ' . t ( '[today]' ) : '' );
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
$result [] = array (
2016-10-01 22:41:25 +00:00
'notify_link' => z_root () . '/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'],
2016-04-19 03:38:38 +00:00
'name' => $rr [ 'xchan_name' ],
2018-12-01 03:01:10 +00:00
'addr' => $rr [ 'xchan_addr' ],
2016-04-19 03:38:38 +00:00
'url' => $rr [ 'xchan_url' ],
'photo' => $rr [ 'xchan_photo_s' ],
'when' => $when ,
2016-10-01 22:41:25 +00:00
'hclass' => ( 'notify-unseen' ),
2016-04-19 03:38:38 +00:00
'message' => t ( 'posted an event' )
);
}
}
2019-04-16 00:34:29 +00:00
json_return_and_die ( [ 'notify' => $result ] );
2016-04-19 03:38:38 +00:00
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( argc () > 1 && ( argv ( 1 ) === 'files' )) {
2017-10-04 20:11:18 +00:00
$result = array ();
2018-12-01 03:01:10 +00:00
$r = q ( " SELECT item.created, xchan.xchan_name, xchan.xchan_addr, xchan.xchan_url, xchan.xchan_photo_s FROM item
2017-10-04 20:11:18 +00:00
LEFT JOIN xchan on author_xchan = xchan_hash
WHERE item . verb = '%s'
AND item . obj_type = '%s'
AND item . uid = % d
AND item . owner_xchan != '%s'
AND item . item_unseen = 1 " ,
dbesc ( ACTIVITY_POST ),
dbesc ( ACTIVITY_OBJ_FILE ),
intval ( local_channel ()),
dbesc ( $ob_hash )
);
2019-04-16 00:34:29 +00:00
if ( $r ) {
foreach ( $r as $rr ) {
2017-10-04 20:11:18 +00:00
$result [] = array (
'notify_link' => z_root () . '/sharedwithme' ,
'name' => $rr [ 'xchan_name' ],
2018-12-01 03:01:10 +00:00
'addr' => $rr [ 'xchan_addr' ],
2017-10-04 20:11:18 +00:00
'url' => $rr [ 'xchan_url' ],
'photo' => $rr [ 'xchan_photo_s' ],
'when' => relative_date ( $rr [ 'created' ]),
'hclass' => ( 'notify-unseen' ),
'message' => t ( 'shared a file with you' )
);
}
}
2019-04-16 00:34:29 +00:00
json_return_and_die ( [ 'notify' => $result ] );
2017-10-04 20:11:18 +00:00
}
2019-04-16 00:34:29 +00:00
if ( argc () > 1 && ( argv ( 1 ) === 'reports' ) && is_site_admin ()) {
$local_result = [];
$r = q ( " SELECT item.created, xchan.xchan_name, xchan.xchan_addr, xchan.xchan_url, xchan.xchan_photo_s FROM item
LEFT JOIN xchan on author_xchan = xchan_hash
WHERE item . type = '%s' AND item . item_unseen = 1 " ,
dbesc ( ITEM_TYPE_REPORT )
);
if ( $r ) {
foreach ( $r as $rv ) {
$result [] = [
'notify_link' => z_root () . '/reports' ,
'name' => $rv [ 'xchan_name' ],
'addr' => $rv [ 'xchan_addr' ],
'url' => $rv [ 'xchan_url' ],
'photo' => $rv [ 'xchan_photo_s' ],
'when' => relative_date ( $rv [ 'created' ]),
'hclass' => ( 'notify-unseen' ),
'message' => t ( 'reported content' )
];
}
}
json_return_and_die ( [ 'notify' => $result ] );
}
2016-04-19 03:38:38 +00:00
/**
* Normal ping - just the counts , no detail
*/
2019-04-16 00:34:29 +00:00
if ( $vnotify & VNOTIFY_SYSTEM ) {
2016-04-19 03:38:38 +00:00
$t = q ( " select count(*) as total from notify where uid = %d and seen = 0 " ,
intval ( local_channel ())
);
2019-04-16 00:34:29 +00:00
if ( $t )
2016-04-19 03:38:38 +00:00
$result [ 'notify' ] = intval ( $t [ 0 ][ 'total' ]);
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $vnotify & VNOTIFY_FILES ) {
2017-07-28 20:42:02 +00:00
$files = q ( " SELECT count(id) as total FROM item
WHERE verb = '%s'
AND obj_type = '%s'
AND uid = % d
AND owner_xchan != '%s'
AND item_unseen = 1 " ,
dbesc ( ACTIVITY_POST ),
dbesc ( ACTIVITY_OBJ_FILE ),
intval ( local_channel ()),
dbesc ( $ob_hash )
);
2019-04-16 00:34:29 +00:00
if ( $files )
2017-07-28 20:42:02 +00:00
$result [ 'files' ] = intval ( $files [ 0 ][ 'total' ]);
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $vnotify & ( VNOTIFY_NETWORK | VNOTIFY_CHANNEL )) {
2018-01-28 11:09:47 +00:00
2018-02-04 19:42:40 +00:00
$r = q ( " SELECT id, item_wall FROM item
2018-02-08 08:47:49 +00:00
WHERE uid = % d and item_unseen = 1
2016-04-19 03:38:38 +00:00
$item_normal
2018-04-11 09:49:33 +00:00
$sql_extra
2017-11-24 14:01:34 +00:00
AND author_xchan != '%s' " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ()),
2017-11-03 12:49:58 +00:00
dbesc ( $ob_hash )
2016-04-19 03:38:38 +00:00
);
2016-10-01 22:41:25 +00:00
if ( $r ) {
2016-04-19 03:38:38 +00:00
$arr = array ( 'items' => $r );
call_hooks ( 'network_ping' , $arr );
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
foreach ( $r as $it ) {
2019-04-16 00:34:29 +00:00
if ( intval ( $it [ 'item_wall' ])) {
2016-04-19 03:38:38 +00:00
$result [ 'home' ] ++ ;
2018-12-09 22:15:35 +00:00
}
else {
2020-02-13 05:36:21 +00:00
$result [ 'stream' ] ++ ;
2018-12-09 22:15:35 +00:00
}
2016-04-19 03:38:38 +00:00
}
}
}
2019-04-16 00:34:29 +00:00
if ( ! ( $vnotify & VNOTIFY_NETWORK ))
2020-02-13 05:36:21 +00:00
$result [ 'stream' ] = 0 ;
2019-04-16 00:34:29 +00:00
if ( ! ( $vnotify & VNOTIFY_CHANNEL ))
2016-04-19 03:38:38 +00:00
$result [ 'home' ] = 0 ;
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $vnotify & VNOTIFY_INTRO ) {
2016-04-19 03:38:38 +00:00
$intr = q ( " SELECT COUNT(abook.abook_id) AS total FROM abook left join xchan on abook.abook_xchan = xchan.xchan_hash where abook_channel = %d and abook_pending = 1 and abook_self = 0 and abook_ignored = 0 and xchan_deleted = 0 and xchan_orphan = 0 " ,
intval ( local_channel ())
);
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $intr )
2016-04-19 03:38:38 +00:00
$result [ 'intros' ] = intval ( $intr [ 0 ][ 'total' ]);
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
$channel = App :: get_channel ();
if ( $vnotify & VNOTIFY_MAIL ) {
2016-04-19 03:38:38 +00:00
$mails = q ( " SELECT count(id) as total from mail
WHERE channel_id = % d AND mail_seen = 0 and from_xchan != '%s' " ,
intval ( local_channel ()),
dbesc ( $channel [ 'channel_hash' ])
);
2019-04-16 00:34:29 +00:00
if ( $mails )
2016-04-19 03:38:38 +00:00
$result [ 'mail' ] = intval ( $mails [ 0 ][ 'total' ]);
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $vnotify & VNOTIFY_REGISTER ) {
if ( App :: $config [ 'system' ][ 'register_policy' ] == REGISTER_APPROVE && is_site_admin ()) {
2016-04-19 03:38:38 +00:00
$regs = q ( " SELECT count(account_id) as total from account where (account_flags & %d) > 0 " ,
intval ( ACCOUNT_PENDING )
);
2019-04-16 00:34:29 +00:00
if ( $regs )
2016-04-19 03:38:38 +00:00
$result [ 'register' ] = intval ( $regs [ 0 ][ 'total' ]);
}
2016-10-01 22:41:25 +00:00
}
2019-04-16 00:34:29 +00:00
if ( $vnotify & VNOTIFY_REPORTS ) {
if ( is_site_admin ()) {
$reps = q ( " SELECT count(id) as total from item where item_type = %d " ,
intval ( ITEM_TYPE_REPORT )
);
if ( $reps )
$result [ 'reports' ] = intval ( $reps [ 0 ][ 'total' ]);
}
}
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $vnotify & ( VNOTIFY_EVENT | VNOTIFY_EVENTTODAY | VNOTIFY_BIRTHDAY )) {
2016-10-04 04:48:53 +00:00
$events = q ( " SELECT etype, dtstart, adjust FROM event
WHERE event . uid = % d AND dtstart < '%s' AND dtstart > '%s' and dismissed = 0
2016-06-02 04:48:54 +00:00
and etype in ( 'event' , 'birthday' )
2016-10-04 04:48:53 +00:00
ORDER BY dtstart ASC " ,
2016-04-19 03:38:38 +00:00
intval ( local_channel ()),
dbesc ( datetime_convert ( 'UTC' , date_default_timezone_get (), 'now + ' . intval ( $evdays ) . ' days' )),
dbesc ( datetime_convert ( 'UTC' , date_default_timezone_get (), 'now - 1 days' ))
);
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $events ) {
2016-04-19 03:38:38 +00:00
$result [ 'all_events' ] = count ( $events );
2016-10-01 22:41:25 +00:00
2019-04-16 00:34:29 +00:00
if ( $result [ 'all_events' ]) {
2016-04-19 03:38:38 +00:00
$str_now = datetime_convert ( 'UTC' , date_default_timezone_get (), 'now' , 'Y-m-d' );
2019-04-16 00:34:29 +00:00
foreach ( $events as $x ) {
2016-04-19 03:38:38 +00:00
$bd = false ;
2019-04-16 00:34:29 +00:00
if ( $x [ 'etype' ] === 'birthday' ) {
2016-04-19 03:38:38 +00:00
$result [ 'birthdays' ] ++ ;
$bd = true ;
}
else {
$result [ 'events' ] ++ ;
}
2019-04-16 00:34:29 +00:00
if ( datetime_convert ( 'UTC' , (( intval ( $x [ 'adjust' ])) ? date_default_timezone_get () : 'UTC' ), $x [ 'dtstart' ], 'Y-m-d' ) === $str_now ) {
2016-04-19 03:38:38 +00:00
$result [ 'all_events_today' ] ++ ;
if ( $bd )
$result [ 'birthdays_today' ] ++ ;
else
$result [ 'events_today' ] ++ ;
}
}
}
}
}
2019-04-16 00:34:29 +00:00
if ( ! ( $vnotify & VNOTIFY_EVENT ))
2016-04-19 03:38:38 +00:00
$result [ 'all_events' ] = $result [ 'events' ] = 0 ;
2019-04-16 00:34:29 +00:00
if ( ! ( $vnotify & VNOTIFY_EVENTTODAY ))
2016-04-19 03:38:38 +00:00
$result [ 'all_events_today' ] = $result [ 'events_today' ] = 0 ;
2019-04-16 00:34:29 +00:00
if ( ! ( $vnotify & VNOTIFY_BIRTHDAY ))
2016-04-19 03:38:38 +00:00
$result [ 'birthdays' ] = 0 ;
2016-10-01 22:41:25 +00:00
2018-06-01 21:22:52 +00:00
2019-04-16 00:34:29 +00:00
if ( $vnotify & VNOTIFY_FORUMS ) {
2018-06-02 20:11:11 +00:00
$forums = get_forum_channels ( local_channel ());
2018-06-01 21:22:52 +00:00
2019-04-16 00:34:29 +00:00
if ( $forums ) {
2018-06-03 11:27:06 +00:00
$perms_sql = item_permissions_sql ( local_channel ()) . item_normal ();
2018-06-02 20:11:11 +00:00
$fcount = count ( $forums );
$forums [ 'total' ] = 0 ;
2018-06-03 11:27:06 +00:00
2019-04-16 00:34:29 +00:00
for ( $x = 0 ; $x < $fcount ; $x ++ ) {
2018-10-27 21:30:07 +00:00
$ttype = TERM_FORUM ;
$p = q ( " SELECT oid AS parent FROM term WHERE uid = " . intval ( local_channel ()) . " AND ttype = $ttype AND term = ' " . protect_sprintf ( dbesc ( $forums [ $x ][ 'xchan_name' ])) . " ' " );
$p = ids_to_querystr ( $p , 'parent' );
$pquery = (( $p ) ? " OR parent IN ( $p ) " : '' );
2018-06-02 20:11:11 +00:00
$r = q ( " select sum(item_unseen) as unseen from item
2018-10-27 21:30:07 +00:00
where uid = % d and ( owner_xchan = '%s' $pquery ) and item_unseen = 1 $perms_sql " ,
2018-06-02 20:11:11 +00:00
intval ( local_channel ()),
dbesc ( $forums [ $x ][ 'xchan_hash' ])
);
2019-04-16 00:34:29 +00:00
if ( $r [ 0 ][ 'unseen' ]) {
2020-02-13 05:36:21 +00:00
$forums [ $x ][ 'notify_link' ] = (( $forums [ $x ][ 'private_forum' ]) ? $forums [ $x ][ 'xchan_url' ] : z_root () . '/stream/?f=&pf=1&cid=' . $forums [ $x ][ 'abook_id' ]);
2018-06-02 20:11:11 +00:00
$forums [ $x ][ 'name' ] = $forums [ $x ][ 'xchan_name' ];
2018-12-01 03:01:10 +00:00
$forums [ $x ][ 'addr' ] = $forums [ $x ][ 'xchan_addr' ];
2018-06-02 20:11:11 +00:00
$forums [ $x ][ 'url' ] = $forums [ $x ][ 'xchan_url' ];
$forums [ $x ][ 'photo' ] = $forums [ $x ][ 'xchan_photo_s' ];
$forums [ $x ][ 'unseen' ] = $r [ 0 ][ 'unseen' ];
$forums [ $x ][ 'private_forum' ] = (( $forums [ $x ][ 'private_forum' ]) ? 'lock' : '' );
$forums [ $x ][ 'message' ] = (( $forums [ $x ][ 'private_forum' ]) ? t ( 'Private forum' ) : t ( 'Public forum' ));
$forums [ 'total' ] = $forums [ 'total' ] + $r [ 0 ][ 'unseen' ];
unset ( $forums [ $x ][ 'abook_id' ]);
unset ( $forums [ $x ][ 'xchan_hash' ]);
unset ( $forums [ $x ][ 'xchan_name' ]);
unset ( $forums [ $x ][ 'xchan_url' ]);
unset ( $forums [ $x ][ 'xchan_photo_s' ]);
}
else {
unset ( $forums [ $x ]);
}
2018-06-01 21:22:52 +00:00
}
2018-06-02 20:11:11 +00:00
$result [ 'forums' ] = $forums [ 'total' ];
unset ( $forums [ 'total' ]);
2018-06-02 13:39:38 +00:00
2018-06-02 20:11:11 +00:00
$result [ 'forums_sub' ] = $forums ;
}
2018-06-01 21:22:52 +00:00
}
2019-04-16 00:34:29 +00:00
json_return_and_die ( $result );
2016-04-19 03:38:38 +00:00
}
2016-10-01 22:41:25 +00:00
2016-04-19 03:38:38 +00:00
}