2011-10-27 03:09:03 +00:00
< ? php
function poco_init ( & $a ) {
2011-11-28 23:11:59 +00:00
$system_mode = false ;
2013-01-29 02:07:31 +00:00
if ( intval ( get_config ( 'system' , 'block_public' )) && ( ! local_user ()) && ( ! remote_user ())) {
logger ( 'mod_poco: block_public' );
2011-11-28 23:11:59 +00:00
http_status_exit ( 401 );
2013-01-29 02:07:31 +00:00
}
2011-11-28 23:11:59 +00:00
2012-11-30 03:43:53 +00:00
$observer = $a -> get_observer ();
2012-10-24 00:14:50 +00:00
if ( argc () > 1 ) {
$user = notags ( trim ( argv ( 1 )));
2011-10-27 03:09:03 +00:00
}
2011-11-28 23:11:59 +00:00
if ( ! x ( $user )) {
$c = q ( " select * from pconfig where cat = 'system' and k = 'suggestme' and v = 1 " );
2013-01-29 02:07:31 +00:00
if ( ! $c ) {
logger ( 'mod_poco: system mode. No candidates.' , LOGGER_DEBUG );
2013-01-30 03:28:19 +00:00
http_status_exit ( 404 );
2013-01-29 02:07:31 +00:00
}
2011-11-28 23:11:59 +00:00
$system_mode = true ;
}
2011-10-27 07:57:19 +00:00
2012-12-30 11:15:58 +00:00
2012-10-24 00:14:50 +00:00
$format = (( $_REQUEST [ 'format' ]) ? $_REQUEST [ 'format' ] : 'json' );
2011-10-27 03:09:03 +00:00
$justme = false ;
2012-10-24 00:14:50 +00:00
if ( argc () > 2 && argv ( 2 ) === '@me' )
2011-10-27 03:09:03 +00:00
$justme = true ;
2012-10-24 00:14:50 +00:00
if ( argc () > 3 ) {
if ( argv ( 3 ) === '@all' )
$justme = false ;
elseif ( argv ( 3 ) === '@self' )
$justme = true ;
}
if ( argc () > 4 && intval ( argv ( 4 )) && $justme == false )
$cid = intval ( argv ( 4 ));
2011-10-27 03:09:03 +00:00
2011-11-28 23:11:59 +00:00
if ( ! $system_mode ) {
2012-10-24 00:14:50 +00:00
2013-01-29 03:24:36 +00:00
$r = q ( " SELECT channel_id from channel where channel_address = '%s' limit 1 " ,
2011-11-28 23:11:59 +00:00
dbesc ( $user )
);
2013-01-29 02:07:31 +00:00
if ( ! $r ) {
logger ( 'mod_poco: user mode. Account not found. ' . $user );
2012-11-30 03:43:53 +00:00
http_status_exit ( 404 );
2013-01-29 02:07:31 +00:00
}
2012-11-30 03:43:53 +00:00
$channel_id = $r [ 0 ][ 'channel_id' ];
2013-01-29 02:07:31 +00:00
$ohash = (( $observer ) ? $observer [ 'xchan_hash' ] : '' );
2012-11-30 03:43:53 +00:00
2013-01-29 02:07:31 +00:00
if ( ! perm_is_allowed ( $channel_id , $ohash , 'view_contacts' )) {
logger ( 'mod_poco: user mode. Permission denied for ' . $ohash . ' user: ' . $user );
http_status_exit ( 401 );
}
2011-10-27 03:09:03 +00:00
2011-11-28 23:11:59 +00:00
}
2011-10-27 03:09:03 +00:00
if ( $justme )
2012-11-30 03:43:53 +00:00
$sql_extra = " and ( abook_flags & " . ABOOK_FLAG_SELF . " ) " ;
2012-04-05 09:15:03 +00:00
else
2012-11-30 03:43:53 +00:00
$sql_extra = " and abook_flags = 0 " ;
2011-10-27 03:09:03 +00:00
if ( $cid )
2012-11-30 03:43:53 +00:00
$sql_extra = sprintf ( " and abook_id = %d " , intval ( $cid ));
2011-10-27 03:09:03 +00:00
2011-11-28 23:11:59 +00:00
if ( $system_mode ) {
2012-11-30 03:43:53 +00:00
$r = q ( " SELECT count(*) as `total` from abook where ( abook_flags & " . ABOOK_FLAG_SELF .
" ) and abook_channel in (select uid from pconfig where cat = 'system' and k = 'suggestme' and v = 1) " );
2011-11-28 23:11:59 +00:00
}
else {
2012-11-30 03:43:53 +00:00
$r = q ( " SELECT count(*) as `total` from abook where abook_channel = %d
2011-11-28 23:11:59 +00:00
$sql_extra " ,
2012-11-30 03:43:53 +00:00
intval ( $channel_id )
2011-11-28 23:11:59 +00:00
);
2014-02-26 03:48:13 +00:00
$c = q ( " select * from menu_item where ( mitem_flags & " . intval ( MENU_ITEM_CHATROOM ) . " ) and allow_cid = '' and allow_gid = '' and deny_cid = '' and deny_gid = '' and mitem_channel_id = %d " ,
intval ( $channel_id )
);
2011-11-28 23:11:59 +00:00
}
2012-11-30 03:43:53 +00:00
if ( $r )
2011-10-27 03:09:03 +00:00
$totalResults = intval ( $r [ 0 ][ 'total' ]);
else
$totalResults = 0 ;
$startIndex = intval ( $_GET [ 'startIndex' ]);
if ( ! $startIndex )
$startIndex = 0 ;
$itemsPerPage = (( x ( $_GET , 'count' ) && intval ( $_GET [ 'count' ])) ? intval ( $_GET [ 'count' ]) : $totalResults );
2011-11-28 23:11:59 +00:00
if ( $system_mode ) {
2012-11-30 03:43:53 +00:00
$r = q ( " SELECT abook.*, xchan.* from abook left join xchan on abook_xchan = xchan_hash where ( abook_flags & " . ABOOK_FLAG_SELF .
" ) and abook_channel in (select uid from pconfig where cat = 'system' and k = 'suggestme' and v = 1) limit %d, %d " ,
2011-11-28 23:11:59 +00:00
intval ( $startIndex ),
intval ( $itemsPerPage )
);
}
else {
2012-11-30 03:43:53 +00:00
$r = q ( " SELECT abook.*, xchan.* from abook left join xchan on abook_xchan = xchan_hash where abook_channel = %d
2011-11-28 23:11:59 +00:00
$sql_extra LIMIT % d , % d " ,
2012-11-30 03:43:53 +00:00
intval ( $channel_id ),
2011-11-28 23:11:59 +00:00
intval ( $startIndex ),
intval ( $itemsPerPage )
);
}
2012-11-30 03:43:53 +00:00
2011-10-27 07:57:19 +00:00
$ret = array ();
if ( x ( $_GET , 'sorted' ))
$ret [ 'sorted' ] = 'false' ;
if ( x ( $_GET , 'filtered' ))
$ret [ 'filtered' ] = 'false' ;
if ( x ( $_GET , 'updatedSince' ))
$ret [ 'updateSince' ] = 'false' ;
2011-10-27 08:54:52 +00:00
$ret [ 'startIndex' ] = ( string ) $startIndex ;
$ret [ 'itemsPerPage' ] = ( string ) $itemsPerPage ;
$ret [ 'totalResults' ] = ( string ) $totalResults ;
2014-02-26 03:48:13 +00:00
if ( $c ) {
$ret [ 'chatrooms' ] = array ();
foreach ( $c as $d ) {
$ret [ 'chatrooms' ][] = array ( 'url' => $d [ 'mitem_link' ], 'desc' => $d [ 'mitem_desc' ]);
}
}
2011-10-27 07:57:19 +00:00
$ret [ 'entry' ] = array ();
$fields_ret = array (
'id' => false ,
2012-11-30 03:43:53 +00:00
'guid' => false ,
'guid_sig' => false ,
'hash' => false ,
2011-10-27 07:57:19 +00:00
'displayName' => false ,
'urls' => false ,
2011-10-27 11:38:33 +00:00
'preferredUsername' => false ,
2013-02-16 06:34:01 +00:00
'photos' => false ,
'rating' => false
2011-10-27 03:09:03 +00:00
);
2011-10-27 07:57:19 +00:00
if (( ! x ( $_GET , 'fields' )) || ( $_GET [ 'fields' ] === '@all' ))
2011-10-27 08:54:52 +00:00
foreach ( $fields_ret as $k => $v )
$fields_ret [ $k ] = true ;
2011-10-27 07:57:19 +00:00
else {
$fields_req = explode ( ',' , $_GET [ 'fields' ]);
foreach ( $fields_req as $f )
$fields_ret [ trim ( $f )] = true ;
}
if ( is_array ( $r )) {
if ( count ( $r )) {
foreach ( $r as $rr ) {
$entry = array ();
if ( $fields_ret [ 'id' ])
2012-11-30 03:43:53 +00:00
$entry [ 'id' ] = $rr [ 'abook_id' ];
if ( $fields_ret [ 'guid' ])
$entry [ 'guid' ] = $rr [ 'xchan_guid' ];
if ( $fields_ret [ 'guid_sig' ])
$entry [ 'guid_sig' ] = $rr [ 'xchan_guid_sig' ];
if ( $fields_ret [ 'hash' ])
$entry [ 'hash' ] = $rr [ 'xchan_hash' ];
2011-10-27 07:57:19 +00:00
if ( $fields_ret [ 'displayName' ])
2012-11-30 03:43:53 +00:00
$entry [ 'displayName' ] = $rr [ 'xchan_name' ];
2011-12-18 08:14:34 +00:00
if ( $fields_ret [ 'urls' ]) {
2012-11-30 03:43:53 +00:00
$entry [ 'urls' ] = array ( array ( 'value' => $rr [ 'xchan_url' ], 'type' => 'profile' ));
2014-09-05 02:34:27 +00:00
$network = $rr [ 'xchan_network' ];
if ( strpos ( $network , 'friendica' ) !== false )
$network = 'friendica' ;
2012-12-30 11:15:58 +00:00
if ( $rr [ 'xchan_addr' ])
2014-09-05 02:34:27 +00:00
$entry [ 'urls' ][] = array ( 'value' => 'acct:' . $rr [ 'xchan_addr' ], 'type' => $network );
2011-12-18 08:22:44 +00:00
}
2011-10-27 07:57:19 +00:00
if ( $fields_ret [ 'preferredUsername' ])
2012-11-30 03:43:53 +00:00
$entry [ 'preferredUsername' ] = substr ( $rr [ 'xchan_addr' ], 0 , strpos ( $rr [ 'xchan_addr' ], '@' ));
2011-10-27 07:57:19 +00:00
if ( $fields_ret [ 'photos' ])
2012-11-30 03:43:53 +00:00
$entry [ 'photos' ] = array ( array ( 'value' => $rr [ 'xchan_photo_l' ], 'mimetype' => $rr [ 'xchan_photo_mimetype' ], 'type' => 'profile' ));
2013-02-16 06:34:01 +00:00
if ( $fields_ret [ 'rating' ]) {
$entry [ 'rating' ] = (( array_key_exists ( 'abook_rating' , $rr )) ? array ( intval ( $rr [ 'abook_rating' ])) : 0 );
// maybe this should be a composite calculated rating in $system_mode
if ( $system_mode )
$entry [ 'rating' ] = 0 ;
}
2011-10-27 07:57:19 +00:00
$ret [ 'entry' ][] = $entry ;
}
2011-10-27 03:09:03 +00:00
}
2011-10-27 07:57:19 +00:00
else
$ret [ 'entry' ][] = array ();
2011-10-27 03:09:03 +00:00
}
2011-10-27 07:57:19 +00:00
else
http_status_exit ( 500 );
2011-10-27 03:09:03 +00:00
2011-10-27 07:57:19 +00:00
if ( $format === 'xml' ) {
header ( 'Content-type: text/xml' );
2011-10-27 11:38:33 +00:00
echo replace_macros ( get_markup_template ( 'poco_xml.tpl' ), array_xmlify ( array ( '$response' => $ret )));
2011-10-27 07:57:19 +00:00
http_status_exit ( 500 );
}
if ( $format === 'json' ) {
header ( 'Content-type: application/json' );
echo json_encode ( $ret );
killme ();
}
else
http_status_exit ( 500 );
2011-10-27 03:09:03 +00:00
}