2016-04-19 03:38:38 +00:00
< ? php
2016-05-24 08:25:13 +00:00
2022-02-16 04:08:28 +00:00
namespace Code\Module ;
2016-04-19 03:38:38 +00:00
2019-04-16 00:34:29 +00:00
use App ;
2022-02-16 04:08:28 +00:00
use Code\Web\Controller ;
use Code\Lib\Enotify ;
use Code\Lib\Apps ;
use Code\Lib\PConfig ;
use Code\Lib\Channel ;
use Code\Extend\Hook ;
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
*
*/
2021-12-02 23:02:31 +00:00
class Ping extends Controller
{
/**
* @ 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
*/
public function init ()
{
$result = [];
$notifs = [];
$result [ 'notify' ] = 0 ;
$result [ 'home' ] = 0 ;
$result [ 'stream' ] = 0 ;
$result [ 'intros' ] = 0 ;
$result [ 'register' ] = 0 ;
$result [ 'moderate' ] = 0 ;
$result [ 'events' ] = 0 ;
$result [ 'events_today' ] = 0 ;
$result [ 'birthdays' ] = 0 ;
$result [ 'birthdays_today' ] = 0 ;
$result [ 'all_events' ] = 0 ;
$result [ 'all_events_today' ] = 0 ;
$result [ 'pubs' ] = 0 ;
$result [ 'files' ] = 0 ;
$result [ 'forums' ] = 0 ;
$result [ 'forums_sub' ] = [];
$result [ 'reports' ] = 0 ;
$vnotify = false ;
$evdays = 3 ;
$my_activity = 0 ;
$item_normal = item_normal ();
2022-12-22 21:04:43 +00:00
$approvals = " AND NOT verb in ('Accept','Reject') " ;
2021-12-02 23:02:31 +00:00
if ( local_channel ()) {
$vnotify = get_pconfig ( local_channel (), 'system' , 'vnotify' );
$evdays = intval ( get_pconfig ( local_channel (), 'system' , 'evdays' ));
$ob_hash = get_observer_hash ();
}
2022-12-30 02:56:52 +00:00
else {
$ob_hash = '' ;
}
2021-12-02 23:02:31 +00:00
// if unset show all visual notification types
if ( $vnotify === false ) {
$vnotify = ( - 1 );
}
if ( $evdays < 1 ) {
$evdays = 3 ;
}
/**
* 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
2022-12-30 02:56:52 +00:00
* on that page which were only valid under the old identity . Your session has changed .
* We will send a notification of this fact back to the browser where it is picked up
2021-12-02 23:02:31 +00:00
* 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' ] = (( isset ( $_GET [ 'uid' ]) && intval ( $_GET [ 'uid' ])) && ( intval ( $_GET [ 'uid' ]) != local_channel ()) ? 1 : 0 );
// 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 );
}
/**
* Update chat presence indication ( if applicable )
*/
if ( get_observer_hash () && ( ! $result [ 'invalid' ])) {
2021-12-03 03:01:39 +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 " ,
2021-12-02 23:02:31 +00:00
dbesc ( get_observer_hash ()),
dbesc ( $_SERVER [ 'REMOTE_ADDR' ])
);
$basic_presence = false ;
if ( $r ) {
$basic_presence = true ;
2021-12-03 03:01:39 +00:00
q (
" update chatpresence set cp_last = '%s' where cp_id = %d " ,
2021-12-02 23:02:31 +00:00
dbesc ( datetime_convert ()),
intval ( $r [ 0 ][ 'cp_id' ])
);
}
if ( ! $basic_presence ) {
2021-12-03 03:01:39 +00:00
q (
" insert into chatpresence ( cp_xchan, cp_last, cp_status, cp_client)
2016-04-19 03:38:38 +00:00
values ( '%s' , '%s' , '%s' , '%s' ) " ,
2021-12-02 23:02:31 +00:00
dbesc ( get_observer_hash ()),
dbesc ( datetime_convert ()),
dbesc ( 'online' ),
dbesc ( $_SERVER [ 'REMOTE_ADDR' ])
);
}
}
2016-10-01 22:41:25 +00:00
2021-12-02 23:02:31 +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
2021-12-03 03:01:39 +00:00
q (
" delete from chatpresence where cp_last < %s - INTERVAL %s and cp_client != 'auto' " ,
db_utcnow (),
db_quoteinterval ( '3 MINUTE' )
2021-12-02 23:02:31 +00:00
);
2016-10-01 22:41:25 +00:00
2018-04-11 09:49:33 +00:00
2021-12-02 23:02:31 +00:00
$sql_extra = '' ;
2020-11-13 04:41:44 +00:00
2021-12-02 23:02:31 +00:00
if ( ! ( $vnotify & VNOTIFY_LIKE )) {
$sql_extra = " AND verb NOT IN (' " . dbesc ( ACTIVITY_LIKE ) . " ', ' " . dbesc ( ACTIVITY_DISLIKE ) . " ') " ;
}
2018-04-11 09:49:33 +00:00
2021-12-02 23:02:31 +00:00
$discover_tab_on = can_view_public_stream ();
2018-04-08 23:51:11 +00:00
2021-12-02 23:02:31 +00:00
$notify_pubs = (( local_channel ()) ? ( $vnotify & VNOTIFY_PUBS ) && $discover_tab_on : $discover_tab_on );
2017-07-28 20:42:02 +00:00
2021-12-02 23:02:31 +00:00
if ( $notify_pubs && local_channel () && ! Apps :: system_app_installed ( local_channel (), 'Public Stream' )) {
$notify_pubs = false ;
}
2019-09-22 06:18:20 +00:00
2022-01-25 01:26:12 +00:00
$sys = Channel :: get_system ();
2017-07-28 20:42:02 +00:00
2021-12-02 23:02:31 +00:00
$seenstr = EMPTY_STR ;
2021-05-27 11:04:16 +00:00
2021-12-02 23:02:31 +00:00
if ( local_channel ()) {
$seen = PConfig :: Get ( local_channel (), 'system' , 'seen_items' , []);
if ( $seen ) {
$seenstr = " and not item.id in ( " . implode ( ',' , $seen ) . " ) " ;
}
}
2021-05-27 11:04:16 +00:00
2021-12-02 23:02:31 +00:00
$loadtime = get_loadtime ( 'pubstream' );
2021-05-27 11:04:16 +00:00
2021-12-02 23:02:31 +00:00
if ( $notify_pubs ) {
2021-12-03 03:01:39 +00:00
$pubs = q (
" SELECT id, author_xchan from item
2017-07-28 20:42:02 +00:00
WHERE uid = % d
2021-05-27 11:04:16 +00:00
AND created > '%s'
$seenstr
2022-12-22 21:04:43 +00:00
$approvals
2018-04-11 09:49:33 +00:00
$item_normal
$sql_extra " ,
2021-12-02 23:02:31 +00:00
intval ( $sys [ 'channel_id' ]),
dbesc ( $loadtime )
);
if ( $pubs ) {
foreach ( $pubs as $p ) {
if ( $p [ 'author_xchan' ] === get_observer_hash ()) {
$my_activity ++ ;
} else {
$result [ 'pubs' ] ++ ;
}
}
}
}
if (( argc () > 1 ) && ( argv ( 1 ) === 'pubs' ) && ( $notify_pubs )) {
$local_result = [];
2021-12-03 03:01:39 +00:00
$r = q (
" SELECT * FROM item
2017-10-06 14:46:27 +00:00
WHERE uid = % d
2018-02-08 08:47:49 +00:00
AND author_xchan != '%s'
2021-05-27 11:04:16 +00:00
AND created > '%s'
$seenstr
2022-12-22 21:04:43 +00:00
$approvals
2017-10-06 14:46:27 +00:00
$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 " ,
2021-12-02 23:02:31 +00:00
intval ( $sys [ 'channel_id' ]),
dbesc ( get_observer_hash ()),
dbesc ( $loadtime )
);
if ( $r ) {
xchan_query ( $r );
foreach ( $r as $rr ) {
2022-11-25 22:02:56 +00:00
$rr [ 'llink' ] = str_replace ( 'display/' , 'pubstream/' , $rr [ 'llink' ]);
2021-12-02 23:02:31 +00:00
$z = Enotify :: format ( $rr );
if ( $z ) {
$local_result [] = $z ;
}
}
}
json_return_and_die ([ 'notify' => $local_result ]);
}
if (( ! local_channel ()) || ( $result [ 'invalid' ])) {
json_return_and_die ( $result );
}
/**
* 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_channel () && ( ! $_SESSION [ 'sudo' ])) {
switch ( $_REQUEST [ 'markRead' ]) {
case 'stream' :
2021-12-03 03:01:39 +00:00
$r = q (
" UPDATE item SET item_unseen = 0 WHERE uid = %d AND item_unseen = 1 " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ())
);
$_SESSION [ 'loadtime_stream' ] = datetime_convert ();
PConfig :: Set ( local_channel (), 'system' , 'loadtime_stream' , $_SESSION [ 'loadtime_stream' ]);
$_SESSION [ 'loadtime_channel' ] = datetime_convert ();
PConfig :: Set ( local_channel (), 'system' , 'loadtime_channel' , $_SESSION [ 'loadtime_channel' ]);
break ;
case 'home' :
2021-12-03 03:01:39 +00:00
$r = q (
" UPDATE item SET item_unseen = 0 WHERE uid = %d AND item_unseen = 1 AND item_wall = 1 " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ())
);
$_SESSION [ 'loadtime_channel' ] = datetime_convert ();
PConfig :: Set ( local_channel (), 'system' , 'loadtime_channel' , $_SESSION [ 'loadtime_channel' ]);
break ;
case 'all_events' :
2021-12-03 03:01:39 +00:00
$r = q (
" UPDATE event SET dismissed = 1 WHERE uid = %d AND dismissed = 0 AND dtstart < '%s' AND dtstart > '%s' " ,
2021-12-02 23:02:31 +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' :
2021-12-03 03:01:39 +00:00
$r = q (
" update notify set seen = 1 where uid = %d " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ())
);
break ;
case 'pubs' :
$_SESSION [ 'loadtime_pubstream' ] = datetime_convert ();
PConfig :: Set ( local_channel (), 'system' , 'loadtime_pubstream' , $_SESSION [ 'loadtime_pubstream' ]);
break ;
default :
break ;
}
}
if ( x ( $_REQUEST , 'markItemRead' ) && local_channel () && ( ! $_SESSION [ 'sudo' ])) {
2021-12-03 03:01:39 +00:00
$r = q (
" UPDATE item SET item_unseen = 0 WHERE uid = %d AND parent = %d " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ()),
intval ( $_REQUEST [ 'markItemRead' ])
);
$id = intval ( $_REQUEST [ 'markItemRead' ]);
$seen = PConfig :: Get ( local_channel (), 'system' , 'seen_items' , []);
if ( ! in_array ( $id , $seen )) {
$seen [] = $id ;
}
PConfig :: Set ( local_channel (), 'system' , 'seen_items' , $seen );
}
/**
* 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' ) {
2021-12-03 03:01:39 +00:00
$t = q (
" SELECT * FROM notify WHERE uid = %d AND seen = 0 ORDER BY CREATED DESC " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ())
);
if ( $t ) {
foreach ( $t as $tt ) {
$message = trim ( strip_tags ( bbcode ( $tt [ 'msg' ])));
2022-11-25 22:08:28 +00:00
if ( str_starts_with ( $message , $tt [ 'xname' ])) {
2021-12-02 23:02:31 +00:00
$message = substr ( $message , strlen ( $tt [ 'xname' ]) + 1 );
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$mid = basename ( $tt [ 'link' ]);
$mid = unpack_link_id ( $mid );
if ( in_array ( $tt [ 'verb' ], [ ACTIVITY_LIKE , ACTIVITY_DISLIKE ])) {
// we need the thread parent
2021-12-03 03:01:39 +00:00
$r = q (
" select thr_parent from item where mid = '%s' and uid = %d limit 1 " ,
2021-12-02 23:02:31 +00:00
dbesc ( $mid ),
intval ( local_channel ())
);
2022-11-25 22:08:28 +00:00
$b64mid = (( str_starts_with ( $r [ 0 ][ 'thr_parent' ], 'b64.' )) ? $r [ 0 ][ 'thr_parent' ] : gen_link_id ( $r [ 0 ][ 'thr_parent' ]));
2021-12-02 23:02:31 +00:00
} else {
2022-11-25 22:08:28 +00:00
$b64mid = (( str_starts_with ( $mid , 'b64.' )) ? $mid : gen_link_id ( $mid ));
2021-12-02 23:02:31 +00:00
}
2022-11-25 22:08:28 +00:00
$notifs [] = [
2021-12-02 23:02:31 +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' ),
'b64mid' => (( $tt [ 'otype' ] == 'item' ) ? $b64mid : 'undefined' ),
'notify_id' => (( $tt [ 'otype' ] == 'item' ) ? $tt [ 'id' ] : 'undefined' ),
'message' => $message
2022-11-25 22:08:28 +00:00
];
2021-12-02 23:02:31 +00:00
}
}
json_return_and_die ([ 'notify' => $notifs ]);
}
if ( argc () > 1 && ( argv ( 1 ) === 'stream' )) {
$local_result = [];
$item_normal_moderate = $item_normal ;
$loadtime = get_loadtime ( 'stream' );
2021-12-03 03:01:39 +00:00
$r = q (
" SELECT * FROM item
2021-05-27 11:04:16 +00:00
WHERE uid = % d
AND author_xchan != '%s'
2021-12-24 03:11:56 +00:00
AND edited > '%s'
2021-05-27 11:04:16 +00:00
$seenstr
2022-12-22 21:04:43 +00:00
$approvals
2021-05-27 11:04:16 +00:00
$item_normal_moderate
$sql_extra
ORDER BY created DESC
LIMIT 300 " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ()),
dbesc ( $ob_hash ),
dbesc ( $loadtime )
);
if ( $r ) {
xchan_query ( $r );
foreach ( $r as $item ) {
$z = Enotify :: format ( $item );
if ( $z ) {
$local_result [] = $z ;
}
}
}
json_return_and_die ([ 'notify' => $local_result ]);
}
if ( argc () > 1 && ( argv ( 1 ) === 'home' )) {
$local_result = [];
$sql_extra .= " and item_wall = 1 " ;
$item_normal_moderate = item_normal_moderate ();
$loadtime = get_loadtime ( 'channel' );
2021-12-03 03:01:39 +00:00
$r = q (
" SELECT * FROM item
2018-02-04 19:42:40 +00:00
WHERE uid = % d
2018-02-08 08:47:49 +00:00
AND author_xchan != '%s'
2021-12-24 03:11:56 +00:00
AND edited > '%s'
2021-05-27 11:04:16 +00:00
$seenstr
2022-12-22 21:04:43 +00:00
$approvals
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 " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ()),
dbesc ( $ob_hash ),
dbesc ( $loadtime )
);
if ( $r ) {
xchan_query ( $r );
foreach ( $r as $item ) {
$z = Enotify :: format ( $item );
if ( $z ) {
$local_result [] = $z ;
}
}
}
json_return_and_die ([ 'notify' => $local_result ]);
}
if ( argc () > 1 && ( argv ( 1 ) === 'intros' )) {
$local_result = [];
2021-12-03 03:01:39 +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 " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ())
);
if ( $r ) {
foreach ( $r as $rr ) {
$local_result [] = [
'notify_link' => z_root () . '/connections/' . $rr [ 'abook_id' ],
'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' )
];
}
}
json_return_and_die ([ 'notify' => $local_result ]);
}
2023-01-04 03:32:03 +00:00
if ( argc () > 1 && ( argv ( 1 ) === 'moderate' )) {
$local_result = [];
$sql_extra .= " and item_wall = 1 " ;
$item_normal_moderate = item_normal_moderate ();
$loadtime = get_loadtime ( 'channel' );
$r = q (
" SELECT * FROM item
WHERE uid = % d
AND author_xchan != '%s'
$seenstr
$approvals
$item_normal_moderate
$sql_extra
AND item_blocked = 4
ORDER BY created DESC
LIMIT 300 " ,
intval ( local_channel ()),
dbesc ( $ob_hash )
);
if ( $r ) {
xchan_query ( $r );
foreach ( $r as $item ) {
$z = Enotify :: format ( $item );
if ( $z ) {
$z [ 'notify_link' ] = str_replace ( '/display/' , '/moderate/' , $z [ 'notify_link' ]);
$local_result [] = $z ;
}
}
}
json_return_and_die ([ 'notify' => $local_result ]);
}
2021-12-02 23:02:31 +00:00
if (( argc () > 1 && ( argv ( 1 ) === 'register' )) && is_site_admin ()) {
$result = [];
2021-12-03 03:01:39 +00:00
$r = q (
" SELECT account_email, account_created from account where (account_flags & %d) > 0 " ,
2021-12-02 23:02:31 +00:00
intval ( ACCOUNT_PENDING )
);
if ( $r ) {
foreach ( $r as $rr ) {
2022-11-25 22:08:28 +00:00
$result [] = [
2021-12-02 23:02:31 +00:00
'notify_link' => z_root () . '/admin/accounts' ,
'name' => $rr [ 'account_email' ],
'addr' => $rr [ 'account_email' ],
'url' => '' ,
2022-01-25 01:26:12 +00:00
'photo' => z_root () . '/' . Channel :: get_default_profile_photo ( 48 ),
2021-12-02 23:02:31 +00:00
'when' => relative_date ( $rr [ 'account_created' ]),
'hclass' => ( 'notify-unseen' ),
'message' => t ( 'requires approval' )
2022-11-25 22:08:28 +00:00
];
2021-12-02 23:02:31 +00:00
}
}
json_return_and_die ([ 'notify' => $result ]);
}
if ( argc () > 1 && ( argv ( 1 ) === 'all_events' )) {
2022-12-30 02:56:52 +00:00
$bd_format = t ( 'g A l, F d' ); // 8 AM Friday, January 18
2021-12-02 23:02:31 +00:00
$result = [];
2021-12-03 03:01:39 +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 " ,
2021-12-02 23:02:31 +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' ))
);
if ( $r ) {
foreach ( $r as $rr ) {
$strt = datetime_convert ( 'UTC' , (( $rr [ 'adjust' ]) ? date_default_timezone_get () : 'UTC' ), $rr [ 'dtstart' ]);
2022-12-30 02:56:52 +00:00
$today = substr ( $strt , 0 , 10 ) === datetime_convert ( 'UTC' , date_default_timezone_get (), 'now' , 'Y-m-d' );
2021-12-02 23:02:31 +00:00
$when = day_translate ( datetime_convert ( 'UTC' , (( $rr [ 'adjust' ]) ? date_default_timezone_get () : 'UTC' ), $rr [ 'dtstart' ], $bd_format )) . (( $today ) ? ' ' . t ( '[today]' ) : '' );
2022-11-25 22:08:28 +00:00
$result [] = [
2021-12-02 23:02:31 +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'],
'name' => $rr [ 'xchan_name' ],
'addr' => $rr [ 'xchan_addr' ],
'url' => $rr [ 'xchan_url' ],
'photo' => $rr [ 'xchan_photo_s' ],
'when' => $when ,
'hclass' => ( 'notify-unseen' ),
'message' => t ( 'posted an event' )
2022-11-25 22:08:28 +00:00
];
2021-12-02 23:02:31 +00:00
}
}
json_return_and_die ([ 'notify' => $result ]);
}
if ( argc () > 1 && ( argv ( 1 ) === 'files' )) {
$result = [];
2021-12-03 03:01:39 +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 " ,
2021-12-02 23:02:31 +00:00
dbesc ( ACTIVITY_POST ),
dbesc ( ACTIVITY_OBJ_FILE ),
intval ( local_channel ()),
dbesc ( $ob_hash )
);
if ( $r ) {
foreach ( $r as $rr ) {
2022-11-25 22:08:28 +00:00
$result [] = [
2021-12-02 23:02:31 +00:00
'notify_link' => z_root () . '/sharedwithme' ,
'name' => $rr [ 'xchan_name' ],
'addr' => $rr [ 'xchan_addr' ],
'url' => $rr [ 'xchan_url' ],
'photo' => $rr [ 'xchan_photo_s' ],
'when' => relative_date ( $rr [ 'created' ]),
'hclass' => ( 'notify-unseen' ),
'message' => t ( 'shared a file with you' )
2022-11-25 22:08:28 +00:00
];
2021-12-02 23:02:31 +00:00
}
}
json_return_and_die ([ 'notify' => $result ]);
}
if ( argc () > 1 && ( argv ( 1 ) === 'reports' ) && is_site_admin ()) {
2022-12-30 02:56:52 +00:00
$result = [];
2021-12-02 23:02:31 +00:00
2021-12-03 03:01:39 +00:00
$r = q (
" SELECT item.created, xchan.xchan_name, xchan.xchan_addr, xchan.xchan_url, xchan.xchan_photo_s FROM item
2019-04-16 00:34:29 +00:00
LEFT JOIN xchan on author_xchan = xchan_hash
WHERE item . type = '%s' AND item . item_unseen = 1 " ,
2021-12-02 23:02:31 +00:00
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 ]);
}
/**
* Normal ping - just the counts , no detail
*/
if ( $vnotify & VNOTIFY_SYSTEM ) {
2021-12-03 03:01:39 +00:00
$t = q (
" select count(*) as total from notify where uid = %d and seen = 0 " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ())
);
2021-12-03 03:01:39 +00:00
if ( $t ) {
2021-12-02 23:02:31 +00:00
$result [ 'notify' ] = intval ( $t [ 0 ][ 'total' ]);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
if ( $vnotify & VNOTIFY_FILES ) {
2021-12-03 03:01:39 +00:00
$files = q (
" SELECT count(id) as total FROM item
2017-07-28 20:42:02 +00:00
WHERE verb = '%s'
AND obj_type = '%s'
AND uid = % d
AND owner_xchan != '%s'
AND item_unseen = 1 " ,
2021-12-02 23:02:31 +00:00
dbesc ( ACTIVITY_POST ),
dbesc ( ACTIVITY_OBJ_FILE ),
intval ( local_channel ()),
dbesc ( $ob_hash )
);
2021-12-03 03:01:39 +00:00
if ( $files ) {
2021-12-02 23:02:31 +00:00
$result [ 'files' ] = intval ( $files [ 0 ][ 'total' ]);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
if ( $vnotify & VNOTIFY_NETWORK ) {
$loadtime = get_loadtime ( 'stream' );
2021-12-03 03:01:39 +00:00
$r = q (
" SELECT id, author_xchan FROM item
2021-12-24 03:11:56 +00:00
WHERE uid = % d and edited > '%s'
2021-05-27 11:04:16 +00:00
$seenstr
2022-12-22 21:04:43 +00:00
$approvals
2021-05-27 11:04:16 +00:00
$item_normal
2021-11-17 01:58:45 +00:00
$sql_extra " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ()),
dbesc ( $loadtime )
);
if ( $r ) {
2022-11-25 22:08:28 +00:00
$arr = [ 'items' => $r ];
2022-02-12 08:50:48 +00:00
Hook :: call ( 'network_ping' , $arr );
2021-12-02 23:02:31 +00:00
foreach ( $r as $it ) {
if ( $it [ 'author_xchan' ] === $ob_hash ) {
$my_activity ++ ;
} else {
$result [ 'stream' ] ++ ;
}
}
}
}
if ( ! ( $vnotify & VNOTIFY_NETWORK )) {
$result [ 'stream' ] = 0 ;
}
if ( $vnotify & VNOTIFY_CHANNEL ) {
$loadtime = get_loadtime ( 'channel' );
2021-12-03 03:01:39 +00:00
$r = q (
" SELECT id, author_xchan FROM item
2021-12-24 03:11:56 +00:00
WHERE item_wall = 1 and uid = % d and edited > '%s'
2022-12-22 21:04:43 +00:00
$seenstr
$approvals
2016-04-19 03:38:38 +00:00
$item_normal
2021-11-17 01:58:45 +00:00
$sql_extra " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ()),
dbesc ( $loadtime )
);
if ( $r ) {
foreach ( $r as $it ) {
if ( $it [ 'author_xchan' ] === $ob_hash ) {
$my_activity ++ ;
} else {
$result [ 'home' ] ++ ;
}
}
}
}
if ( ! ( $vnotify & VNOTIFY_CHANNEL )) {
$result [ 'home' ] = 0 ;
}
2023-01-04 03:32:03 +00:00
if ( $vnotify & VNOTIFY_MODERATE ) {
$mods = q ( " SELECT COUNT(id) AS total from item where uid = %d and item_blocked = %d " ,
intval ( local_channel ()),
intval ( ITEM_MODERATED )
);
if ( $mods ) {
$result [ 'moderate' ] = intval ( $mods [ 0 ][ 'total' ]);
}
}
2021-12-02 23:02:31 +00:00
if ( $vnotify & VNOTIFY_INTRO ) {
2021-12-03 03:01:39 +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 " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ())
);
2021-12-03 03:01:39 +00:00
if ( $intr ) {
2021-12-02 23:02:31 +00:00
$result [ 'intros' ] = intval ( $intr [ 0 ][ 'total' ]);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
if ( $vnotify & VNOTIFY_REGISTER ) {
if ( App :: $config [ 'system' ][ 'register_policy' ] == REGISTER_APPROVE && is_site_admin ()) {
2021-12-03 03:01:39 +00:00
$regs = q (
" SELECT count(account_id) as total from account where (account_flags & %d) > 0 " ,
2021-12-02 23:02:31 +00:00
intval ( ACCOUNT_PENDING )
);
2021-12-03 03:01:39 +00:00
if ( $regs ) {
2021-12-02 23:02:31 +00:00
$result [ 'register' ] = intval ( $regs [ 0 ][ 'total' ]);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
}
if ( $vnotify & VNOTIFY_REPORTS ) {
if ( is_site_admin ()) {
2021-12-03 03:01:39 +00:00
$reps = q (
" SELECT count(id) as total from item where item_type = %d " ,
2021-12-02 23:02:31 +00:00
intval ( ITEM_TYPE_REPORT )
);
2021-12-03 03:01:39 +00:00
if ( $reps ) {
2021-12-02 23:02:31 +00:00
$result [ 'reports' ] = intval ( $reps [ 0 ][ 'total' ]);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
}
if ( $vnotify & ( VNOTIFY_EVENT | VNOTIFY_EVENTTODAY | VNOTIFY_BIRTHDAY )) {
2021-12-03 03:01:39 +00:00
$events = q (
" SELECT etype, dtstart, adjust FROM event
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 ASC " ,
2021-12-02 23:02:31 +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' ))
);
if ( $events ) {
$result [ 'all_events' ] = count ( $events );
if ( $result [ 'all_events' ]) {
$str_now = datetime_convert ( 'UTC' , date_default_timezone_get (), 'now' , 'Y-m-d' );
foreach ( $events as $x ) {
$bd = false ;
if ( $x [ 'etype' ] === 'birthday' ) {
$result [ 'birthdays' ] ++ ;
$bd = true ;
} else {
$result [ 'events' ] ++ ;
}
if ( datetime_convert ( 'UTC' , (( intval ( $x [ 'adjust' ])) ? date_default_timezone_get () : 'UTC' ), $x [ 'dtstart' ], 'Y-m-d' ) === $str_now ) {
$result [ 'all_events_today' ] ++ ;
2021-12-03 03:01:39 +00:00
if ( $bd ) {
2021-12-02 23:02:31 +00:00
$result [ 'birthdays_today' ] ++ ;
2021-12-03 03:01:39 +00:00
} else {
2021-12-02 23:02:31 +00:00
$result [ 'events_today' ] ++ ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
}
}
}
}
2021-12-03 03:01:39 +00:00
if ( ! ( $vnotify & VNOTIFY_EVENT )) {
2021-12-02 23:02:31 +00:00
$result [ 'all_events' ] = $result [ 'events' ] = 0 ;
2021-12-03 03:01:39 +00:00
}
if ( ! ( $vnotify & VNOTIFY_EVENTTODAY )) {
2021-12-02 23:02:31 +00:00
$result [ 'all_events_today' ] = $result [ 'events_today' ] = 0 ;
2021-12-03 03:01:39 +00:00
}
if ( ! ( $vnotify & VNOTIFY_BIRTHDAY )) {
2021-12-02 23:02:31 +00:00
$result [ 'birthdays' ] = 0 ;
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
2022-12-12 06:49:00 +00:00
/*
2021-12-02 23:02:31 +00:00
if ( $vnotify & VNOTIFY_FORUMS ) {
$forums = get_forum_channels ( local_channel ());
if ( $forums ) {
$perms_sql = item_permissions_sql ( local_channel ()) . item_normal ();
$fcount = count ( $forums );
$forums [ 'total' ] = 0 ;
for ( $x = 0 ; $x < $fcount ; $x ++ ) {
$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 ) " : '' );
2021-12-03 03:01:39 +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 " ,
2021-12-02 23:02:31 +00:00
intval ( local_channel ()),
dbesc ( $forums [ $x ][ 'xchan_hash' ])
);
if ( $r [ 0 ][ 'unseen' ]) {
$forums [ $x ][ 'notify_link' ] = (( $forums [ $x ][ 'private_forum' ]) ? $forums [ $x ][ 'xchan_url' ] : z_root () . '/stream/?f=&pf=1&cid=' . $forums [ $x ][ 'abook_id' ]);
$forums [ $x ][ 'name' ] = $forums [ $x ][ 'xchan_name' ];
$forums [ $x ][ 'addr' ] = $forums [ $x ][ 'xchan_addr' ];
$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 group' ) : t ( 'Public group' ));
$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 ]);
}
}
$result [ 'forums' ] = $forums [ 'total' ];
unset ( $forums [ 'total' ]);
$result [ 'forums_sub' ] = $forums ;
}
}
2022-12-12 06:49:00 +00:00
*/
2022-12-30 02:56:52 +00:00
// Mark all stream notifications seen if all three of them are caught up.
2021-12-02 23:02:31 +00:00
// This also resets the pconfig storage for items_seen
if (( ! $my_activity ) && ( ! ( intval ( $result [ 'home' ]) + intval ( $result [ 'stream' ]) + intval ( $result [ 'pubs' ])))) {
PConfig :: Delete ( local_channel (), 'system' , 'seen_items' );
$_SESSION [ 'loadtime_channel' ] = datetime_convert ();
$_SESSION [ 'loadtime_stream' ] = datetime_convert ();
$_SESSION [ 'loadtime_pubstream' ] = datetime_convert ();
PConfig :: Set ( local_channel (), 'system' , 'loadtime_channel' , $_SESSION [ 'loadtime_channel' ]);
PConfig :: Set ( local_channel (), 'system' , 'loadtime_stream' , $_SESSION [ 'loadtime_stream' ]);
PConfig :: Set ( local_channel (), 'system' , 'loadtime_pubstream' , $_SESSION [ 'loadtime_pubstream' ]);
}
json_return_and_die ( $result );
}
2016-04-19 03:38:38 +00:00
}