diff options
Diffstat (limited to 'include/api.php')
-rw-r--r-- | include/api.php | 1291 |
1 files changed, 421 insertions, 870 deletions
diff --git a/include/api.php b/include/api.php index a2ca587a1..53db2b2a3 100644 --- a/include/api.php +++ b/include/api.php @@ -10,6 +10,7 @@ require_once('include/photos.php'); require_once('include/items.php'); require_once('include/attach.php'); require_once('include/api_auth.php'); +require_once('include/api_zot.php'); /* * @@ -49,7 +50,7 @@ require_once('include/api_auth.php'); return false; } } - if ($_SESSION["allow_api"]) + if ($_SESSION['allow_api']) return local_channel(); return false; } @@ -57,11 +58,11 @@ require_once('include/api_auth.php'); function api_date($str){ //Wed May 23 06:01:13 +0000 2007 - return datetime_convert('UTC', 'UTC', $str, "D M d H:i:s +0000 Y" ); + return datetime_convert('UTC', 'UTC', $str, 'D M d H:i:s +0000 Y' ); } - function api_register_func($path, $func, $auth=false) { + function api_register_func($path, $func, $auth = false) { \Zotlabs\Lib\Api_router::register($path,$func,$auth); } @@ -90,7 +91,12 @@ require_once('include/api_auth.php'); $info = \Zotlabs\Lib\Api_router::find($p); - logger('info: ' . $p . ' type: ' . $type . ' ' . print_r($info,true)); + if(in_array($type, [ 'rss', 'atom', 'as' ])) { + // These types no longer supported. + $info = false; + } + + logger('API info: ' . $p . ' type: ' . $type . ' ' . print_r($info,true), LOGGER_DEBUG,LOG_INFO); if($info) { @@ -111,96 +117,53 @@ require_once('include/api_auth.php'); return; switch($type) { - case "xml": - $r = mb_convert_encoding($r, "UTF-8",mb_detect_encoding($r)); - header ("Content-Type: text/xml"); - return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r; + case 'xml': + header ('Content-Type: text/xml'); + return $r; break; - case "json": - header ("Content-Type: application/json"); - if($r) { - foreach($r as $rv) { - $json = json_encode($rv); - } - } + case 'json': + header ('Content-Type: application/json'); // Lookup JSONP to understand these lines. They provide cross-domain AJAX ability. if ($_GET['callback']) - $json = $_GET['callback'] . '(' . $json . ')' ; - return $json; - break; - case "rss": - header ("Content-Type: application/rss+xml"); - return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r; - break; - case "atom": - header ("Content-Type: application/atom+xml"); - return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r; - break; - case "as": - if($r) { - foreach($r as $rv) { - $json = json_encode($rv); - } - } - return $json; + $r = $_GET['callback'] . '(' . $r . ')' ; + return $r; break; - } } - header("HTTP/1.1 404 Not Found"); + header('HTTP/1.1 404 Not Found'); logger('API call not implemented: ' . App::$query_string . ' - ' . print_r($_REQUEST,true)); $r = '<status><error>not implemented</error></status>'; switch($type){ - case "xml": - header ("Content-Type: text/xml"); - return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r; + case 'xml': + header ('Content-Type: text/xml'); + return '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $r; break; case "json": - header ("Content-Type: application/json"); + header ('Content-Type: application/json'); return json_encode(array('error' => 'not implemented')); break; case "rss": - header ("Content-Type: application/rss+xml"); - return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r; + header ('Content-Type: application/rss+xml'); + return '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $r; break; case "atom": - header ("Content-Type: application/atom+xml"); - return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r; + header ('Content-Type: application/atom+xml'); + return '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $r; break; } } /** - * RSS extra info - */ - - function api_rss_extra( $arr, $user_info){ - if (is_null($user_info)) $user_info = api_get_user($a); - $arr['$user'] = $user_info; - $arr['$rss'] = array( - 'alternate' => $user_info['url'], - 'self' => z_root(). "/". App::$query_string, - 'base' => z_root(), - 'updated' => api_date(null), - 'atom_updated' => datetime_convert('UTC','UTC','now',ATOM_TIME), - 'language' => $user_info['language'], - 'logo' => z_root()."/images/rm-64.png", - ); - - return $arr; - } - - /** * Returns user info array. */ - function api_get_user( $contact_id = null, $contact_xchan = null){ + function api_get_user($contact_id = null, $contact_xchan = null){ $user = null; - $extra_query = ""; + $extra_query = ''; if(! is_null($contact_xchan)) { @@ -208,8 +171,8 @@ require_once('include/api_auth.php'); $extra_query = " and abook_xchan = '" . dbesc($contact_xchan) . "' "; } else { - if(!is_null($contact_id)){ - $user=$contact_id; + if(! is_null($contact_id)){ + $user = $contact_id; $extra_query = " AND abook_id = %d "; } @@ -220,8 +183,8 @@ require_once('include/api_auth.php'); if(is_null($user) && x($_GET, 'screen_name')) { $user = dbesc($_GET['screen_name']); $extra_query = " AND xchan_addr like '%s@%%' "; - if (api_user()!==false) - $extra_query .= " AND abook_channel = ".intval(api_user()); + if(api_user() !== false) + $extra_query .= " AND abook_channel = " . intval(api_user()); } } @@ -241,14 +204,15 @@ require_once('include/api_auth.php'); // user info $uinfo = q("SELECT * from abook left join xchan on abook_xchan = xchan_hash - WHERE 1 + WHERE true $extra_query", $user ); - if (count($uinfo)==0) { - return False; + if (! $uinfo) { + return false; } + $following = false; if(intval($uinfo[0]['abook_self'])) { @@ -265,49 +229,53 @@ require_once('include/api_auth.php'); $r = q("SELECT COUNT(id) as total FROM item WHERE uid = %d AND item_wall = 1 $item_normal - AND allow_cid='' AND allow_gid='' AND deny_cid='' AND deny_gid='' + AND allow_cid = '' AND allow_gid = '' AND deny_cid = '' AND deny_gid = '' AND item_private = 0 ", intval($usr[0]['channel_id']) ); - $countitms = $r[0]['total']; - $following = true; + if($r) { + $countitms = $r[0]['total']; + $following = true; + } } else { $r = q("SELECT COUNT(id) as total FROM item WHERE author_xchan = '%s' - AND allow_cid='' AND allow_gid='' AND deny_cid='' AND deny_gid='' + AND allow_cid = '' AND allow_gid = '' AND deny_cid = '' AND deny_gid = '' AND item_private = 0 ", intval($uinfo[0]['xchan_hash']) ); - $countitms = $r[0]['total']; - + if($r) { + $countitms = $r[0]['total']; + } $following = ((get_abconfig($uinfo[0]['abook_channel'],$uinfo[0]['abook_xchan'],'my_perms','view_stream')) ? true : false ); } - // count friends if($usr) { $r = q("SELECT COUNT(abook_id) as total FROM abook WHERE abook_channel = %d AND abook_self = 0 ", intval($usr[0]['channel_id']) ); - $countfriends = $r[0]['total']; - $countfollowers = $r[0]['total']; + if($r) { + $countfriends = $r[0]['total']; + $countfollowers = $r[0]['total']; + } } $r = q("SELECT count(id) as total FROM item where item_starred = 1 and uid = %d " . item_normal(), intval($uinfo[0]['channel_id']) ); - $starred = $r[0]['total']; + if($r) + $starred = $r[0]['total']; - if(! intval($uinfo[0]['abook_self'])) { $countfriends = 0; $countfollowers = 0; $starred = 0; } - $ret = Array( + $ret = array( 'id' => intval($uinfo[0]['abook_id']), 'self' => (intval($uinfo[0]['abook_self']) ? 1 : 0), 'uid' => intval($uinfo[0]['abook_channel']), @@ -317,11 +285,11 @@ require_once('include/api_auth.php'); 'location' => ($usr) ? $usr[0]['channel_location'] : '', 'profile_image_url' => $uinfo[0]['xchan_photo_l'], 'url' => $uinfo[0]['xchan_url'], - 'contact_url' => z_root() . "/connections/".$uinfo[0]['abook_id'], + 'contact_url' => z_root() . '/connections/'.$uinfo[0]['abook_id'], 'protected' => false, 'friends_count' => intval($countfriends), 'created_at' => api_date($uinfo[0]['abook_created']), - 'utc_offset' => "+00:00", + 'utc_offset' => '+00:00', 'time_zone' => 'UTC', //$uinfo[0]['timezone'], 'geo_enabled' => false, 'statuses_count' => intval($countitms), //#XXX: fix me @@ -347,6 +315,7 @@ require_once('include/api_auth.php'); $x = api_get_status($uinfo[0]['xchan_hash']); if($x) $ret['status'] = $x; + // logger('api_get_user: ' . print_r($ret,true)); return $ret; @@ -447,19 +416,23 @@ require_once('include/api_auth.php'); /** * load api $templatename for $type and replace $data array */ + function api_apply_template($templatename, $type, $data){ switch($type){ - case "atom": - case "rss": - case "xml": - $data = array_xmlify($data); - $tpl = get_markup_template("api_".$templatename."_".$type.".tpl"); - $ret = replace_macros($tpl, $data); + case 'xml': + if($data) { + foreach($data as $k => $v) + $ret = arrtoxml(str_replace('$','',$k),$v); + } break; - case "json": + case 'json': default: - $ret = $data; + if($data) { + foreach($data as $rv) { + $ret = json_encode($rv); + } + } break; } @@ -472,22 +445,23 @@ require_once('include/api_auth.php'); * returns a 401 status code and an error message if not. * http://developer.twitter.com/doc/get/account/verify_credentials */ - function api_account_verify_credentials( $type){ - if (api_user()===false) return false; - $user_info = api_get_user($a); - return api_apply_template("user", $type, array('$user' => $user_info)); - + function api_account_verify_credentials($type){ + if(api_user()===false) + return false; + $user_info = api_get_user(); + return api_apply_template('user', $type, array('user' => $user_info)); } api_register_func('api/account/verify_credentials','api_account_verify_credentials', true); + api_register_func('api/1.1/account/verify_credentials','api_account_verify_credentials', true); function api_account_logout( $type){ require_once('include/auth.php'); App::$session->nuke(); - return api_apply_template("user", $type, array('$user' => null)); - + return api_apply_template('user', $type, array('user' => null)); } api_register_func('api/account/logout','api_account_logout', false); + api_register_func('api/1.1/account/logout','api_account_logout', false); @@ -501,274 +475,13 @@ require_once('include/api_auth.php'); } - /* - * Red basic channel export - */ - - function api_export_basic( $type) { - if(api_user() === false) { - logger('api_export_basic: no user'); - return false; - } - - require_once('include/channel.php'); - - json_return_and_die(identity_basic_export(api_user(),(($_REQUEST['posts']) ? intval($_REQUEST['posts']) : 0 ))); - } - api_register_func('api/export/basic','api_export_basic', true); - api_register_func('api/red/channel/export/basic','api_export_basic', true); - api_register_func('api/z/1.0/channel/export/basic','api_export_basic', true); - - - function api_channel_stream( $type) { - if(api_user() === false) { - logger('api_channel_stream: no user'); - return false; - } - - if($_SERVER['REQUEST_METHOD'] == 'POST') { - json_return_and_die(post_activity_item($_REQUEST)); - } - else { - // fetch stream - - } - } - api_register_func('api/red/channel/stream','api_channel_stream', true); - api_register_func('api/z/1.0/channel/stream','api_channel_stream', true); - - function api_attach_list($type) { - logger('api_user: ' . api_user()); - json_return_and_die(attach_list_files(api_user(),get_observer_hash(),'','','','created asc')); - } - api_register_func('api/red/files','api_attach_list', true); - api_register_func('api/z/1.0/files','api_attach_list', true); - - - function api_file_meta($type) { - if (api_user()===false) return false; - if(! $_REQUEST['file_id']) return false; - $r = q("select * from attach where uid = %d and hash = '%s' limit 1", - intval(api_user()), - dbesc($_REQUEST['file_id']) - ); - if($r) { - unset($r[0]['content']); - $ret = array('attach' => $r[0]); - json_return_and_die($ret); - } - killme(); - } - - api_register_func('api/red/filemeta', 'api_file_meta', true); - api_register_func('api/z/1.0/filemeta', 'api_file_meta', true); - - - function api_file_data($type) { - if (api_user()===false) return false; - if(! $_REQUEST['file_id']) return false; - $start = (($_REQUEST['start']) ? intval($_REQUEST['start']) : 0); - $length = (($_REQUEST['length']) ? intval($_REQUEST['length']) : 0); - - $r = q("select * from attach where uid = %d and hash = '%s' limit 1", - intval(api_user()), - dbesc($_REQUEST['file_id']) - ); - if($r) { - $ptr = $r[0]; - if($length === 0) - $length = intval($ptr['filesize']); - - if($ptr['is_dir']) - $ptr['content'] = ''; - elseif(! intval($r[0]['os_storage'])) { - $ptr['start'] = $start; - $x = substr(dbunescbin($ptr['content'],$start,$length)); - $ptr['length'] = strlen($x); - $ptr['content'] = base64_encode($x); - } - else { - $fp = fopen(dbunescbin($ptr['content']),'r'); - if($fp) { - $seek = fseek($fp,$start,SEEK_SET); - $x = fread($fp,$length); - $ptr['start'] = $start; - $ptr['length'] = strlen($x); - $ptr['content'] = base64_encode($x); - } - } - - $ret = array('attach' => $ptr); - json_return_and_die($ret); - } - killme(); - } - - api_register_func('api/red/filedata', 'api_file_data', true); - api_register_func('api/z/1.0/filedata', 'api_file_data', true); - - function api_file_export($type) { - if (api_user()===false) return false; - if(! $_REQUEST['file_id']) return false; - - $ret = attach_export_data(api_user(),$_REQUEST['file_id']); - if($ret) { - json_return_and_die($ret); - } - killme(); - } - - api_register_func('api/red/file/export', 'api_file_export', true); - api_register_func('api/z/1.0/file/export', 'api_file_export', true); - - function api_file_detail($type) { - if (api_user()===false) return false; - if(! $_REQUEST['file_id']) return false; - $r = q("select * from attach where uid = %d and hash = '%s' limit 1", - intval(api_user()), - dbesc($_REQUEST['file_id']) - ); - if($r) { - if($r[0]['is_dir']) - $r[0]['content'] = ''; - elseif(intval($r[0]['os_storage'])) - $r[0]['content'] = base64_encode(file_get_contents(dbunescbin($r[0]['content']))); - else - $r[0]['content'] = base64_encode(dbunescbin($r[0]['content'])); - - $ret = array('attach' => $r[0]); - json_return_and_die($ret); - } - killme(); - } - - - - - - api_register_func('api/red/file', 'api_file_detail', true); - api_register_func('api/z/1.0/file', 'api_file_detail', true); - - - function api_albums($type) { - json_return_and_die(photos_albums_list(App::get_channel(),App::get_observer())); - } - api_register_func('api/red/albums','api_albums', true); - api_register_func('api/z/1.0/albums','api_albums', true); - - function api_photos($type) { - $album = $_REQUEST['album']; - json_return_and_die(photos_list_photos(App::get_channel(),App::get_observer(),$album)); - } - api_register_func('api/red/photos','api_photos', true); - api_register_func('api/z/1.0/photos','api_photos', true); - - function api_photo_detail($type) { - if (api_user()===false) return false; - if(! $_REQUEST['photo_id']) return false; - $scale = ((array_key_exists('scale',$_REQUEST)) ? intval($_REQUEST['scale']) : 0); - $r = q("select * from photo where uid = %d and resource_id = '%s' and imgscale = %d limit 1", - intval(local_channel()), - dbesc($_REQUEST['photo_id']), - intval($scale) - ); - if($r) { - $data = dbunescbin($r[0]['content']); - if(array_key_exists('os_storage',$r[0]) && intval($r[0]['os_storage'])) - $data = file_get_contents($data); - $r[0]['content'] = base64_encode($data); - $ret = array('photo' => $r[0]); - $i = q("select id from item where uid = %d and resource_type = 'photo' and resource_id = '%s' limit 1", - intval(local_channel()), - dbesc($_REQUEST['photo_id']) - ); - if($i) { - $ii = q("select * from item where parent = %d order by id", - intval($i[0]['id']) - ); - if($ii) { - xchan_query($ii,true,0); - $ii = fetch_post_tags($ii,true); - if($ii) { - $ret['item'] = array(); - foreach($ii as $iii) - $ret['item'][] = encode_item($iii,true); - } - } - } - - json_return_and_die($ret); - } - killme(); - } - - api_register_func('api/red/photo', 'api_photo_detail', true); - api_register_func('api/z/1.0/photo', 'api_photo_detail', true); - - - function api_group_members($type) { - if(api_user() === false) - return false; - - if($_REQUEST['group_id']) { - $r = q("select * from groups where uid = %d and id = %d limit 1", - intval(api_user()), - intval($_REQUEST['group_id']) - ); - if($r) { - $x = q("select * from group_member left join xchan on group_member.xchan = xchan.xchan_hash - left join abook on abook_xchan = xchan_hash where gid = %d", - intval($_REQUEST['group_id']) - ); - json_return_and_die($x); - } - } - } - - api_register_func('api/red/group_members','api_group_members', true); - api_register_func('api/z/1.0/group_members','api_group_members', true); - - - - - function api_group($type) { - if(api_user() === false) - return false; - - $r = q("select * from groups where uid = %d", - intval(api_user()) - ); - json_return_and_die($r); - } - api_register_func('api/red/group','api_group', true); - api_register_func('api/z/1.0/group','api_group', true); - - - function api_red_xchan($type) { - logger('api_xchan'); - - if(api_user() === false) - return false; - logger('api_xchan'); - require_once('include/hubloc.php'); - - if($_SERVER['REQUEST_METHOD'] === 'POST') { - $r = xchan_store($_REQUEST); - } - $r = xchan_fetch($_REQUEST); - json_return_and_die($r); - }; - - api_register_func('api/red/xchan','api_red_xchan',true); - api_register_func('api/z/1.0/xchan','api_red_xchan',true); - function api_statuses_mediap( $type) { if (api_user() === false) { logger('api_statuses_update: no user'); return false; } - $user_info = api_get_user($a); + $user_info = api_get_user(); // logger('status_with_media: ' . print_r($_REQUEST,true), LOGGER_DEBUG); @@ -799,7 +512,7 @@ require_once('include/api_auth.php'); $mod->post(); - $_REQUEST['body']=$txt."\n\n".$posted; + $_REQUEST['body']= $txt . "\n\n" . $posted; $mod = new Zotlabs\Module\Item(); $mod->post(); @@ -807,7 +520,9 @@ require_once('include/api_auth.php'); // this should output the last post (the one we just posted). return api_status_show($type); } + api_register_func('api/statuses/mediap','api_statuses_mediap', true); + api_register_func('api/1.1/statuses/mediap','api_statuses_mediap', true); function api_statuses_update( $type) { if (api_user() === false) { @@ -824,7 +539,7 @@ require_once('include/api_auth.php'); $_REQUEST['api_source'] = true; - $user_info = api_get_user($a); + $user_info = api_get_user(); // convert $_POST array items to the form we use for web posts. @@ -928,80 +643,8 @@ require_once('include/api_auth.php'); } api_register_func('api/statuses/update_with_media','api_statuses_update', true); api_register_func('api/statuses/update','api_statuses_update', true); - - - function red_item_new( $type) { - - if (api_user() === false) { - logger('api_red_item_new: no user'); - return false; - } - - logger('api_red_item_new: REQUEST ' . print_r($_REQUEST,true)); - logger('api_red_item_new: FILES ' . print_r($_FILES,true)); - - - // set this so that the item_post() function is quiet and doesn't redirect or emit json - - $_REQUEST['api_source'] = true; - $_REQUEST['profile_uid'] = api_user(); - - if(x($_FILES,'media')) { - $_FILES['userfile'] = $_FILES['media']; - // upload the image if we have one - $_REQUEST['silent']='1'; //tell wall_upload function to return img info instead of echo - $mod = new Zotlabs\Module\Wall_upload(); - $media = $mod->post(); - if(strlen($media)>0) - $_REQUEST['body'] .= "\n\n".$media; - } - - $mod = new Zotlabs\Module\Item(); - $x = $mod->post(); - json_return_and_die($x); - } - - api_register_func('api/red/item/new','red_item_new', true); - api_register_func('api/z/1.0/item/new','red_item_new', true); - - - function red_item( $type) { - - if (api_user() === false) { - logger('api_red_item_full: no user'); - return false; - } - - if($_REQUEST['mid']) { - $arr = array('mid' => $_REQUEST['mid']); - } - elseif($_REQUEST['item_id']) { - $arr = array('item_id' => $_REQUEST['item_id']); - } - else - json_return_and_die(array()); - - $arr['start'] = 0; - $arr['records'] = 999999; - $arr['item_type'] = '*'; - - $i = items_fetch($arr,App::get_channel(),get_observer_hash()); - - if(! $i) - json_return_and_die(array()); - - $ret = array(); - $tmp = array(); - foreach($i as $ii) { - $tmp[] = encode_item($ii,true); - } - $ret['item'] = $tmp; - - json_return_and_die($ret); - } - - api_register_func('api/red/item/full','red_item', true); - api_register_func('api/z/1.0/item/full','red_item', true); + api_register_func('api/1.1/statuses/update_with_media','api_statuses_update', true); + api_register_func('api/1.1/statuses/update','api_statuses_update', true); @@ -1068,7 +711,7 @@ require_once('include/api_auth.php'); } function api_status_show( $type){ - $user_info = api_get_user($a); + $user_info = api_get_user(); // get last public message @@ -1131,7 +774,7 @@ require_once('include/api_auth.php'); unset($status_info['user']['status']); } - return api_apply_template("status", $type, array('$status' => $status_info)); + return api_apply_template('status', $type, array('$status' => $status_info)); } @@ -1146,7 +789,7 @@ require_once('include/api_auth.php'); function api_users_show( $type){ - $user_info = api_get_user($a); + $user_info = api_get_user(); require_once('include/security.php'); $item_normal = item_normal(); @@ -1204,10 +847,12 @@ require_once('include/api_auth.php'); ); } - return api_apply_template("user", $type, array('$user' => $user_info)); + return api_apply_template('user', $type, array('$user' => $user_info)); } + api_register_func('api/users/show','api_users_show'); + api_register_func('api/1.1/users/show','api_users_show'); /** * @@ -1221,27 +866,26 @@ require_once('include/api_auth.php'); if (api_user() === false) return false; - $user_info = api_get_user($a); + $user_info = api_get_user(); // get last network messages // params - $count = (x($_REQUEST,'count')?$_REQUEST['count']:20); - $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); + $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 20); + $page = (x($_REQUEST,'page') ? $_REQUEST['page']-1 : 0); if($page < 0) $page = 0; - $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); - $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0); - $exclude_replies = (x($_REQUEST,'exclude_replies')?1:0); - //$since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); + $since_id = (x($_REQUEST,'since_id') ? $_REQUEST['since_id'] : 0); + $max_id = (x($_REQUEST,'max_id') ? $_REQUEST['max_id'] : 0); + $exclude_replies = (x($_REQUEST,'exclude_replies') ? 1 : 0); - $start = $page*$count; + $start = $page * $count; //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false); $sql_extra = ''; if ($max_id > 0) - $sql_extra .= ' AND item.id <= '.intval($max_id); + $sql_extra .= ' AND item.id <= ' . intval($max_id); if ($exclude_replies > 0) $sql_extra .= ' AND item.parent = item.id'; @@ -1250,7 +894,7 @@ require_once('include/api_auth.php'); require_once('include/permissions.php'); if(! perm_is_allowed($user_info['uid'],(($observer) ? $observer['xchan_hash'] : ''),'view_stream')) return ''; - $sql_extra .= " and item_private = 0 "; + $sql_extra .= ' and item_private = 0 '; } $item_normal = item_normal(); @@ -1279,42 +923,32 @@ require_once('include/api_auth.php'); ); } - $data = array('$statuses' => $ret); - switch($type){ - case "atom": - case "rss": - $data = api_rss_extra( $data, $user_info); - break; - case "as": - $as = api_format_as( $ret, $user_info); - $as['title'] = App::$config['sitename']." Home Timeline"; - $as['link']['url'] = z_root()."/".$user_info["screen_name"]."/all"; - return($as); - break; - } - - return api_apply_template("timeline", $type, $data); + return api_apply_template('timeline', $type, $data); } + api_register_func('api/statuses/home_timeline','api_statuses_home_timeline', true); api_register_func('api/statuses/friends_timeline','api_statuses_home_timeline', true); + api_register_func('api/1.1/statuses/home_timeline','api_statuses_home_timeline', true); + api_register_func('api/1.1/statuses/friends_timeline','api_statuses_home_timeline', true); function api_statuses_public_timeline( $type){ - if (api_user()===false) return false; + if(api_user() === false) + return false; - $user_info = api_get_user($a); + $user_info = api_get_user(); $sys = get_sys_channel(); // params - $count = (x($_REQUEST,'count')?$_REQUEST['count']:20); - $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); - if ($page<0) $page=0; - $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); - $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0); - //$since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); + $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 20); + $page = (x($_REQUEST,'page') ? $_REQUEST['page']-1 : 0); + if($page < 0) + $page=0; + $since_id = (x($_REQUEST,'since_id') ? $_REQUEST['since_id'] : 0); + $max_id = (x($_REQUEST,'max_id') ? $_REQUEST['max_id'] : 0); - $start = $page*$count; + $start = $page * $count; //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false); @@ -1341,32 +975,20 @@ require_once('include/api_auth.php'); $ret = api_format_items($r,$user_info); - $data = array('$statuses' => $ret); - switch($type){ - case "atom": - case "rss": - $data = api_rss_extra( $data, $user_info); - break; - case "as": - $as = api_format_as( $ret, $user_info); - $as['title'] = App::$config['sitename']. " " . t('Public Timeline'); - $as['link']['url'] = z_root()."/"; - return($as); - break; - } + $data = array('statuses' => $ret); - return api_apply_template("timeline", $type, $data); + return api_apply_template('timeline', $type, $data); } + api_register_func('api/statuses/public_timeline','api_statuses_public_timeline', true); + api_register_func('api/1.1/statuses/public_timeline','api_statuses_public_timeline', true); - /** - * - */ - function api_statuses_show( $type){ - if (api_user()===false) return false; + function api_statuses_show($type){ + if(api_user()===false) + return false; - $user_info = api_get_user($a); + $user_info = api_get_user(); // params $id = intval(argv(3)); @@ -1375,8 +997,8 @@ require_once('include/api_auth.php'); logger('API: api_statuses_show: '.$id); - //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false); - $conversation = (x($_REQUEST,'conversation')?1:0); + //$include_entities = (x($_REQUEST,'include_entities') ? $_REQUEST['include_entities'] : false); + $conversation = (x($_REQUEST,'conversation') ? 1 : 0); $sql_extra = ''; if ($conversation) @@ -1385,7 +1007,8 @@ require_once('include/api_auth.php'); $sql_extra .= " AND item.id = %d"; $item_normal = item_normal(); - $r = q("select * from item where true $item_normal $sql_extra", + $r = q("select * from item where uid = %d $item_normal $sql_extra", + intval(api_user()), intval($id) ); @@ -1395,35 +1018,33 @@ require_once('include/api_auth.php'); if ($conversation) { - $data = array('$statuses' => $ret); - return api_apply_template("timeline", $type, $data); + $data = array('statuses' => $ret); + return api_apply_template('timeline', $type, $data); } else { - $data = array('$status' => $ret[0]); - /*switch($type){ - case "atom": - case "rss": - $data = api_rss_extra( $data, $user_info); - }*/ - return api_apply_template("status", $type, $data); + $data = array('status' => $ret[0]); + return api_apply_template('status', $type, $data); } } + api_register_func('api/statuses/show','api_statuses_show', true); + api_register_func('api/1.1/statuses/show','api_statuses_show', true); /** * */ function api_statuses_repeat( $type){ - if (api_user()===false) return false; + if(api_user()===false) + return false; - $user_info = api_get_user($a); + $user_info = api_get_user(); // params $id = intval(argv(3)); - logger('API: api_statuses_repeat: '.$id); + logger('API: api_statuses_repeat: ' . $id); - //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false); + //$include_entities = (x($_REQUEST,'include_entities') ? $_REQUEST['include_entities'] : false); $observer = App::get_observer(); @@ -1434,8 +1055,8 @@ require_once('include/api_auth.php'); ); if(perm_is_allowed($r[0]['uid'],$observer['xchan_hash'],'view_stream')) { - if ($r[0]['body'] != "") { - $_REQUEST['body'] = html_entity_decode("♲ ", ENT_QUOTES, 'UTF-8')."[zrl=".$r[0]['reply_url']."]".$r[0]['reply_author']."[/zrl] \n".$r[0]['body']; + if ($r[0]['body'] != '') { + $_REQUEST['body'] = html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . '[zrl=' . $r[0]['reply_url'] . ']' . $r[0]['reply_author'] . '[/zrl] ' . "\n" . $r[0]['body']; $_REQUEST['profile_uid'] = api_user(); $_REQUEST['type'] = 'wall'; $_REQUEST['api_source'] = true; @@ -1447,22 +1068,25 @@ require_once('include/api_auth.php'); return false; if ($type == 'xml') - $ok = "true"; + $ok = 'true'; else - $ok = "ok"; + $ok = 'ok'; return api_apply_template('test', $type, array('$ok' => $ok)); } + api_register_func('api/statuses/retweet','api_statuses_repeat', true); + api_register_func('api/1.1/statuses/retweet','api_statuses_repeat', true); /** * */ function api_statuses_destroy( $type){ - if (api_user()===false) return false; + if(api_user()===false) + return false; - $user_info = api_get_user($a); + $user_info = api_get_user(); // params $id = intval(argv(3)); @@ -1508,13 +1132,15 @@ require_once('include/api_auth.php'); if ($type == 'xml') - $ok = "true"; + $ok = 'true'; else - $ok = "ok"; + $ok = 'ok'; return api_apply_template('test', $type, array('$ok' => $ok)); } + api_register_func('api/statuses/destroy','api_statuses_destroy', true); + api_register_func('api/1.1/statuses/destroy','api_statuses_destroy', true); /** * @@ -1526,27 +1152,22 @@ require_once('include/api_auth.php'); function api_statuses_mentions( $type){ if (api_user()===false) return false; - $user_info = api_get_user($a); + $user_info = api_get_user(); // get last network messages // params - $count = (x($_REQUEST,'count')?$_REQUEST['count']:20); - $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); - if ($page<0) $page=0; - $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); - $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0); - //$since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); + $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 20); + $page = (x($_REQUEST,'page') ? $_REQUEST['page']-1 : 0); + if($page < 0) + $page=0; + $since_id = (x($_REQUEST,'since_id') ? $_REQUEST['since_id'] : 0); + $max_id = (x($_REQUEST,'max_id') ? $_REQUEST['max_id'] : 0); - $start = $page*$count; + $start = $page * $count; //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false); - $myurl = z_root() . '/channel/'. App::$user['nickname']; - $myurl = substr($myurl,strpos($myurl,'://')+3); - $myurl = str_replace(array('www.','.'),array('','\\.'),$myurl); - $diasp_url = str_replace('/channel/','/u/',$myurl); - $sql_extra .= " AND item_mentionsme = 1 "; if ($max_id > 0) $sql_extra .= " AND item.id <= " . intval($max_id) . " "; @@ -1565,85 +1186,57 @@ require_once('include/api_auth.php'); xchan_query($r,true); - $ret = api_format_items($r,$user_info); + $data = array('statuses' => $ret); - $data = array('$statuses' => $ret); - switch($type){ - case "atom": - case "rss": - $data = api_rss_extra( $data, $user_info); - break; - case "as": - $as = api_format_as( $ret, $user_info); - $as["title"] = App::$config['sitename']." Mentions"; - $as['link']['url'] = z_root()."/"; - return($as); - break; - } - - return api_apply_template("timeline", $type, $data); + return api_apply_template('timeline', $type, $data); } + api_register_func('api/statuses/mentions','api_statuses_mentions', true); // FIXME?? I don't think mentions and replies are congruent in this case api_register_func('api/statuses/replies','api_statuses_mentions', true); + api_register_func('api/1.1/statuses/mentions','api_statuses_mentions', true); + api_register_func('api/1.1/statuses/replies','api_statuses_mentions', true); + function api_statuses_user_timeline( $type){ - if (api_user()===false) return false; + if(api_user() === false) + return false; - $user_info = api_get_user($a); - // get last network messages + $user_info = api_get_user(); + // get last network messages - logger("api_statuses_user_timeline: api_user: ". api_user() . + logger('api_statuses_user_timeline: api_user: '. api_user() . "\nuser_info: ".print_r($user_info, true) . "\n_REQUEST: ".print_r($_REQUEST, true), LOGGER_DEBUG); // params - $count = (x($_REQUEST,'count')?$_REQUEST['count']:20); - $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); - if ($page<0) $page=0; - $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); - $exclude_replies = (x($_REQUEST,'exclude_replies')?1:0); - //$since_id = 0;//$since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); + $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 20); + $page = (x($_REQUEST,'page') ? $_REQUEST['page']-1 : 0); + if($page < 0) + $page = 0; + $since_id = (x($_REQUEST,'since_id') ? $_REQUEST['since_id'] : 0); + $exclude_replies = (x($_REQUEST,'exclude_replies') ? 1 :0); - $start = $page*$count; + $start = $page * $count; $sql_extra = ''; - if ($user_info['self']==1) $sql_extra .= " AND item.wall = 1 "; //FIXME - this isn't yet implemented if ($exclude_replies > 0) $sql_extra .= ' AND item.parent = item.id'; -// $r = q("SELECT item.*, item.id AS item_id, -// contact.name, contact.photo, contact.url, contact.rel, -// contact.network, contact.thumb, contact.dfrn_id, contact.self, -// contact.id AS cid, contact.uid AS contact-uid -// FROM item, contact -// WHERE item.uid = %d -// AND item.contact-id = %d -// AND item.visible = 1 and item.moderated = 0 AND item.deleted = 0 -// AND contact.id = item.contact-id -// AND contact.blocked = 0 AND contact.pending = 0 -// $sql_extra -// AND item.id>%d -// ORDER BY item.received DESC LIMIT %d ,%d ", -// intval(api_user()), -// intval($user_info['id']), -// intval($since_id), -// intval($start), intval($count) -// ); - - $arr = array( - 'uid' => api_user(), - 'since_id' => $since_id, - 'start' => $start, - 'records' => $count); + $arr = [ + 'uid' => api_user(), + 'since_id' => $since_id, + 'start' => $start, + 'records' => $count + ]; - if ($user_info['self']==1) + if ($user_info['self'] === 1) $arr['wall'] = 1; else $arr['cid'] = $user_info['id']; @@ -1653,18 +1246,12 @@ require_once('include/api_auth.php'); $ret = api_format_items($r,$user_info); - - $data = array('$statuses' => $ret); - switch($type){ - case "atom": - case "rss": - $data = api_rss_extra( $data, $user_info); - } - - return api_apply_template("timeline", $type, $data); + $data = array('statuses' => $ret); + return(api_apply_template('timeline', $type, $data)); } api_register_func('api/statuses/user_timeline','api_statuses_user_timeline', true); + api_register_func('api/1.1/statuses/user_timeline','api_statuses_user_timeline', true); @@ -1674,17 +1261,16 @@ require_once('include/api_auth.php'); * * api v1 : https://web.archive.org/web/20131019055350/https://dev.twitter.com/docs/api/1/post/favorites/create/%3Aid */ - function api_favorites_create_destroy( $type){ + function api_favorites_create_destroy($type){ - logger('favorites_create_destroy'); - - if (api_user()===false) + if(api_user() === false) return false; - $action = str_replace(".".$type,"",argv(2)); + $action = str_replace('.' . $type,'',argv(2)); if (argc() > 3) { $itemid = intval(argv(3)); - } else { + } + else { $itemid = intval($_REQUEST['id']); } @@ -1693,14 +1279,14 @@ require_once('include/api_auth.php'); intval(api_user()) ); - if (! $item) + if(! $item) return false; switch($action){ - case "create": + case 'create': $flags = $item[0]['item_starred'] = 1; break; - case "destroy": + case 'destroy': $flags = $item[0]['item_starred'] = 0; break; default: @@ -1722,48 +1308,43 @@ require_once('include/api_auth.php'); xchan_query($item,true); - - $user_info = api_get_user($a); + $user_info = api_get_user(); $rets = api_format_items($item,$user_info); $ret = $rets[0]; - $data = array('$status' => $ret); - switch($type){ - case "atom": - case "rss": - $data = api_rss_extra( $data, $user_info); - } + $data = array('status' => $ret); - return api_apply_template("status", $type, $data); + return api_apply_template('status', $type, $data); } api_register_func('api/favorites/create', 'api_favorites_create_destroy', true); api_register_func('api/favorites/destroy', 'api_favorites_create_destroy', true); - + api_register_func('api/1.1/favorites/create', 'api_favorites_create_destroy', true); + api_register_func('api/1.1/favorites/destroy', 'api_favorites_create_destroy', true); function api_favorites( $type){ - if (api_user()===false) + if(api_user()===false) return false; - $user_info = api_get_user($a); + $user_info = api_get_user(); // params - $count = (x($_REQUEST,'count')?$_REQUEST['count']:20); - $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); + $count = (x($_REQUEST,'count') ? $_REQUEST['count'] : 20); + $page = (x($_REQUEST,'page') ? $_REQUEST['page']-1 : 0); if($page < 0) $page = 0; - $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); - $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0); - $exclude_replies = (x($_REQUEST,'exclude_replies')?1:0); + $since_id = (x($_REQUEST,'since_id') ? $_REQUEST['since_id'] : 0); + $max_id = (x($_REQUEST,'max_id') ? $_REQUEST['max_id'] : 0); + $exclude_replies = (x($_REQUEST,'exclude_replies') ? 1 :0); - $start = $page*$count; + $start = $page * $count; - //$include_entities = (x($_REQUEST,'include_entities')?$_REQUEST['include_entities']:false); + //$include_entities = (x($_REQUEST,'include_entities') ? $_REQUEST['include_entities'] : false); $sql_extra = ''; if ($max_id > 0) - $sql_extra .= ' AND item.id <= '.intval($max_id); + $sql_extra .= ' AND item.id <= ' . intval($max_id); if ($exclude_replies > 0) $sql_extra .= ' AND item.parent = item.id'; @@ -1772,7 +1353,7 @@ require_once('include/api_auth.php'); require_once('include/permissions.php'); if(! perm_is_allowed($user_info['uid'],(($observer) ? $observer['xchan_hash'] : ''),'view_stream')) return ''; - $sql_extra .= " and item_private = 0 "; + $sql_extra .= ' and item_private = 0 '; } $item_normal = item_normal(); @@ -1791,94 +1372,15 @@ require_once('include/api_auth.php'); $ret = api_format_items($r,$user_info); - $data = array('$statuses' => $ret); - switch($type){ - case "atom": - case "rss": - $data = api_rss_extra( $data, $user_info); - break; - case "as": - $as = api_format_as( $ret, $user_info); - $as['title'] = App::$config['sitename']." Home Timeline"; - $as['link']['url'] = z_root()."/".$user_info["screen_name"]."/all"; - return($as); - break; - } - - return api_apply_template("timeline", $type, $data); + $data = array('statuses' => $ret); + return(api_apply_template('timeline', $type, $data)); } api_register_func('api/favorites','api_favorites', true); + api_register_func('api/1.1/favorites','api_favorites', true); - - - function api_format_as( $ret, $user_info) { - - $as = array(); - $as['title'] = App::$config['sitename']." Public Timeline"; - $items = array(); - foreach ($ret as $item) { - $singleitem["actor"]["displayName"] = $item["user"]["name"]; - $singleitem["actor"]["id"] = $item["user"]["contact_url"]; - $avatar[0]["url"] = $item["user"]["profile_image_url"]; - $avatar[0]["rel"] = "avatar"; - $avatar[0]["type"] = ""; - $avatar[0]["width"] = 96; - $avatar[0]["height"] = 96; - $avatar[1]["url"] = $item["user"]["profile_image_url"]; - $avatar[1]["rel"] = "avatar"; - $avatar[1]["type"] = ""; - $avatar[1]["width"] = 48; - $avatar[1]["height"] = 48; - $avatar[2]["url"] = $item["user"]["profile_image_url"]; - $avatar[2]["rel"] = "avatar"; - $avatar[2]["type"] = ""; - $avatar[2]["width"] = 24; - $avatar[2]["height"] = 24; - $singleitem["actor"]["avatarLinks"] = $avatar; - - $singleitem["actor"]["image"]["url"] = $item["user"]["profile_image_url"]; - $singleitem["actor"]["image"]["rel"] = "avatar"; - $singleitem["actor"]["image"]["type"] = ""; - $singleitem["actor"]["image"]["width"] = 96; - $singleitem["actor"]["image"]["height"] = 96; - $singleitem["actor"]["type"] = "person"; - $singleitem["actor"]["url"] = $item["person"]["contact_url"]; - $singleitem["actor"]["statusnet:profile_info"]["local_id"] = $item["user"]["id"]; - $singleitem["actor"]["statusnet:profile_info"]["following"] = $item["user"]["following"] ? "true" : "false"; - $singleitem["actor"]["statusnet:profile_info"]["blocking"] = "false"; - $singleitem["actor"]["contact"]["preferredUsername"] = $item["user"]["screen_name"]; - $singleitem["actor"]["contact"]["displayName"] = $item["user"]["name"]; - $singleitem["actor"]["contact"]["addresses"] = ""; - - $singleitem["body"] = $item["text"]; - $singleitem["object"]["displayName"] = $item["text"]; - $singleitem["object"]["id"] = $item["url"]; - $singleitem["object"]["type"] = "note"; - $singleitem["object"]["url"] = $item["url"]; - //$singleitem["context"] =; - $singleitem["postedTime"] = date("c", strtotime($item["published"])); - $singleitem["provider"]["objectType"] = "service"; - $singleitem["provider"]["displayName"] = "Test"; - $singleitem["provider"]["url"] = "http://test.tld"; - $singleitem["title"] = $item["text"]; - $singleitem["verb"] = "post"; - $singleitem["statusnet:notice_info"]["local_id"] = $item["id"]; - $singleitem["statusnet:notice_info"]["source"] = $item["source"]; - $singleitem["statusnet:notice_info"]["favorite"] = "false"; - $singleitem["statusnet:notice_info"]["repeated"] = "false"; - //$singleitem["original"] = $item; - $items[] = $singleitem; - } - $as['items'] = $items; - $as['link']['url'] = z_root()."/".$user_info["screen_name"]."/all"; - $as['link']['rel'] = "alternate"; - $as['link']['type'] = "text/html"; - return($as); - } - function api_format_message($item, $recipient, $sender) { // standard meta information $ret = array( @@ -1895,17 +1397,17 @@ require_once('include/api_auth.php'); //don't send title to regular StatusNET requests to avoid confusing these apps if (x($_GET, 'getText')) { $ret['title'] = $item['title'] ; - if ($_GET["getText"] == "html") { + if ($_GET['getText'] === 'html') { $ret['text'] = prepare_text($item['body'],$item['mimetype']); } - elseif ($_GET["getText"] == "plain") { + elseif ($_GET['getText'] === 'plain') { $ret['text'] = html2plain(prepare_text($item['body'],$item['mimetype']), 0); } } else { - $ret['text'] = $item['title']."\n".html2plain(prepare_text($item['body'],$item['mimetype']), 0); + $ret['text'] = $item['title'] . "\n" . html2plain(prepare_text($item['body'],$item['mimetype']),0); } - if (isset($_GET["getUserObjects"]) && $_GET["getUserObjects"] == "false") { + if (isset($_GET['getUserObjects']) && $_GET['getUserObjects'] == 'false') { unset($ret['sender']); unset($ret['recipient']); } @@ -1964,12 +1466,12 @@ require_once('include/api_auth.php'); if (($statustitle != '') and (strpos($statusbody, $statustitle) !== false)) $statustext = trim($statusbody); else - $statustext = trim($statustitle."\n\n".$statusbody); + $statustext = trim($statustitle . "\n\n" . $statusbody); $status = array( 'text' => $statustext, - 'truncated' => False, + 'truncated' => false, 'created_at' => api_date($item['created']), 'in_reply_to_status_id' => $in_reply_to_status_id, 'source' => (($item['app']) ? $item['app'] : 'web'), @@ -1998,8 +1500,8 @@ require_once('include/api_auth.php'); 'entities' => '', 'objecttype' => (($item['obj_type']) ? $item['obj_type'] : ACTIVITY_OBJ_NOTE), 'verb' => (($item['verb']) ? $item['verb'] : ACTIVITY_POST), - 'self' => z_root()."/api/statuses/show/".$item['id'].".".$type, - 'edit' => z_root()."/api/statuses/show/".$item['id'].".".$type, + 'self' => z_root().'/api/statuses/show/'.$item['id'].'.'.$type, + 'edit' => z_root().'/api/statuses/show/'.$item['id'].'.'.$type, ); $status = array_merge($status, $status2); @@ -2019,25 +1521,29 @@ require_once('include/api_auth.php'); 'hourly_limit' => (string) 150, 'reset_time' => datetime_convert('UTC','UTC','now + 1 hour',ATOM_TIME), ); - if ($type == "xml") + if ($type == 'xml') $hash['resettime_in_seconds'] = $hash['reset_time_in_seconds']; return api_apply_template('ratelimit', $type, array('$hash' => $hash)); } + api_register_func('api/account/rate_limit_status','api_account_rate_limit_status',true); + api_register_func('api/1.1/account/rate_limit_status','api_account_rate_limit_status',true); function api_help_test($type) { if ($type == 'xml') - $ok = "true"; + $ok = 'true'; else - $ok = "ok"; + $ok = 'ok'; - return api_apply_template('test', $type, array('$ok' => $ok)); + return api_apply_template('test', $type, array('ok' => $ok)); } + api_register_func('api/help/test','api_help_test',false); + api_register_func('api/1.1/help/test','api_help_test',false); /** * https://dev.twitter.com/docs/api/1/get/statuses/friends @@ -2045,8 +1551,9 @@ require_once('include/api_auth.php'); * returns: json, xml **/ function api_statuses_f( $type, $qtype) { - if (api_user()===false) return false; - $user_info = api_get_user($a); + if(api_user()===false) + return false; + $user_info = api_get_user(); // friends and followers only for self @@ -2061,80 +1568,106 @@ require_once('include/api_auth.php'); */ /*$ret=Array(); - return array('$users' => $ret);*/ + return array('users' => $ret);*/ return false; } -// @fixme - update for hubzilla extensible perms using abconfig or find a better way to do it - // For Red, the closest thing we can do to figure out if you're friends is if both of you are sending each other your streams. - // This won't work if either of you send your stream to everybody on the network - if($qtype == 'friends') - $sql_extra = sprintf(" AND ( abook_their_perms & %d )>0 and ( abook_my_perms & %d )>0 ", intval(PERMS_W_STREAM), intval(PERMS_W_STREAM)); - if($qtype == 'followers') - $sql_extra = sprintf(" AND ( abook_my_perms & %d )>0 and not ( abook_their_perms & %d )>0 ", intval(PERMS_W_STREAM), intval(PERMS_W_STREAM)); - - $r = q("SELECT abook_id FROM abook where abook_self = 0 and abook_channel = %d $sql_extra", - intval(api_user()) - ); + + + + if($qtype == 'friends') { + $r = q("select abook_id from abook left join abconfig on abook_xchan = xchan and abook_channel = chan + where chan = %d and abook_self = 0 and abook_pending = 0 and cat = 'my_perms' and k = 'view_stream' and v = '1' ", + intval(api_user()) + ); + } + + if($qtype == 'followers') { + $r = q("select abook_id from abook left join abconfig on abook_xchan = xchan and abook_channel = chan + where chan = %d and abook_self = 0 and abook_pending = 0 and cat = 'their_perms' and k = 'view_stream' and v = '1' ", + intval(api_user()) + ); + } $ret = array(); - foreach($r as $cid){ - $ret[] = api_get_user( $cid['abook_id']); + + if($r) { + foreach($r as $cid) { + $ret[] = api_get_user($cid['abook_id']); + } } - - return array('$users' => $ret); + return array('users' => $ret); } - function api_statuses_friends( $type){ - $data = api_statuses_f($type,"friends"); - if ($data===false) return false; - return api_apply_template("friends", $type, $data); + + function api_statuses_friends($type){ + $data = api_statuses_f($type,'friends'); + if($data === false) + return false; + return(api_apply_template('friends', $type, $data)); } - function api_statuses_followers( $type){ - $data = api_statuses_f($type,"followers"); - if ($data===false) return false; - return api_apply_template("friends", $type, $data); + function api_statuses_followers($type){ + $data = api_statuses_f($type,'followers'); + if ($data === false) + return false; + return(api_apply_template('friends', $type, $data)); } + api_register_func('api/statuses/friends','api_statuses_friends',true); api_register_func('api/statuses/followers','api_statuses_followers',true); - - - - + api_register_func('api/1.1/statuses/friends','api_statuses_friends',true); + api_register_func('api/1.1/statuses/followers','api_statuses_followers',true); function api_statusnet_config($type) { - load_config('system'); - - $name = get_config('system','sitename'); - $server = App::get_hostname(); - $logo = z_root() . '/images/rm-64.png'; - $email = get_config('system','admin_email'); - $closed = ((get_config('system','register_policy') == REGISTER_CLOSED) ? 'true' : 'false'); - $private = ((get_config('system','block_public')) ? 'true' : 'false'); - $textlimit = (string) ((get_config('system','max_import_size')) ? get_config('system','max_import_size') : 200000); + $name = get_config('system','sitename'); + $server = App::get_hostname(); + $logo = z_root() . '/images/hz-64.png'; + $email = get_config('system','admin_email'); + $closed = ((get_config('system','register_policy') == REGISTER_CLOSED) ? true : false); + $private = ((get_config('system','block_public')) ? true : false); + $textlimit = ((get_config('system','max_import_size')) ? get_config('system','max_import_size') : 200000); if(get_config('system','api_import_size')) - $texlimit = string(get_config('system','api_import_size')); - $ssl = ((get_config('system','have_ssl')) ? 'true' : 'false'); - $sslserver = (($ssl === 'true') ? str_replace('http:','https:',z_root()) : ''); - - $config = array( - 'site' => array('name' => $name,'server' => $server, 'theme' => 'default', 'path' => '', - 'logo' => $logo, 'fancy' => 'true', 'language' => 'en', 'email' => $email, 'broughtby' => '', - 'broughtbyurl' => '', 'timezone' => 'UTC', 'closed' => $closed, 'inviteonly' => 'false', - 'private' => $private, 'textlimit' => $textlimit, 'sslserver' => $sslserver, 'ssl' => $ssl, - 'shorturllength' => '30', - 'hubzilla' => array( - 'PLATFORM_NAME' => Zotlabs\Lib\System::get_platform_name(), - 'STD_VERSION' => Zotlabs\Lib\System::get_project_version(), - 'ZOT_REVISION' => ZOT_REVISION, - 'DB_UPDATE_VERSION' => Zotlabs\Lib\System::get_update_version() - ) - )); - - return api_apply_template('config', $type, array('$config' => $config)); + $texlimit = get_config('system','api_import_size'); + + $m = parse_url(z_root()); + + $ssl = (($m['scheme'] === 'https') ? true : false); + $sslserver = (($ssl) ? str_replace('http:','https:',z_root()) : ''); + + $config = [ + 'site' => [ + 'name' => $name, + 'server' => $server, + 'theme' => 'default', + 'path' => '', + 'logo' => $logo, + 'fancy' => true, + 'language' => 'en', + 'email' => $email, + 'broughtby' => '', + 'broughtbyurl' => '', + 'timezone' => 'UTC', + 'closed' => $closed, + 'inviteonly' => false, + 'private' => $private, + 'textlimit' => $textlimit, + 'sslserver' => $sslserver, + 'ssl' => $ssl, + 'shorturllength' => 30, + + 'platform' => [ + 'PLATFORM_NAME' => Zotlabs\Lib\System::get_platform_name(), + 'STD_VERSION' => Zotlabs\Lib\System::get_project_version(), + 'ZOT_REVISION' => ZOT_REVISION, + 'DB_UPDATE_VERSION' => Zotlabs\Lib\System::get_update_version() + ] + ] + ]; + + return api_apply_template('config', $type, array('config' => $config)); } api_register_func('api/statusnet/config','api_statusnet_config',false); @@ -2147,12 +1680,12 @@ require_once('include/api_auth.php'); // liar if($type === 'xml') { - header("Content-type: application/xml"); + header('Content-type: application/xml'); echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<version>0.9.7</version>' . "\r\n"; killme(); } elseif($type === 'json') { - header("Content-type: application/json"); + header('Content-type: application/json'); echo '"0.9.7"'; killme(); } @@ -2163,12 +1696,12 @@ require_once('include/api_auth.php'); function api_friendica_version($type) { if($type === 'xml') { - header("Content-type: application/xml"); + header('Content-type: application/xml'); echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<version>' . Zotlabs\Lib\System::get_project_version() . '</version>' . "\r\n"; killme(); } elseif($type === 'json') { - header("Content-type: application/json"); + header('Content-type: application/json'); echo '"' . Zotlabs\Lib\System::get_project_version() . '"'; killme(); } @@ -2179,35 +1712,39 @@ require_once('include/api_auth.php'); function api_ff_ids($type,$qtype) { + if(! api_user()) return false; + if($qtype == 'friends') { + $r = q("select abook_id from abook left join abconfig on abook_xchan = xchan and abook_channel = chan + where chan = %d and abook_self = 0 and abook_pending = 0 and cat = 'my_perms' and k = 'view_stream' and v = '1' ", + intval(api_user()) + ); + } - // For Red, the closest thing we can do to figure out if you're friends is if both of you are sending each other your streams. - // This won't work if either of you send your stream to everybody on the network - - if($qtype == 'friends') - $sql_extra = sprintf(" AND ( abook_their_perms & %d )>0 and ( abook_my_perms & %d )>0 ", intval(PERMS_W_STREAM), intval(PERMS_W_STREAM)); - if($qtype == 'followers') - $sql_extra = sprintf(" AND ( abook_my_perms & %d )>0 and not ( abook_their_perms & %d )>0 ", intval(PERMS_W_STREAM), intval(PERMS_W_STREAM)); - - $r = q("SELECT abook_id FROM abook where abook_self = 0 and abook_channel = %d $sql_extra", - intval(api_user()) - ); + if($qtype == 'followers') { + $r = q("select abook_id from abook left join abconfig on abook_xchan = xchan and abook_channel = chan + where chan = %d and abook_self = 0 and abook_pending = 0 and cat = 'their_perms' and k = 'view_stream' and v = '1' ", + intval(api_user()) + ); + } - if(is_array($r)) { + if($r) { if($type === 'xml') { - header("Content-type: application/xml"); + header('Content-type: application/xml'); echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<ids>' . "\r\n"; - foreach($r as $rr) - echo '<id>' . $rr['abook_id'] . '</id>' . "\r\n"; + foreach($r as $rv) + echo '<id>' . $rv['abook_id'] . '</id>' . "\r\n"; echo '</ids>' . "\r\n"; killme(); } elseif($type === 'json') { $ret = array(); - header("Content-type: application/json"); - foreach($r as $rr) $ret[] = $rr['abook_id']; + header('Content-type: application/json'); + foreach($r as $rv) { + $ret[] = $rv['abook_id']; + } echo json_encode($ret); killme(); } @@ -2222,102 +1759,112 @@ require_once('include/api_auth.php'); } api_register_func('api/friends/ids','api_friends_ids',true); api_register_func('api/followers/ids','api_followers_ids',true); + api_register_func('api/1.1/friends/ids','api_friends_ids',true); + api_register_func('api/1.1/followers/ids','api_followers_ids',true); function api_direct_messages_new( $type) { if (api_user()===false) return false; - if (!x($_POST, "text") || !x($_POST,"screen_name")) return; + if (!x($_POST, 'text') || !x($_POST,'screen_name')) return; - $sender = api_get_user($a); + $sender = api_get_user(); - require_once("include/message.php"); + require_once('include/message.php'); // in a decentralised world the screen name is ambiguous - $r = q("SELECT abook_id FROM abook left join xchan on abook_xchan = xchan_hash WHERE abook_channel=%d and xchan_addr like '%s'", - intval(api_user()), - dbesc($_POST['screen_name'] . '@%') + $r = q("SELECT abook_id FROM abook left join xchan on abook_xchan = xchan_hash + WHERE abook_channel = %d and xchan_addr like '%s'", + intval(api_user()), + dbesc($_POST['screen_name'] . '@%') ); - $recipient = api_get_user( $r[0]['abook_id']); - $replyto = ''; - $sub = ''; - if (x($_REQUEST,'replyto')) { + $recipient = api_get_user($r[0]['abook_id']); + $replyto = ''; + $sub = ''; + + if(array_key_exists('replyto',$_REQUEST) && $_REQUEST['replyto']) { $r = q('SELECT parent_mid, title FROM mail WHERE uid=%d AND id=%d', - intval(api_user()), - intval($_REQUEST['replyto'])); - $replyto = $r[0]['parent_mid']; - $sub = $r[0]['title']; + intval(api_user()), + intval($_REQUEST['replyto']) + ); + if($r) { + $replyto = $r[0]['parent_mid']; + $sub = $r[0]['title']; + } } else { - if (x($_REQUEST,'title')) { + if(x($_REQUEST,'title')) { $sub = $_REQUEST['title']; } else { - $sub = ((strlen($_POST['text'])>10)?substr($_POST['text'],0,10)."...":$_POST['text']); + $sub = ((strlen($_POST['text']) > 10) ? substr($_POST['text'],0,10) . '...' : $_POST['text']); } } $id = send_message(api_user(),$recipient['guid'], $_POST['text'], $sub, $replyto); - if ($id > (-1)) { - $r = q("SELECT * FROM mail WHERE id = %d", intval($id)); - $ret = api_format_message($r[0], $recipient, $sender); - - } else { - $ret = array("error"=>$id); + if($id > (-1)) { + $r = q("SELECT * FROM mail WHERE id = %d", + intval($id) + ); + if(! $r) + return false; + + $ret = api_format_message($r[0], $recipient, $sender); + } + else { + $ret = [ 'error' => $id ]; } - $data = Array('$messages'=>$ret); - - switch($type){ - case "atom": - case "rss": - $data = api_rss_extra( $data, $user_info); - } - - return api_apply_template("direct_messages", $type, $data); + $data = [ 'messages' => $ret ]; + return(api_apply_template('direct_messages', $type, $data)); } + api_register_func('api/direct_messages/new','api_direct_messages_new',true); + api_register_func('api/1.1/direct_messages/new','api_direct_messages_new',true); function api_direct_messages_box( $type, $box) { - if (api_user()===false) return false; + if(api_user() === false) + return false; - $user_info = api_get_user($a); + $user_info = api_get_user(); // params - $count = (x($_GET,'count')?$_GET['count']:20); - $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); - if ($page<0) $page=0; + $count = (x($_GET,'count') ? $_GET['count'] : 20); + $page = (x($_REQUEST,'page') ? $_REQUEST['page'] - 1 : 0); + if($page < 0) + $page=0; - $start = $page*$count; + $start = $page * $count; $channel = App::get_channel(); $profile_url = z_root() . '/channel/' . $channel['channel_address']; - if ($box=="sentbox") { - $sql_extra = "from_xchan = '".dbesc( $channel['channel_hash'] )."'"; + if ($box === 'sentbox') { + $sql_extra = "from_xchan = '" . dbesc( $channel['channel_hash'] ) . "'"; } - elseif ($box=="conversation") { - $sql_extra = "parent_mid = '".dbesc( $_GET["uri"] ) ."'"; + elseif($box === 'conversation') { + $sql_extra = "parent_mid = '" . dbesc($_GET['uri']) . "'"; } - elseif ($box=="all") { - $sql_extra = "true"; + elseif($box === 'all') { + $sql_extra = 'true'; } - elseif ($box=="inbox") { - $sql_extra = "from_xchan != '".dbesc( $channel['channel_hash'] )."'"; + elseif($box === 'inbox') { + $sql_extra = "from_xchan != '" . dbesc($channel['channel_hash']) . "'"; } $r = q("SELECT * FROM mail WHERE channel_id = %d AND $sql_extra ORDER BY created DESC LIMIT %d OFFSET %d", intval(api_user()), - intval($count), intval($start) + intval($count), + intval($start) ); - $ret = Array(); + $ret = array(); if($r) { foreach($r as $item) { - if ($item['from_xchan'] == $channel['channel_hash']) { + if ($item['from_xchan'] === $channel['channel_hash']) { $sender = $user_info; $recipient = api_get_user( null, $item['to_xchan']); } @@ -2326,39 +1873,38 @@ require_once('include/api_auth.php'); $recipient = $user_info; } - $ret[]=api_format_message($item, $recipient, $sender); + $ret[] = api_format_message($item, $recipient, $sender); } } - - $data = array('$messages' => $ret); - switch($type){ - case "atom": - case "rss": - $data = api_rss_extra( $data, $user_info); - } - - return api_apply_template("direct_messages", $type, $data); + $data = array('messages' => $ret); + return(api_apply_template('direct_messages', $type, $data)); } - function api_direct_messages_sentbox( $type){ - return api_direct_messages_box( $type, "sentbox"); + function api_direct_messages_sentbox($type){ + return api_direct_messages_box($type, 'sentbox'); } - function api_direct_messages_inbox( $type){ - return api_direct_messages_box( $type, "inbox"); + function api_direct_messages_inbox($type){ + return api_direct_messages_box($type, 'inbox'); } - function api_direct_messages_all( $type){ - return api_direct_messages_box( $type, "all"); + function api_direct_messages_all($type){ + return api_direct_messages_box($type, 'all'); } - function api_direct_messages_conversation( $type){ - return api_direct_messages_box( $type, "conversation"); + function api_direct_messages_conversation($type){ + return api_direct_messages_box($type, 'conversation'); } + api_register_func('api/direct_messages/conversation','api_direct_messages_conversation',true); api_register_func('api/direct_messages/all','api_direct_messages_all',true); api_register_func('api/direct_messages/sent','api_direct_messages_sentbox',true); api_register_func('api/direct_messages','api_direct_messages_inbox',true); + api_register_func('api/1.1/direct_messages/conversation','api_direct_messages_conversation',true); + api_register_func('api/1.1/direct_messages/all','api_direct_messages_all',true); + api_register_func('api/1.1/direct_messages/sent','api_direct_messages_sentbox',true); + api_register_func('api/1.1/direct_messages','api_direct_messages_inbox',true); + function api_oauth_request_token( $type){ try{ @@ -2368,7 +1914,7 @@ require_once('include/api_auth.php'); $r = $oauth->fetch_request_token($req); }catch(Exception $e){ logger('oauth_exception: ' . print_r($e->getMessage(),true)); - echo "error=". OAuth1Util::urlencode_rfc3986($e->getMessage()); + echo 'error=' . OAuth1Util::urlencode_rfc3986($e->getMessage()); killme(); } echo $r; @@ -2378,10 +1924,12 @@ require_once('include/api_auth.php'); function api_oauth_access_token( $type){ try{ $oauth = new ZotOAuth1(); - $req = OAuth1Request::from_request(); - $r = $oauth->fetch_access_token($req); - }catch(Exception $e){ - echo "error=". OAuth1Util::urlencode_rfc3986($e->getMessage()); killme(); + $req = OAuth1Request::from_request(); + $r = $oauth->fetch_access_token($req); + } + catch(Exception $e) { + echo 'error=' . OAuth1Util::urlencode_rfc3986($e->getMessage()); + killme(); } echo $r; killme(); @@ -2390,6 +1938,9 @@ require_once('include/api_auth.php'); api_register_func('api/oauth/request_token', 'api_oauth_request_token', false); api_register_func('api/oauth/access_token', 'api_oauth_access_token', false); + api_register_func('api/1.1/oauth/request_token', 'api_oauth_request_token', false); + api_register_func('api/1.1/oauth/access_token', 'api_oauth_access_token', false); + /* Not implemented by now: |