]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Replace dba::select(limit => 1) by dba::selectOne
[friendica.git] / include / api.php
index c08ba7b3da3e6dc0c3f3053858bf515adc895fdb..16d18a37ca162785701c041ff0b09d2ef2ce382b 100644 (file)
@@ -1,15 +1,17 @@
 <?php
 /**
- * @file include/api.php
  * Friendica implementation of statusnet/twitter API
  *
+ * @file include/api.php
  * @todo Automatically detect if incoming data is HTML or BBCode
  */
 use Friendica\App;
+use Friendica\Content\ContactSelector;
 use Friendica\Content\Feature;
 use Friendica\Core\System;
 use Friendica\Core\Config;
 use Friendica\Core\NotificationsManager;
+use Friendica\Core\PConfig;
 use Friendica\Core\Worker;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
@@ -37,7 +39,6 @@ require_once 'include/html2plain.php';
 require_once 'mod/share.php';
 require_once 'mod/item.php';
 require_once 'include/security.php';
-require_once 'include/contact_selectors.php';
 require_once 'include/html2bbcode.php';
 require_once 'mod/wall_upload.php';
 require_once 'mod/proxy.php';
@@ -54,11 +55,11 @@ $API = array();
 $called_api = null;
 
 /**
- * @brief Auth API user
- *
  * It is not sufficient to use local_user() to check whether someone is allowed to use the API,
  * because this will open CSRF holes (just embed an image with src=friendicasite.com/api/statuses/update?status=CSRF
  * into a page, and visitors will post something without noticing it).
+ *
+ * @brief Auth API user
  */
 function api_user()
 {
@@ -70,13 +71,13 @@ function api_user()
 }
 
 /**
- * @brief Get source name from API client
- *
  * Clients can send 'source' parameter to be show in post metadata
  * as "sent via <source>".
  * Some clients doesn't send a source param, we support ones we know
  * (only Twidere, atm)
  *
+ * @brief Get source name from API client
+ *
  * @return string
  *             Client source name, default to "api" if unset/unknown
  */
@@ -109,9 +110,9 @@ function api_date($str)
 }
 
 /**
- * @brief Register API endpoint
+ * Register a function to be the endpoint for defined API path.
  *
- * Register a function to be the endpont for defined API path.
+ * @brief Register API endpoint
  *
  * @param string $path   API URL path, relative to System::baseUrl()
  * @param string $func   Function name to call on path request
@@ -141,11 +142,11 @@ function api_register_func($path, $func, $auth = false, $method = API_METHOD_ANY
 }
 
 /**
- * @brief Login API user
- *
  * Log in user via OAuth1 or Simple HTTP Auth.
  * Simple Auth allow username in form of <pre>user@server</pre>, ignoring server part
  *
+ * @brief Login API user
+ *
  * @param object $a App
  * @hook 'authenticate'
  *             array $addon_auth
@@ -185,7 +186,7 @@ function api_login(App $a)
        }
 
        if (!x($_SERVER, 'PHP_AUTH_USER')) {
-               logger('API_login: ' . print_r($_SERVER,true), LOGGER_DEBUG);
+               logger('API_login: ' . print_r($_SERVER, true), LOGGER_DEBUG);
                header('WWW-Authenticate: Basic realm="Friendica"');
                throw new UnauthorizedException("This API requires login");
        }
@@ -216,16 +217,16 @@ function api_login(App $a)
                */
        call_hooks('authenticate', $addon_auth);
 
-       if (($addon_auth['authenticated']) && (count($addon_auth['user_record']))) {
+       if ($addon_auth['authenticated'] && count($addon_auth['user_record'])) {
                $record = $addon_auth['user_record'];
        } else {
                $user_id = User::authenticate(trim($user), trim($password));
                if ($user_id) {
-                       $record = dba::select('user', [], ['uid' => $user_id], ['limit' => 1]);
+                       $record = dba::selectOne('user', [], ['uid' => $user_id]);
                }
        }
 
-       if ((! $record) || (! count($record))) {
+       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');
@@ -241,12 +242,12 @@ function api_login(App $a)
 }
 
 /**
- * @brief Check HTTP method of called API
- *
  * API endpoints can define which HTTP method to accept when called.
  * This function check the current HTTP method agains endpoint
  * registered method.
  *
+ * @brief Check HTTP method of called API
+ *
  * @param string $method Required methods, uppercase, separated by comma
  * @return bool
  */
@@ -259,10 +260,10 @@ function api_check_method($method)
 }
 
 /**
- * @brief Main API entry point
- *
  * Authenticate user, call registered API function, set HTTP headers
  *
+ * @brief Main API entry point
+ *
  * @param object $a App
  * @return string API call result
  */
@@ -303,7 +304,7 @@ function api_call(App $a)
                                logger('API parameters: ' . print_r($_REQUEST, true));
 
                                $stamp =  microtime(true);
-                               $r = call_user_func($info['func'], $type);
+                               $return = call_user_func($info['func'], $type);
                                $duration = (float) (microtime(true) - $stamp);
                                logger("API call duration: " . round($duration, 2) . "\t" . $a->query_string, LOGGER_DEBUG);
 
@@ -351,7 +352,7 @@ function api_call(App $a)
                                        }
                                }
 
-                               if (false === $r) {
+                               if (false === $return) {
                                        /*
                                                * api function returned false withour throw an
                                                * exception. This should not happend, throw a 500
@@ -362,26 +363,27 @@ function api_call(App $a)
                                switch ($type) {
                                        case "xml":
                                                header("Content-Type: text/xml");
-                                               return $r;
                                                break;
                                        case "json":
                                                header("Content-Type: application/json");
-                                               foreach ($r as $rr)
+                                               foreach ($return as $rr) {
                                                        $json = json_encode($rr);
-                                                       if (x($_GET, 'callback')) {
-                                                               $json = $_GET['callback'] . "(" . $json . ")";
-                                                       }
-                                                       return $json;
+                                               }
+                                               if (x($_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;
+                                               $return  = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
                                                break;
                                        case "atom":
                                                header("Content-Type: application/atom+xml");
-                                               return '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $r;
+                                               $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
                                                break;
                                }
+                               return $return;
                        }
                }
 
@@ -398,7 +400,7 @@ function api_call(App $a)
  *
  * @param string $type Return type (xml, json, rss, as)
  * @param object $e    HTTPException Error object
- * @return strin error message formatted as $type
+ * @return string error message formatted as $type
  */
 function api_error($type, $e)
 {
@@ -411,26 +413,25 @@ function api_error($type, $e)
                        "code" => $e->httpcode . " " . $e->httpdesc,
                        "request" => $a->query_string);
 
-       $ret = api_format_data('status', $type, array('status' => $error));
+       $return = api_format_data('status', $type, array('status' => $error));
 
        switch ($type) {
                case "xml":
                        header("Content-Type: text/xml");
-                       return $ret;
                        break;
                case "json":
                        header("Content-Type: application/json");
-                       return json_encode($ret);
+                       $return = json_encode($return);
                        break;
                case "rss":
                        header("Content-Type: application/rss+xml");
-                       return $ret;
                        break;
                case "atom":
                        header("Content-Type: application/atom+xml");
-                       return $ret;
                        break;
        }
+
+       return $return;
 }
 
 /**
@@ -472,7 +473,7 @@ function api_rss_extra(App $a, $arr, $user_info)
  */
 function api_unique_id_to_nurl($id)
 {
-       $r = dba::select('contact', array('nurl'), array('uid' => 0, 'id' => $id), array('limit' => 1));
+       $r = dba::selectOne('contact', array('nurl'), array('uid' => 0, 'id' => $id));
 
        if (DBM::is_result($r)) {
                return $r["nurl"];
@@ -494,7 +495,6 @@ function api_get_user(App $a, $contact_id = null)
        $user = null;
        $extra_query = "";
        $url = "";
-       $nick = "";
 
        logger("api_get_user: Fetching user data for user ".$contact_id, LOGGER_DEBUG);
 
@@ -538,7 +538,6 @@ function api_get_user(App $a, $contact_id = null)
        }
        if (is_null($user) && x($_GET, 'screen_name')) {
                $user = dbesc($_GET['screen_name']);
-               $nick = $user;
                $extra_query = "AND `contact`.`nick` = '%s' ";
                if (api_user() !== false) {
                        $extra_query .= "AND `contact`.`uid`=".intval(api_user());
@@ -547,7 +546,6 @@ function api_get_user(App $a, $contact_id = null)
 
        if (is_null($user) && x($_GET, 'profileurl')) {
                $user = dbesc(normalise_link($_GET['profileurl']));
-               $nick = $user;
                $extra_query = "AND `contact`.`nurl` = '%s' ";
                if (api_user() !== false) {
                        $extra_query .= "AND `contact`.`uid`=".intval(api_user());
@@ -571,7 +569,6 @@ function api_get_user(App $a, $contact_id = null)
                        }
                } else {
                        $user = dbesc($user);
-                       $nick = $user;
                        $extra_query = "AND `contact`.`nick` = '%s' ";
                        if (api_user() !== false) {
                                $extra_query .= "AND `contact`.`uid`=" . intval(api_user());
@@ -613,7 +610,7 @@ function api_get_user(App $a, $contact_id = null)
                }
 
                if (DBM::is_result($r)) {
-                       $network_name = network_to_name($r[0]['network'], $r[0]['url']);
+                       $network_name = ContactSelector::networkToName($r[0]['network'], $r[0]['url']);
 
                        // If no nick where given, extract it from the address
                        if (($r[0]['nick'] == "") || ($r[0]['name'] == $r[0]['nick'])) {
@@ -737,17 +734,31 @@ function api_get_user(App $a, $contact_id = null)
                $uinfo[0]['nick'] = api_get_nick($uinfo[0]["url"]);
        }
 
-       $network_name = network_to_name($uinfo[0]['network'], $uinfo[0]['url']);
+       $network_name = ContactSelector::networkToName($uinfo[0]['network'], $uinfo[0]['url']);
 
        $pcontact_id  = Contact::getIdForURL($uinfo[0]['url'], 0, true);
 
+       if (!empty($profile[0]['about'])) {
+               $description = $profile[0]['about'];
+       } else {
+               $description = $uinfo[0]["about"];
+       }
+
+       if (!empty($usr[0]['default-location'])) {
+               $location = $usr[0]['default-location'];
+       } elseif (!empty($uinfo[0]["location"])) {
+               $location = $uinfo[0]["location"];
+       } else {
+               $location = $network_name;
+       }
+
        $ret = array(
                'id' => intval($pcontact_id),
                'id_str' => (string) intval($pcontact_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,
-               'description' => (($profile) ? $profile[0]['pdesc'] : null),
+               'location' => $location,
+               'description' => $description,
                'profile_image_url' => $uinfo[0]['micro'],
                'profile_image_url_https' => $uinfo[0]['micro'],
                'url' => $uinfo[0]['url'],
@@ -779,6 +790,37 @@ function api_get_user(App $a, $contact_id = null)
                'network' => $uinfo[0]['network'],
        );
 
+       // If this is a local user and it uses Frio, we can get its color preferences.
+       if ($ret['self']) {
+               $theme_info = dba::selectOne('user', ['theme'], ['uid' => $ret['uid']]);
+               if ($theme_info['theme'] === 'frio') {
+                       $schema = PConfig::get($ret['uid'], 'frio', 'schema');
+                       if ($schema && ($schema != '---')) {
+                               if (file_exists('view/theme/frio/schema/'.$schema.'.php')) {
+                                       $schemefile = 'view/theme/frio/schema/'.$schema.'.php';
+                                       require_once $schemefile;
+                               }
+                       } else {
+                               $nav_bg = PConfig::get($ret['uid'], 'frio', 'nav_bg');
+                               $link_color = PConfig::get($ret['uid'], 'frio', 'link_color');
+                               $bgcolor = PConfig::get($ret['uid'], 'frio', 'background_color');
+                       }
+                       if (!$nav_bg) {
+                               $nav_bg = "#708fa0";
+                       }
+                       if (!$link_color) {
+                               $link_color = "#6fdbe8";
+                       }
+                       if (!$bgcolor) {
+                               $bgcolor = "#ededed";
+                       }
+
+                       $ret['profile_sidebar_fill_color'] = str_replace('#', '', $nav_bg);
+                       $ret['profile_link_color'] = str_replace('#', '', $link_color);
+                       $ret['profile_background_color'] = str_replace('#', '', $bgcolor);
+               }
+       }
+
        return $ret;
 }
 
@@ -914,12 +956,10 @@ function api_create_xml($data, $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
+ * @return (string|object|array) XML data or JSON data
  */
 function api_format_data($root_element, $type, $data)
 {
-       $a = get_app();
-
        switch ($type) {
                case "atom":
                case "rss":
@@ -941,7 +981,9 @@ function api_format_data($root_element, $type, $data)
 /**
  * Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful;
  * returns a 401 status code and an error message if not.
- * http://developer.twitter.com/doc/get/account/verify_credentials
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/get-account-verify_credentials
+ *
+ * @param string $type Return type (atom, rss, xml, json)
  */
 function api_account_verify_credentials($type)
 {
@@ -982,11 +1024,13 @@ function api_account_verify_credentials($type)
        return api_format_data("user", $type, array('user' => $user_info));
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/account/verify_credentials', 'api_account_verify_credentials', true);
 
 /**
  * Get data from $_POST or $_GET
+ *
+ * @param string $k
  */
 function requestdata($k)
 {
@@ -999,7 +1043,13 @@ function requestdata($k)
        return null;
 }
 
-/*Waitman Gobble Mod*/
+/**
+ * Waitman Gobble Mod
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_statuses_mediap($type)
 {
        $a = get_app();
@@ -1043,6 +1093,14 @@ function api_statuses_mediap($type)
 /// @TODO move this to top of file or somewhere better!
 api_register_func('api/statuses/mediap', 'api_statuses_mediap', true, API_METHOD_POST);
 
+/**
+ * Updates the user’s current status.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update
+ */
 function api_statuses_update($type)
 {
 
@@ -1053,12 +1111,9 @@ function api_statuses_update($type)
                throw new ForbiddenException();
        }
 
-       $user_info = api_get_user($a);
+       api_get_user($a);
 
        // convert $_POST array items to the form we use for web posts.
-
-       // logger('api_post: ' . print_r($_POST,true));
-
        if (requestdata('htmlstatus')) {
                $txt = requestdata('htmlstatus');
                if ((strpos($txt, '<') !== false) || (strpos($txt, '>') !== false)) {
@@ -1214,11 +1269,17 @@ function api_statuses_update($type)
        return api_status_show($type);
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/statuses/update', 'api_statuses_update', true, API_METHOD_POST);
 api_register_func('api/statuses/update_with_media', 'api_statuses_update', true, API_METHOD_POST);
 
-function api_media_upload($type)
+/**
+ * Uploads an image to Friendica.
+ *
+ * @return array
+ * @see https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload
+ */
+function api_media_upload()
 {
        $a = get_app();
 
@@ -1227,7 +1288,7 @@ function api_media_upload($type)
                throw new ForbiddenException();
        }
 
-       $user_info = api_get_user($a);
+       api_get_user($a);
 
        if (!x($_FILES, 'media')) {
                // Output error
@@ -1253,9 +1314,15 @@ function api_media_upload($type)
        return array("media" => $returndata);
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/media/upload', 'api_media_upload', true, API_METHOD_POST);
 
+/**
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_status_show($type)
 {
        $a = get_app();
@@ -1338,9 +1405,9 @@ function api_status_show($type)
                }
 
                if (($lastwall['item_network'] != "") && ($status["source"] == 'web')) {
-                       $status_info["source"] = network_to_name($lastwall['item_network'], $user_info['url']);
-               } elseif (($lastwall['item_network'] != "") && (network_to_name($lastwall['item_network'], $user_info['url']) != $status_info["source"])) {
-                       $status_info["source"] = trim($status_info["source"].' ('.network_to_name($lastwall['item_network'], $user_info['url']).')');
+                       $status_info["source"] = ContactSelector::networkToName($lastwall['item_network'], $user_info['url']);
+               } elseif (($lastwall['item_network'] != "") && (ContactSelector::networkToName($lastwall['item_network'], $user_info['url']) != $status_info["source"])) {
+                       $status_info["source"] = trim($status_info["source"].' ('.ContactSelector::networkToName($lastwall['item_network'], $user_info['url']).')');
                }
 
                // "uid" and "self" are only needed for some internal stuff, so remove it from here
@@ -1360,7 +1427,9 @@ function api_status_show($type)
 /**
  * Returns extended information of a given user, specified by ID or screen name as per the required id parameter.
  * The author's most recent status will be returned inline.
- * http://developer.twitter.com/doc/get/users/show
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-show
  */
 function api_users_show($type)
 {
@@ -1427,11 +1496,11 @@ function api_users_show($type)
                }
 
                if (($lastwall['item_network'] != "") && ($user_info["status"]["source"] == 'web')) {
-                       $user_info["status"]["source"] = network_to_name($lastwall['item_network'], $user_info['url']);
+                       $user_info["status"]["source"] = ContactSelector::networkToName($lastwall['item_network'], $user_info['url']);
                }
 
-               if (($lastwall['item_network'] != "") && (network_to_name($lastwall['item_network'], $user_info['url']) != $user_info["status"]["source"])) {
-                       $user_info["status"]["source"] = trim($user_info["status"]["source"] . ' (' . network_to_name($lastwall['item_network'], $user_info['url']) . ')');
+               if (($lastwall['item_network'] != "") && (ContactSelector::networkToName($lastwall['item_network'], $user_info['url']) != $user_info["status"]["source"])) {
+                       $user_info["status"]["source"] = trim($user_info["status"]["source"] . ' (' . ContactSelector::networkToName($lastwall['item_network'], $user_info['url']) . ')');
                }
        }
 
@@ -1446,12 +1515,18 @@ function api_users_show($type)
 api_register_func('api/users/show', 'api_users_show');
 api_register_func('api/externalprofile/show', 'api_users_show');
 
+/**
+ * Search a public user account.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-search
+ */
 function api_users_search($type)
 {
        $a = get_app();
 
-       $page = (x($_REQUEST, 'page') ? $_REQUEST['page'] - 1 : 0);
-
        $userlist = array();
 
        if (x($_GET, 'q')) {
@@ -1464,7 +1539,7 @@ function api_users_search($type)
                if (DBM::is_result($r)) {
                        $k = 0;
                        foreach ($r as $user) {
-                               $user_info = api_get_user($a, $user["id"], "json");
+                               $user_info = api_get_user($a, $user["id"]);
 
                                if ($type == "xml") {
                                        $userlist[$k++.":user"] = $user_info;
@@ -1479,6 +1554,7 @@ function api_users_search($type)
        } else {
                throw new BadRequestException("User not found.");
        }
+
        return api_format_data("users", $type, $userlist);
 }
 
@@ -1486,11 +1562,104 @@ function api_users_search($type)
 api_register_func('api/users/search', 'api_users_search');
 
 /**
+ * Return user objects
+ *
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-lookup
+ *
+ * @param string $type Return format: json or xml
+ *
+ * @return array|string
+ * @throws NotFoundException if the results are empty.
+ */
+function api_users_lookup($type)
+{
+       $users = array();
+
+       if (x($_REQUEST['user_id'])) {
+               foreach (explode(',', $_REQUEST['user_id']) as $id) {
+                       if (!empty($id)) {
+                               $users[] = api_get_user(get_app(), $id);
+                       }
+               }
+       }
+
+       if (empty($users)) {
+               throw new NotFoundException;
+       }
+
+       return api_format_data("users", $type, array('users' => $users));
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/users/lookup', 'api_users_lookup', true);
+
+/**
+ * Returns statuses that match a specified query.
+ *
+ * @see https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets
+ *
+ * @param string $type Return format: json, xml, atom, rss
+ *
+ * @return array|string
+ * @throws BadRequestException if the "q" parameter is missing.
+ */
+function api_search($type)
+{
+       $data = array();
+
+       if (!x($_REQUEST, 'q')) {
+               throw new BadRequestException("q parameter is required.");
+       }
+
+       if (x($_REQUEST, 'rpp')) {
+               $count = $_REQUEST['rpp'];
+       } elseif (x($_REQUEST, 'count')) {
+               $count = $_REQUEST['count'];
+       } else {
+               $count = 15;
+       }
+
+       $since_id = (x($_REQUEST, 'since_id') ? $_REQUEST['since_id'] : 0);
+       $max_id = (x($_REQUEST, 'max_id') ? $_REQUEST['max_id'] : 0);
+       $page = (x($_REQUEST, 'page') ? $_REQUEST['page'] - 1 : 0);
+
+       $start = $page * $count;
+
+       if ($max_id > 0) {
+               $sql_extra .= ' AND `item`.`id` <= ' . intval($max_id);
+       }
+
+       $r = dba::p(
+               "SELECT ".item_fieldlists()."
+               FROM `item` ".item_joins()."
+               WHERE ".item_condition()." AND (`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))
+               AND `item`.`body` LIKE CONCAT('%',?,'%')
+               $sql_extra
+               AND `item`.`id`>?
+               ORDER BY `item`.`id` DESC LIMIT ".intval($start)." ,".intval($count)." ",
+               api_user(),
+               $_REQUEST['q'],
+               $since_id
+       );
+
+       $data['status'] = api_format_items(dba::inArray($r), api_get_user(get_app()));
+
+       return api_format_data("statuses", $type, $data);
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/search/tweets', 'api_search', true);
+api_register_func('api/search', 'api_search', true);
+
+/**
+ * Returns the most recent statuses posted by the user and the users they follow.
+ *
+ * @see https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-home_timeline
  *
- * http://developer.twitter.com/doc/get/statuses/home_timeline
+ * @param string $type Return type (atom, rss, xml, json)
  *
- * TODO: Optional parameters
- * TODO: Add reply info
+ * @todo Optional parameters
+ * @todo Add reply info
  */
 function api_statuses_home_timeline($type)
 {
@@ -1568,7 +1737,7 @@ function api_statuses_home_timeline($type)
                $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);
+                       q("UPDATE `item` SET `unseen` = 0 WHERE `unseen` AND `id` IN (%s)", $idlist);
                }
        }
 
@@ -1587,6 +1756,13 @@ function api_statuses_home_timeline($type)
 api_register_func('api/statuses/home_timeline', 'api_statuses_home_timeline', true);
 api_register_func('api/statuses/friends_timeline', 'api_statuses_home_timeline', true);
 
+/**
+ * Returns the most recent statuses from public users.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_statuses_public_timeline($type)
 {
        $a = get_app();
@@ -1612,39 +1788,63 @@ function api_statuses_public_timeline($type)
 
        $start = $page * $count;
 
-       if ($max_id > 0) {
-               $sql_extra = 'AND `item`.`id` <= ' . intval($max_id);
-       }
-       if ($exclude_replies > 0) {
-               $sql_extra .= ' AND `item`.`parent` = `item`.`id`';
-       }
-       if ($conversation_id > 0) {
-               $sql_extra .= ' AND `item`.`parent` = ' . intval($conversation_id);
-       }
+       if ($exclude_replies && !$conversation_id) {
+               if ($max_id > 0) {
+                       $sql_extra = 'AND `thread`.`iid` <= ' . intval($max_id);
+               }
 
-       $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`,
-               `user`.`nickname`, `user`.`hidewall`
-               FROM `item`
-               STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` = `item`.`uid`
-                       AND (NOT `contact`.`blocked` OR `contact`.`pending`)
-               STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
-                       AND NOT `user`.`hidewall`
-               WHERE `verb` = '%s' AND `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
-               AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
-               AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
-               AND NOT `item`.`private` AND `item`.`wall`
-               $sql_extra
-               AND `item`.`id`>%d
-               ORDER BY `item`.`id` DESC LIMIT %d, %d ",
-               dbesc(ACTIVITY_POST),
-               intval($since_id),
-               intval($start),
-               intval($count)
-       );
+               $r = dba::p(
+                       "SELECT " . item_fieldlists() . "
+                       FROM `thread`
+                       STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
+                       " . item_joins() . "
+                       STRAIGHT_JOIN `user` ON `user`.`uid` = `thread`.`uid`
+                               AND NOT `user`.`hidewall`
+                       AND `verb` = ?
+                       AND NOT `thread`.`private`
+                       AND `thread`.`wall`
+                       AND `thread`.`visible`
+                       AND NOT `thread`.`deleted`
+                       AND NOT `thread`.`moderated`
+                       AND `thread`.`iid` > ?
+                       $sql_extra
+                       ORDER BY `thread`.`iid` DESC
+                       LIMIT " . intval($start) . ", " . intval($count),
+                       ACTIVITY_POST,
+                       $since_id
+               );
+
+               $r = dba::inArray($r);
+       } else {
+               if ($max_id > 0) {
+                       $sql_extra = 'AND `item`.`id` <= ' . intval($max_id);
+               }
+               if ($conversation_id > 0) {
+                       $sql_extra .= ' AND `item`.`parent` = ' . intval($conversation_id);
+               }
+
+               $r = dba::p(
+                       "SELECT " . item_fieldlists() . "
+                       FROM `item`
+                       " . item_joins() . "
+                       STRAIGHT_JOIN `user` ON `user`.`uid` = `item`.`uid`
+                               AND NOT `user`.`hidewall`
+                       AND `verb` = ?
+                       AND NOT `item`.`private`
+                       AND `item`.`wall`
+                       AND `item`.`visible`
+                       AND NOT `item`.`deleted`
+                       AND NOT `item`.`moderated`
+                       AND `item`.`id` > ?
+                       $sql_extra
+                       ORDER BY `item`.`id` DESC
+                       LIMIT " . intval($start) . ", " . intval($count),
+                       ACTIVITY_POST,
+                       $since_id
+               );
+
+               $r = dba::inArray($r);
+       }
 
        $ret = api_format_items($r, $user_info, false, $type);
 
@@ -1663,6 +1863,8 @@ function api_statuses_public_timeline($type)
 api_register_func('api/statuses/public_timeline', 'api_statuses_public_timeline', true);
 
 /**
+ * Returns the most recent statuses posted by users this node knows about.
+ *
  * @brief Returns the list of public federated posts this node knows about
  *
  * @param string $type Return format: json, xml, atom, rss
@@ -1695,13 +1897,18 @@ function api_statuses_networkpublic_timeline($type)
                $sql_extra = 'AND `thread`.`iid` <= ' . intval($max_id);
        }
 
-       $r = dba::p("SELECT " . item_fieldlists() . "
+       $r = dba::p(
+               "SELECT " . item_fieldlists() . "
                FROM `thread`
-               INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
+               STRAIGHT_JOIN `item` ON `item`.`id` = `thread`.`iid`
                " . item_joins() . "
                WHERE `thread`.`uid` = 0
                AND `verb` = ?
-               AND `item`.`id` > ?
+               AND NOT `thread`.`private`
+               AND `thread`.`visible`
+               AND NOT `thread`.`deleted`
+               AND NOT `thread`.`moderated`
+               AND `thread`.`iid` > ?
                $sql_extra
                ORDER BY `thread`.`iid` DESC
                LIMIT " . intval($start) . ", " . intval($count),
@@ -1728,7 +1935,11 @@ function api_statuses_networkpublic_timeline($type)
 api_register_func('api/statuses/networkpublic_timeline', 'api_statuses_networkpublic_timeline', true);
 
 /**
- * @TODO nothing to say?
+ * Returns a single status.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/get-statuses-show-id
  */
 function api_statuses_show($type)
 {
@@ -1799,7 +2010,10 @@ function api_statuses_show($type)
 api_register_func('api/statuses/show', 'api_statuses_show', true);
 
 /**
- * @TODO nothing to say?
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @todo nothing to say?
  */
 function api_conversation_show($type)
 {
@@ -1863,10 +2077,12 @@ function api_conversation_show($type)
                AND `item`.`uid` = %d AND `item`.`verb` = '%s'
                AND `item`.`id`>%d $sql_extra
                ORDER BY `item`.`id` DESC LIMIT %d ,%d",
-               intval($id), intval(api_user()),
+               intval($id),
+               intval(api_user()),
                dbesc(ACTIVITY_POST),
                intval($since_id),
-               intval($start), intval($count)
+               intval($start),
+               intval($count)
        );
 
        if (!DBM::is_result($r)) {
@@ -1884,7 +2100,11 @@ api_register_func('api/conversation/show', 'api_conversation_show', true);
 api_register_func('api/statusnet/conversation', 'api_conversation_show', true);
 
 /**
- * @TODO nothing to say?
+ * Repeats a status.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-retweet-id
  */
 function api_statuses_repeat($type)
 {
@@ -1896,7 +2116,7 @@ function api_statuses_repeat($type)
                throw new ForbiddenException();
        }
 
-       $user_info = api_get_user($a);
+       api_get_user($a);
 
        // params
        $id = intval($a->argv[3]);
@@ -1962,7 +2182,11 @@ function api_statuses_repeat($type)
 api_register_func('api/statuses/retweet', 'api_statuses_repeat', true, API_METHOD_POST);
 
 /**
- * @TODO nothing to say?
+ * Destroys a specific status.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-destroy-id
  */
 function api_statuses_destroy($type)
 {
@@ -1972,7 +2196,7 @@ function api_statuses_destroy($type)
                throw new ForbiddenException();
        }
 
-       $user_info = api_get_user($a);
+       api_get_user($a);
 
        // params
        $id = intval($a->argv[3]);
@@ -1999,8 +2223,11 @@ function api_statuses_destroy($type)
 api_register_func('api/statuses/destroy', 'api_statuses_destroy', true, API_METHOD_DELETE);
 
 /**
- * @TODO Nothing more than an URL to say?
- * http://developer.twitter.com/doc/get/statuses/mentions
+ * Returns the most recent mentions.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @see http://developer.twitter.com/doc/get/statuses/mentions
  */
 function api_statuses_mentions($type)
 {
@@ -2021,23 +2248,20 @@ function api_statuses_mentions($type)
 
 
        // params
-       $count = (x($_REQUEST, 'count') ? $_REQUEST['count'] : 20);
-       $page = (x($_REQUEST, 'page') ? $_REQUEST['page'] -1 : 0);
-       if ($page < 0) {
-               $page = 0;
+       $since_id = defaults($_REQUEST, 'since_id', 0);
+       $max_id   = defaults($_REQUEST, 'max_id'  , 0);
+       $count    = defaults($_REQUEST, 'count'   , 20);
+       $page     = defaults($_REQUEST, 'page'    , 1);
+       if ($page < 1) {
+               $page = 1;
        }
-       $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);
 
-       $start = $page * $count;
+       $start = ($page - 1) * $count;
 
        // Ugly code - should be changed
        $myurl = System::baseUrl() . '/profile/'. $a->user['nickname'];
        $myurl = substr($myurl, strpos($myurl, '://') + 3);
-       //$myurl = str_replace(array('www.','.'),array('','\\.'),$myurl);
        $myurl = str_replace('www.', '', $myurl);
-       $diasp_url = str_replace('/profile/', '/u/', $myurl);
 
        if ($max_id > 0) {
                $sql_extra = ' AND `item`.`id` <= ' . intval($max_id);
@@ -2086,11 +2310,14 @@ api_register_func('api/statuses/mentions', 'api_statuses_mentions', true);
 api_register_func('api/statuses/replies', 'api_statuses_mentions', true);
 
 /**
+ * Returns the most recent statuses posted by the user.
+ *
  * @brief Returns a user's public timeline
  *
  * @param string $type Either "json" or "xml"
  * @return string|array
  * @throws ForbiddenException
+ * @see https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline
  */
 function api_statuses_user_timeline($type)
 {
@@ -2174,14 +2401,16 @@ function api_statuses_user_timeline($type)
        return api_format_data("statuses", $type, $data);
 }
 
-/// @TODO move to top of file or somwhere better
-api_register_func('api/statuses/user_timeline','api_statuses_user_timeline', true);
+/// @TODO move to top of file or somewhere better
+api_register_func('api/statuses/user_timeline', 'api_statuses_user_timeline', true);
 
 /**
- * Star/unstar an item
+ * Star/unstar an item.
  * param: id : id of the item
  *
- * api v1 : https://web.archive.org/web/20131019055350/https://dev.twitter.com/docs/api/1/post/favorites/create/%3Aid
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @see https://web.archive.org/web/20131019055350/https://dev.twitter.com/docs/api/1/post/favorites/create/%3Aid
  */
 function api_favorites_create_destroy($type)
 {
@@ -2226,7 +2455,7 @@ function api_favorites_create_destroy($type)
                        throw new BadRequestException("Invalid action ".$action);
        }
 
-       $r = q("UPDATE item SET starred=%d WHERE id=%d AND uid=%d",     $item[0]['starred'], $itemid, api_user());
+       $r = q("UPDATE item SET starred=%d WHERE id=%d AND uid=%d", $item[0]['starred'], $itemid, api_user());
 
        q("UPDATE thread SET starred=%d WHERE iid=%d AND uid=%d", $item[0]['starred'], $itemid, api_user());
 
@@ -2249,10 +2478,17 @@ function api_favorites_create_destroy($type)
        return api_format_data("status", $type, $data);
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 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);
 
+/**
+ * Returns the most recent favorite statuses.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return string|array
+ */
 function api_favorites($type)
 {
        global $called_api;
@@ -2324,9 +2560,17 @@ function api_favorites($type)
        return api_format_data("statuses", $type, $data);
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/favorites', 'api_favorites', true);
 
+/**
+ *
+ * @param array $item
+ * @param array $recipient
+ * @param array $sender
+ *
+ * @return array
+ */
 function api_format_messages($item, $recipient, $sender)
 {
        // standard meta information
@@ -2371,6 +2615,12 @@ function api_format_messages($item, $recipient, $sender)
        return $ret;
 }
 
+/**
+ *
+ * @param array $item
+ *
+ * @return array
+ */
 function api_convert_item($item)
 {
        $body = $item['body'];
@@ -2440,6 +2690,12 @@ function api_convert_item($item)
        );
 }
 
+/**
+ *
+ * @param string $body
+ *
+ * @return array|false
+ */
 function api_get_attachments(&$body)
 {
        $text = $body;
@@ -2471,15 +2727,16 @@ function api_get_attachments(&$body)
        return $attachments;
 }
 
+/**
+ *
+ * @param string $text
+ * @param string $bbcode
+ *
+ * @return array
+ * @todo Links at the first character of the post
+ */
 function api_get_entitities(&$text, $bbcode)
 {
-       /*
-       To-Do:
-       * Links at the first character of the post
-       */
-
-       $a = get_app();
-
        $include_entities = strtolower(x($_REQUEST, 'include_entities') ? $_REQUEST['include_entities'] : "false");
 
        if ($include_entities != "true") {
@@ -2546,14 +2803,15 @@ function api_get_entitities(&$text, $bbcode)
        foreach ($ordered_urls as $url) {
                if ((substr($url["title"], 0, 7) != "http://") && (substr($url["title"], 0, 8) != "https://")
                        && !strpos($url["title"], "http://") && !strpos($url["title"], "https://")
-               )
+               ) {
                        $display_url = $url["title"];
-               else {
+               else {
                        $display_url = str_replace(array("http://www.", "https://www."), array("", ""), $url["url"]);
                        $display_url = str_replace(array("http://", "https://"), array("", ""), $display_url);
 
-                       if (strlen($display_url) > 26)
+                       if (strlen($display_url) > 26) {
                                $display_url = substr($display_url, 0, 25)."…";
+                       }
                }
 
                //$start = strpos($text, $url, $offset);
@@ -2572,8 +2830,9 @@ function api_get_entitities(&$text, $bbcode)
        foreach ($images[1] as $image) {
                //$start = strpos($text, $url, $offset);
                $start = iconv_strpos($text, $image, 0, "UTF-8");
-               if (!($start === false))
+               if (!($start === false)) {
                        $ordered_images[$start] = $image;
+               }
        }
        //$entities["media"] = array();
        $offset = 0;
@@ -2582,8 +2841,9 @@ function api_get_entitities(&$text, $bbcode)
                $display_url = str_replace(array("http://www.", "https://www."), array("", ""), $url);
                $display_url = str_replace(array("http://", "https://"), array("", ""), $display_url);
 
-               if (strlen($display_url) > 26)
+               if (strlen($display_url) > 26) {
                        $display_url = substr($display_url, 0, 25)."…";
+               }
 
                $start = iconv_strpos($text, $url, $offset, "UTF-8");
                if (!($start === false)) {
@@ -2633,25 +2893,32 @@ function api_get_entitities(&$text, $bbcode)
 
        return $entities;
 }
-function api_format_items_embeded_images(&$item, $text)
+
+/**
+ *
+ * @param array $item
+ * @param string $text
+ *
+ * @return string
+ */
+function api_format_items_embeded_images($item, $text)
 {
        $text = preg_replace_callback(
-               "|data:image/([^;]+)[^=]+=*|m",
-               function ($match) use ($item) {
-                       return System::baseUrl()."/display/".$item['guid'];
+               '|data:image/([^;]+)[^=]+=*|m',
+               function () use ($item) {
+                       return System::baseUrl() . '/display/' . $item['guid'];
                },
                $text
        );
        return $text;
 }
 
-
 /**
  * @brief return <a href='url'>name</a> as array
  *
  * @param string $txt text
  * @return array
- *                     name => 'name'
+ *                     'name' => 'name',
  *                     'url => 'url'
  */
 function api_contactlink_to_array($txt)
@@ -2677,8 +2944,10 @@ function api_contactlink_to_array($txt)
  * @brief return likes, dislikes and attend status for item
  *
  * @param array $item array
+ * @param string $type Return type (atom, rss, xml, json)
+ *
  * @return array
- *                     likes => int count
+ *                     likes => int count,
  *                     dislikes => int count
  */
 function api_format_items_activities(&$item, $type = "json")
@@ -2734,8 +3003,9 @@ function api_format_items_activities(&$item, $type = "json")
                        $xml_activities["friendica:".$k] = $v;
                        // add user data into xml output
                        $k_user = 0;
-                       foreach ($v as $user)
+                       foreach ($v as $user) {
                                $xml_activities["friendica:".$k][$k_user++.":user"] = $user;
+                       }
                }
                $activities = $xml_activities;
        }
@@ -2747,62 +3017,62 @@ function api_format_items_activities(&$item, $type = "json")
 /**
  * @brief return data from profiles
  *
- * @param array  $profile array containing data from db table 'profile'
- * @param string $type    Known types are 'atom', 'rss', 'xml' and 'json'
+ * @param array  $profile_row array containing data from db table 'profile'
  * @return array
  */
-function api_format_items_profiles(&$profile = null, $type = "json")
+function api_format_items_profiles($profile_row)
 {
-       if ($profile != null) {
-               $profile = array('profile_id' => $profile['id'],
-                                               'profile_name' => $profile['profile-name'],
-                                               'is_default' => $profile['is-default'] ? true : false,
-                                               'hide_friends'=> $profile['hide-friends'] ? true : false,
-                                               'profile_photo' => $profile['photo'],
-                                               'profile_thumb' => $profile['thumb'],
-                                               'publish' => $profile['publish'] ? true : false,
-                                               'net_publish' => $profile['net-publish'] ? true : false,
-                                               'description' => $profile['pdesc'],
-                                               'date_of_birth' => $profile['dob'],
-                                               'address' => $profile['address'],
-                                               'city' => $profile['locality'],
-                                               'region' => $profile['region'],
-                                               'postal_code' => $profile['postal-code'],
-                                               'country' => $profile['country-name'],
-                                               'hometown' => $profile['hometown'],
-                                               'gender' => $profile['gender'],
-                                               'marital' => $profile['marital'],
-                                               'marital_with' => $profile['with'],
-                                               'marital_since' => $profile['howlong'],
-                                               'sexual' => $profile['sexual'],
-                                               'politic' => $profile['politic'],
-                                               'religion' => $profile['religion'],
-                                               'public_keywords' => $profile['pub_keywords'],
-                                               'private_keywords' => $profile['prv_keywords'],
-                                               'likes' => bbcode(api_clean_plain_items($profile['likes']), false, false, 2, false),
-                                               'dislikes' => bbcode(api_clean_plain_items($profile['dislikes']), false, false, 2, false),
-                                               'about' => bbcode(api_clean_plain_items($profile['about']), false, false, 2, false),
-                                               'music' => bbcode(api_clean_plain_items($profile['music']), false, false, 2, false),
-                                               'book' => bbcode(api_clean_plain_items($profile['book']), false, false, 2, false),
-                                               'tv' => bbcode(api_clean_plain_items($profile['tv']), false, false, 2, false),
-                                               'film' => bbcode(api_clean_plain_items($profile['film']), false, false, 2, false),
-                                               'interest' => bbcode(api_clean_plain_items($profile['interest']), false, false, 2, false),
-                                               'romance' => bbcode(api_clean_plain_items($profile['romance']), false, false, 2, false),
-                                               'work' => bbcode(api_clean_plain_items($profile['work']), false, false, 2, false),
-                                               'education' => bbcode(api_clean_plain_items($profile['education']), false, false, 2, false),
-                                               'social_networks' => bbcode(api_clean_plain_items($profile['contact']), false, false, 2, false),
-                                               'homepage' => $profile['homepage'],
-                                               'users' => null);
-               return $profile;
-       }
+       $profile = array(
+               'profile_id'       => $profile_row['id'],
+               'profile_name'     => $profile_row['profile-name'],
+               'is_default'       => $profile_row['is-default'] ? true : false,
+               'hide_friends'     => $profile_row['hide-friends'] ? true : false,
+               'profile_photo'    => $profile_row['photo'],
+               'profile_thumb'    => $profile_row['thumb'],
+               'publish'          => $profile_row['publish'] ? true : false,
+               'net_publish'      => $profile_row['net-publish'] ? true : false,
+               'description'      => $profile_row['pdesc'],
+               'date_of_birth'    => $profile_row['dob'],
+               'address'          => $profile_row['address'],
+               'city'             => $profile_row['locality'],
+               'region'           => $profile_row['region'],
+               'postal_code'      => $profile_row['postal-code'],
+               'country'          => $profile_row['country-name'],
+               'hometown'         => $profile_row['hometown'],
+               'gender'           => $profile_row['gender'],
+               'marital'          => $profile_row['marital'],
+               'marital_with'     => $profile_row['with'],
+               'marital_since'    => $profile_row['howlong'],
+               'sexual'           => $profile_row['sexual'],
+               'politic'          => $profile_row['politic'],
+               'religion'         => $profile_row['religion'],
+               'public_keywords'  => $profile_row['pub_keywords'],
+               'private_keywords' => $profile_row['prv_keywords'],
+               'likes'            => bbcode(api_clean_plain_items($profile_row['likes'])    , false, false, 2, false),
+               'dislikes'         => bbcode(api_clean_plain_items($profile_row['dislikes']) , false, false, 2, false),
+               'about'            => bbcode(api_clean_plain_items($profile_row['about'])    , false, false, 2, false),
+               'music'            => bbcode(api_clean_plain_items($profile_row['music'])    , false, false, 2, false),
+               'book'             => bbcode(api_clean_plain_items($profile_row['book'])     , false, false, 2, false),
+               'tv'               => bbcode(api_clean_plain_items($profile_row['tv'])       , false, false, 2, false),
+               'film'             => bbcode(api_clean_plain_items($profile_row['film'])     , false, false, 2, false),
+               'interest'         => bbcode(api_clean_plain_items($profile_row['interest']) , false, false, 2, false),
+               'romance'          => bbcode(api_clean_plain_items($profile_row['romance'])  , false, false, 2, false),
+               'work'             => bbcode(api_clean_plain_items($profile_row['work'])     , false, false, 2, false),
+               'education'        => bbcode(api_clean_plain_items($profile_row['education']), false, false, 2, false),
+               'social_networks'  => bbcode(api_clean_plain_items($profile_row['contact'])  , false, false, 2, false),
+               'homepage'         => $profile_row['homepage'],
+               'users'            => null
+       );
+       return $profile;
 }
 
 /**
  * @brief format items to be returned by api
  *
- * @param array $r array of items
- * @param array $user_info
- * @param bool $filter_user filter items by $user_info
+ * @param array  $r array of items
+ * @param array  $user_info
+ * @param bool   $filter_user filter items by $user_info
+ * @param string $type Return type (atom, rss, xml, json)
  */
 function api_format_items($r, $user_info, $filter_user = false, $type = "json")
 {
@@ -2861,9 +3131,9 @@ function api_format_items($r, $user_info, $filter_user = false, $type = "json")
                }
 
                if (($item['item_network'] != "") && ($status["source"] == 'web')) {
-                       $status["source"] = network_to_name($item['item_network'], $user_info['url']);
-               } elseif (($item['item_network'] != "") && (network_to_name($item['item_network'], $user_info['url']) != $status["source"])) {
-                       $status["source"] = trim($status["source"].' ('.network_to_name($item['item_network'], $user_info['url']).')');
+                       $status["source"] = ContactSelector::networkToName($item['item_network'], $user_info['url']);
+               } elseif (($item['item_network'] != "") && (ContactSelector::networkToName($item['item_network'], $user_info['url']) != $status["source"])) {
+                       $status["source"] = trim($status["source"].' ('.ContactSelector::networkToName($item['item_network'], $user_info['url']).')');
                }
 
 
@@ -2903,12 +3173,13 @@ function api_format_items($r, $user_info, $filter_user = false, $type = "json")
                if ($item["coord"] != "") {
                        $coords = explode(' ', $item["coord"]);
                        if (count($coords) == 2) {
-                               if ($type == "json")
+                               if ($type == "json") {
                                        $status["geo"] = array('type' => 'Point',
                                                        '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
+                               } else {// Not sure if this is the official format - if someone founds a documentation we can check
                                        $status["georss:point"] = $item["coord"];
+                               }
                        }
                }
                $ret[] = $status;
@@ -2916,6 +3187,13 @@ function api_format_items($r, $user_info, $filter_user = false, $type = "json")
        return $ret;
 }
 
+/**
+ * Returns the remaining number of API requests available to the user before the API limit is reached.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_account_rate_limit_status($type)
 {
        if ($type == "xml") {
@@ -2941,9 +3219,16 @@ function api_account_rate_limit_status($type)
        return api_format_data('hash', $type, array('hash' => $hash));
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/account/rate_limit_status', 'api_account_rate_limit_status', true);
 
+/**
+ * Returns the string "ok" in the requested format with a 200 OK HTTP status code.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_help_test($type)
 {
        if ($type == 'xml') {
@@ -2955,9 +3240,15 @@ function api_help_test($type)
        return api_format_data('ok', $type, array("ok" => $ok));
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/help/test', 'api_help_test', false);
 
+/**
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_lists($type)
 {
        $ret = array();
@@ -2965,9 +3256,17 @@ function api_lists($type)
        return api_format_data('lists', $type, array("lists_list" => $ret));
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/lists', 'api_lists', true);
 
+/**
+ * Returns all lists the user subscribes to.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/get-lists-list
+ */
 function api_lists_list($type)
 {
        $ret = array();
@@ -2975,15 +3274,15 @@ function api_lists_list($type)
        return api_format_data('lists', $type, array("lists_list" => $ret));
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/lists/list', 'api_lists_list', true);
 
 /**
- * @brief Returns either the friends of the follower list
- *
- * Note: Considers friends and followers lists to be private and won't return
+ * Considers friends and followers lists to be private and won't return
  * anything if any user_id parameter is passed.
  *
+ * @brief Returns either the friends of the follower list
+ *
  * @param string $qtype Either "friends" or "followers"
  * @return boolean|array
  * @throws ForbiddenException
@@ -3017,10 +3316,10 @@ function api_statuses_f($qtype)
                return false;
        }
 
+       $sql_extra = '';
        if ($qtype == 'friends') {
                $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
-       }
-       if ($qtype == 'followers') {
+       } elseif ($qtype == 'followers') {
                $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
        }
 
@@ -3029,12 +3328,20 @@ function api_statuses_f($qtype)
                $sql_extra = " AND false ";
        }
 
+       if ($qtype == 'blocks') {
+               $sql_filter = 'AND `blocked` AND NOT `pending`';
+       } elseif ($qtype == 'incoming') {
+               $sql_filter = 'AND `pending`';
+       } else {
+               $sql_filter = 'AND (NOT `blocked` OR `pending`)';
+       }
+
        $r = q(
                "SELECT `nurl`
                FROM `contact`
                WHERE `uid` = %d
                AND NOT `self`
-               AND (NOT `blocked` OR `pending`)
+               $sql_filter
                $sql_extra
                ORDER BY `nick`
                LIMIT %d, %d",
@@ -3060,6 +3367,8 @@ function api_statuses_f($qtype)
 
 
 /**
+ * Returns the user's friends.
+ *
  * @brief Returns the list of friends of the provided user
  *
  * @deprecated By Twitter API in favor of friends/list
@@ -3077,7 +3386,9 @@ function api_statuses_friends($type)
 }
 
 /**
- * @brief Returns the list of friends of the provided user
+ * Returns the user's followers.
+ *
+ * @brief Returns the list of followers of the provided user
  *
  * @deprecated By Twitter API in favor of friends/list
  *
@@ -3097,6 +3408,61 @@ function api_statuses_followers($type)
 api_register_func('api/statuses/friends', 'api_statuses_friends', true);
 api_register_func('api/statuses/followers', 'api_statuses_followers', true);
 
+/**
+ * Returns the list of blocked users
+ *
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/mute-block-report-users/api-reference/get-blocks-list
+ *
+ * @param string $type Either "json" or "xml"
+ *
+ * @return boolean|string|array
+ */
+function api_blocks_list($type)
+{
+       $data =  api_statuses_f('blocks');
+       if ($data === false) {
+               return false;
+       }
+       return api_format_data("users", $type, $data);
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/blocks/list', 'api_blocks_list', true);
+
+/**
+ * Returns the list of pending users IDs
+ *
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friendships-incoming
+ *
+ * @param string $type Either "json" or "xml"
+ *
+ * @return boolean|string|array
+ */
+function api_friendships_incoming($type)
+{
+       $data =  api_statuses_f('incoming');
+       if ($data === false) {
+               return false;
+       }
+
+       $ids = array();
+       foreach ($data['user'] as $user) {
+               $ids[] = $user['id'];
+       }
+
+       return api_format_data("ids", $type, array('id' => $ids));
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/friendships/incoming', 'api_friendships_incoming', true);
+
+/**
+ * Returns the instance's configuration information.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_statusnet_config($type)
 {
        $a = get_app();
@@ -3109,7 +3475,7 @@ function api_statusnet_config($type)
        $private = ((Config::get('system', 'block_public')) ? 'true' : 'false');
        $textlimit = (string) (($a->config['max_import_size']) ? $a->config['max_import_size'] : 200000);
        if ($a->config['api_import_size']) {
-               $texlimit = string($a->config['api_import_size']);
+               $textlimit = (string) $a->config['api_import_size'];
        }
        $ssl = ((Config::get('system', 'have_ssl')) ? 'true' : 'false');
        $sslserver = (($ssl === 'true') ? str_replace('http:', 'https:', System::baseUrl()) : '');
@@ -3136,6 +3502,12 @@ function api_statusnet_config($type)
 api_register_func('api/gnusocial/config', 'api_statusnet_config', false);
 api_register_func('api/statusnet/config', 'api_statusnet_config', false);
 
+/**
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_statusnet_version($type)
 {
        // liar
@@ -3149,30 +3521,20 @@ api_register_func('api/gnusocial/version', 'api_statusnet_version', false);
 api_register_func('api/statusnet/version', 'api_statusnet_version', false);
 
 /**
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
  * @todo use api_format_data() to return data
  */
-function api_ff_ids($type,$qtype)
+function api_ff_ids($type)
 {
-       $a = get_app();
-
        if (! api_user()) {
                throw new ForbiddenException();
        }
 
-       $user_info = api_get_user($a);
+       api_get_user($a);
 
-       if ($qtype == 'friends') {
-               $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_SHARING), intval(CONTACT_IS_FRIEND));
-       }
-       if ($qtype == 'followers') {
-               $sql_extra = sprintf(" AND ( `rel` = %d OR `rel` = %d ) ", intval(CONTACT_IS_FOLLOWER), intval(CONTACT_IS_FRIEND));
-       }
-
-       if (!$user_info["self"]) {
-               $sql_extra = " AND false ";
-       }
-
-       $stringify_ids = (x($_REQUEST, 'stringify_ids') ? $_REQUEST['stringify_ids'] : false);
+       $stringify_ids = defaults($_REQUEST, 'stringify_ids', false);
 
        $r = q(
                "SELECT `pcontact`.`id` FROM `contact`
@@ -3180,7 +3542,6 @@ function api_ff_ids($type,$qtype)
                        WHERE `contact`.`uid` = %s AND NOT `contact`.`self`",
                intval(api_user())
        );
-
        if (!DBM::is_result($r)) {
                return;
        }
@@ -3197,28 +3558,56 @@ function api_ff_ids($type,$qtype)
        return api_format_data("ids", $type, array('id' => $ids));
 }
 
+/**
+ * Returns the ID of every user the user is following.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-friends-ids
+ */
 function api_friends_ids($type)
 {
-       return api_ff_ids($type, 'friends');
+       return api_ff_ids($type);
 }
 
+/**
+ * Returns the ID of every user following the user.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-followers-ids
+ */
 function api_followers_ids($type)
 {
-       return api_ff_ids($type, 'followers');
+       return api_ff_ids($type);
 }
 
 /// @TODO move to top of file or somewhere better
 api_register_func('api/friends/ids', 'api_friends_ids', true);
 api_register_func('api/followers/ids', 'api_followers_ids', true);
 
+/**
+ * Sends a new direct message.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ * @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/new-message
+ */
 function api_direct_messages_new($type)
 {
 
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
 
-       if (!x($_POST, "text") || (!x($_POST, "screen_name") && !x($_POST, "user_id"))) return;
+       if (!x($_POST, "text") || (!x($_POST, "screen_name") && !x($_POST, "user_id"))) {
+               return;
+       }
 
        $sender = api_get_user($a);
 
@@ -3273,17 +3662,19 @@ function api_direct_messages_new($type)
        }
 
        return api_format_data("direct-messages", $type, $data);
-
 }
 
 /// @TODO move to top of file or somewhere better
 api_register_func('api/direct_messages/new', 'api_direct_messages_new', true, API_METHOD_POST);
 
 /**
+ * Destroys a direct message.
+ *
  * @brief delete a direct_message from mail table through api
  *
  * @param string $type Known types are 'atom', 'rss', 'xml' and 'json'
  * @return string
+ * @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/delete-message
  */
 function api_direct_messages_destroy($type)
 {
@@ -3352,12 +3743,19 @@ function api_direct_messages_destroy($type)
                }
        }
        /// @todo return JSON data like Twitter API not yet implemented
-
 }
 
 /// @TODO move to top of file or somewhere better
 api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', true, API_METHOD_DELETE);
 
+/**
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ * @param string $box
+ * @param string $verbose
+ *
+ * @return array|string
+ */
 function api_direct_messages_box($type, $box, $verbose)
 {
        $a = get_app();
@@ -3449,24 +3847,52 @@ function api_direct_messages_box($type, $box, $verbose)
        return api_format_data("direct-messages", $type, $data);
 }
 
+/**
+ * Returns the most recent direct messages sent by the user.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ * @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/get-sent-message
+ */
 function api_direct_messages_sentbox($type)
 {
        $verbose = (x($_GET, 'friendica_verbose') ? strtolower($_GET['friendica_verbose']) : "false");
        return api_direct_messages_box($type, "sentbox", $verbose);
 }
 
+/**
+ * Returns the most recent direct messages sent to the user.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ * @see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/get-messages
+ */
 function api_direct_messages_inbox($type)
 {
        $verbose = (x($_GET, 'friendica_verbose') ? strtolower($_GET['friendica_verbose']) : "false");
        return api_direct_messages_box($type, "inbox", $verbose);
 }
 
+/**
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_direct_messages_all($type)
 {
        $verbose = (x($_GET, 'friendica_verbose') ? strtolower($_GET['friendica_verbose']) : "false");
        return api_direct_messages_box($type, "all", $verbose);
 }
 
+/**
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_direct_messages_conversation($type)
 {
        $verbose = (x($_GET, 'friendica_verbose') ? strtolower($_GET['friendica_verbose']) : "false");
@@ -3479,7 +3905,12 @@ 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);
 
-function api_oauth_request_token($type)
+/**
+ * Returns an OAuth Request Token.
+ *
+ * @see https://oauth.net/core/1.0/#auth_step1
+ */
+function api_oauth_request_token()
 {
        $oauth1 = new FKOAuth1();
        try {
@@ -3492,7 +3923,13 @@ function api_oauth_request_token($type)
        killme();
 }
 
-function api_oauth_access_token($type)
+/**
+ * Returns an OAuth Access Token.
+ *
+ * @return array|string
+ * @see https://oauth.net/core/1.0/#auth_step3
+ */
+function api_oauth_access_token()
 {
        $oauth1 = new FKOAuth1();
        try {
@@ -3534,8 +3971,9 @@ function api_fr_photoalbum_delete($type)
                intval(api_user()),
                dbesc($album)
        );
-       if (!DBM::is_result($r))
+       if (!DBM::is_result($r)) {
                throw new BadRequestException("album not available");
+       }
 
        // function for setting the items to "deleted = 1" which ensures that comments, likes etc. are not shown anymore
        // to the user and the contacts of the users (drop_items() performs the federation of the deletion to other networks
@@ -3866,7 +4304,7 @@ function api_fr_photo_delete($type)
  * @brief returns the details of a specified photo id, if scale is given, returns the photo data in base 64
  *
  * @param string $type Known types are 'atom', 'rss', 'xml' and 'json'
- * @return string
+ * @return string|array
  */
 function api_fr_photo_detail($type)
 {
@@ -3888,10 +4326,14 @@ function api_fr_photo_detail($type)
 
 
 /**
+ * Updates the user’s profile image.
+ *
  * @brief updates the profile image for the user (either a specified profile or the default profile)
  *
  * @param string $type Known types are 'atom', 'rss', 'xml' and 'json'
+ *
  * @return string
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/post-account-update_profile_image
  */
 function api_account_update_profile_image($type)
 {
@@ -3899,7 +4341,7 @@ function api_account_update_profile_image($type)
                throw new ForbiddenException();
        }
        // input params
-       $profileid = (x($_REQUEST, 'profile_id') ? $_REQUEST['profile_id'] : 0);
+       $profileid = defaults($_REQUEST, 'profile_id', 0);
 
        // error if image data is missing
        if (!x($_FILES, 'image')) {
@@ -3945,13 +4387,13 @@ function api_account_update_profile_image($type)
        }
        // change specified profile or all profiles to the new resource-id
        if ($is_default_profile) {
-               $r = q(
+               q(
                        "UPDATE `photo` SET `profile` = 0 WHERE `profile` = 1 AND `resource-id` != '%s' AND `uid` = %d",
                        dbesc($data['photo']['id']),
                        intval(local_user())
                );
 
-               $r = q(
+               q(
                        "UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s'  WHERE `self` AND `uid` = %d",
                        dbesc(System::baseUrl() . '/photo/' . $data['photo']['id'] . '-4.' . $fileext),
                        dbesc(System::baseUrl() . '/photo/' . $data['photo']['id'] . '-5.' . $fileext),
@@ -3959,7 +4401,7 @@ function api_account_update_profile_image($type)
                        intval(local_user())
                );
        } else {
-               $r = q(
+               q(
                        "UPDATE `profile` SET `photo` = '%s', `thumb` = '%s' WHERE `id` = %d AND `uid` = %d",
                        dbesc(System::baseUrl() . '/photo/' . $data['photo']['id'] . '-4.' . $filetype),
                        dbesc(System::baseUrl() . '/photo/' . $data['photo']['id'] . '-5.' . $filetype),
@@ -3971,7 +4413,7 @@ function api_account_update_profile_image($type)
        // we'll set the updated profile-photo timestamp even if it isn't the default profile,
        // so that browsers will do a cache update unconditionally
 
-       $r = q(
+       q(
                "UPDATE `contact` SET `avatar-date` = '%s' WHERE `self` = 1 AND `uid` = %d",
                dbesc(datetime_convert()),
                intval(local_user())
@@ -4005,7 +4447,47 @@ api_register_func('api/friendica/photo/delete', 'api_fr_photo_delete', true, API
 api_register_func('api/friendica/photo', 'api_fr_photo_detail', true);
 api_register_func('api/account/update_profile_image', 'api_account_update_profile_image', true, API_METHOD_POST);
 
+/**
+ * Update user profile
+ *
+ * @param string $type Known types are 'atom', 'rss', 'xml' and 'json'
+ *
+ * @return array|string
+ */
+function api_account_update_profile($type)
+{
+       $local_user = api_user();
+       $api_user = api_get_user(get_app());
 
+       if (!empty($_POST['name'])) {
+               dba::update('profile', ['name' => $_POST['name']], ['uid' => $local_user]);
+               dba::update('user', ['username' => $_POST['name']], ['uid' => $local_user]);
+               dba::update('contact', ['name' => $_POST['name']], ['uid' => $local_user, 'self' => 1]);
+               dba::update('contact', ['name' => $_POST['name']], ['id' => $api_user['id']]);
+       }
+
+       if (isset($_POST['description'])) {
+               dba::update('profile', ['about' => $_POST['description']], ['uid' => $local_user]);
+               dba::update('contact', ['about' => $_POST['description']], ['uid' => $local_user, 'self' => 1]);
+               dba::update('contact', ['about' => $_POST['description']], ['id' => $api_user['id']]);
+       }
+
+       Worker::add(PRIORITY_LOW, 'ProfileUpdate', $local_user);
+       // Update global directory in background
+       if ($api_user['url'] && strlen(Config::get('system', 'directory'))) {
+               Worker::add(PRIORITY_LOW, "Directory", $api_user['url']);
+       }
+
+       return api_account_verify_credentials($type);
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/account/update_profile', 'api_account_update_profile', true, API_METHOD_POST);
+
+/**
+ *
+ * @param string $acl_string
+ */
 function check_acl_input($acl_string)
 {
        if ($acl_string == null || $acl_string == " ") {
@@ -4031,6 +4513,21 @@ function check_acl_input($acl_string)
        return $contact_not_found;
 }
 
+/**
+ *
+ * @param string  $mediatype
+ * @param array   $media
+ * @param string  $type
+ * @param string  $album
+ * @param string  $allow_cid
+ * @param string  $deny_cid
+ * @param string  $allow_gid
+ * @param string  $deny_gid
+ * @param string  $desc
+ * @param integer $profile
+ * @param boolean $visibility
+ * @param string  $photo_id
+ */
 function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $desc, $profile = 0, $visibility = false, $photo_id = null)
 {
        $visitor   = 0;
@@ -4071,7 +4568,8 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
        }
        logger(
                "File upload src: " . $src . " - filename: " . $filename .
-               " - size: " . $filesize . " - type: " . $filetype, LOGGER_DEBUG
+               " - size: " . $filesize . " - type: " . $filetype,
+               LOGGER_DEBUG
        );
 
        // check if there was a php upload error
@@ -4080,7 +4578,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
        }
        // check against max upload size within Friendica instance
        $maximagesize = Config::get('system', 'maximagesize');
-       if (($maximagesize) && ($filesize > $maximagesize)) {
+       if ($maximagesize && ($filesize > $maximagesize)) {
                $formattedBytes = formatBytes($maximagesize);
                throw new InternalServerErrorException("image size exceeds Friendica config setting (uploaded size: $formattedBytes)");
        }
@@ -4178,6 +4676,16 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
        }
 }
 
+/**
+ *
+ * @param string  $hash
+ * @param string  $allow_cid
+ * @param string  $deny_cid
+ * @param string  $allow_gid
+ * @param string  $deny_gid
+ * @param string  $filetype
+ * @param boolean $visibility
+ */
 function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $filetype, $visibility = false)
 {
        // get data about the api authenticated user
@@ -4223,6 +4731,14 @@ function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $f
        item_store($arr);
 }
 
+/**
+ *
+ * @param string $type
+ * @param int    $scale
+ * @param string $photo_id
+ *
+ * @return array
+ */
 function prepare_photo_data($type, $scale, $photo_id)
 {
        $scale_sql = ($scale === false ? "" : sprintf("AND scale=%d", intval($scale)));
@@ -4343,8 +4859,8 @@ function prepare_photo_data($type, $scale, $photo_id)
  */
 function api_friendica_remoteauth()
 {
-       $url = ((x($_GET, 'url')) ? $_GET['url'] : '');
-       $c_url = ((x($_GET, 'c_url')) ? $_GET['c_url'] : '');
+       $url = (x($_GET, 'url') ? $_GET['url'] : '');
+       $c_url = (x($_GET, 'c_url') ? $_GET['c_url'] : '');
 
        if ($url === '' || $c_url === '') {
                throw new BadRequestException("Wrong parameters.");
@@ -4354,26 +4870,22 @@ function api_friendica_remoteauth()
 
        // traditional DFRN
 
-       $r = q(
-               "SELECT * FROM `contact` WHERE `id` = %d AND `nurl` = '%s' LIMIT 1",
-               dbesc($c_url),
-               intval(api_user())
-       );
+       $r = dba::selectOne('contact', [], ['uid' => api_user(), 'nurl' => $c_url]);
 
-       if ((! DBM::is_result($r)) || ($r[0]['network'] !== NETWORK_DFRN)) {
+       if (!DBM::is_result($r) || ($r['network'] !== NETWORK_DFRN)) {
                throw new BadRequestException("Unknown contact");
        }
 
-       $cid = $r[0]['id'];
+       $cid = $r['id'];
 
-       $dfrn_id = $orig_id = (($r[0]['issued-id']) ? $r[0]['issued-id'] : $r[0]['dfrn-id']);
+       $dfrn_id = defaults($r, 'issued-id', $r['dfrn-id']);
 
-       if ($r[0]['duplex'] && $r[0]['issued-id']) {
-               $orig_id = $r[0]['issued-id'];
+       if ($r['duplex'] && $r['issued-id']) {
+               $orig_id = $r['issued-id'];
                $dfrn_id = '1:' . $orig_id;
        }
-       if ($r[0]['duplex'] && $r[0]['dfrn-id']) {
-               $orig_id = $r[0]['dfrn-id'];
+       if ($r['duplex'] && $r['dfrn-id']) {
+               $orig_id = $r['dfrn-id'];
                $dfrn_id = '0:' . $orig_id;
        }
 
@@ -4389,10 +4901,10 @@ function api_friendica_remoteauth()
                intval(time() + 45)
        );
 
-       logger($r[0]['name'] . ' ' . $sec, LOGGER_DEBUG);
-       $dest = (($url) ? '&destination_url=' . $url : '');
+       logger($r['name'] . ' ' . $sec, LOGGER_DEBUG);
+       $dest = ($url ? '&destination_url=' . $url : '');
        goaway(
-               $r[0]['poll'] . '?dfrn_id=' . $dfrn_id
+               $r['poll'] . '?dfrn_id=' . $dfrn_id
                . '&dfrn_version=' . DFRN_PROTOCOL_VERSION
                . '&type=profile&sec=' . $sec . $dest . $quiet
        );
@@ -4403,7 +4915,7 @@ api_register_func('api/friendica/remoteauth', 'api_friendica_remoteauth', true);
  * @brief Return the item shared, if the item contains only the [share] tag
  *
  * @param array $item Sharer item
- * @return array Shared item or false if not a reshare
+ * @return array|false Shared item or false if not a reshare
  */
 function api_share_as_retweet(&$item)
 {
@@ -4474,8 +4986,9 @@ function api_share_as_retweet(&$item)
 
        $posted = "";
        preg_match("/posted='(.*?)'/ism", $attributes, $matches);
-       if ($matches[1] != "")
+       if ($matches[1] != "") {
                $posted = $matches[1];
+       }
 
        preg_match('/posted="(.*?)"/ism', $attributes, $matches);
        if ($matches[1] != "") {
@@ -4497,16 +5010,18 @@ function api_share_as_retweet(&$item)
        $reshared_item["edited"] = $posted;
 
        return $reshared_item;
-
 }
 
+/**
+ *
+ * @param string $profile
+ *
+ * @return string|false
+ * @todo remove trailing junk from profile url
+ * @todo pump.io check has to check the website
+ */
 function api_get_nick($profile)
 {
-       /* To-Do:
-               - remove trailing junk from profile url
-               - pump.io check has to check the website
-       */
-
        $nick = "";
 
        $r = q(
@@ -4581,6 +5096,12 @@ function api_get_nick($profile)
        return false;
 }
 
+/**
+ *
+ * @param array $item
+ *
+ * @return array
+ */
 function api_in_reply_to($item)
 {
        $in_reply_to = array();
@@ -4592,9 +5113,11 @@ function api_in_reply_to($item)
        $in_reply_to['screen_name'] = null;
 
        if (($item['thr-parent'] != $item['uri']) && (intval($item['parent']) != intval($item['id']))) {
-               $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
+               $r = q(
+                       "SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1",
                        intval($item['uid']),
-                       dbesc($item['thr-parent']));
+                       dbesc($item['thr-parent'])
+               );
 
                if (DBM::is_result($r)) {
                        $in_reply_to['status_id'] = intval($r[0]['id']);
@@ -4604,7 +5127,8 @@ function api_in_reply_to($item)
 
                $in_reply_to['status_id_str'] = (string) intval($in_reply_to['status_id']);
 
-               $r = q("SELECT `contact`.`nick`, `contact`.`name`, `contact`.`id`, `contact`.`url` FROM item
+               $r = q(
+                       "SELECT `contact`.`nick`, `contact`.`name`, `contact`.`id`, `contact`.`url` FROM item
                        STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`author-id`
                        WHERE `item`.`id` = %d LIMIT 1",
                        intval($in_reply_to['status_id'])
@@ -4636,6 +5160,12 @@ function api_in_reply_to($item)
        return $in_reply_to;
 }
 
+/**
+ *
+ * @param string $Text
+ *
+ * @return string
+ */
 function api_clean_plain_items($Text)
 {
        $include_entities = strtolower(x($_REQUEST, 'include_entities') ? $_REQUEST['include_entities'] : "false");
@@ -4666,62 +5196,85 @@ function api_clean_attachments($body)
 {
        $data = get_attachment_data($body);
 
-       if (!$data)
+       if (!$data) {
                return $body;
-
+       }
        $body = "";
 
-       if (isset($data["text"]))
+       if (isset($data["text"])) {
                $body = $data["text"];
-
-       if (($body == "") && (isset($data["title"])))
+       }
+       if (($body == "") && isset($data["title"])) {
                $body = $data["title"];
-
-       if (isset($data["url"]))
+       }
+       if (isset($data["url"])) {
                $body .= "\n".$data["url"];
-
+       }
        $body .= $data["after"];
 
        return $body;
 }
 
+/**
+ *
+ * @param array $contacts
+ *
+ * @return array
+ */
 function api_best_nickname(&$contacts)
 {
        $best_contact = array();
 
-       if (count($contact) == 0)
+       if (count($contact) == 0) {
                return;
+       }
 
-       foreach ($contacts as $contact)
+       foreach ($contacts as $contact) {
                if ($contact["network"] == "") {
                        $contact["network"] = "dfrn";
                        $best_contact = array($contact);
                }
+       }
 
-       if (sizeof($best_contact) == 0)
-               foreach ($contacts as $contact)
-                       if ($contact["network"] == "dfrn")
+       if (sizeof($best_contact) == 0) {
+               foreach ($contacts as $contact) {
+                       if ($contact["network"] == "dfrn") {
                                $best_contact = array($contact);
+                       }
+               }
+       }
 
-       if (sizeof($best_contact) == 0)
-               foreach ($contacts as $contact)
-                       if ($contact["network"] == "dspr")
+       if (sizeof($best_contact) == 0) {
+               foreach ($contacts as $contact) {
+                       if ($contact["network"] == "dspr") {
                                $best_contact = array($contact);
+                       }
+               }
+       }
 
-       if (sizeof($best_contact) == 0)
-               foreach ($contacts as $contact)
-                       if ($contact["network"] == "stat")
+       if (sizeof($best_contact) == 0) {
+               foreach ($contacts as $contact) {
+                       if ($contact["network"] == "stat") {
                                $best_contact = array($contact);
+                       }
+               }
+       }
 
-       if (sizeof($best_contact) == 0)
-               foreach ($contacts as $contact)
-                       if ($contact["network"] == "pump")
+       if (sizeof($best_contact) == 0) {
+               foreach ($contacts as $contact) {
+                       if ($contact["network"] == "pump") {
                                $best_contact = array($contact);
+                       }
+               }
+       }
 
-       if (sizeof($best_contact) == 0)
-               foreach ($contacts as $contact)
-                       if ($contact["network"] == "twit")
+       if (sizeof($best_contact) == 0) {
+               foreach ($contacts as $contact) {
+                       if ($contact["network"] == "twit") {
                                $best_contact = array($contact);
+                       }
+               }
+       }
 
        if (sizeof($best_contact) == 1) {
                $contacts = $best_contact;
@@ -4730,12 +5283,20 @@ function api_best_nickname(&$contacts)
        }
 }
 
-// return all or a specified group of the user with the containing contacts
+/**
+ * Return all or a specified group of the user with the containing contacts.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_friendica_group_show($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
 
        // params
        $user_info = api_get_user($a);
@@ -4750,8 +5311,9 @@ function api_friendica_group_show($type)
                        intval($gid)
                );
                // error message if specified gid is not in database
-               if (!DBM::is_result($r))
+               if (!DBM::is_result($r)) {
                        throw new BadRequestException("gid not available");
+               }
        } else {
                $r = q(
                        "SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d",
@@ -4785,7 +5347,13 @@ function api_friendica_group_show($type)
 api_register_func('api/friendica/group_show', 'api_friendica_group_show', true);
 
 
-// delete the specified group of the user
+/**
+ * Delete the specified group of the user.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_friendica_group_delete($type)
 {
        $a = get_app();
@@ -4841,12 +5409,20 @@ function api_friendica_group_delete($type)
 api_register_func('api/friendica/group_delete', 'api_friendica_group_delete', true, API_METHOD_DELETE);
 
 
-// create the specified group with the posted array of contacts
+/**
+ * Create the specified group with the posted array of contacts.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_friendica_group_create($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
 
        // params
        $user_info = api_get_user($a);
@@ -4856,8 +5432,9 @@ function api_friendica_group_create($type)
        $users = $json['user'];
 
        // error if no name specified
-       if ($name == "")
+       if ($name == "") {
                throw new BadRequestException('group name not specified');
+       }
 
        // get data of the specified group name
        $rname = q(
@@ -4866,8 +5443,9 @@ function api_friendica_group_create($type)
                dbesc($name)
        );
        // error message if specified group name already exists
-       if (DBM::is_result($rname))
+       if (DBM::is_result($rname)) {
                throw new BadRequestException('group name already exists');
+       }
 
        // check if specified group name is a deleted group
        $rname = q(
@@ -4876,8 +5454,9 @@ function api_friendica_group_create($type)
                dbesc($name)
        );
        // error message if specified group name already exists
-       if (DBM::is_result($rname))
+       if (DBM::is_result($rname)) {
                $reactivate_group = true;
+       }
 
        // create group
        $ret = Group::create($uid, $name);
@@ -4898,9 +5477,9 @@ function api_friendica_group_create($type)
                        intval($cid),
                        intval($uid)
                );
-               if (count($contact))
-                       $result = Group::addMember($gid, $cid);
-               else {
+               if (count($contact)) {
+                       Group::addMember($gid, $cid);
+               else {
                        $erroraddinguser = true;
                        $errorusers[] = $cid;
                }
@@ -4914,12 +5493,20 @@ function api_friendica_group_create($type)
 api_register_func('api/friendica/group_create', 'api_friendica_group_create', true, API_METHOD_POST);
 
 
-// update the specified group with the posted array of contacts
+/**
+ * Update the specified group with the posted array of contacts.
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_friendica_group_update($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
 
        // params
        $user_info = api_get_user($a);
@@ -4930,12 +5517,14 @@ function api_friendica_group_update($type)
        $users = $json['user'];
 
        // error if no name specified
-       if ($name == "")
+       if ($name == "") {
                throw new BadRequestException('group name not specified');
+       }
 
        // error if no gid specified
-       if ($gid == "")
+       if ($gid == "") {
                throw new BadRequestException('gid not specified');
+       }
 
        // remove members
        $members = Contact::getByGroupId($gid);
@@ -4945,7 +5534,7 @@ function api_friendica_group_update($type)
                        $found = ($user['cid'] == $cid ? true : false);
                }
                if (!$found) {
-                       $ret = Group::removeMemberByName($uid, $name, $cid);
+                       Group::removeMemberByName($uid, $name, $cid);
                }
        }
 
@@ -4962,7 +5551,7 @@ function api_friendica_group_update($type)
                );
 
                if (count($contact)) {
-                       $result = Group::addMember($gid, $cid);
+                       Group::addMember($gid, $cid);
                } else {
                        $erroraddinguser = true;
                        $errorusers[] = $cid;
@@ -4977,11 +5566,19 @@ function api_friendica_group_update($type)
 
 api_register_func('api/friendica/group_update', 'api_friendica_group_update', true, API_METHOD_POST);
 
+/**
+ *
+ * @param string $type Return type (atom, rss, xml, json)
+ *
+ * @return array|string
+ */
 function api_friendica_activity($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
        $verb = strtolower($a->argv[3]);
        $verb = preg_replace("|\..*$|", "", $verb);
 
@@ -5001,7 +5598,7 @@ function api_friendica_activity($type)
        }
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/friendica/activity/like', 'api_friendica_activity', true, API_METHOD_POST);
 api_register_func('api/friendica/activity/dislike', 'api_friendica_activity', true, API_METHOD_POST);
 api_register_func('api/friendica/activity/attendyes', 'api_friendica_activity', true, API_METHOD_POST);
@@ -5023,16 +5620,21 @@ function api_friendica_notification($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
-       if ($a->argc!==3) throw new BadRequestException("Invalid argument count");
+       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);
 
        if ($type == "xml") {
                $xmlnotes = array();
-               foreach ($notes as $note)
+               foreach ($notes as $note) {
                        $xmlnotes[] = array("@attributes" => $note);
+               }
 
                $notes = $xmlnotes;
        }
@@ -5041,10 +5643,10 @@ function api_friendica_notification($type)
 }
 
 /**
- * @brief Set notification as seen and returns associated item (if possible)
- *
  * POST request with 'id' param as notification id
  *
+ * @brief Set notification as seen and returns associated item (if possible)
+ *
  * @param string $type Known types are 'atom', 'rss', 'xml' and 'json'
  * @return string
  */
@@ -5052,14 +5654,20 @@ function api_friendica_notification_seen($type)
 {
        $a = get_app();
 
-       if (api_user() === false) throw new ForbiddenException();
-       if ($a->argc!==4) throw new BadRequestException("Invalid argument count");
+       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");
+       if (is_null($note)) {
+               throw new BadRequestException("Invalid argument");
+       }
 
        $nm->setSeen($note);
        if ($note['otype']=='item') {
@@ -5081,7 +5689,7 @@ function api_friendica_notification_seen($type)
        return api_format_data('result', $type, array('result' => "success"));
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 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);
 
@@ -5139,14 +5747,14 @@ function api_friendica_direct_messages_setseen($type)
        }
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/friendica/direct_messages_setseen', 'api_friendica_direct_messages_setseen', true);
 
 /**
  * @brief search for direct_messages containing a searchstring through api
  *
  * @param string $type Known types are 'atom', 'rss', 'xml' and 'json'
- * @return string (success: success=true if found and search_result contains found messages
+ * @return string (success: success=true if found and search_result contains found messages,
  *                          success=false if nothing was found, search_result='nothing found',
  *                error: result=error with error message)
  */
@@ -5202,7 +5810,7 @@ function api_friendica_direct_messages_search($type)
        return api_format_data("direct_message_search", $type, array('$result' => $success));
 }
 
-/// @TODO move to top of file or somwhere better
+/// @TODO move to top of file or somewhere better
 api_register_func('api/friendica/direct_messages_search', 'api_friendica_direct_messages_search', true);
 
 /**
@@ -5247,7 +5855,7 @@ function api_friendica_profile_show($type)
        // loop through all returned profiles and retrieve data and users
        $k = 0;
        foreach ($r as $rr) {
-               $profile = api_format_items_profiles($rr, $type);
+               $profile = api_format_items_profiles($rr);
 
                // select all users from contact table, loop and prepare standard return for user data
                $users = array();
@@ -5282,18 +5890,49 @@ function api_friendica_profile_show($type)
 }
 api_register_func('api/friendica/profile/show', 'api_friendica_profile_show', true, API_METHOD_GET);
 
+/**
+ * Returns a list of saved searches.
+ *
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/get-saved_searches-list
+ *
+ * @param  string $type Return format: json or xml
+ *
+ * @return string|array
+ */
+function api_saved_searches_list($type)
+{
+       $terms = dba::select('search', array('id', 'term'), array('uid' => local_user()));
+
+       $result = array();
+       while ($term = $terms->fetch()) {
+               $result[] = array(
+                       'name' => $term['term'],
+                       'query' => $term['term'],
+                       'id_str' => $term['id'],
+                       'id' => intval($term['id'])
+               );
+       }
+
+       dba::close($terms);
+
+       return api_format_data("terms", $type, array('terms' => $result));
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/saved_searches/list', 'api_saved_searches_list', true);
+
 /*
 @TODO Maybe open to implement?
 To.Do:
-    [pagename] => api/1.1/statuses/lookup.json
-    [id] => 605138389168451584
-    [include_cards] => true
-    [cards_platform] => Android-12
-    [include_entities] => true
-    [include_my_retweet] => 1
-    [include_rts] => 1
-    [include_reply_count] => true
-    [include_descendent_reply_count] => true
+       [pagename] => api/1.1/statuses/lookup.json
+       [id] => 605138389168451584
+       [include_cards] => true
+       [cards_platform] => Android-12
+       [include_entities] => true
+       [include_my_retweet] => 1
+       [include_rts] => 1
+       [include_reply_count] => true
+       [include_descendent_reply_count] => true
 (?)