]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Fix more undefined variable/index notice in tests
[friendica.git] / include / api.php
index 819d2c75a00c624488586eabbb02d633ad3081c8..85f489b7994b73d49031682aa4dc792876ad0fe7 100644 (file)
@@ -90,11 +90,15 @@ function api_source()
        }
 
        // Support for known clients that doesn't send a source name
-       if (strpos($_SERVER['HTTP_USER_AGENT'], "Twidere") !== false) {
-               return "Twidere";
+       if (!empty($_SERVER['HTTP_USER_AGENT'])) {
+               if(strpos($_SERVER['HTTP_USER_AGENT'], "Twidere") !== false) {
+                       return "Twidere";
+               }
+
+               logger("Unrecognized user-agent ".$_SERVER['HTTP_USER_AGENT'], LOGGER_DEBUG);
        }
 
-       logger("Unrecognized user-agent ".$_SERVER['HTTP_USER_AGENT'], LOGGER_DEBUG);
+       logger("Empty user-agent", LOGGER_DEBUG);
 
        return "api";
 }
@@ -193,8 +197,8 @@ function api_login(App $a)
                throw new UnauthorizedException("This API requires login");
        }
 
-       $user = $_SERVER['PHP_AUTH_USER'];
-       $password = $_SERVER['PHP_AUTH_PW'];
+       $user = defaults($_SERVER, 'PHP_AUTH_USER', '');
+       $password = defaults($_SERVER, 'PHP_AUTH_PW', '');
 
        // allow "user@server" login (but ignore 'server' part)
        $at = strstr($user, "@", true);
@@ -258,7 +262,7 @@ function api_check_method($method)
        if ($method == "*") {
                return true;
        }
-       return (strpos($method, $_SERVER['REQUEST_METHOD']) !== false);
+       return (stripos($method, defaults($_SERVER, 'REQUEST_METHOD', 'GET')) !== false);
 }
 
 /**
@@ -298,7 +302,7 @@ function api_call(App $a)
                                //unset($_SERVER['PHP_AUTH_USER']);
 
                                /// @TODO should be "true ==[=] $info['auth']", if you miss only one = character, you assign a variable (only with ==). Let's make all this even.
-                               if ($info['auth'] === true && api_user() === false) {
+                               if (!empty($info['auth']) && api_user() === false) {
                                        api_login($a);
                                }
 
@@ -571,6 +575,7 @@ function api_get_user(App $a, $contact_id = null)
                }
        }
 
+       // $called_api is the API path exploded on / and is expected to have at least 2 elements
        if (is_null($user) && ($a->argc > (count($called_api) - 1)) && (count($called_api) > 0)) {
                $argid = count($called_api);
                list($user, $null) = explode(".", $a->argv[$argid]);
@@ -659,7 +664,7 @@ function api_get_user(App $a, $contact_id = null)
                                'geo_enabled' => false,
                                'verified' => false,
                                'statuses_count' => 0,
-                               'lang' => '',
+                               'language' => '',
                                'contributors_enabled' => false,
                                'is_translator' => false,
                                'is_translation_enabled' => false,
@@ -740,7 +745,7 @@ function api_get_user(App $a, $contact_id = null)
                'geo_enabled' => false,
                'verified' => true,
                'statuses_count' => intval($countitems),
-               'lang' => '',
+               'language' => '',
                'contributors_enabled' => false,
                'is_translator' => false,
                'is_translation_enabled' => false,
@@ -773,13 +778,13 @@ function api_get_user(App $a, $contact_id = null)
                                $link_color = PConfig::get($ret['uid'], 'frio', 'link_color');
                                $bgcolor = PConfig::get($ret['uid'], 'frio', 'background_color');
                        }
-                       if (!$nav_bg) {
+                       if (empty($nav_bg)) {
                                $nav_bg = "#708fa0";
                        }
-                       if (!$link_color) {
+                       if (empty($link_color)) {
                                $link_color = "#6fdbe8";
                        }
-                       if (!$bgcolor) {
+                       if (empty($bgcolor)) {
                                $bgcolor = "#ededed";
                        }
 
@@ -801,12 +806,12 @@ function api_get_user(App $a, $contact_id = null)
  */
 function api_item_get_user(App $a, $item)
 {
-       $status_user = api_get_user($a, $item["author-id"]);
+       $status_user = api_get_user($a, defaults($item, 'author-id', null));
 
-       $status_user["protected"] = $item["private"];
+       $status_user["protected"] = defaults($item, 'private', 0);
 
-       if ($item['thr-parent'] == $item['uri']) {
-               $owner_user = api_get_user($a, $item["owner-id"]);
+       if (defaults($item, 'thr-parent', '') == defaults($item, 'uri', '')) {
+               $owner_user = api_get_user($a, defaults($item, 'author-id', null));
        } else {
                $owner_user = $status_user;
        }
@@ -1270,8 +1275,8 @@ function api_status_show($type)
        }
 
        // get last public wall message
-       $condition = ["`owner-id` = ? AND `uid` = ? AND `type` != 'activity' ".$privacy_sql,
-               $user_info['pid'], api_user()];
+       $condition = ['owner-id' => $user_info['pid'], 'uid' => api_user(),
+               'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]];
        $lastwall = dba::selectFirst('item', [], $condition, ['order' => ['id' => true]]);
 
        if (DBM::is_result($lastwall)) {
@@ -1308,7 +1313,7 @@ function api_status_show($type)
                        'favorited' => $lastwall['starred'] ? true : false,
                        'retweeted' => false,
                        'possibly_sensitive' => false,
-                       'lang' => "",
+                       'language' => "",
                        'statusnet_html' => $converted["html"],
                        'statusnet_conversation_id' => $lastwall['parent'],
                        'external_url' => System::baseUrl() . "/display/" . $lastwall['guid'],
@@ -1355,8 +1360,8 @@ function api_users_show($type)
 
        $user_info = api_get_user($a);
 
-       $condition = ["`owner-id` = ? AND `uid` = ? AND `verb` = ? AND `type` != 'activity' AND NOT `private`",
-               $user_info['pid'], api_user(), ACTIVITY_POST];
+       $condition = ['owner-id' => $user_info['pid'], 'uid' => api_user(),
+               'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'private' => false];
        $lastwall = dba::selectFirst('item', [], $condition, ['order' => ['id' => true]]);
 
        if (DBM::is_result($lastwall)) {
@@ -1477,7 +1482,7 @@ function api_users_lookup($type)
 {
        $users = [];
 
-       if (x($_REQUEST['user_id'])) {
+       if (!empty($_REQUEST['user_id'])) {
                foreach (explode(',', $_REQUEST['user_id']) as $id) {
                        if (!empty($id)) {
                                $users[] = api_get_user(get_app(), $id);
@@ -1534,10 +1539,10 @@ function api_search($type)
 
        $start = $page * $count;
 
-       $condition = ["`verb` = ? AND `item`.`id` > ?
+       $condition = ["`gravity` IN (?, ?) AND `item`.`id` > ?
                AND (`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))
                AND `item`.`body` LIKE CONCAT('%',?,'%')",
-               ACTIVITY_POST, $since_id, api_user(), $_REQUEST['q']];
+               GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, api_user(), $_REQUEST['q']];
 
        if ($max_id > 0) {
                $condition[0] .= " AND `item`.`id` <= ?";
@@ -1597,7 +1602,8 @@ function api_statuses_home_timeline($type)
 
        $start = $page * $count;
 
-       $condition = ["`uid` = ? AND `verb` = ? AND `item`.`id` > ?", api_user(), ACTIVITY_POST, $since_id];
+       $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `item`.`id` > ?",
+               api_user(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id];
 
        if ($max_id > 0) {
                $condition[0] .= " AND `item`.`id` <= ?";
@@ -1625,7 +1631,7 @@ function api_statuses_home_timeline($type)
        }
 
        if (!empty($idarray)) {
-               $unseen = dba::exists('item', ['unseen' => true, 'id' => $idarray]);
+               $unseen = Item::exists(['unseen' => true, 'id' => $idarray]);
                if ($unseen) {
                        Item::update(['unseen' => false], ['unseen' => true, 'id' => $idarray]);
                }
@@ -1680,8 +1686,8 @@ function api_statuses_public_timeline($type)
        $sql_extra = '';
 
        if ($exclude_replies && !$conversation_id) {
-               $condition = ["`verb` = ? AND `iid` > ? AND NOT `private` AND `wall` AND NOT `user`.`hidewall`",
-                       ACTIVITY_POST, $since_id];
+               $condition = ["`gravity` IN (?, ?) AND `iid` > ? AND NOT `private` AND `wall` AND NOT `user`.`hidewall`",
+                       GRAVITY_PARENT, GRAVITY_COMMENT, $since_id];
 
                if ($max_id > 0) {
                        $condition[0] .= " AND `thread`.`iid` <= ?";
@@ -1693,8 +1699,8 @@ function api_statuses_public_timeline($type)
 
                $r = Item::inArray($statuses);
        } else {
-               $condition = ["`verb` = ? AND `id` > ? AND NOT `private` AND `wall` AND NOT `user`.`hidewall` AND `item`.`origin`",
-                       ACTIVITY_POST, $since_id];
+               $condition = ["`gravity` IN (?, ?) AND `id` > ? AND NOT `private` AND `wall` AND NOT `user`.`hidewall` AND `item`.`origin`",
+                       GRAVITY_PARENT, GRAVITY_COMMENT, $since_id];
 
                if ($max_id > 0) {
                        $condition[0] .= " AND `item`.`id` <= ?";
@@ -1756,8 +1762,8 @@ function api_statuses_networkpublic_timeline($type)
        }
        $start = ($page - 1) * $count;
 
-       $condition = ["`uid` = 0 AND `verb` = ? AND `thread`.`iid` > ? AND NOT `private`",
-               ACTIVITY_POST, $since_id];
+       $condition = ["`uid` = 0 AND `gravity` IN (?, ?) AND `thread`.`iid` > ? AND NOT `private`",
+               GRAVITY_PARENT, GRAVITY_COMMENT, $since_id];
 
        if ($max_id > 0) {
                $condition[0] .= " AND `thread`.`iid` <= ?";
@@ -1829,10 +1835,10 @@ function api_statuses_show($type)
        $id = $item['id'];
 
        if ($conversation) {
-               $condition = ['parent' => $id, 'verb' => ACTIVITY_POST];
+               $condition = ['parent' => $id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]];
                $params = ['order' => ['id' => true]];
        } else {
-               $condition = ['id' => $id, 'verb' => ACTIVITY_POST];
+               $condition = ['id' => $id, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]];
                $params = [];
        }
 
@@ -1908,8 +1914,8 @@ function api_conversation_show($type)
 
        $id = $parent['id'];
 
-       $condition = ["`parent` = ? AND `uid` IN (0, ?) AND `verb` = ? AND `item`.`id` > ?",
-               $id, api_user(), ACTIVITY_POST, $since_id];
+       $condition = ["`parent` = ? AND `uid` IN (0, ?) AND `gravity` IN (?, ?) AND `item`.`id` > ?",
+               $id, api_user(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id];
 
        if ($max_id > 0) {
                $condition[0] .= " AND `item`.`id` <= ?";
@@ -2077,9 +2083,9 @@ function api_statuses_mentions($type)
 
        $start = ($page - 1) * $count;
 
-       $condition = ["`uid` = ? AND `verb` = ? AND `item`.`id` > ? AND `author-id` != ?
+       $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `item`.`id` > ? AND `author-id` != ?
                AND `item`.`parent` IN (SELECT `iid` FROM `thread` WHERE `thread`.`uid` = ? AND `thread`.`mention` AND NOT `thread`.`ignored`)",
-               api_user(), ACTIVITY_POST, $since_id, $user_info['pid'], api_user()];
+               api_user(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, $user_info['pid'], api_user()];
 
        if ($max_id > 0) {
                $condition[0] .= " AND `item`.`id` <= ?";
@@ -2145,8 +2151,8 @@ function api_statuses_user_timeline($type)
        }
        $start = ($page - 1) * $count;
 
-       $condition = ["`uid` = ? AND `verb` = ? AND `item`.`id` > ? AND `item`.`contact-id` = ?",
-               api_user(), ACTIVITY_POST, $since_id, $user_info['cid']];
+       $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `item`.`id` > ? AND `item`.`contact-id` = ?",
+               api_user(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, $user_info['cid']];
 
        if ($user_info['self'] == 1) {
                $condition[0] .= ' AND `item`.`wall` ';
@@ -2204,7 +2210,7 @@ function api_favorites_create_destroy($type)
        // for versioned api.
        /// @TODO We need a better global soluton
        $action_argv_id = 2;
-       if ($a->argv[1] == "1.1") {
+       if (count($a->argv) > 1 && $a->argv[1] == "1.1") {
                $action_argv_id = 3;
        }
 
@@ -2299,8 +2305,8 @@ function api_favorites($type)
 
                $start = $page*$count;
 
-               $condition = ["`uid` = ? AND `verb` = ? AND `id` > ? AND `starred`",
-                       api_user(), ACTIVITY_POST, $since_id];
+               $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `id` > ? AND `starred`",
+                       api_user(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id];
 
                $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
 
@@ -3099,8 +3105,8 @@ function api_lists_statuses($type)
 
        $start = $page * $count;
 
-       $condition = ["`uid` = ? AND `verb` = ? AND `id` > ? AND `group_member`.`gid` = ?",
-               api_user(), ACTIVITY_POST, $since_id, $_REQUEST['list_id']];
+       $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `id` > ? AND `group_member`.`gid` = ?",
+               api_user(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, $_REQUEST['list_id']];
 
        if ($max_id > 0) {
                $condition[0] .= " AND `item`.`id` <= ?";
@@ -4630,8 +4636,8 @@ function prepare_photo_data($type, $scale, $photo_id)
        $data['photo']['friendica_activities'] = api_format_items_activities($item, $type);
 
        // retrieve comments on photo
-       $condition = ["`parent` = ? AND `uid` = ? AND (`verb` = ? OR `type`='photo')",
-               $item[0]['parent'], api_user(), ACTIVITY_POST];
+       $condition = ["`parent` = ? AND `uid` = ? AND (`gravity` IN (?, ?) OR `type`='photo')",
+               $item[0]['parent'], api_user(), GRAVITY_PARENT, GRAVITY_COMMENT];
 
        $statuses = Item::selectForUser(api_user(), [], $condition);