2010-07-20 05:52:31 +00:00
< ? php
2012-07-13 14:09:29 +00:00
require_once ( 'include/bbcode.php' );
2012-10-27 11:43:39 +00:00
require_once ( 'include/notify.php' );
2012-05-22 23:01:07 +00:00
2012-07-13 14:09:29 +00:00
function ping_init ( & $a ) {
2011-09-28 07:30:22 +00:00
2012-07-13 14:09:29 +00:00
$result = array ();
$notifs = array ();
2012-05-22 23:01:07 +00:00
2012-07-16 04:06:07 +00:00
$result [ 'notify' ] = 0 ;
$result [ 'home' ] = 0 ;
$result [ 'network' ] = 0 ;
$result [ 'intros' ] = 0 ;
$result [ 'mail' ] = 0 ;
$result [ 'register' ] = 0 ;
2012-07-29 00:21:42 +00:00
$result [ 'events' ] = 0 ;
2012-07-29 00:39:26 +00:00
$result [ 'events_today' ] = 0 ;
2012-08-17 06:11:24 +00:00
$result [ 'birthdays' ] = 0 ;
$result [ 'birthdays_today' ] = 0 ;
2012-08-27 06:05:00 +00:00
$result [ 'all_events' ] = 0 ;
$result [ 'all_events_today' ] = 0 ;
2012-07-16 04:06:07 +00:00
$result [ 'notice' ] = array ();
$result [ 'info' ] = array ();
2012-07-26 05:55:43 +00:00
$t0 = dba_timer ();
2012-07-16 04:06:07 +00:00
2012-07-13 14:09:29 +00:00
header ( " content-type: application/json " );
2012-05-22 23:01:07 +00:00
2012-07-16 04:06:07 +00:00
$result [ 'invalid' ] = (( intval ( $_GET [ 'uid' ])) && ( intval ( $_GET [ 'uid' ]) != local_user ()) ? 1 : 0 );
if ( x ( $_SESSION , 'sysmsg' )){
foreach ( $_SESSION [ 'sysmsg' ] as $m ){
$result [ 'notice' ][] = array ( 'message' => $m );
}
unset ( $_SESSION [ 'sysmsg' ]);
}
if ( x ( $_SESSION , 'sysmsg_info' )){
foreach ( $_SESSION [ 'sysmsg_info' ] as $m ){
$result [ 'info' ][] = array ( 'message' => $m );
}
unset ( $_SESSION [ 'sysmsg_info' ]);
}
if (( ! local_user ()) || ( $result [ 'invalid' ])) {
2012-07-13 14:09:29 +00:00
echo json_encode ( $result );
killme ();
}
2012-02-24 00:50:29 +00:00
2012-08-30 06:03:03 +00:00
if ( argc () > 1 && argv ( 1 ) === 'notify' ) {
2012-03-01 00:26:07 +00:00
$t = q ( " select count(*) as total from notify where uid = %d and seen = 0 " ,
2012-02-22 03:03:55 +00:00
intval ( local_user ())
);
2012-03-01 00:26:07 +00:00
if ( $t && intval ( $t [ 0 ][ 'total' ]) > 49 ) {
$z = q ( " select * from notify where uid = %d
and seen = 0 order by date desc limit 0 , 50 " ,
intval ( local_user ())
);
}
else {
$z1 = q ( " select * from notify where uid = %d
and seen = 0 order by date desc limit 0 , 50 " ,
intval ( local_user ())
);
$z2 = q ( " select * from notify where uid = %d
and seen = 1 order by date desc limit 0 , % d " ,
intval ( local_user ()),
intval ( 50 - intval ( $t [ 0 ][ 'total' ]))
);
$z = array_merge ( $z1 , $z2 );
}
2012-07-13 14:09:29 +00:00
if ( count ( $z )) {
foreach ( $z as $zz ) {
$notifs [] = array (
'notify_link' => $a -> get_baseurl () . '/notify/view/' . $zz [ 'id' ],
'name' => $zz [ 'name' ],
'url' => $zz [ 'url' ],
'photo' => $zz [ 'photo' ],
'when' => relative_date ( $zz [ 'date' ]),
2012-07-18 05:11:24 +00:00
'class' => (( $zz [ 'seen' ]) ? 'notify-seen' : 'notify-unseen' ),
2012-07-13 14:09:29 +00:00
'message' => strip_tags ( bbcode ( $zz [ 'msg' ]))
);
2011-09-28 07:30:22 +00:00
}
2011-08-23 11:52:20 +00:00
}
2012-07-13 14:09:29 +00:00
echo json_encode ( array ( 'notify' => $notifs ));
killme ();
2011-09-28 07:30:22 +00:00
2012-07-13 14:09:29 +00:00
}
2012-10-27 11:43:39 +00:00
2013-01-08 10:49:08 +00:00
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
2013-01-08 19:59:29 +00:00
where channel_id = % d and not ( mail_flags & % d ) and not ( mail_flags & % d )
2013-01-08 10:49:08 +00:00
and from_xchan != '%s' order by created desc limit 0 , 50 " ,
intval ( local_user ()),
intval ( MAIL_SEEN ),
intval ( MAIL_DELETED ),
dbesc ( $channel [ 'channel_hash' ])
);
if ( $t ) {
foreach ( $t as $zz ) {
// $msg = sprintf( t('sent you a private message.'), $zz['xchan_name']);
$notifs [] = array (
'notify_link' => $a -> get_baseurl () . '/message/' . $zz [ 'id' ],
'name' => $zz [ 'xchan_name' ],
'url' => $zz [ 'xchan_url' ],
'photo' => $zz [ 'xchan_photo_s' ],
'when' => relative_date ( $zz [ 'created' ]),
'class' => (( $zz [ 'mail_flags' ] & MAIL_SEEN ) ? 'notify-seen' : 'notify-unseen' ),
'message' => t ( 'sent you a private message' ),
);
}
}
echo json_encode ( array ( 'notify' => $notifs ));
killme ();
}
2012-10-27 11:43:39 +00:00
if ( argc () > 1 && ( argv ( 1 ) === 'network' || argv ( 1 ) === 'home' )) {
$result = array ();
2012-12-02 23:45:13 +00:00
2012-11-30 04:46:45 +00:00
$r = q ( " SELECT * FROM item
WHERE item_restrict = % d and ( item_flags & % d ) and uid = % d " ,
2012-10-27 11:43:39 +00:00
intval ( ITEM_VISIBLE ),
intval ( ITEM_UNSEEN ),
intval ( local_user ())
);
2012-12-02 23:45:13 +00:00
2012-10-27 11:43:39 +00:00
if ( $r ) {
2012-11-14 00:04:22 +00:00
xchan_query ( $r );
2012-10-27 11:43:39 +00:00
foreach ( $r as $item ) {
2012-11-30 04:46:45 +00:00
if (( argv ( 1 ) === 'home' ) && ( ! ( $item [ 'item_flags' ] & ITEM_WALL )))
2012-10-27 11:43:39 +00:00
continue ;
$result [] = format_notification ( $item );
}
}
2012-11-30 04:46:45 +00:00
logger ( 'ping: ' . print_r ( $result , true ));
2013-01-08 19:59:29 +00:00
echo json_encode ( array ( 'notify' => $result ));
2012-10-27 11:43:39 +00:00
killme ();
}
2013-01-10 07:07:13 +00:00
if ( argc () > 1 && ( argv ( 1 ) === 'intros' )) {
2012-12-02 23:45:13 +00:00
$result = array ();
$r = q ( " SELECT abook.*, xchan.* FROM abook left join xchan on abook_xchan = xchan_hash
WHERE abook_channel = % d and ( abook_flags & % d ) and not ( abook_flags & % d ) " ,
intval ( local_user ()),
intval ( ABOOK_FLAG_PENDING ),
intval ( ABOOK_FLAG_SELF )
);
if ( $r ) {
foreach ( $r as $rr ) {
$result [] = array (
2013-01-10 07:07:13 +00:00
'notify_link' => $a -> get_baseurl () . '/intro/' . $rr [ 'abook_id' ],
2012-12-02 23:45:13 +00:00
'name' => $rr [ 'xchan_name' ],
'url' => $rr [ 'xchan_url' ],
'photo' => $rr [ 'xchan_photo_s' ],
'when' => relative_date ( $rr [ 'abook_created' ]),
'class' => ( 'notify-unseen' ),
2013-01-10 07:07:13 +00:00
'message' => t ( 'added your channel' )
2012-12-02 23:45:13 +00:00
);
}
}
logger ( 'ping: ' . print_r ( $result , true ));
2013-01-10 07:07:13 +00:00
echo json_encode ( array ( 'notify' => $result ));
2012-12-02 23:45:13 +00:00
killme ();
}
2012-10-27 11:43:39 +00:00
// Normal ping - just the counts
2012-07-13 14:09:29 +00:00
$t = q ( " select count(*) as total from notify where uid = %d and seen = 0 " ,
intval ( local_user ())
);
if ( $t )
$result [ 'notify' ] = intval ( $t [ 0 ][ 'total' ]);
2012-07-26 05:55:43 +00:00
$t1 = dba_timer ();
2012-10-08 01:44:06 +00:00
$r = q ( " SELECT id, item_restrict, item_flags FROM item
2012-12-20 00:58:25 +00:00
WHERE ( item_restrict = % d ) and ( item_flags & % d ) and uid = % d " ,
2012-10-08 01:44:06 +00:00
intval ( ITEM_VISIBLE ),
intval ( ITEM_UNSEEN ),
2012-07-13 14:09:29 +00:00
intval ( local_user ())
);
2012-09-28 00:48:10 +00:00
if ( count ( $r )) {
2012-10-02 01:02:11 +00:00
$arr = array ( 'items' => $r );
call_hooks ( 'network_ping' , $arr );
2012-09-28 00:48:10 +00:00
2012-07-13 14:09:29 +00:00
foreach ( $r as $it ) {
2012-10-08 01:44:06 +00:00
if ( $it [ 'item_flags' ] & ITEM_WALL )
2012-07-13 14:09:29 +00:00
$result [ 'home' ] ++ ;
else
$result [ 'network' ] ++ ;
2011-09-28 07:30:22 +00:00
}
2012-07-13 14:09:29 +00:00
}
2011-08-17 19:59:06 +00:00
2012-07-26 05:55:43 +00:00
$t2 = dba_timer ();
2012-12-20 00:48:17 +00:00
$intr = q ( " select count(abook_id) as total from abook where (abook_flags & %d) and abook_channel = %d " ,
intval ( ABOOK_FLAG_PENDING ),
2012-07-13 14:09:29 +00:00
intval ( local_user ())
);
2012-07-26 05:55:43 +00:00
$t3 = dba_timer ();
2012-12-20 00:48:17 +00:00
if ( $intr )
$result [ 'intros' ] = intval ( $intr [ 0 ][ 'total' ]);
2012-07-13 14:09:29 +00:00
2012-07-26 05:55:43 +00:00
$t4 = dba_timer ();
2012-12-05 02:24:46 +00:00
$channel = get_app () -> get_channel ();
2012-07-26 05:55:43 +00:00
2012-12-05 02:24:46 +00:00
$mails = q ( " SELECT count(id) as total from mail
WHERE channel_id = % d AND not ( mail_flags & % d ) and from_xchan != '%s' " ,
2012-07-13 14:09:29 +00:00
intval ( local_user ()),
2012-12-05 02:24:46 +00:00
intval ( MAIL_SEEN ),
dbesc ( $channel [ 'channel_hash' ])
2012-07-13 14:09:29 +00:00
);
if ( $mails )
$result [ 'mail' ] = intval ( $mails [ 0 ][ 'total' ]);
2013-01-03 21:50:23 +00:00
if ( $a -> config [ 'system' ][ 'register_policy' ] == REGISTER_APPROVE && is_site_admin ()){
2012-07-13 14:09:29 +00:00
$regs = q ( " SELECT `contact`.`name`, `contact`.`url`, `contact`.`micro`, `register`.`created`, COUNT(*) as `total` FROM `contact` RIGHT JOIN `register` ON `register`.`uid`=`contact`.`uid` WHERE `contact`.`self`=1 " );
if ( $regs )
$result [ 'register' ] = intval ( $regs [ 0 ][ 'total' ]);
}
2012-07-26 05:55:43 +00:00
$t5 = dba_timer ();
2013-01-03 10:10:37 +00:00
$events = q ( " SELECT type, start, adjust FROM `event`
2013-01-16 04:13:25 +00:00
WHERE `event` . `uid` = % d AND start < '%s' AND start > '%s' and `ignore` = 0
2012-07-29 00:21:42 +00:00
ORDER BY `start` ASC " ,
intval ( local_user ()),
2013-01-16 04:13:25 +00:00
dbesc ( datetime_convert ( 'UTC' , date_default_timezone_get (), 'now + 7 days' )),
dbesc ( datetime_convert ( 'UTC' , date_default_timezone_get (), 'now - 1 days' ))
2012-07-29 00:21:42 +00:00
);
2013-01-03 10:10:37 +00:00
if ( $events ) {
$result [ 'all_events' ] = count ( $events );
2012-07-26 05:55:43 +00:00
2012-08-27 06:05:00 +00:00
if ( $result [ 'all_events' ]) {
2012-07-29 00:39:26 +00:00
$str_now = datetime_convert ( 'UTC' , $a -> timezone , 'now' , 'Y-m-d' );
foreach ( $events as $x ) {
2012-08-17 06:11:24 +00:00
$bd = false ;
if ( $x [ 'type' ] === 'birthday' ) {
$result [ 'birthdays' ] ++ ;
$bd = true ;
}
2012-08-27 06:05:00 +00:00
else {
$result [ 'events' ] ++ ;
}
2012-08-17 06:11:24 +00:00
if ( datetime_convert ( 'UTC' ,(( intval ( $x [ 'adjust' ])) ? $a -> timezone : 'UTC' ), $x [ 'start' ], 'Y-m-d' ) === $str_now ) {
2012-08-27 06:05:00 +00:00
$result [ 'all_events_today' ] ++ ;
2012-08-17 06:11:24 +00:00
if ( $bd )
$result [ 'birthdays_today' ] ++ ;
2012-08-27 06:05:00 +00:00
else
$result [ 'events_today' ] ++ ;
2012-08-17 06:11:24 +00:00
}
2012-07-29 00:39:26 +00:00
}
}
}
2012-07-26 05:55:43 +00:00
$x = json_encode ( $result );
2011-09-05 12:01:21 +00:00
2012-07-26 05:55:43 +00:00
$t6 = dba_timer ();
// logger('ping timer: ' . sprintf('%01.4f %01.4f %01.4f %01.4f %01.4f %01.4f',$t6 - $t5, $t5 - $t4, $t4 - $t3, $t3 - $t2, $t2 - $t1, $t1 - $t0));
echo $x ;
2010-07-20 05:52:31 +00:00
killme ();
2012-07-13 14:09:29 +00:00
2010-07-20 05:52:31 +00:00
}