Some more improvements to xml.php, code cleanup

This commit is contained in:
Michael Vogel 2016-07-17 23:59:35 +02:00
parent d7f093cb2e
commit 838f976715
3 changed files with 161 additions and 242 deletions

View File

@ -287,12 +287,7 @@
switch($type){ switch($type){
case "xml": case "xml":
header ("Content-Type: text/xml"); header ("Content-Type: text/xml");
return $r;
if (substr($r, 0, 5) == "<?xml")
return $r;
$r = mb_convert_encoding($r, "UTF-8",mb_detect_encoding($r));
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
break; break;
case "json": case "json":
header ("Content-Type: application/json"); header ("Content-Type: application/json");
@ -332,27 +327,29 @@
function api_error(&$a, $type, $e) { function api_error(&$a, $type, $e) {
$error = ($e->getMessage()!==""?$e->getMessage():$e->httpdesc); $error = ($e->getMessage()!==""?$e->getMessage():$e->httpdesc);
# TODO: https://dev.twitter.com/overview/api/response-codes # TODO: https://dev.twitter.com/overview/api/response-codes
$xmlstr = "<status><error>{$error}</error><code>{$e->httpcode} {$e->httpdesc}</code><request>{$a->query_string}</request></status>";
$error = array("error" => $error,
"code" => $e->httpcode." ".$e->httpdesc,
"request" => $a->query_string);
$ret = api_format_data('status', $type, array('status' => $error));
switch($type){ switch($type){
case "xml": case "xml":
header ("Content-Type: text/xml"); header ("Content-Type: text/xml");
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$xmlstr; return $ret;
break; break;
case "json": case "json":
header ("Content-Type: application/json"); header ("Content-Type: application/json");
return json_encode(array( return json_encode($ret);
'error' => $error,
'request' => $a->query_string,
'code' => $e->httpcode." ".$e->httpdesc
));
break; break;
case "rss": case "rss":
header ("Content-Type: application/rss+xml"); header ("Content-Type: application/rss+xml");
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$xmlstr; return $ret;
break; break;
case "atom": case "atom":
header ("Content-Type: application/atom+xml"); header ("Content-Type: application/atom+xml");
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$xmlstr; return $ret;
break; break;
} }
} }
@ -679,37 +676,6 @@
return (array($status_user, $owner_user)); return (array($status_user, $owner_user));
} }
/**
* @brief transform $data array in xml without a template
*
* @param array $data
* @return string xml string
*/
function api_array_to_xml($data, $ename="") {
$attrs="";
$childs="";
if (count($data)==1 && !is_array($data[array_keys($data)[0]])) {
$ename = array_keys($data)[0];
$ename = trim($ename,'$');
$v = $data[$ename];
return "<$ename>$v</$ename>";
}
foreach($data as $k=>$v) {
$k=trim($k,'$');
if (!is_array($v)) {
$attrs .= sprintf('%s="%s" ', $k, $v);
} else {
if (is_numeric($k)) $k=trim($ename,'s');
$childs.=api_array_to_xml($v, $k);
}
}
$res = $childs;
if ($ename!="") $res = "<$ename $attrs>$res</$ename>";
return $res;
}
/** /**
* @brief walks recursively through an array with the possibility to change value and key * @brief walks recursively through an array with the possibility to change value and key
* *
@ -752,28 +718,29 @@
$key = "statusnet:".substr($key, 10); $key = "statusnet:".substr($key, 10);
elseif (substr($key, 0, 10) == "friendica_") elseif (substr($key, 0, 10) == "friendica_")
$key = "friendica:".substr($key, 10); $key = "friendica:".substr($key, 10);
elseif (in_array($key, array("like", "dislike", "attendyes", "attendno", "attendmaybe"))) //else
$key = "friendica:".$key; // $key = "default:".$key;
return (!in_array($key, array("attachments", "friendica:activities", "coordinates"))); return true;
} }
/** /**
* @brief Creates the XML from a JSON style array * @brief Creates the XML from a JSON style array
* *
* @param array $data JSON style array * @param array $data JSON style array
* @param string $template Name of the root element * @param string $root_element Name of the root element
* *
* @return boolean string The XML data * @return string The XML data
*/ */
function api_create_xml($data, $root_element) { function api_create_xml($data, $root_element) {
$childname = key($data); $childname = key($data);
$data2 = array_pop($data); $data2 = array_pop($data);
$key = key($data2); $key = key($data2);
$namespaces = array("statusnet" => "http://status.net/schema/api/1/", $namespaces = array("" => "http://api.twitter.com",
"friendica" => "http://friendi.ca/schema/api/1/"); "statusnet" => "http://status.net/schema/api/1/",
"friendica" => "http://friendi.ca/schema/api/1/",
"georss" => "http://www.georss.org/georss");
/// @todo Auto detection of needed namespaces /// @todo Auto detection of needed namespaces
if (in_array($root_element, array("ok", "hash", "config", "version", "ids", "notes", "photos"))) if (in_array($root_element, array("ok", "hash", "config", "version", "ids", "notes", "photos")))
@ -798,9 +765,15 @@
} }
/** /**
* load api $templatename for $type and replace $data array * @brief Formats the data according to the data type
*
* @param string $root_element Name of the root element
* @param string $type Return type (atom, rss, xml, json)
* @param array $data JSON style array
*
* @return (string|object) XML data or JSON data
*/ */
function api_apply_template($templatename, $type, $data){ function api_format_data($root_element, $type, $data){
$a = get_app(); $a = get_app();
@ -808,21 +781,7 @@
case "atom": case "atom":
case "rss": case "rss":
case "xml": case "xml":
$ret = api_create_xml($data, $templatename); $ret = api_create_xml($data, $root_element);
break;
$data = array_xmlify($data);
if ($templatename==="<auto>") {
$ret = api_array_to_xml($data);
} else {
$tpl = get_markup_template("api_".$templatename."_".$type.".tpl");
if(! $tpl) {
header ("Content-Type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<status><error>not implemented</error></status>';
killme();
}
$ret = replace_macros($tpl, $data);
}
break; break;
case "json": case "json":
$ret = $data; $ret = $data;
@ -870,7 +829,7 @@
unset($user_info["uid"]); unset($user_info["uid"]);
unset($user_info["self"]); unset($user_info["self"]);
return api_apply_template("user", $type, array('user' => $user_info)); return api_format_data("user", $type, array('user' => $user_info));
} }
api_register_func('api/account/verify_credentials','api_account_verify_credentials', true); api_register_func('api/account/verify_credentials','api_account_verify_credentials', true);
@ -1178,6 +1137,11 @@
$converted = api_convert_item($lastwall); $converted = api_convert_item($lastwall);
if ($type == "xml")
$geo = "georss:point";
else
$geo = "geo";
$status_info = array( $status_info = array(
'created_at' => api_date($lastwall['created']), 'created_at' => api_date($lastwall['created']),
'id' => intval($lastwall['id']), 'id' => intval($lastwall['id']),
@ -1191,7 +1155,7 @@
'in_reply_to_user_id_str' => $in_reply_to_user_id_str, 'in_reply_to_user_id_str' => $in_reply_to_user_id_str,
'in_reply_to_screen_name' => $in_reply_to_screen_name, 'in_reply_to_screen_name' => $in_reply_to_screen_name,
'user' => $user_info, 'user' => $user_info,
'geo' => NULL, $geo => NULL,
'coordinates' => "", 'coordinates' => "",
'place' => "", 'place' => "",
'contributors' => "", 'contributors' => "",
@ -1227,7 +1191,7 @@
if ($type == "raw") if ($type == "raw")
return($status_info); return($status_info);
return api_apply_template("statuses", $type, array('status' => $status_info)); return api_format_data("statuses", $type, array('status' => $status_info));
} }
@ -1289,6 +1253,11 @@
$converted = api_convert_item($lastwall); $converted = api_convert_item($lastwall);
if ($type == "xml")
$geo = "georss:point";
else
$geo = "geo";
$user_info['status'] = array( $user_info['status'] = array(
'text' => $converted["text"], 'text' => $converted["text"],
'truncated' => false, 'truncated' => false,
@ -1301,7 +1270,7 @@
'in_reply_to_user_id' => $in_reply_to_user_id, 'in_reply_to_user_id' => $in_reply_to_user_id,
'in_reply_to_user_id_str' => $in_reply_to_user_id_str, 'in_reply_to_user_id_str' => $in_reply_to_user_id_str,
'in_reply_to_screen_name' => $in_reply_to_screen_name, 'in_reply_to_screen_name' => $in_reply_to_screen_name,
'geo' => NULL, $geo => NULL,
'favorited' => $lastwall['starred'] ? true : false, 'favorited' => $lastwall['starred'] ? true : false,
'statusnet_html' => $converted["html"], 'statusnet_html' => $converted["html"],
'statusnet_conversation_id' => $lastwall['parent'], 'statusnet_conversation_id' => $lastwall['parent'],
@ -1324,7 +1293,7 @@
unset($user_info["uid"]); unset($user_info["uid"]);
unset($user_info["self"]); unset($user_info["self"]);
return api_apply_template("user", $type, array('user' => $user_info)); return api_format_data("user", $type, array('user' => $user_info));
} }
api_register_func('api/users/show','api_users_show'); api_register_func('api/users/show','api_users_show');
@ -1341,11 +1310,14 @@
$r = q("SELECT `id` FROM `gcontact` WHERE `nick`='%s'", dbesc($_GET["q"])); $r = q("SELECT `id` FROM `gcontact` WHERE `nick`='%s'", dbesc($_GET["q"]));
if (count($r)) { if (count($r)) {
$k = 0;
foreach ($r AS $user) { foreach ($r AS $user) {
$user_info = api_get_user($a, $user["id"]); $user_info = api_get_user($a, $user["id"], "json");
//echo print_r($user_info, true)."\n";
$userdata = api_apply_template("user", $type, array('users' => $user_info)); if ($type == "xml")
$userlist[] = $userdata["user"]; $userlist[$k++.":user"] = $user_info;
else
$userlist[] = $user_info;
} }
$userlist = array("users" => $userlist); $userlist = array("users" => $userlist);
} else { } else {
@ -1354,7 +1326,7 @@
} else { } else {
throw new BadRequestException("User not found."); throw new BadRequestException("User not found.");
} }
return ($userlist); return api_format_data("users", $type, $userlist);
} }
api_register_func('api/users/search','api_users_search'); api_register_func('api/users/search','api_users_search');
@ -1417,7 +1389,7 @@
intval($start), intval($count) intval($start), intval($count)
); );
$ret = api_format_items($r,$user_info); $ret = api_format_items($r,$user_info, false, $type);
// Set all posts from the query above to seen // Set all posts from the query above to seen
$idarray = array(); $idarray = array();
@ -1441,7 +1413,7 @@
break; break;
} }
return api_apply_template("statuses", $type, $data); return api_format_data("statuses", $type, $data);
} }
api_register_func('api/statuses/home_timeline','api_statuses_home_timeline', true); 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/statuses/friends_timeline','api_statuses_home_timeline', true);
@ -1492,7 +1464,7 @@
intval($start), intval($start),
intval($count)); intval($count));
$ret = api_format_items($r,$user_info); $ret = api_format_items($r,$user_info, false, $type);
$data = array('status' => $ret); $data = array('status' => $ret);
@ -1503,7 +1475,7 @@
break; break;
} }
return api_apply_template("statuses", $type, $data); return api_format_data("statuses", $type, $data);
} }
api_register_func('api/statuses/public_timeline','api_statuses_public_timeline', true); api_register_func('api/statuses/public_timeline','api_statuses_public_timeline', true);
@ -1553,14 +1525,14 @@
throw new BadRequestException("There is no status with this id."); throw new BadRequestException("There is no status with this id.");
} }
$ret = api_format_items($r,$user_info); $ret = api_format_items($r,$user_info, false, $type);
if ($conversation) { if ($conversation) {
$data = array('status' => $ret); $data = array('status' => $ret);
return api_apply_template("statuses", $type, $data); return api_format_data("statuses", $type, $data);
} else { } else {
$data = array('status' => $ret[0]); $data = array('status' => $ret[0]);
return api_apply_template("status", $type, $data); return api_format_data("status", $type, $data);
} }
} }
api_register_func('api/statuses/show','api_statuses_show', true); api_register_func('api/statuses/show','api_statuses_show', true);
@ -1628,10 +1600,10 @@
if (!$r) if (!$r)
throw new BadRequestException("There is no conversation with this id."); throw new BadRequestException("There is no conversation with this id.");
$ret = api_format_items($r,$user_info); $ret = api_format_items($r,$user_info, false, $type);
$data = array('status' => $ret); $data = array('status' => $ret);
return api_apply_template("statuses", $type, $data); return api_format_data("statuses", $type, $data);
} }
api_register_func('api/conversation/show','api_conversation_show', true); api_register_func('api/conversation/show','api_conversation_show', true);
api_register_func('api/statusnet/conversation','api_conversation_show', true); api_register_func('api/statusnet/conversation','api_conversation_show', true);
@ -1795,7 +1767,7 @@
intval($start), intval($count) intval($start), intval($count)
); );
$ret = api_format_items($r,$user_info); $ret = api_format_items($r,$user_info, false, $type);
$data = array('status' => $ret); $data = array('status' => $ret);
@ -1806,7 +1778,7 @@
break; break;
} }
return api_apply_template("statuses", $type, $data); return api_format_data("statuses", $type, $data);
} }
api_register_func('api/statuses/mentions','api_statuses_mentions', true); api_register_func('api/statuses/mentions','api_statuses_mentions', true);
api_register_func('api/statuses/replies','api_statuses_mentions', true); api_register_func('api/statuses/replies','api_statuses_mentions', true);
@ -1863,7 +1835,7 @@
intval($start), intval($count) intval($start), intval($count)
); );
$ret = api_format_items($r,$user_info, true); $ret = api_format_items($r,$user_info, true, $type);
$data = array('status' => $ret); $data = array('status' => $ret);
switch($type){ switch($type){
@ -1872,7 +1844,7 @@
$data = api_rss_extra($a, $data, $user_info); $data = api_rss_extra($a, $data, $user_info);
} }
return api_apply_template("statuses", $type, $data); return api_format_data("statuses", $type, $data);
} }
api_register_func('api/statuses/user_timeline','api_statuses_user_timeline', true); api_register_func('api/statuses/user_timeline','api_statuses_user_timeline', true);
@ -1926,7 +1898,7 @@
$user_info = api_get_user($a); $user_info = api_get_user($a);
$rets = api_format_items($item,$user_info); $rets = api_format_items($item,$user_info, false, $type);
$ret = $rets[0]; $ret = $rets[0];
$data = array('status' => $ret); $data = array('status' => $ret);
@ -1936,7 +1908,7 @@
$data = api_rss_extra($a, $data, $user_info); $data = api_rss_extra($a, $data, $user_info);
} }
return api_apply_template("status", $type, $data); return api_format_data("status", $type, $data);
} }
api_register_func('api/favorites/create', 'api_favorites_create_destroy', true, API_METHOD_POST); api_register_func('api/favorites/create', 'api_favorites_create_destroy', true, API_METHOD_POST);
api_register_func('api/favorites/destroy', 'api_favorites_create_destroy', true, API_METHOD_DELETE); api_register_func('api/favorites/destroy', 'api_favorites_create_destroy', true, API_METHOD_DELETE);
@ -1989,7 +1961,7 @@
intval($start), intval($count) intval($start), intval($count)
); );
$ret = api_format_items($r,$user_info); $ret = api_format_items($r,$user_info, false, $type);
} }
@ -2000,7 +1972,7 @@
$data = api_rss_extra($a, $data, $user_info); $data = api_rss_extra($a, $data, $user_info);
} }
return api_apply_template("statuses", $type, $data); return api_format_data("statuses", $type, $data);
} }
api_register_func('api/favorites','api_favorites', true); api_register_func('api/favorites','api_favorites', true);
@ -2319,7 +2291,7 @@
* likes => int count * likes => int count
* dislikes => int count * dislikes => int count
*/ */
function api_format_items_activities(&$item) { function api_format_items_activities(&$item, $type = "json") {
$activities = array( $activities = array(
'like' => array(), 'like' => array(),
'dislike' => array(), 'dislike' => array(),
@ -2335,6 +2307,14 @@
builtin_activity_puller($i, $activities); builtin_activity_puller($i, $activities);
} }
if ($type == "xml") {
$xml_activities = array();
foreach ($activities as $k => $v)
$xml_activities["friendica:".$k] = $v;
$activities = $xml_activities;
}
$res = array(); $res = array();
$uri = $item['uri']."-l"; $uri = $item['uri']."-l";
foreach($activities as $k => $v) { foreach($activities as $k => $v) {
@ -2351,7 +2331,7 @@
* @param array $user_info * @param array $user_info
* @param bool $filter_user filter items by $user_info * @param bool $filter_user filter items by $user_info
*/ */
function api_format_items($r,$user_info, $filter_user = false) { function api_format_items($r,$user_info, $filter_user = false, $type = "json") {
$a = get_app(); $a = get_app();
$ret = Array(); $ret = Array();
@ -2405,6 +2385,11 @@
$converted = api_convert_item($item); $converted = api_convert_item($item);
if ($type == "xml")
$geo = "georss:point";
else
$geo = "geo";
$status = array( $status = array(
'text' => $converted["text"], 'text' => $converted["text"],
'truncated' => False, 'truncated' => False,
@ -2417,14 +2402,14 @@
'in_reply_to_user_id' => $in_reply_to_user_id, 'in_reply_to_user_id' => $in_reply_to_user_id,
'in_reply_to_user_id_str' => $in_reply_to_user_id_str, 'in_reply_to_user_id_str' => $in_reply_to_user_id_str,
'in_reply_to_screen_name' => $in_reply_to_screen_name, 'in_reply_to_screen_name' => $in_reply_to_screen_name,
'geo' => NULL, $geo => NULL,
'favorited' => $item['starred'] ? true : false, 'favorited' => $item['starred'] ? true : false,
'user' => $status_user , 'user' => $status_user ,
'friendica_owner' => $owner_user, 'friendica_owner' => $owner_user,
//'entities' => NULL, //'entities' => NULL,
'statusnet_html' => $converted["html"], 'statusnet_html' => $converted["html"],
'statusnet_conversation_id' => $item['parent'], 'statusnet_conversation_id' => $item['parent'],
'friendica_activities' => api_format_items_activities($item), 'friendica_activities' => api_format_items_activities($item, $type),
); );
if (count($converted["attachments"]) > 0) if (count($converted["attachments"]) > 0)
@ -2462,7 +2447,7 @@
$retweeted_status['text'] = $rt_converted["text"]; $retweeted_status['text'] = $rt_converted["text"];
$retweeted_status['statusnet_html'] = $rt_converted["html"]; $retweeted_status['statusnet_html'] = $rt_converted["html"];
$retweeted_status['friendica_activities'] = api_format_items_activities($retweeted_item); $retweeted_status['friendica_activities'] = api_format_items_activities($retweeted_item, $type);
$retweeted_status['created_at'] = api_date($retweeted_item['created']); $retweeted_status['created_at'] = api_date($retweeted_item['created']);
$status['retweeted_status'] = $retweeted_status; $status['retweeted_status'] = $retweeted_status;
} }
@ -2475,12 +2460,14 @@
if ($item["coord"] != "") { if ($item["coord"] != "") {
$coords = explode(' ',$item["coord"]); $coords = explode(' ',$item["coord"]);
if (count($coords) == 2) { if (count($coords) == 2) {
$status["geo"] = array('type' => 'Point', if ($type == "json")
'coordinates' => array((float) $coords[0], $status["geo"] = array('type' => 'Point',
(float) $coords[1])); 'coordinates' => array((float) $coords[0],
(float) $coords[1]));
else // Not sure if this is the official format - if someone founds a documentation we can check
$status["georss:point"] = $item["coord"];
} }
} }
$ret[] = $status; $ret[] = $status;
}; };
return $ret; return $ret;
@ -2489,14 +2476,7 @@
function api_account_rate_limit_status(&$a,$type) { function api_account_rate_limit_status(&$a,$type) {
if ($type == "json") if ($type == "xml")
$hash = array(
'reset_time_in_seconds' => strtotime('now + 1 hour'),
'remaining_hits' => (string) 150,
'hourly_limit' => (string) 150,
'reset_time' => api_date(datetime_convert('UTC','UTC','now + 1 hour',ATOM_TIME)),
);
else
$hash = array( $hash = array(
'remaining-hits' => (string) 150, 'remaining-hits' => (string) 150,
'@attributes' => array("type" => "integer"), '@attributes' => array("type" => "integer"),
@ -2507,8 +2487,15 @@
'reset_time_in_seconds' => strtotime('now + 1 hour'), 'reset_time_in_seconds' => strtotime('now + 1 hour'),
'@attributes4' => array("type" => "integer"), '@attributes4' => array("type" => "integer"),
); );
else
$hash = array(
'reset_time_in_seconds' => strtotime('now + 1 hour'),
'remaining_hits' => (string) 150,
'hourly_limit' => (string) 150,
'reset_time' => api_date(datetime_convert('UTC','UTC','now + 1 hour',ATOM_TIME)),
);
return api_apply_template('hash', $type, array('hash' => $hash)); return api_format_data('hash', $type, array('hash' => $hash));
} }
api_register_func('api/account/rate_limit_status','api_account_rate_limit_status',true); api_register_func('api/account/rate_limit_status','api_account_rate_limit_status',true);
@ -2518,19 +2505,19 @@
else else
$ok = "ok"; $ok = "ok";
return api_apply_template('ok', $type, array("ok" => $ok)); return api_format_data('ok', $type, array("ok" => $ok));
} }
api_register_func('api/help/test','api_help_test',false); api_register_func('api/help/test','api_help_test',false);
function api_lists(&$a,$type) { function api_lists(&$a,$type) {
$ret = array(); $ret = array();
return api_apply_template('lists', $type, array("lists_list" => $ret)); return api_format_data('lists', $type, array("lists_list" => $ret));
} }
api_register_func('api/lists','api_lists',true); api_register_func('api/lists','api_lists',true);
function api_lists_list(&$a,$type) { function api_lists_list(&$a,$type) {
$ret = array(); $ret = array();
return api_apply_template('lists', $type, array("lists_list" => $ret)); return api_format_data('lists', $type, array("lists_list" => $ret));
} }
api_register_func('api/lists/list','api_lists_list',true); api_register_func('api/lists/list','api_lists_list',true);
@ -2584,12 +2571,12 @@
function api_statuses_friends(&$a, $type){ function api_statuses_friends(&$a, $type){
$data = api_statuses_f($a,$type,"friends"); $data = api_statuses_f($a,$type,"friends");
if ($data===false) return false; if ($data===false) return false;
return api_apply_template("users", $type, $data); return api_format_data("users", $type, $data);
} }
function api_statuses_followers(&$a, $type){ function api_statuses_followers(&$a, $type){
$data = api_statuses_f($a,$type,"followers"); $data = api_statuses_f($a,$type,"followers");
if ($data===false) return false; if ($data===false) return false;
return api_apply_template("users", $type, $data); return api_format_data("users", $type, $data);
} }
api_register_func('api/statuses/friends','api_statuses_friends',true); api_register_func('api/statuses/friends','api_statuses_friends',true);
api_register_func('api/statuses/followers','api_statuses_followers',true); api_register_func('api/statuses/followers','api_statuses_followers',true);
@ -2627,7 +2614,7 @@
), ),
); );
return api_apply_template('config', $type, array('config' => $config)); return api_format_data('config', $type, array('config' => $config));
} }
api_register_func('api/statusnet/config','api_statusnet_config',false); api_register_func('api/statusnet/config','api_statusnet_config',false);
@ -2636,12 +2623,12 @@
// liar // liar
$fake_statusnet_version = "0.9.7"; $fake_statusnet_version = "0.9.7";
return api_apply_template('version', $type, array('version' => $fake_statusnet_version)); return api_format_data('version', $type, array('version' => $fake_statusnet_version));
} }
api_register_func('api/statusnet/version','api_statusnet_version',false); api_register_func('api/statusnet/version','api_statusnet_version',false);
/** /**
* @todo use api_apply_template() to return data * @todo use api_format_data() to return data
*/ */
function api_ff_ids(&$a,$type,$qtype) { function api_ff_ids(&$a,$type,$qtype) {
if(! api_user()) throw new ForbiddenException(); if(! api_user()) throw new ForbiddenException();
@ -2672,7 +2659,7 @@
else else
$ids[] = intval($rr['id']); $ids[] = intval($rr['id']);
return api_apply_template("ids", $type, array('id' => $ids)); return api_format_data("ids", $type, array('id' => $ids));
} }
function api_friends_ids(&$a,$type) { function api_friends_ids(&$a,$type) {
@ -2740,7 +2727,7 @@
$data = api_rss_extra($a, $data, $user_info); $data = api_rss_extra($a, $data, $user_info);
} }
return api_apply_template("direct-messages", $type, $data); return api_format_data("direct-messages", $type, $data);
} }
api_register_func('api/direct_messages/new','api_direct_messages_new',true, API_METHOD_POST); api_register_func('api/direct_messages/new','api_direct_messages_new',true, API_METHOD_POST);
@ -2827,7 +2814,7 @@
$data = api_rss_extra($a, $data, $user_info); $data = api_rss_extra($a, $data, $user_info);
} }
return api_apply_template("direct-messages", $type, $data); return api_format_data("direct-messages", $type, $data);
} }
@ -2896,15 +2883,15 @@
$photo['type'] = $rr['type']; $photo['type'] = $rr['type'];
$thumb = $a->get_baseurl()."/photo/".$rr['resource-id']."-".$rr['scale'].".".$typetoext[$rr['type']]; $thumb = $a->get_baseurl()."/photo/".$rr['resource-id']."-".$rr['scale'].".".$typetoext[$rr['type']];
if ($type == "json") { if ($type == "xml")
$data['photo'][] = array("@attributes" => $photo, "1" => $thumb);
else {
$photo['thumb'] = $thumb; $photo['thumb'] = $thumb;
$data['photo'][] = $photo; $data['photo'][] = $photo;
} else {
$data['photo'][] = array("@attributes" => $photo, "1" => $thumb);
} }
} }
} }
return api_apply_template("photos", $type, $data); return api_format_data("photos", $type, $data);
} }
function api_fr_photo_detail(&$a,$type) { function api_fr_photo_detail(&$a,$type) {
@ -2932,16 +2919,24 @@
if ($r) { if ($r) {
$data = array('photo' => $r[0]); $data = array('photo' => $r[0]);
$data['photo']['id'] = $data['photo']['resource-id'];
if ($scale !== false) { if ($scale !== false) {
$data['photo']['data'] = base64_encode($data['photo']['data']); $data['photo']['data'] = base64_encode($data['photo']['data']);
} else { } else {
unset($data['photo']['datasize']); //needed only with scale param unset($data['photo']['datasize']); //needed only with scale param
} }
$data['photo']['link'] = array(); if ($type == "xml") {
for($k=intval($data['photo']['minscale']); $k<=intval($data['photo']['maxscale']); $k++) { $data['photo']['links'] = array();
$data['photo']['link'][$k] = $a->get_baseurl()."/photo/".$data['photo']['resource-id']."-".$k.".".$typetoext[$data['photo']['type']]; for ($k=intval($data['photo']['minscale']); $k<=intval($data['photo']['maxscale']); $k++)
$data['photo']['links'][$k.":link"]["@attributes"] = array("type" => $data['photo']['type'],
"scale" => $k,
"href" => $a->get_baseurl()."/photo/".$data['photo']['resource-id']."-".$k.".".$typetoext[$data['photo']['type']]);
} else {
$data['photo']['link'] = array();
for ($k=intval($data['photo']['minscale']); $k<=intval($data['photo']['maxscale']); $k++) {
$data['photo']['link'][$k] = $a->get_baseurl()."/photo/".$data['photo']['resource-id']."-".$k.".".$typetoext[$data['photo']['type']];
}
} }
$data['photo']['id'] = $data['photo']['resource-id'];
unset($data['photo']['resource-id']); unset($data['photo']['resource-id']);
unset($data['photo']['minscale']); unset($data['photo']['minscale']);
unset($data['photo']['maxscale']); unset($data['photo']['maxscale']);
@ -2950,7 +2945,7 @@
throw new NotFoundException(); throw new NotFoundException();
} }
return api_apply_template("photo_detail", $type, $data); return api_format_data("photo_detail", $type, $data);
} }
api_register_func('api/friendica/photos/list', 'api_fr_photos_list', true); api_register_func('api/friendica/photos/list', 'api_fr_photos_list', true);
@ -3291,13 +3286,24 @@
foreach ($r as $rr) { foreach ($r as $rr) {
$members = group_get_members($rr['id']); $members = group_get_members($rr['id']);
$users = array(); $users = array();
foreach ($members as $member) {
$user = api_get_user($a, $member['nurl']); if ($type == "xml") {
$users[] = $user; $user_element = "users";
$k = 0;
foreach ($members as $member) {
$user = api_get_user($a, $member['nurl']);
$users[$k++.":user"] = $user;
}
} else {
$user_element = "user";
foreach ($members as $member) {
$user = api_get_user($a, $member['nurl']);
$users[] = $user;
}
} }
$grps[] = array('name' => $rr['name'], 'gid' => $rr['id'], 'user' => $users); $grps[] = array('name' => $rr['name'], 'gid' => $rr['id'], $user_element => $users);
} }
return api_apply_template("group_show", $type, array('groups' => $grps)); return api_format_data("groups", $type, array('group' => $grps));
} }
api_register_func('api/friendica/group_show', 'api_friendica_group_show', true); api_register_func('api/friendica/group_show', 'api_friendica_group_show', true);
@ -3338,7 +3344,7 @@
if ($ret) { if ($ret) {
// return success // return success
$success = array('success' => $ret, 'gid' => $gid, 'name' => $name, 'status' => 'deleted', 'wrong users' => array()); $success = array('success' => $ret, 'gid' => $gid, 'name' => $name, 'status' => 'deleted', 'wrong users' => array());
return api_apply_template("group_delete", $type, array('result' => $success)); return api_format_data("group_delete", $type, array('result' => $success));
} }
else else
throw new BadRequestException('other API error'); throw new BadRequestException('other API error');
@ -3404,7 +3410,7 @@
// return success message incl. missing users in array // return success message incl. missing users in array
$status = ($erroraddinguser ? "missing user" : ($reactivate_group ? "reactivated" : "ok")); $status = ($erroraddinguser ? "missing user" : ($reactivate_group ? "reactivated" : "ok"));
$success = array('success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers); $success = array('success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers);
return api_apply_template("group_create", $type, array('result' => $success)); return api_format_data("group_create", $type, array('result' => $success));
} }
api_register_func('api/friendica/group_create', 'api_friendica_group_create', true, API_METHOD_POST); api_register_func('api/friendica/group_create', 'api_friendica_group_create', true, API_METHOD_POST);
@ -3461,7 +3467,7 @@
// return success message incl. missing users in array // return success message incl. missing users in array
$status = ($erroraddinguser ? "missing user" : "ok"); $status = ($erroraddinguser ? "missing user" : "ok");
$success = array('success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers); $success = array('success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers);
return api_apply_template("group_update", $type, array('result' => $success)); return api_format_data("group_update", $type, array('result' => $success));
} }
api_register_func('api/friendica/group_update', 'api_friendica_group_update', true, API_METHOD_POST); api_register_func('api/friendica/group_update', 'api_friendica_group_update', true, API_METHOD_POST);
@ -3476,11 +3482,11 @@
$res = do_like($id, $verb); $res = do_like($id, $verb);
if ($res) { if ($res) {
if ($type == 'xml') if ($type == "xml")
$ok = "true"; $ok = "true";
else else
$ok = "ok"; $ok = "ok";
return api_apply_template('test', $type, array('ok' => $ok)); return api_format_data('ok', $type, array('ok' => $ok));
} else { } else {
throw new BadRequestException('Error adding activity'); throw new BadRequestException('Error adding activity');
} }
@ -3519,7 +3525,7 @@
$notes = $xmlnotes; $notes = $xmlnotes;
} }
return api_apply_template("notes", $type, array('note' => $notes)); return api_format_data("notes", $type, array('note' => $notes));
} }
/** /**
@ -3551,13 +3557,13 @@
if ($r!==false) { if ($r!==false) {
// we found the item, return it to the user // we found the item, return it to the user
$user_info = api_get_user($a); $user_info = api_get_user($a);
$ret = api_format_items($r,$user_info); $ret = api_format_items($r,$user_info, false, $type);
$data = array('statuses' => $ret); $data = array('status' => $ret);
return api_apply_template("timeline", $type, $data); return api_format_data("status", $type, $data);
} }
// the item can't be found, but we set the note as seen, so we count this as a success // the item can't be found, but we set the note as seen, so we count this as a success
} }
return api_apply_template('<auto>', $type, array('status' => "success")); return api_format_data('result', $type, array('result' => "success"));
} }
api_register_func('api/friendica/notification/seen', 'api_friendica_notification_seen', true, API_METHOD_POST); api_register_func('api/friendica/notification/seen', 'api_friendica_notification_seen', true, API_METHOD_POST);

View File

@ -64,6 +64,8 @@ class xml {
$element_parts = explode(":", $attr_key); $element_parts = explode(":", $attr_key);
if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]])) if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
$namespace = $namespaces[$element_parts[0]]; $namespace = $namespaces[$element_parts[0]];
elseif (isset($namespaces[""]))
$namespace = $namespaces[""];
else else
$namespace = NULL; $namespace = NULL;
@ -76,6 +78,8 @@ class xml {
$element_parts = explode(":", $key); $element_parts = explode(":", $key);
if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]])) if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
$namespace = $namespaces[$element_parts[0]]; $namespace = $namespaces[$element_parts[0]];
elseif (isset($namespaces[""]))
$namespace = $namespaces[""];
else else
$namespace = NULL; $namespace = NULL;

View File

@ -1,91 +0,0 @@
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">
<generator uri="http://status.net" version="0.9.7">StatusNet</generator>
<id>{{$rss.self}}</id>
<title>Friendica</title>
<subtitle>Friendica API feed</subtitle>
<logo>{{$rss.logo}}</logo>
<updated>{{$rss.atom_updated}}</updated>
<link type="text/html" rel="alternate" href="{{$rss.alternate}}"/>
<link type="application/atom+xml" rel="self" href="{{$rss.self}}"/>
<author>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>{{$user.url}}</uri>
<name>{{$user.name}}</name>
<link rel="alternate" type="text/html" href="{{$user.url}}"/>
<link rel="avatar" type="image/jpeg" media:width="106" media:height="106" href="{{$user.profile_image_url}}"/>
<link rel="avatar" type="image/jpeg" media:width="96" media:height="96" href="{{$user.profile_image_url}}"/>
<link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="{{$user.profile_image_url}}"/>
<link rel="avatar" type="image/jpeg" media:width="24" media:height="24" href="{{$user.profile_image_url}}"/>
<georss:point></georss:point>
<poco:preferredUsername>{{$user.screen_name}}</poco:preferredUsername>
<poco:displayName>{{$user.name}}</poco:displayName>
<poco:urls>
<poco:type>homepage</poco:type>
<poco:value>{{$user.url}}</poco:value>
<poco:primary>true</poco:primary>
</poco:urls>
<statusnet:profile_info local_id="{{$user.id}}"></statusnet:profile_info>
</author>
<!--Deprecation warning: activity:subject is present only for backward compatibility. It will be removed in the next version of StatusNet.-->
<activity:subject>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<id>{{$user.contact_url}}</id>
<title>{{$user.name}}</title>
<link rel="alternate" type="text/html" href="{{$user.url}}"/>
<link rel="avatar" type="image/jpeg" media:width="106" media:height="106" href="{{$user.profile_image_url}}"/>
<link rel="avatar" type="image/jpeg" media:width="96" media:height="96" href="{{$user.profile_image_url}}"/>
<link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="{{$user.profile_image_url}}"/>
<link rel="avatar" type="image/jpeg" media:width="24" media:height="24" href="{{$user.profile_image_url}}"/>
<poco:preferredUsername>{{$user.screen_name}}</poco:preferredUsername>
<poco:displayName>{{$user.name}}</poco:displayName>
<poco:urls>
<poco:type>homepage</poco:type>
<poco:value>{{$user.url}}</poco:value>
<poco:primary>true</poco:primary>
</poco:urls>
<statusnet:profile_info local_id="{{$user.id}}"></statusnet:profile_info>
</activity:subject>
{{foreach $statuses as $status}}
<entry>
<activity:object-type>{{$status.objecttype}}</activity:object-type>
<id>{{$status.message_id}}</id>
<title>{{$status.text}}</title>
<content type="html">{{$status.statusnet_html}}</content>
<link rel="alternate" type="text/html" href="{{$status.url}}"/>
<activity:verb>{{$status.verb}}</activity:verb>
<published>{{$status.published}}</published>
<updated>{{$status.updated}}</updated>
<link rel="self" type="application/atom+xml" href="{{$status.self}}"/>
<link rel="edit" type="application/atom+xml" href="{{$status.edit}}"/>
<statusnet:notice_info local_id="{{$status.id}}" source="{{$status.source}}" >
</statusnet:notice_info>
<author>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<uri>{{$status.user.url}}</uri>
<name>{{$status.user.name}}</name>
<link rel="alternate" type="text/html" href="{{$status.user.url}}"/>
<link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="{{$status.user.profile_image_url}}"/>
<georss:point/>
<poco:preferredUsername>{{$status.user.screen_name}}</poco:preferredUsername>
<poco:displayName>{{$status.user.name}}</poco:displayName>
<poco:address/>
<poco:urls>
<poco:type>homepage</poco:type>
<poco:value>{{$status.user.url}}</poco:value>
<poco:primary>true</poco:primary>
</poco:urls>
</author>
<link rel="ostatus:conversation" type="text/html" href="{{$status.url}}"/>
</entry>
{{/foreach}}
</feed>