]> 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 60a9acce0dbd6c1e84eb9170e477290572ae66dd..7f2fbac32a05f23e3b78e559c9936b4ed038f31f 100644 (file)
@@ -10,6 +10,7 @@ use Friendica\App;
 use Friendica\Content\ContactSelector;
 use Friendica\Content\Feature;
 use Friendica\Content\Text\BBCode;
+use Friendica\Content\Text\HTML;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
@@ -41,11 +42,9 @@ use Friendica\Util\Network;
 use Friendica\Util\XML;
 
 require_once 'include/conversation.php';
-require_once 'include/html2plain.php';
 require_once 'mod/share.php';
 require_once 'mod/item.php';
 require_once 'include/security.php';
-require_once 'include/html2bbcode.php';
 require_once 'mod/wall_upload.php';
 require_once 'mod/proxy.php';
 
@@ -648,6 +647,8 @@ function api_get_user(App $a, $contact_id = null)
                                'description' => $r[0]["about"],
                                'profile_image_url' => $r[0]["micro"],
                                'profile_image_url_https' => $r[0]["micro"],
+                               'profile_image_url_profile_size' => $r[0]["thumb"],
+                               'profile_image_url_large' => $r[0]["photo"],
                                'url' => $r[0]["url"],
                                'protected' => false,
                                'followers_count' => 0,
@@ -783,6 +784,8 @@ function api_get_user(App $a, $contact_id = null)
                'description' => $description,
                'profile_image_url' => $uinfo[0]['micro'],
                'profile_image_url_https' => $uinfo[0]['micro'],
+               'profile_image_url_profile_size' => $uinfo[0]["thumb"],
+               'profile_image_url_large' => $uinfo[0]["photo"],
                'url' => $uinfo[0]['url'],
                'protected' => false,
                'followers_count' => intval($countfollowers),
@@ -1096,7 +1099,7 @@ function api_statuses_mediap($type)
                $purifier = new HTMLPurifier($config);
                $txt = $purifier->purify($txt);
        }
-       $txt = html2bbcode($txt);
+       $txt = HTML::toBBCode($txt);
 
        $a->argv[1]=$user_info['screen_name']; //should be set to username?
 
@@ -1147,7 +1150,7 @@ function api_statuses_update($type)
                        $purifier = new HTMLPurifier($config);
                        $txt = $purifier->purify($txt);
 
-                       $_REQUEST['body'] = html2bbcode($txt);
+                       $_REQUEST['body'] = HTML::toBBCode($txt);
                }
        } else {
                $_REQUEST['body'] = requestdata('status');
@@ -1627,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 = '';
 
@@ -1665,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);
 }
@@ -1687,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();
        }
 
@@ -1698,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);
@@ -1789,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);
@@ -1898,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;
 
@@ -1968,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]);
 
@@ -2042,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);
@@ -2255,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();
        }
 
@@ -2266,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);
@@ -2347,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) .
@@ -2517,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']);
@@ -2624,10 +2628,10 @@ function api_format_messages($item, $recipient, $sender)
                if ($_GET['getText'] == 'html') {
                        $ret['text'] = BBCode::convert($item['body'], false);
                } elseif ($_GET['getText'] == 'plain') {
-                       $ret['text'] = trim(html2plain(BBCode::convert(api_clean_plain_items($item['body']), false, 2, true), 0));
+                       $ret['text'] = trim(HTML::toPlaintext(BBCode::convert(api_clean_plain_items($item['body']), false, 2, true), 0));
                }
        } else {
-               $ret['text'] = $item['title'] . "\n" . html2plain(BBCode::convert(api_clean_plain_items($item['body']), false, 2, true), 0);
+               $ret['text'] = $item['title'] . "\n" . HTML::toPlaintext(BBCode::convert(api_clean_plain_items($item['body']), false, 2, true), 0);
        }
        if (x($_GET, 'getUserObjects') && $_GET['getUserObjects'] == 'false') {
                unset($ret['sender']);
@@ -2650,7 +2654,7 @@ function api_convert_item($item)
 
        // Workaround for ostatus messages where the title is identically to the body
        $html = BBCode::convert(api_clean_plain_items($body), false, 2, true);
-       $statusbody = trim(html2plain($html, 0));
+       $statusbody = trim(HTML::toPlaintext($html, 0));
 
        // handle data: images
        $statusbody = api_format_items_embeded_images($item, $statusbody);
@@ -3266,12 +3270,14 @@ function api_help_test($type)
 api_register_func('api/help/test', 'api_help_test', false);
 
 /**
+ * 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($type)
+function api_lists_list($type)
 {
        $ret = [];
        /// @TODO $ret is not filled here?
@@ -3279,25 +3285,140 @@ function api_lists($type)
 }
 
 /// @TODO move to top of file or somewhere better
-api_register_func('api/lists', 'api_lists', true);
+api_register_func('api/lists/list', 'api_lists_list', true);
+api_register_func('api/lists/subscriptions', 'api_lists_list', true);
 
 /**
- * Returns all lists the user subscribes to.
+ * Returns all groups the user owns.
  *
  * @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
+ * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/get-lists-ownerships
  */
-function api_lists_list($type)
+function api_lists_ownerships($type)
 {
-       $ret = [];
-       /// @TODO $ret is not filled here?
-       return api_format_data('lists', $type, ["lists_list" => $ret]);
+       $a = get_app();
+
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
+
+       // params
+       $user_info = api_get_user($a);
+       $uid = $user_info['uid'];
+
+       $groups = dba::select('group', [], ['deleted' => 0, 'uid' => $uid]);
+
+       // loop through all groups
+       $lists = [];
+       foreach ($groups as $group) {
+               if ($group['visible']) {
+                       $mode = 'public';
+               } else {
+                       $mode = 'private';
+               }
+               $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' => $lists]]);
 }
 
 /// @TODO move to top of file or somewhere better
-api_register_func('api/lists/list', 'api_lists_list', true);
+api_register_func('api/lists/ownerships', 'api_lists_ownerships', true);
+
+/**
+ * Returns recent statuses from users in the specified group.
+ *
+ * @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-ownerships
+ */
+function api_lists_statuses($type)
+{
+       $a = get_app();
+
+       $user_info = api_get_user($a);
+       if (api_user() === false || $user_info === false) {
+               throw new ForbiddenException();
+       }
+
+       unset($_REQUEST["user_id"]);
+       unset($_GET["user_id"]);
+
+       unset($_REQUEST["screen_name"]);
+       unset($_GET["screen_name"]);
+
+       if (empty($_REQUEST['list_id'])) {
+               throw new BadRequestException('list_id not specified');
+       }
+
+       // params
+       $count = (x($_REQUEST, 'count') ? $_REQUEST['count'] : 20);
+       $page = (x($_REQUEST, 'page') ? $_REQUEST['page'] - 1 : 0);
+       if ($page < 0) {
+               $page = 0;
+       }
+       $since_id = (x($_REQUEST, 'since_id') ? $_REQUEST['since_id'] : 0);
+       $max_id = (x($_REQUEST, 'max_id') ? $_REQUEST['max_id'] : 0);
+       $exclude_replies = (x($_REQUEST, 'exclude_replies') ? 1 : 0);
+       $conversation_id = (x($_REQUEST, 'conversation_id') ? $_REQUEST['conversation_id'] : 0);
+
+       $start = $page * $count;
+
+       $sql_extra = '';
+       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);
+       }
+
+       $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`,
+               `contact`.`id` AS `cid`, `group_member`.`gid`
+               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 `group_member` ON `group_member`.`contact-id` = `item`.`contact-id`
+               WHERE `item`.`uid` = ? AND `verb` = ?
+               AND `item`.`visible` AND NOT `item`.`moderated` AND NOT `item`.`deleted`
+               $sql_extra
+               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']
+       );
+
+       $items = api_format_items(dba::inArray($statuses), $user_info, false, $type);
+
+       $data = ['status' => $items];
+       switch ($type) {
+               case "atom":
+               case "rss":
+                       $data = api_rss_extra($a, $data, $user_info);
+                       break;
+       }
+
+       return api_format_data("statuses", $type, $data);
+}
+
+/// @TODO move to top of file or somewhere better
+api_register_func('api/lists/statuses', 'api_lists_statuses', true);
 
 /**
  * Considers friends and followers lists to be private and won't return
@@ -3783,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();
        }
 
@@ -3808,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
@@ -4766,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, ");
 
@@ -4846,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;
@@ -5433,15 +5561,15 @@ 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.
+ * Delete a group.
  *
  * @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/post-lists-destroy
  */
-function api_friendica_group_create($type)
+function api_lists_destroy($type)
 {
        $a = get_app();
 
@@ -5451,11 +5579,45 @@ function api_friendica_group_create($type)
 
        // params
        $user_info = api_get_user($a);
-       $name = (x($_REQUEST, 'name') ? $_REQUEST['name'] : "");
+       $gid = (x($_REQUEST, 'list_id') ? $_REQUEST['list_id'] : 0);
        $uid = $user_info['uid'];
-       $json = json_decode($_POST['json'], true);
-       $users = $json['user'];
 
+       // error if no gid specified
+       if ($gid == 0) {
+               throw new BadRequestException('gid not specified');
+       }
+
+       // get data of the specified group id
+       $group = dba::selectFirst('group', [], ['uid' => $uid, 'id' => $gid]);
+       // error message if specified gid is not in database
+       if (!$group) {
+               throw new BadRequestException('gid not available');
+       }
+
+       if (Group::remove($gid)) {
+               $list = [
+                       'name' => $group['name'],
+                       'id' => intval($gid),
+                       'id_str' => (string) $gid,
+                       'user' => $user_info
+               ];
+
+               return api_format_data("lists", $type, ['lists' => $list]);
+       }
+}
+api_register_func('api/lists/destroy', 'api_lists_destroy', true, API_METHOD_DELETE);
+
+/**
+ * Add a new group to the database.
+ *
+ * @param  string $name  Group name
+ * @param  int   $uid   User ID
+ * @param  array  $users List of users to add to the group
+ *
+ * @return array
+ */
+function group_create($name, $uid, $users = [])
+{
        // error if no name specified
        if ($name == "") {
                throw new BadRequestException('group name not specified');
@@ -5512,11 +5674,72 @@ function api_friendica_group_create($type)
 
        // return success message incl. missing users in array
        $status = ($erroraddinguser ? "missing user" : ($reactivate_group ? "reactivated" : "ok"));
-       $success = ['success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers];
+
+       return ['success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers];
+}
+
+/**
+ * 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();
+       }
+
+       // params
+       $user_info = api_get_user($a);
+       $name = (x($_REQUEST, 'name') ? $_REQUEST['name'] : "");
+       $uid = $user_info['uid'];
+       $json = json_decode($_POST['json'], true);
+       $users = $json['user'];
+
+       $success = group_create($name, $uid, $users);
+
        return api_format_data("group_create", $type, ['result' => $success]);
 }
 api_register_func('api/friendica/group_create', 'api_friendica_group_create', true, API_METHOD_POST);
 
+/**
+ * Create a new group.
+ *
+ * @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/post-lists-create
+ */
+function api_lists_create($type)
+{
+       $a = get_app();
+
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
+
+       // params
+       $user_info = api_get_user($a);
+       $name = (x($_REQUEST, 'name') ? $_REQUEST['name'] : "");
+       $uid = $user_info['uid'];
+
+       $success = group_create($name, $uid);
+       if ($success['success']) {
+               $grp = [
+                       'name' => $success['name'],
+                       'id' => intval($success['gid']),
+                       'id_str' => (string) $success['gid'],
+                       'user' => $user_info
+               ];
+
+               return api_format_data("lists", $type, ['lists'=>$grp]);
+       }
+}
+api_register_func('api/lists/create', 'api_lists_create', true, API_METHOD_POST);
 
 /**
  * Update the specified group with the posted array of contacts.
@@ -5591,6 +5814,56 @@ function api_friendica_group_update($type)
 
 api_register_func('api/friendica/group_update', 'api_friendica_group_update', true, API_METHOD_POST);
 
+/**
+ * Update information about a group.
+ *
+ * @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/post-lists-update
+ */
+function api_lists_update($type)
+{
+       $a = get_app();
+
+       if (api_user() === false) {
+               throw new ForbiddenException();
+       }
+
+       // params
+       $user_info = api_get_user($a);
+       $gid = (x($_REQUEST, 'list_id') ? $_REQUEST['list_id'] : 0);
+       $name = (x($_REQUEST, 'name') ? $_REQUEST['name'] : "");
+       $uid = $user_info['uid'];
+
+       // error if no gid specified
+       if ($gid == 0) {
+               throw new BadRequestException('gid not specified');
+       }
+
+       // get data of the specified group id
+       $group = dba::selectFirst('group', [], ['uid' => $uid, 'id' => $gid]);
+       // error message if specified gid is not in database
+       if (!$group) {
+               throw new BadRequestException('gid not available');
+       }
+
+       if (Group::update($gid, $name)) {
+               $list = [
+                       'name' => $name,
+                       'id' => intval($gid),
+                       'id_str' => (string) $gid,
+                       'user' => $user_info
+               ];
+
+               return api_format_data("lists", $type, ['lists' => $list]);
+       }
+
+       return api_format_data("group_update", $type, ['result' => $success]);
+}
+
+api_register_func('api/lists/update', 'api_lists_update', true, API_METHOD_POST);
+
 /**
  *
  * @param string $type Return type (atom, rss, xml, json)
@@ -5678,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) {
@@ -5704,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);
@@ -5932,10 +6205,12 @@ function api_saved_searches_list($type)
        $result = [];
        while ($term = $terms->fetch()) {
                $result[] = [
-                       'name' => $term['term'],
-                       'query' => $term['term'],
+                       'created_at' => api_date(time()),
+                       'id' => intval($term['id']),
                        'id_str' => $term['id'],
-                       'id' => intval($term['id'])
+                       'name' => $term['term'],
+                       'position' => null,
+                       'query' => $term['term']
                ];
        }