X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fapi.php;h=c86a3cbe4bc3295cee41b37c79643a90c12e7d1f;hb=b9da831138667700d465f15ad50df650ab4da427;hp=d2d010c72754ff1bbccc29e8a7a44eb11eae80ab;hpb=8a5fada863364c729decea75c7b6e1549a7b81ce;p=friendica.git diff --git a/include/api.php b/include/api.php index d2d010c727..c86a3cbe4b 100644 --- a/include/api.php +++ b/include/api.php @@ -23,6 +23,8 @@ require_once('include/message.php'); require_once('include/group.php'); require_once('include/like.php'); + require_once('include/NotificationsManager.php'); + require_once('include/plaintext.php'); define('API_METHOD_ANY','*'); @@ -160,10 +162,7 @@ if (!isset($_SERVER['PHP_AUTH_USER'])) { logger('API_login: ' . print_r($_SERVER,true), LOGGER_DEBUG); header('WWW-Authenticate: Basic realm="Friendica"'); - header('HTTP/1.0 401 Unauthorized'); - die((api_error($a, 'json', "This api requires login"))); - - //die('This api requires login'); + throw new UnauthorizedException("This API requires login"); } $user = $_SERVER['PHP_AUTH_USER']; @@ -215,8 +214,9 @@ if((! $record) || (! count($record))) { logger('API_login failure: ' . print_r($_SERVER,true), LOGGER_DEBUG); header('WWW-Authenticate: Basic realm="Friendica"'); - header('HTTP/1.0 401 Unauthorized'); - die('This api requires login'); + #header('HTTP/1.0 401 Unauthorized'); + #die('This api requires login'); + throw new UnauthorizedException("This API requires login"); } authenticate_success($record); $_SESSION["allow_api"] = true; @@ -250,7 +250,7 @@ */ function api_call(&$a){ GLOBAL $API, $called_api; - + $type="json"; if (strpos($a->query_string, ".xml")>0) $type="xml"; if (strpos($a->query_string, ".json")>0) $type="json"; @@ -270,8 +270,6 @@ api_login($a); } - load_contact_links(api_user()); - logger('API call for ' . $a->user['username'] . ': ' . $a->query_string); logger('API parameters: ' . print_r($_REQUEST,true)); @@ -330,7 +328,8 @@ * * @param Api $a * @param string $type Return type (xml, json, rss, as) - * @param string $error Error message + * @param HTTPException $error Error object + * @return strin error message formatted as $type */ function api_error(&$a, $type, $e) { $error = ($e->getMessage()!==""?$e->getMessage():$e->httpdesc); @@ -393,7 +392,7 @@ * Contact url or False if contact id is unknown */ function api_unique_id_to_url($id){ - $r = q("SELECT `url` FROM `unique_contacts` WHERE `id`=%d LIMIT 1", + $r = q("SELECT `url` FROM `gcontact` WHERE `id`=%d LIMIT 1", intval($id)); if ($r) return ($r[0]["url"]); @@ -503,9 +502,7 @@ $r = array(); if ($url != "") - $r = q("SELECT * FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", $url); - elseif ($nick != "") - $r = q("SELECT * FROM `unique_contacts` WHERE `nick`='%s' LIMIT 1", $nick); + $r = q("SELECT * FROM `gcontact` WHERE `nurl`='%s' LIMIT 1", dbesc(normalise_link($url))); if ($r) { // If no nick where given, extract it from the address @@ -517,14 +514,14 @@ 'id_str' => (string) $r[0]["id"], 'name' => $r[0]["name"], 'screen_name' => (($r[0]['nick']) ? $r[0]['nick'] : $r[0]['name']), - 'location' => NULL, - 'description' => NULL, + 'location' => $r[0]["location"], + 'description' => $r[0]["about"], 'url' => $r[0]["url"], 'protected' => false, 'followers_count' => 0, 'friends_count' => 0, 'listed_count' => 0, - 'created_at' => api_date(0), + 'created_at' => api_date($r[0]["created"]), 'favourites_count' => 0, 'utc_offset' => 0, 'time_zone' => 'UTC', @@ -535,8 +532,8 @@ 'contributors_enabled' => false, 'is_translator' => false, 'is_translation_enabled' => false, - 'profile_image_url' => $r[0]["avatar"], - 'profile_image_url_https' => $r[0]["avatar"], + 'profile_image_url' => $r[0]["photo"], + 'profile_image_url_https' => $r[0]["photo"], 'following' => false, 'follow_request_sent' => false, 'notifications' => false, @@ -546,7 +543,7 @@ 'uid' => 0, 'cid' => 0, 'self' => 0, - 'network' => '', + 'network' => $r[0]["network"], ); return $ret; @@ -617,22 +614,14 @@ $uinfo[0]['nick'] = api_get_nick($uinfo[0]["url"]); } - // Fetching unique id - $r = q("SELECT id FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", dbesc(normalise_link($uinfo[0]['url']))); - - // If not there, then add it - if (count($r) == 0) { - q("INSERT INTO `unique_contacts` (`url`, `name`, `nick`, `avatar`) VALUES ('%s', '%s', '%s', '%s')", - dbesc(normalise_link($uinfo[0]['url'])), dbesc($uinfo[0]['name']),dbesc($uinfo[0]['nick']), dbesc($uinfo[0]['micro'])); - - $r = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", dbesc(normalise_link($uinfo[0]['url']))); - } - $network_name = network_to_name($uinfo[0]['network'], $uinfo[0]['url']); + $gcontact_id = get_gcontact_id(array("url" => $uinfo[0]['url'], "network" => $uinfo[0]['network'], + "photo" => $uinfo[0]['micro'], "name" => $uinfo[0]['name'])); + $ret = Array( - 'id' => intval($r[0]['id']), - 'id_str' => (string) intval($r[0]['id']), + 'id' => intval($gcontact_id), + 'id_str' => (string) intval($gcontact_id), 'name' => (($uinfo[0]['name']) ? $uinfo[0]['name'] : $uinfo[0]['nick']), 'screen_name' => (($uinfo[0]['nick']) ? $uinfo[0]['nick'] : $uinfo[0]['name']), 'location' => ($usr) ? $usr[0]['default-location'] : $network_name, @@ -666,45 +655,12 @@ function api_item_get_user(&$a, $item) { - $author = q("SELECT * FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", - dbesc(normalise_link($item['author-link']))); - - if (count($author) == 0) { - q("INSERT INTO `unique_contacts` (`url`, `name`, `avatar`) VALUES ('%s', '%s', '%s')", - dbesc(normalise_link($item["author-link"])), dbesc($item["author-name"]), dbesc($item["author-avatar"])); + // Make sure that there is an entry in the global contacts for author and owner + get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'], + "photo" => $item['author-avatar'], "name" => $item['author-name'])); - $author = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", - dbesc(normalise_link($item['author-link']))); - } else if ($item["author-link"].$item["author-name"] != $author[0]["url"].$author[0]["name"]) { - $r = q("SELECT `id` FROM `unique_contacts` WHERE `name` = '%s' AND `avatar` = '%s' AND url = '%s'", - dbesc($item["author-name"]), dbesc($item["author-avatar"]), - dbesc(normalise_link($item["author-link"]))); - - if (!$r) - q("UPDATE `unique_contacts` SET `name` = '%s', `avatar` = '%s' WHERE `url` = '%s'", - dbesc($item["author-name"]), dbesc($item["author-avatar"]), - dbesc(normalise_link($item["author-link"]))); - } - - $owner = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", - dbesc(normalise_link($item['owner-link']))); - - if (count($owner) == 0) { - q("INSERT INTO `unique_contacts` (`url`, `name`, `avatar`) VALUES ('%s', '%s', '%s')", - dbesc(normalise_link($item["owner-link"])), dbesc($item["owner-name"]), dbesc($item["owner-avatar"])); - - $owner = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", - dbesc(normalise_link($item['owner-link']))); - } else if ($item["owner-link"].$item["owner-name"] != $owner[0]["url"].$owner[0]["name"]) { - $r = q("SELECT `id` FROM `unique_contacts` WHERE `name` = '%s' AND `avatar` = '%s' AND url = '%s'", - dbesc($item["owner-name"]), dbesc($item["owner-avatar"]), - dbesc(normalise_link($item["owner-link"]))); - - if (!$r) - q("UPDATE `unique_contacts` SET `name` = '%s', `avatar` = '%s' WHERE `url` = '%s'", - dbesc($item["owner-name"]), dbesc($item["owner-avatar"]), - dbesc(normalise_link($item["owner-link"]))); - } + get_gcontact_id(array("url" => $item['owner-link'], "network" => $item['network'], + "photo" => $item['owner-avatar'], "name" => $item['owner-name'])); // Comments in threads may appear as wall-to-wall postings. // So only take the owner at the top posting. @@ -723,6 +679,34 @@ } + /** + * @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[0])) { + $ename = array_keys($data)[0]; + $v = $data[$ename]; + return "<$ename>$v"; + } + 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"; + return $res; + } + /** * load api $templatename for $type and replace $data array */ @@ -735,13 +719,17 @@ case "rss": case "xml": $data = array_xmlify($data); - $tpl = get_markup_template("api_".$templatename."_".$type.".tpl"); - if(! $tpl) { - header ("Content-Type: text/xml"); - echo ''."\n".'not implemented'; - killme(); + if ($templatename==="") { + $ret = api_array_to_xml($data); + } else { + $tpl = get_markup_template("api_".$templatename."_".$type.".tpl"); + if(! $tpl) { + header ("Content-Type: text/xml"); + echo ''."\n".'not implemented'; + killme(); + } + $ret = replace_macros($tpl, $data); } - $ret = replace_macros($tpl, $data); break; case "json": $ret = $data; @@ -824,8 +812,6 @@ if((strpos($txt,'<') !== false) || (strpos($txt,'>') !== false)) { - require_once('library/HTMLPurifier.auto.php'); - $txt = html2bb_video($txt); $config = HTMLPurifier_Config::createDefault(); $config->set('Cache.DefinitionImpl', null); @@ -865,9 +851,6 @@ if(requestdata('htmlstatus')) { $txt = requestdata('htmlstatus'); if((strpos($txt,'<') !== false) || (strpos($txt,'>') !== false)) { - - require_once('library/HTMLPurifier.auto.php'); - $txt = html2bb_video($txt); $config = HTMLPurifier_Config::createDefault(); @@ -918,7 +901,8 @@ if ($posts_day > $throttle_day) { logger('Daily posting limit reached for user '.api_user(), LOGGER_DEBUG); - die(api_error($a, $type, sprintf(t("Daily posting limit of %d posts reached. The post was rejected."), $throttle_day))); + #die(api_error($a, $type, sprintf(t("Daily posting limit of %d posts reached. The post was rejected."), $throttle_day))); + throw new TooManyRequestsException(sprintf(t("Daily posting limit of %d posts reached. The post was rejected."), $throttle_day)); } } @@ -937,7 +921,9 @@ if ($posts_week > $throttle_week) { logger('Weekly posting limit reached for user '.api_user(), LOGGER_DEBUG); - die(api_error($a, $type, sprintf(t("Weekly posting limit of %d posts reached. The post was rejected."), $throttle_week))); + #die(api_error($a, $type, sprintf(t("Weekly posting limit of %d posts reached. The post was rejected."), $throttle_week))); + throw new TooManyRequestsException(sprintf(t("Weekly posting limit of %d posts reached. The post was rejected."), $throttle_week)); + } } @@ -956,7 +942,8 @@ if ($posts_month > $throttle_month) { logger('Monthly posting limit reached for user '.api_user(), LOGGER_DEBUG); - die(api_error($a, $type, sprintf(t("Monthly posting limit of %d posts reached. The post was rejected."), $throttle_month))); + #die(api_error($a, $type, sprintf(t("Monthly posting limit of %d posts reached. The post was rejected."), $throttle_month))); + throw new TooManyRequestsException(sprintf(t("Monthly posting limit of %d posts reached. The post was rejected."), $throttle_month)); } } @@ -1073,7 +1060,7 @@ $in_reply_to_status_id= intval($lastwall['parent']); $in_reply_to_status_id_str = (string) intval($lastwall['parent']); - $r = q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", dbesc(normalise_link($lastwall['item-author']))); + $r = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($lastwall['item-author']))); if ($r) { if ($r[0]['nick'] == "") $r[0]['nick'] = api_get_nick($r[0]["url"]); @@ -1195,7 +1182,7 @@ $in_reply_to_status_id = intval($lastwall['parent']); $in_reply_to_status_id_str = (string) intval($lastwall['parent']); - $r = q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", dbesc(normalise_link($reply[0]['item-author']))); + $r = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($reply[0]['item-author']))); if ($r) { if ($r[0]['nick'] == "") $r[0]['nick'] = api_get_nick($r[0]["url"]); @@ -1256,9 +1243,9 @@ $userlist = array(); if (isset($_GET["q"])) { - $r = q("SELECT id FROM `unique_contacts` WHERE `name`='%s'", dbesc($_GET["q"])); + $r = q("SELECT id FROM `gcontact` WHERE `name`='%s'", dbesc($_GET["q"])); if (!count($r)) - $r = q("SELECT `id` FROM `unique_contacts` WHERE `nick`='%s'", dbesc($_GET["q"])); + $r = q("SELECT `id` FROM `gcontact` WHERE `nick`='%s'", dbesc($_GET["q"])); if (count($r)) { foreach ($r AS $user) { @@ -1322,7 +1309,7 @@ $r = q("SELECT STRAIGHT_JOIN `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `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` + `contact`.`id` AS `cid` FROM `item`, `contact` WHERE `item`.`uid` = %d AND `verb` = '%s' AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0 @@ -1346,9 +1333,12 @@ $idlist = implode(",", $idarray); - if ($idlist != "") - $r = q("UPDATE `item` SET `unseen` = 0 WHERE `unseen` AND `id` IN (%s)", $idlist); + if ($idlist != "") { + $unseen = q("SELECT `id` FROM `item` WHERE `unseen` AND `id` IN (%s)", $idlist); + if ($unseen) + $r = q("UPDATE `item` SET `unseen` = 0 WHERE `unseen` AND `id` IN (%s)", $idlist); + } $data = array('$statuses' => $ret); switch($type){ @@ -1398,7 +1388,7 @@ $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`network`, `contact`.`thumb`, `contact`.`self`, `contact`.`writable`, - `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`, + `contact`.`id` AS `cid`, `user`.`nickname`, `user`.`hidewall` FROM `item` STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id` STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid` @@ -1467,7 +1457,7 @@ $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `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` + `contact`.`id` AS `cid` FROM `item`, `contact` WHERE `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0 AND `contact`.`id` = `item`.`contact-id` AND `item`.`uid` = %d AND `item`.`verb` = '%s' @@ -1536,15 +1526,21 @@ if ($max_id > 0) $sql_extra = ' AND `item`.`id` <= '.intval($max_id); + // Not sure why this query was so complicated. We should keep it here for a while, + // just to make sure that we really don't need it. + // FROM `item` INNER JOIN (SELECT `uri`,`parent` FROM `item` WHERE `id` = %d) AS `temp1` + // ON (`item`.`thr-parent` = `temp1`.`uri` AND `item`.`parent` = `temp1`.`parent`) + $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `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` INNER JOIN (SELECT `uri`,`parent` FROM `item` WHERE `id` = %d) AS `temp1` - ON (`item`.`thr-parent` = `temp1`.`uri` AND `item`.`parent` = `temp1`.`parent`), `contact` - WHERE `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0 - AND `item`.`uid` = %d AND `item`.`verb` = '%s' AND `contact`.`id` = `item`.`contact-id` - AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 + `contact`.`id` AS `cid` + FROM `item` + INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` + WHERE `item`.`parent` = %d AND `item`.`visible` + AND NOT `item`.`moderated` AND NOT `item`.`deleted` + AND `item`.`uid` = %d AND `item`.`verb` = '%s' + AND NOT `contact`.`blocked` AND NOT `contact`.`pending` AND `item`.`id`>%d $sql_extra ORDER BY `item`.`id` DESC LIMIT %d ,%d", intval($id), intval(api_user()), @@ -1562,6 +1558,7 @@ return api_apply_template("timeline", $type, $data); } api_register_func('api/conversation/show','api_conversation_show', true); + api_register_func('api/statusnet/conversation','api_conversation_show', true); /** @@ -1589,11 +1586,13 @@ $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `contact`.`nick` as `reply_author`, `contact`.`name`, `contact`.`photo` as `reply_photo`, `contact`.`url` as `reply_url`, `contact`.`rel`, `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`, - `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid` + `contact`.`id` AS `cid` FROM `item`, `contact` WHERE `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 + AND NOT `item`.`private` AND `item`.`allow_cid` = '' AND `item`.`allow`.`gid` = '' + AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = '' $sql_extra AND `item`.`id`=%d", intval($id) @@ -1622,7 +1621,8 @@ $_REQUEST["source"] = api_source(); item_post($a); - } + } else + throw new ForbiddenException(); // this should output the last post (the one we just posted). $called_api = null; @@ -1699,14 +1699,14 @@ $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `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` + `contact`.`id` AS `cid` + FROM `item` FORCE INDEX (`uid_id`), `contact` WHERE `item`.`uid` = %d AND `verb` = '%s' AND NOT (`item`.`author-link` IN ('https://%s', 'http://%s')) - AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0 + AND `item`.`visible` AND NOT `item`.`moderated` AND NOT `item`.`deleted` AND `contact`.`id` = `item`.`contact-id` - AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0 - AND `item`.`parent` IN (SELECT `iid` from thread where uid = %d AND `mention` AND !`ignored`) + AND NOT `contact`.`blocked` AND NOT `contact`.`pending` + AND `item`.`parent` IN (SELECT `iid` FROM `thread` WHERE `uid` = %d AND `mention` AND !`ignored`) $sql_extra AND `item`.`id`>%d ORDER BY `item`.`id` DESC LIMIT %d ,%d ", @@ -1776,7 +1776,7 @@ $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `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` + `contact`.`id` AS `cid` FROM `item`, `contact` WHERE `item`.`uid` = %d AND `verb` = '%s' AND `item`.`contact-id` = %d @@ -1821,7 +1821,7 @@ $action_argv_id=2; if ($a->argv[1]=="1.1") $action_argv_id=3; - if ($a->argc<=$action_argv_id) die(api_error($a, $type, t("Invalid request."))); + if ($a->argc<=$action_argv_id) throw new BadRequestException("Invalid request."); $action = str_replace(".".$type,"",$a->argv[$action_argv_id]); if ($a->argc==$action_argv_id+2) { $itemid = intval($a->argv[$action_argv_id+1]); @@ -1904,7 +1904,7 @@ $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`, `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` + `contact`.`id` AS `cid` FROM `item`, `contact` WHERE `item`.`uid` = %d AND `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0 @@ -2067,6 +2067,16 @@ $statushtml = trim(bbcode($body, false, false)); + $search = array("
", "
", "
", + "

", "

", "

", "

", + "

", "

", "

", "

", + "
", "
", "
", "
"); + $replace = array("
\n", "\n
", "
\n", + "\n

", "

\n", "\n

", "

\n", + "\n

", "

\n", "\n

", "

\n", + "\n
", "
\n", "\n
", "
\n"); + $statushtml = str_replace($search, $replace, $statushtml); + if ($item['title'] != "") $statushtml = "

".bbcode($item['title'])."

\n".$statushtml; @@ -2277,21 +2287,26 @@ */ function api_format_items_likes(&$item) { $activities = array( - ACTIVITY_LIKE => 'like', - ACTIVITY_DISLIKE => 'dislike', - ACTIVITY_ATTEND => 'attendyes', - ACTIVITY_ATTENDNO => 'attendno', - ACTIVITY_ATTENDMAYBE => 'attendmaybe' + 'like' => array(), + 'dislike' => array(), + 'attendyes' => array(), + 'attendno' => array(), + 'attendmaybe' => array() ); - $r = q("SELECT verb, count(verb) as n FROM item WHERE parent=%d GROUP BY verb", - intval($item['id'])); + $items = q('SELECT * FROM item + WHERE uid=%d AND `thr-parent`="%s" AND visible AND NOT deleted', + intval($item['uid']), + dbesc($item['uri'])); + foreach ($items as $i){ + builtin_activity_puller($i, $activities); + } $res = array(); - foreach($r as $row) { - if (x($activities, $row['verb'])) { - $res[$activities[$row['verb']]] = $row['n']; - } + $uri = $item['uri']; + foreach($activities as $k => $v) { + $res[$k] = (x($v,$uri)?$v[$uri]:0); } + return $res; } @@ -2302,7 +2317,7 @@ * @param array $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) { $a = get_app(); $ret = Array(); @@ -2336,7 +2351,7 @@ intval(api_user()), intval($in_reply_to_status_id)); if ($r) { - $r = q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", dbesc(normalise_link($r[0]['author-link']))); + $r = q("SELECT * FROM `gcontact` WHERE `url` = '%s'", dbesc(normalise_link($r[0]['author-link']))); if ($r) { if ($r[0]['nick'] == "") @@ -2591,7 +2606,7 @@ $stringify_ids = (x($_REQUEST,'stringify_ids')?$_REQUEST['stringify_ids']:false); - $r = q("SELECT `unique_contact`.`id` FROM contact, `unique_contacts` WHERE contact.nurl = unique_contacts.url AND `uid` = %d AND `self` = 0 AND `blocked` = 0 AND `pending` = 0 $sql_extra", + $r = q("SELECT `gcontact`.`id` FROM `contact`, `gcontact` WHERE `contact`.`nurl` = `gcontact`.`nurl` AND `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` $sql_extra", intval(api_user()) ); @@ -2854,7 +2869,7 @@ $scale_sql = ($scale === false ? "" : sprintf("and scale=%d",intval($scale))); $data_sql = ($scale === false ? "" : "data, "); - $r = q("select %s `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`, + $r = q("select %s `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`, `type`, `height`, `width`, `datasize`, `profile`, min(`scale`) as minscale, max(`scale`) as maxscale from photo where `uid` = %d and `resource-id` = '%s' %s group by `resource-id`", $data_sql, @@ -3089,11 +3104,8 @@ //} - if ($nick != "") { - q("UPDATE `unique_contacts` SET `nick` = '%s' WHERE `nick` != '%s' AND url = '%s'", - dbesc($nick), dbesc($nick), dbesc(normalise_link($profile))); + if ($nick != "") return($nick); - } return(false); } @@ -3111,42 +3123,37 @@ $Text = preg_replace("/\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",'[url=$1]$1[/url]',$Text); } - $Text = preg_replace_callback("((.*?)\[class=(.*?)\](.*?)\[\/class\])ism","api_cleanup_share",$Text); + // Simplify "attachment" element + $Text = api_clean_attachments($Text); + return($Text); } - function api_cleanup_share($shared) { - if ($shared[2] != "type-link") - return($shared[0]); - - if (!preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$shared[3], $bookmark)) - return($shared[0]); - - $title = ""; - $link = ""; - - if (isset($bookmark[2][0])) - $title = $bookmark[2][0]; - - if (isset($bookmark[1][0])) - $link = $bookmark[1][0]; + /** + * @brief Removes most sharing information for API text export + * + * @param string $body The original body + * + * @return string Cleaned body + */ + function api_clean_attachments($body) { + $data = get_attachment_data($body); - if (strpos($shared[1],$title) !== false) - $title = ""; + if (!$data) + return $body; - if (strpos($shared[1],$link) !== false) - $link = ""; + $body = ""; - $text = trim($shared[1]); + if (isset($data["text"])) + $body = $data["text"]; - //if (strlen($text) < strlen($title)) - if (($text == "") AND ($title != "")) - $text .= "\n\n".trim($title); + if (($body == "") AND (isset($data["title"]))) + $body = $data["title"]; - if ($link != "") - $text .= "\n".trim($link); + if (isset($data["url"])) + $body .= "\n".$data["url"]; - return(trim($text)); + return $body; } function api_best_nickname(&$contacts) { @@ -3395,8 +3402,9 @@ function api_friendica_activity(&$a, $type) { if (api_user()===false) throw new ForbiddenException(); - #$verb = (x($_REQUEST, 'verb') ? strtolower($_REQUEST['verb']) : ''); $verb = strtolower($a->argv[3]); + $verb = preg_replace("|\..*$|", "", $verb); + $id = (x($_REQUEST, 'id') ? $_REQUEST['id'] : 0); $res = do_like($id, $verb); @@ -3406,7 +3414,7 @@ $ok = "true"; else $ok = "ok"; - return api_apply_template('test', $type, array("$ok" => $ok)); + return api_apply_template('test', $type, array('ok' => $ok)); } else { throw new BadRequestException('Error adding activity'); } @@ -3423,6 +3431,64 @@ api_register_func('api/friendica/activity/unattendno', 'api_friendica_activity', true, API_METHOD_POST); api_register_func('api/friendica/activity/unattendmaybe', 'api_friendica_activity', true, API_METHOD_POST); + /** + * @brief Returns notifications + * + * @param App $a + * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' + * @return string + */ + function api_friendica_notification(&$a, $type) { + if (api_user()===false) throw new ForbiddenException(); + if ($a->argc!==3) throw new BadRequestException("Invalid argument count"); + $nm = new NotificationsManager(); + + $notes = $nm->getAll(array(), "+seen -date", 50); + return api_apply_template("", $type, array('$notes' => $notes)); + } + + /** + * @brief Set notification as seen and returns associated item (if possible) + * + * POST request with 'id' param as notification id + * + * @param App $a + * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' + * @return string + */ + function api_friendica_notification_seen(&$a, $type){ + if (api_user()===false) throw new ForbiddenException(); + if ($a->argc!==4) throw new BadRequestException("Invalid argument count"); + + $id = (x($_REQUEST, 'id') ? intval($_REQUEST['id']) : 0); + + $nm = new NotificationsManager(); + $note = $nm->getByID($id); + if (is_null($note)) throw new BadRequestException("Invalid argument"); + + $nm->setSeen($note); + if ($note['otype']=='item') { + // would be really better with an ItemsManager and $im->getByID() :-P + $r = q("SELECT * FROM `item` WHERE `id`=%d AND `uid`=%d", + intval($note['iid']), + intval(local_user()) + ); + if ($r!==false) { + // we found the item, return it to the user + $user_info = api_get_user($a); + $ret = api_format_items($r,$user_info); + $data = array('$statuses' => $ret); + return api_apply_template("timeline", $type, $data); + } + // the item can't be found, but we set the note as seen, so we count this as a success + } + return api_apply_template('', $type, array('status' => "success")); + } + + api_register_func('api/friendica/notification/seen', 'api_friendica_notification_seen', true, API_METHOD_POST); + api_register_func('api/friendica/notification', 'api_friendica_notification', true, API_METHOD_GET); + + /* To.Do: [pagename] => api/1.1/statuses/lookup.json