]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
Changed "??" to "?:"
[friendica.git] / include / api.php
index e641003170de89164aa1682d85e78eb1b020673f..5c708c0b70a079cdc6f65fb9bbd6735a4bf960aa 100644 (file)
@@ -1416,32 +1416,37 @@ function api_users_search($type)
        $userlist = [];
 
        if (!empty($_GET['q'])) {
-               $r = q("SELECT id FROM `contact` WHERE `uid` = 0 AND `name` = '%s'", DBA::escape($_GET["q"]));
-
-               if (!DBA::isResult($r)) {
-                       $r = q("SELECT `id` FROM `contact` WHERE `uid` = 0 AND `nick` = '%s'", DBA::escape($_GET["q"]));
-               }
+               $contacts = Contact::selectToArray(
+                       ['id'],
+                       [
+                               '`uid` = 0 AND (`name` = ? OR `nick` = ? OR `url` = ? OR `addr` = ?)',
+                               $_GET['q'],
+                               $_GET['q'],
+                               $_GET['q'],
+                               $_GET['q'],
+                       ]
+               );
 
-               if (DBA::isResult($r)) {
+               if (DBA::isResult($contacts)) {
                        $k = 0;
-                       foreach ($r as $user) {
-                               $user_info = api_get_user($a, $user["id"]);
+                       foreach ($contacts as $contact) {
+                               $user_info = api_get_user($a, $contact['id']);
 
-                               if ($type == "xml") {
-                                       $userlist[$k++.":user"] = $user_info;
+                               if ($type == 'xml') {
+                                       $userlist[$k++ . ':user'] = $user_info;
                                } else {
                                        $userlist[] = $user_info;
                                }
                        }
-                       $userlist = ["users" => $userlist];
+                       $userlist = ['users' => $userlist];
                } else {
-                       throw new BadRequestException("User ".$_GET["q"]." not found.");
+                       throw new NotFoundException('User ' . $_GET['q'] . ' not found.');
                }
        } else {
-               throw new BadRequestException("No user specified.");
+               throw new BadRequestException('No search term specified.');
        }
 
-       return api_format_data("users", $type, $userlist);
+       return api_format_data('users', $type, $userlist);
 }
 
 /// @TODO move to top of file or somewhere better
@@ -1502,7 +1507,9 @@ function api_search($type)
        $a = \get_app();
        $user_info = api_get_user($a);
 
-       if (api_user() === false || $user_info === false) { throw new ForbiddenException(); }
+       if (api_user() === false || $user_info === false) {
+               throw new ForbiddenException();
+       }
 
        if (empty($_REQUEST['q'])) {
                throw new BadRequestException('q parameter is required.');
@@ -1566,7 +1573,21 @@ function api_search($type)
                }
        }
 
-       $statuses = Item::selectForUser(api_user(), [], $condition, $params);
+       $statuses = [];
+
+       if (parse_url($searchTerm, PHP_URL_SCHEME) != '') {
+               $id = Item::fetchByLink($searchTerm, api_user());
+               if (!$id) {
+                       // Public post
+                       $id = Item::fetchByLink($searchTerm);
+               }
+
+               if (!empty($id)) {
+                       $statuses = Item::select([], ['id' => $id]);
+               }
+       }
+
+       $statuses = $statuses ?: Item::selectForUser(api_user(), [], $condition, $params);
 
        $data['status'] = api_format_items(Item::inArray($statuses), $user_info);
 
@@ -2144,8 +2165,8 @@ function api_statuses_mentions($type)
 
        $start = max(0, ($page - 1) * $count);
 
-       $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`)",
+       $condition = ["`uid` = ? AND `gravity` IN (?, ?) AND `item`.`id` > ? AND `author-id` != ? AND `mention`
+               AND `item`.`parent` IN (SELECT `iid` FROM `thread` WHERE `thread`.`uid` = ? AND NOT `thread`.`ignored`)",
                api_user(), GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, $user_info['pid'], api_user()];
 
        if ($max_id > 0) {
@@ -2830,9 +2851,10 @@ function api_format_items_activities($item, $type = "json")
                'attendyes' => [],
                'attendno' => [],
                'attendmaybe' => [],
+               'announce' => [],
        ];
 
-       $condition = ['uid' => $item['uid'], 'thr-parent' => $item['uri']];
+       $condition = ['uid' => $item['uid'], 'thr-parent' => $item['uri'], 'gravity' => GRAVITY_ACTIVITY];
        $ret = Item::selectForUser($item['uid'], ['author-id', 'verb'], $condition);
 
        while ($parent_item = Item::fetch($ret)) {
@@ -2857,6 +2879,9 @@ function api_format_items_activities($item, $type = "json")
                        case Activity::ATTENDMAYBE:
                                $activities['attendmaybe'][] = $user;
                                break;
+                       case Activity::ANNOUNCE:
+                               $activities['announce'][] = $user;
+                               break;
                        default:
                                break;
                }
@@ -3605,6 +3630,7 @@ api_register_func('api/statusnet/version', 'api_statusnet_version', false);
  *
  * @param string $type Return type (atom, rss, xml, json)
  *
+ * @param int $rel A contact relationship constant
  * @return array|string|void
  * @throws BadRequestException
  * @throws ForbiddenException
@@ -3613,7 +3639,7 @@ api_register_func('api/statusnet/version', 'api_statusnet_version', false);
  * @throws UnauthorizedException
  * @todo use api_format_data() to return data
  */
-function api_ff_ids($type)
+function api_ff_ids($type, int $rel)
 {
        if (!api_user()) {
                throw new ForbiddenException();
@@ -3625,26 +3651,29 @@ function api_ff_ids($type)
 
        $stringify_ids = $_REQUEST['stringify_ids'] ?? false;
 
-       $r = q(
-               "SELECT `pcontact`.`id` FROM `contact`
-                       INNER JOIN `contact` AS `pcontact` ON `contact`.`nurl` = `pcontact`.`nurl` AND `pcontact`.`uid` = 0
-                       WHERE `contact`.`uid` = %s AND NOT `contact`.`self`",
-               intval(api_user())
+       $contacts = DBA::p("SELECT `pcontact`.`id`
+               FROM `contact`
+               INNER JOIN `contact` AS `pcontact`
+                   ON `contact`.`nurl` = `pcontact`.`nurl`
+                   AND `pcontact`.`uid` = 0
+               WHERE `contact`.`uid` = ?
+               AND NOT `contact`.`self`
+               AND `contact`.`rel` IN (?, ?)",
+               api_user(),
+               $rel,
+               Contact::FRIEND
        );
-       if (!DBA::isResult($r)) {
-               return;
-       }
 
        $ids = [];
-       foreach ($r as $rr) {
+       foreach (DBA::toArray($contacts) as $contact) {
                if ($stringify_ids) {
-                       $ids[] = $rr['id'];
+                       $ids[] = $contact['id'];
                } else {
-                       $ids[] = intval($rr['id']);
+                       $ids[] = intval($contact['id']);
                }
        }
 
-       return api_format_data("ids", $type, ['id' => $ids]);
+       return api_format_data('ids', $type, ['id' => $ids]);
 }
 
 /**
@@ -3655,11 +3684,14 @@ function api_ff_ids($type)
  * @return array|string
  * @throws BadRequestException
  * @throws ForbiddenException
+ * @throws ImagickException
+ * @throws InternalServerErrorException
+ * @throws UnauthorizedException
  * @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);
+       return api_ff_ids($type, Contact::SHARING);
 }
 
 /**
@@ -3670,11 +3702,14 @@ function api_friends_ids($type)
  * @return array|string
  * @throws BadRequestException
  * @throws ForbiddenException
+ * @throws ImagickException
+ * @throws InternalServerErrorException
+ * @throws UnauthorizedException
  * @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);
+       return api_ff_ids($type, Contact::FOLLOWER);
 }
 
 /// @TODO move to top of file or somewhere better
@@ -5063,14 +5098,17 @@ function api_friendica_remoteauth()
        // traditional DFRN
 
        $contact = DBA::selectFirst('contact', [], ['uid' => api_user(), 'nurl' => $c_url]);
-
-       if (!DBA::isResult($contact) || ($contact['network'] !== Protocol::DFRN)) {
+       if (!DBA::isResult($contact)) {
                throw new BadRequestException("Unknown contact");
        }
 
        $cid = $contact['id'];
 
-       $dfrn_id = $contact['issued-id'] ?? $contact['dfrn-id'];
+       $dfrn_id = $contact['issued-id'] ?: $contact['dfrn-id'];
+
+       if (($contact['network'] !== Protocol::DFRN) || empty($dfrn_id)) {
+               System::externalRedirect($url ?: $c_url);
+       }
 
        if ($contact['duplex'] && $contact['issued-id']) {
                $orig_id = $contact['issued-id'];