diff options
author | zotlabs <mike@macgirvin.com> | 2016-10-11 16:13:52 -0700 |
---|---|---|
committer | zotlabs <mike@macgirvin.com> | 2016-10-11 16:13:52 -0700 |
commit | 03aacc35b39ddb1ef0cd2474a41f3f6270b270e9 (patch) | |
tree | c61cc38afdefa052c1fe57fa587aeaa77be4e231 /include/api.php | |
parent | 6706bed676a9b6e8fa5d1d6e72644f37fb59c4d0 (diff) | |
download | volse-hubzilla-03aacc35b39ddb1ef0cd2474a41f3f6270b270e9.tar.gz volse-hubzilla-03aacc35b39ddb1ef0cd2474a41f3f6270b270e9.tar.bz2 volse-hubzilla-03aacc35b39ddb1ef0cd2474a41f3f6270b270e9.zip |
start on the API re-write
Diffstat (limited to 'include/api.php')
-rw-r--r-- | include/api.php | 436 |
1 files changed, 51 insertions, 385 deletions
diff --git a/include/api.php b/include/api.php index a2ca587a1..efd15df48 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'); /* * @@ -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) { @@ -112,21 +118,15 @@ require_once('include/api_auth.php'); 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; + return $r; break; case "json": header ("Content-Type: application/json"); - if($r) { - foreach($r as $rv) { - $json = json_encode($rv); - } - } // Lookup JSONP to understand these lines. They provide cross-domain AJAX ability. if ($_GET['callback']) - $json = $_GET['callback'] . '(' . $json . ')' ; - return $json; + $r = $_GET['callback'] . '(' . $r . ')' ; + return $r; break; case "rss": header ("Content-Type: application/rss+xml"); @@ -136,14 +136,6 @@ require_once('include/api_auth.php'); 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; - break; } @@ -197,7 +189,7 @@ require_once('include/api_auth.php'); * 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 = ""; @@ -208,8 +200,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 +212,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 +233,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'])) { @@ -269,8 +262,10 @@ require_once('include/api_auth.php'); 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 @@ -279,35 +274,37 @@ require_once('include/api_auth.php'); 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']), @@ -347,6 +344,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 +445,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); + if($data) { + foreach($data as $k => $v) + $ret = arrtoxml(str_replace('$','',$k),$v); + } break; case "json": default: - $ret = $data; + if($data) { + foreach($data as $rv) { + $ret = json_encode($rv); + } + } break; } @@ -472,11 +474,11 @@ 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; + 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)); - + return api_apply_template('user', $type, array('user' => $user_info)); } api_register_func('api/account/verify_credentials','api_account_verify_credentials', true); @@ -484,8 +486,7 @@ require_once('include/api_auth.php'); 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); @@ -501,267 +502,6 @@ 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) { @@ -930,80 +670,6 @@ require_once('include/api_auth.php'); 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); - - function api_get_status($xchan_hash) { require_once('include/security.php'); @@ -2110,7 +1776,7 @@ require_once('include/api_auth.php'); $name = get_config('system','sitename'); $server = App::get_hostname(); - $logo = z_root() . '/images/rm-64.png'; + $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'); @@ -2134,7 +1800,7 @@ require_once('include/api_auth.php'); ) )); - return api_apply_template('config', $type, array('$config' => $config)); + return api_apply_template('config', $type, array('config' => $config)); } api_register_func('api/statusnet/config','api_statusnet_config',false); |