]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Throw a ForbiddenException if api_get_user() returns false
[friendica.git] / include / api.php
index 8cd7d23a73c4723f1074b17dab64bf5ca6c14276..7f2fbac32a05f23e3b78e559c9936b4ed038f31f 100644 (file)
@@ -1630,6 +1630,13 @@ api_register_func('api/users/lookup', 'api_users_lookup', true);
  */
 function api_search($type)
 {
+       $a = get_app();
+       $user_info = api_get_user($a);
+
+       if (api_user() === false || $user_info === false) {
+               throw new ForbiddenException();
+       }
+
        $data = [];
        $sql_extra = '';
 
@@ -1668,7 +1675,7 @@ function api_search($type)
                $since_id
        );
 
-       $data['status'] = api_format_items(dba::inArray($r), api_get_user(get_app()));
+       $data['status'] = api_format_items(dba::inArray($r), $user_info);
 
        return api_format_data("statuses", $type, $data);
 }
@@ -1690,8 +1697,9 @@ api_register_func('api/search', 'api_search', true);
 function api_statuses_home_timeline($type)
 {
        $a = get_app();
+       $user_info = api_get_user($a);
 
-       if (api_user() === false) {
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 
@@ -1701,8 +1709,7 @@ function api_statuses_home_timeline($type)
        unset($_REQUEST["screen_name"]);
        unset($_GET["screen_name"]);
 
-       $user_info = api_get_user($a);
-       // get last newtork messages
+       // get last network messages
 
        // params
        $count = (x($_REQUEST, 'count') ? $_REQUEST['count'] : 20);
@@ -1792,13 +1799,13 @@ api_register_func('api/statuses/friends_timeline', 'api_statuses_home_timeline',
 function api_statuses_public_timeline($type)
 {
        $a = get_app();
+       $user_info = api_get_user($a);
 
-       if (api_user() === false) {
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 
-       $user_info = api_get_user($a);
-       // get last newtork messages
+       // get last network messages
 
        // params
        $count = (x($_REQUEST, 'count') ? $_REQUEST['count'] : 20);
@@ -1901,13 +1908,12 @@ api_register_func('api/statuses/public_timeline', 'api_statuses_public_timeline'
 function api_statuses_networkpublic_timeline($type)
 {
        $a = get_app();
+       $user_info = api_get_user($a);
 
-       if (api_user() === false) {
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 
-       $user_info = api_get_user($a);
-
        $since_id        = x($_REQUEST, 'since_id')        ? $_REQUEST['since_id']        : 0;
        $max_id          = x($_REQUEST, 'max_id')          ? $_REQUEST['max_id']          : 0;
 
@@ -1971,13 +1977,12 @@ api_register_func('api/statuses/networkpublic_timeline', 'api_statuses_networkpu
 function api_statuses_show($type)
 {
        $a = get_app();
+       $user_info = api_get_user($a);
 
-       if (api_user() === false) {
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 
-       $user_info = api_get_user($a);
-
        // params
        $id = intval($a->argv[3]);
 
@@ -2045,13 +2050,12 @@ api_register_func('api/statuses/show', 'api_statuses_show', true);
 function api_conversation_show($type)
 {
        $a = get_app();
+       $user_info = api_get_user($a);
 
-       if (api_user() === false) {
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 
-       $user_info = api_get_user($a);
-
        // params
        $id = intval($a->argv[3]);
        $count = (x($_REQUEST, 'count') ? $_REQUEST['count'] : 20);
@@ -2258,8 +2262,9 @@ api_register_func('api/statuses/destroy', 'api_statuses_destroy', true, API_METH
 function api_statuses_mentions($type)
 {
        $a = get_app();
+       $user_info = api_get_user($a);
 
-       if (api_user() === false) {
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 
@@ -2269,9 +2274,7 @@ function api_statuses_mentions($type)
        unset($_REQUEST["screen_name"]);
        unset($_GET["screen_name"]);
 
-       $user_info = api_get_user($a);
-       // get last newtork messages
-
+       // get last network messages
 
        // params
        $since_id = defaults($_REQUEST, 'since_id', 0);
@@ -2350,13 +2353,12 @@ api_register_func('api/statuses/replies', 'api_statuses_mentions', true);
 function api_statuses_user_timeline($type)
 {
        $a = get_app();
+       $user_info = api_get_user($a);
 
-       if (api_user() === false) {
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 
-       $user_info = api_get_user($a);
-
        logger(
                "api_statuses_user_timeline: api_user: ". api_user() .
                        "\nuser_info: ".print_r($user_info, true) .
@@ -2520,15 +2522,14 @@ function api_favorites($type)
        global $called_api;
 
        $a = get_app();
+       $user_info = api_get_user($a);
 
-       if (api_user() === false) {
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 
        $called_api = [];
 
-       $user_info = api_get_user($a);
-
        // in friendica starred item are private
        // return favorites only for self
        logger('api_favorites: self:' . $user_info['self']);
@@ -3307,27 +3308,25 @@ function api_lists_ownerships($type)
        $user_info = api_get_user($a);
        $uid = $user_info['uid'];
 
-       $r = q(
-               "SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d",
-               intval($uid)
-       );
+       $groups = dba::select('group', [], ['deleted' => 0, 'uid' => $uid]);
 
        // loop through all groups
-       foreach ($r as $rr) {
-               if ($rr['visible']) {
+       $lists = [];
+       foreach ($groups as $group) {
+               if ($group['visible']) {
                        $mode = 'public';
                } else {
                        $mode = 'private';
                }
-               $grps[] = [
-                       'name' => $rr['name'],
-                       'id' => intval($rr['id']),
-                       'id_str' => (string) $rr['id'],
+               $lists[] = [
+                       'name' => $group['name'],
+                       'id' => intval($group['id']),
+                       'id_str' => (string) $group['id'],
                        'user' => $user_info,
                        'mode' => $mode
                ];
        }
-       return api_format_data("lists", $type, ['lists' => ['lists'=>$grps]]);
+       return api_format_data("lists", $type, ['lists' => ['lists' => $lists]]);
 }
 
 /// @TODO move to top of file or somewhere better
@@ -3345,7 +3344,8 @@ function api_lists_statuses($type)
 {
        $a = get_app();
 
-       if (api_user() === false) {
+       $user_info = api_get_user($a);
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 
@@ -3355,8 +3355,9 @@ function api_lists_statuses($type)
        unset($_REQUEST["screen_name"]);
        unset($_GET["screen_name"]);
 
-       $user_info = api_get_user($a);
-       // get last newtork messages
+       if (empty($_REQUEST['list_id'])) {
+               throw new BadRequestException('list_id not specified');
+       }
 
        // params
        $count = (x($_REQUEST, 'count') ? $_REQUEST['count'] : 20);
@@ -3366,7 +3367,6 @@ function api_lists_statuses($type)
        }
        $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);
        $exclude_replies = (x($_REQUEST, 'exclude_replies') ? 1 : 0);
        $conversation_id = (x($_REQUEST, 'conversation_id') ? $_REQUEST['conversation_id'] : 0);
 
@@ -3383,7 +3383,7 @@ function api_lists_statuses($type)
                $sql_extra .= ' AND `item`.`parent` = ' . intval($conversation_id);
        }
 
-       $r = q(
+       $statuses = dba::p(
                "SELECT `item`.*, `item`.`id` AS `item_id`, `item`.`network` AS `item_network`,
                `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
                `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
@@ -3392,39 +3392,21 @@ function api_lists_statuses($type)
                STRAIGHT_JOIN `contact` ON `contact`.`id` = `item`.`contact-id` AND `contact`.`uid` = `item`.`uid`
                        AND (NOT `contact`.`blocked` OR `contact`.`pending`)
                STRAIGHT_JOIN `group_member` ON `group_member`.`contact-id` = `item`.`contact-id`
-               WHERE `item`.`uid` = %d AND `verb` = '%s'
+               WHERE `item`.`uid` = ? AND `verb` = ?
                AND `item`.`visible` AND NOT `item`.`moderated` AND NOT `item`.`deleted`
                $sql_extra
-               AND `item`.`id`>%d
-               AND `group_member`.`gid` = %d
-               ORDER BY `item`.`id` DESC LIMIT %d ,%d ",
-               intval(api_user()),
-               dbesc(ACTIVITY_POST),
-               intval($since_id),
-               intval($_REQUEST['list_id']),
-               intval($start),
-               intval($count)
+               AND `item`.`id`>?
+               AND `group_member`.`gid` = ?
+               ORDER BY `item`.`id` DESC LIMIT ".intval($start)." ,".intval($count),
+               api_user(),
+               ACTIVITY_POST,
+               $since_id,
+               $_REQUEST['list_id']
        );
 
-       $ret = api_format_items($r, $user_info, false, $type);
-
-       // Set all posts from the query above to seen
-       $idarray = [];
-       foreach ($r as $item) {
-               $idarray[] = intval($item["id"]);
-       }
-
-       $idlist = implode(",", $idarray);
+       $items = api_format_items(dba::inArray($statuses), $user_info, false, $type);
 
-       if ($idlist != "") {
-               $unseen = q("SELECT `id` FROM `item` WHERE `unseen` AND `id` IN (%s)", $idlist);
-
-               if ($unseen) {
-                       q("UPDATE `item` SET `unseen` = 0 WHERE `unseen` AND `id` IN (%s)", $idlist);
-               }
-       }
-
-       $data = ['status' => $ret];
+       $data = ['status' => $items];
        switch ($type) {
                case "atom":
                case "rss":
@@ -3922,8 +3904,9 @@ api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy',
 function api_direct_messages_box($type, $box, $verbose)
 {
        $a = get_app();
+       $user_info = api_get_user($a);
 
-       if (api_user() === false) {
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
 
@@ -3947,7 +3930,6 @@ function api_direct_messages_box($type, $box, $verbose)
        unset($_REQUEST["screen_name"]);
        unset($_GET["screen_name"]);
 
-       $user_info = api_get_user($a);
        $profile_url = $user_info["url"];
 
        // pagination
@@ -4905,6 +4887,13 @@ function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $f
  */
 function prepare_photo_data($type, $scale, $photo_id)
 {
+       $a = get_app();
+       $user_info = api_get_user($a);
+
+       if ($user_info === false) {
+               throw new ForbiddenException();
+       }
+
        $scale_sql = ($scale === false ? "" : sprintf("AND scale=%d", intval($scale)));
        $data_sql = ($scale === false ? "" : "data, ");
 
@@ -4985,7 +4974,7 @@ function prepare_photo_data($type, $scale, $photo_id)
        );
 
        // prepare output of comments
-       $commentData = api_format_items($r, api_get_user(get_app()), false, $type);
+       $commentData = api_format_items($r, $user_info, false, $type);
        $comments = [];
        if ($type == "xml") {
                $k = 0;
@@ -5599,25 +5588,21 @@ function api_lists_destroy($type)
        }
 
        // get data of the specified group id
-       $r = q(
-               "SELECT * FROM `group` WHERE `uid` = %d AND `id` = %d",
-               intval($uid),
-               intval($gid)
-       );
+       $group = dba::selectFirst('group', [], ['uid' => $uid, 'id' => $gid]);
        // error message if specified gid is not in database
-       if (!DBM::is_result($r)) {
+       if (!$group) {
                throw new BadRequestException('gid not available');
        }
 
        if (Group::remove($gid)) {
-               $grp = [
-                       'name' => $r[0]['name'],
+               $list = [
+                       'name' => $group['name'],
                        'id' => intval($gid),
                        'id_str' => (string) $gid,
                        'user' => $user_info
                ];
 
-               return api_format_data("lists", $type, ['lists'=>$grp]);
+               return api_format_data("lists", $type, ['lists' => $list]);
        }
 }
 api_register_func('api/lists/destroy', 'api_lists_destroy', true, API_METHOD_DELETE);
@@ -5729,7 +5714,7 @@ api_register_func('api/friendica/group_create', 'api_friendica_group_create', tr
  * @return array|string
  * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/post-lists-create
  */
-function api_list_create($type)
+function api_lists_create($type)
 {
        $a = get_app();
 
@@ -5754,7 +5739,7 @@ function api_list_create($type)
                return api_format_data("lists", $type, ['lists'=>$grp]);
        }
 }
-api_register_func('api/lists/create', 'api_list_create', true, API_METHOD_POST);
+api_register_func('api/lists/create', 'api_lists_create', true, API_METHOD_POST);
 
 /**
  * Update the specified group with the posted array of contacts.
@@ -5857,25 +5842,21 @@ function api_lists_update($type)
        }
 
        // get data of the specified group id
-       $r = q(
-               "SELECT * FROM `group` WHERE `uid` = %d AND `id` = %d",
-               intval($uid),
-               intval($gid)
-       );
+       $group = dba::selectFirst('group', [], ['uid' => $uid, 'id' => $gid]);
        // error message if specified gid is not in database
-       if (!DBM::is_result($r)) {
+       if (!$group) {
                throw new BadRequestException('gid not available');
        }
 
        if (Group::update($gid, $name)) {
-               $grp = [
+               $list = [
                        'name' => $name,
                        'id' => intval($gid),
                        'id_str' => (string) $gid,
                        'user' => $user_info
                ];
 
-               return api_format_data("lists", $type, ['lists'=>$grp]);
+               return api_format_data("lists", $type, ['lists' => $list]);
        }
 
        return api_format_data("group_update", $type, ['result' => $success]);
@@ -5970,8 +5951,9 @@ function api_friendica_notification($type)
 function api_friendica_notification_seen($type)
 {
        $a = get_app();
+       $user_info = api_get_user($a);
 
-       if (api_user() === false) {
+       if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
        if ($a->argc!==4) {
@@ -5996,7 +5978,6 @@ function api_friendica_notification_seen($type)
                );
                if ($r!==false) {
                        // we found the item, return it to the user
-                       $user_info = api_get_user($a);
                        $ret = api_format_items($r, $user_info, false, $type);
                        $data = ['status' => $ret];
                        return api_format_data("status", $type, $data);