diff --git a/composer.lock b/composer.lock index 97bea93ec5..c0e14fe525 100644 --- a/composer.lock +++ b/composer.lock @@ -3647,16 +3647,16 @@ }, { "name": "smarty/smarty", - "version": "v3.1.40", + "version": "v3.1.43", "source": { "type": "git", "url": "https://github.com/smarty-php/smarty.git", - "reference": "9d4f8309ed49702e0d7152f9983c3a9c4b98eb9d" + "reference": "273f7e00fec034f6d61112552e9caf08d19565b7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/smarty-php/smarty/zipball/9d4f8309ed49702e0d7152f9983c3a9c4b98eb9d", - "reference": "9d4f8309ed49702e0d7152f9983c3a9c4b98eb9d", + "url": "https://api.github.com/repos/smarty-php/smarty/zipball/273f7e00fec034f6d61112552e9caf08d19565b7", + "reference": "273f7e00fec034f6d61112552e9caf08d19565b7", "shasum": "" }, "require": { @@ -3700,7 +3700,7 @@ "keywords": [ "templating" ], - "time": "2021-10-13T10:04:31+00:00" + "time": "2022-01-10T09:52:40+00:00" }, { "name": "spomky-labs/base64url", diff --git a/include/api.php b/include/api.php deleted file mode 100644 index cf29a75e66..0000000000 --- a/include/api.php +++ /dev/null @@ -1,848 +0,0 @@ -. - * - * 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\Core\ACL; -use Friendica\Core\Logger; -use Friendica\Database\DBA; -use Friendica\DI; -use Friendica\Model\Contact; -use Friendica\Model\Group; -use Friendica\Model\Photo; -use Friendica\Model\Post; -use Friendica\Module\BaseApi; -use Friendica\Network\HTTPException; -use Friendica\Util\Images; - -$API = []; - -/** - * Register a function to be the endpoint for defined API path. - * - * @param string $path API URL path, relative to DI::baseUrl() - * @param string $func Function name to call on path request - */ -function api_register_func($path, $func) -{ - global $API; - - $API[$path] = [ - 'func' => $func, - ]; - - // Workaround for hotot - $path = str_replace("api/", "api/1.1/", $path); - - $API[$path] = [ - 'func' => $func, - ]; -} - -/** - * Main API entry point - * - * Authenticate user, call registered API function, set HTTP headers - * - * @param App\Arguments $args The app arguments (optional, will retrieved by the DI-Container in case of missing) - * @return string|array API call result - * @throws Exception - */ -function api_call($command, $extension) -{ - global $API; - - Logger::info('Legacy API call', ['command' => $command, 'extension' => $extension]); - - try { - foreach ($API as $p => $info) { - if (strpos($command, $p) === 0) { - Logger::debug(BaseApi::LOG_PREFIX . 'parameters', ['module' => 'api', 'action' => 'call', 'parameters' => $_REQUEST]); - - $stamp = microtime(true); - $return = call_user_func($info['func'], $extension); - $duration = floatval(microtime(true) - $stamp); - - Logger::info(BaseApi::LOG_PREFIX . 'duration {duration}', ['module' => 'api', 'action' => 'call', 'duration' => round($duration, 2)]); - - DI::profiler()->saveLog(DI::logger(), BaseApi::LOG_PREFIX . 'performance'); - - if (false === $return) { - /* - * api function returned false withour throw an - * exception. This should not happend, throw a 500 - */ - throw new HTTPException\InternalServerErrorException(); - } - - switch ($extension) { - case "xml": - header("Content-Type: text/xml"); - break; - case "json": - header("Content-Type: application/json"); - if (!empty($return)) { - $json = json_encode(end($return)); - if (!empty($_GET['callback'])) { - $json = $_GET['callback'] . "(" . $json . ")"; - } - $return = $json; - } - break; - case "rss": - header("Content-Type: application/rss+xml"); - $return = '' . "\n" . $return; - break; - case "atom": - header("Content-Type: application/atom+xml"); - $return = '' . "\n" . $return; - break; - } - return $return; - } - } - - Logger::warning(BaseApi::LOG_PREFIX . 'not implemented', ['module' => 'api', 'action' => 'call', 'query' => DI::args()->getQueryString()]); - throw new HTTPException\NotFoundException(); - } catch (HTTPException $e) { - Logger::notice(BaseApi::LOG_PREFIX . 'got exception', ['module' => 'api', 'action' => 'call', 'query' => DI::args()->getQueryString(), 'error' => $e]); - DI::apiResponse()->error($e->getCode(), $e->getDescription(), $e->getMessage(), $extension); - } -} - -/** - * - * @param string $type - * @param int $scale - * @param string $photo_id - * - * @return array - * @throws HTTPException\BadRequestException - * @throws HTTPException\ForbiddenException - * @throws ImagickException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\NotFoundException - * @throws HTTPException\UnauthorizedException - */ -function prepare_photo_data($type, $scale, $photo_id, $uid) -{ - $scale_sql = ($scale === false ? "" : sprintf("AND scale=%d", intval($scale))); - $data_sql = ($scale === false ? "" : "data, "); - - // added allow_cid, allow_gid, deny_cid, deny_gid to output as string like stored in database - // clients needs to convert this in their way for further processing - $r = DBA::toArray(DBA::p( - "SELECT $data_sql `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`, - `type`, `height`, `width`, `datasize`, `profile`, `allow_cid`, `deny_cid`, `allow_gid`, `deny_gid`, - MIN(`scale`) AS `minscale`, MAX(`scale`) AS `maxscale` - FROM `photo` WHERE `uid` = ? AND `resource-id` = ? $scale_sql GROUP BY - `resource-id`, `created`, `edited`, `title`, `desc`, `album`, `filename`, - `type`, `height`, `width`, `datasize`, `profile`, `allow_cid`, `deny_cid`, `allow_gid`, `deny_gid`", - $uid, - $photo_id - )); - - // prepare output data for photo - if (DBA::isResult($r)) { - $data = ['photo' => $r[0]]; - $data['photo']['id'] = $data['photo']['resource-id']; - if ($scale !== false) { - $data['photo']['data'] = base64_encode($data['photo']['data']); - } else { - unset($data['photo']['datasize']); //needed only with scale param - } - if ($type == "xml") { - $data['photo']['links'] = []; - for ($k = intval($data['photo']['minscale']); $k <= intval($data['photo']['maxscale']); $k++) { - $data['photo']['links'][$k . ":link"]["@attributes"] = ["type" => $data['photo']['type'], - "scale" => $k, - "href" => DI::baseUrl() . "/photo/" . $data['photo']['resource-id'] . "-" . $k . Images::getExtensionByMimeType($data['photo']['type'])]; - } - } else { - $data['photo']['link'] = []; - // when we have profile images we could have only scales from 4 to 6, but index of array always needs to start with 0 - $i = 0; - for ($k = intval($data['photo']['minscale']); $k <= intval($data['photo']['maxscale']); $k++) { - $data['photo']['link'][$i] = DI::baseUrl() . "/photo/" . $data['photo']['resource-id'] . "-" . $k . Images::getExtensionByMimeType($data['photo']['type']); - $i++; - } - } - unset($data['photo']['resource-id']); - unset($data['photo']['minscale']); - unset($data['photo']['maxscale']); - } else { - throw new HTTPException\NotFoundException(); - } - - // retrieve item element for getting activities (like, dislike etc.) related to photo - $condition = ['uid' => $uid, 'resource-id' => $photo_id]; - $item = Post::selectFirst(['id', 'uid', 'uri', 'uri-id', 'parent', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid'], $condition); - if (!DBA::isResult($item)) { - throw new HTTPException\NotFoundException('Photo-related item not found.'); - } - - $data['photo']['friendica_activities'] = DI::friendicaActivities()->createFromUriId($item['uri-id'], $item['uid'], $type); - - // retrieve comments on photo - $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)", - $item['parent'], $uid, GRAVITY_PARENT, GRAVITY_COMMENT]; - - $statuses = Post::selectForUser($uid, [], $condition); - - // prepare output of comments - $commentData = []; - while ($status = DBA::fetch($statuses)) { - $commentData[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'])->toArray(); - } - DBA::close($statuses); - - $comments = []; - if ($type == "xml") { - $k = 0; - foreach ($commentData as $comment) { - $comments[$k++ . ":comment"] = $comment; - } - } else { - foreach ($commentData as $comment) { - $comments[] = $comment; - } - } - $data['photo']['friendica_comments'] = $comments; - - // include info if rights on photo and rights on item are mismatching - $rights_mismatch = $data['photo']['allow_cid'] != $item['allow_cid'] || - $data['photo']['deny_cid'] != $item['deny_cid'] || - $data['photo']['allow_gid'] != $item['allow_gid'] || - $data['photo']['deny_gid'] != $item['deny_gid']; - $data['photo']['rights_mismatch'] = $rights_mismatch; - - return $data; -} - -/** - * TWITTER API - */ - -/** - * 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) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $ret = []; - /// @TODO $ret is not filled here? - return DI::apiResponse()->formatData('lists', $type, ["lists_list" => $ret]); -} - -api_register_func('api/lists/list', 'api_lists_list', true); -api_register_func('api/lists/subscriptions', 'api_lists_list', true); - -/** - * Returns all groups the user owns. - * - * @param string $type Return type (atom, rss, xml, json) - * - * @return array|string - * @throws HTTPException\BadRequestException - * @throws HTTPException\ForbiddenException - * @throws ImagickException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\UnauthorizedException - * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/get-lists-ownerships - */ -function api_lists_ownerships($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $uid = BaseApi::getCurrentUserID(); - - // params - $user_info = DI::twitterUser()->createFromUserId($uid, true)->toArray(); - - $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 DI::apiResponse()->formatData("lists", $type, ['lists' => ['lists' => $lists]]); -} - -api_register_func('api/lists/ownerships', 'api_lists_ownerships', true); - -/** - * list all photos of the authenticated user - * - * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' - * @return string|array - * @throws HTTPException\ForbiddenException - * @throws HTTPException\InternalServerErrorException - */ -function api_fr_photos_list($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $uid = BaseApi::getCurrentUserID(); - - $r = DBA::toArray(DBA::p( - "SELECT `resource-id`, MAX(scale) AS `scale`, `album`, `filename`, `type`, MAX(`created`) AS `created`, - MAX(`edited`) AS `edited`, MAX(`desc`) AS `desc` FROM `photo` - WHERE `uid` = ? AND NOT `photo-type` IN (?, ?) GROUP BY `resource-id`, `album`, `filename`, `type`", - $uid, Photo::CONTACT_AVATAR, Photo::CONTACT_BANNER - )); - - $data = ['photo'=>[]]; - if (DBA::isResult($r)) { - foreach ($r as $rr) { - $photo = []; - $photo['id'] = $rr['resource-id']; - $photo['album'] = $rr['album']; - $photo['filename'] = $rr['filename']; - $photo['type'] = $rr['type']; - $thumb = DI::baseUrl() . "/photo/" . $rr['resource-id'] . "-" . $rr['scale'] . Images::getExtensionByMimeType($rr['type']); - $photo['created'] = $rr['created']; - $photo['edited'] = $rr['edited']; - $photo['desc'] = $rr['desc']; - - if ($type == "xml") { - $data['photo'][] = ["@attributes" => $photo, "1" => $thumb]; - } else { - $photo['thumb'] = $thumb; - $data['photo'][] = $photo; - } - } - } - return DI::apiResponse()->formatData("photos", $type, $data); -} - -api_register_func('api/friendica/photos/list', 'api_fr_photos_list', true); - -/** - * upload a new photo or change an existing photo - * - * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' - * @return string|array - * @throws HTTPException\BadRequestException - * @throws HTTPException\ForbiddenException - * @throws ImagickException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\NotFoundException - */ -function api_fr_photo_create_update($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - // input params - $photo_id = $_REQUEST['photo_id'] ?? null; - $desc = $_REQUEST['desc'] ?? null; - $album = $_REQUEST['album'] ?? null; - $album_new = $_REQUEST['album_new'] ?? null; - $allow_cid = $_REQUEST['allow_cid'] ?? null; - $deny_cid = $_REQUEST['deny_cid' ] ?? null; - $allow_gid = $_REQUEST['allow_gid'] ?? null; - $deny_gid = $_REQUEST['deny_gid' ] ?? null; - - // do several checks on input parameters - // we do not allow calls without album string - if ($album == null) { - throw new HTTPException\BadRequestException("no albumname specified"); - } - // if photo_id == null --> we are uploading a new photo - if ($photo_id == null) { - $mode = "create"; - - // error if no media posted in create-mode - if (empty($_FILES['media'])) { - // Output error - throw new HTTPException\BadRequestException("no media data submitted"); - } - - // album_new will be ignored in create-mode - $album_new = ""; - } else { - $mode = "update"; - - // check if photo is existing in databasei - if (!Photo::exists(['resource-id' => $photo_id, 'uid' => $uid, 'album' => $album])) { - throw new HTTPException\BadRequestException("photo not available"); - } - } - - // checks on acl strings provided by clients - $acl_input_error = false; - $acl_input_error |= !ACL::isValidContact($allow_cid, $uid); - $acl_input_error |= !ACL::isValidContact($deny_cid, $uid); - $acl_input_error |= !ACL::isValidGroup($allow_gid, $uid); - $acl_input_error |= !ACL::isValidGroup($deny_gid, $uid); - if ($acl_input_error) { - throw new HTTPException\BadRequestException("acl data invalid"); - } - // now let's upload the new media in create-mode - if ($mode == "create") { - $photo = Photo::upload($uid, $_FILES['media'], $album, trim($allow_cid), trim($allow_gid), trim($deny_cid), trim($deny_gid), $desc); - - // return success of updating or error message - if (!empty($photo)) { - $data = prepare_photo_data($type, false, $photo['resource_id'], $uid); - return DI::apiResponse()->formatData("photo_create", $type, $data); - } else { - throw new HTTPException\InternalServerErrorException("unknown error - uploading photo failed, see Friendica log for more information"); - } - } - - // now let's do the changes in update-mode - if ($mode == "update") { - $updated_fields = []; - - if (!is_null($desc)) { - $updated_fields['desc'] = $desc; - } - - if (!is_null($album_new)) { - $updated_fields['album'] = $album_new; - } - - if (!is_null($allow_cid)) { - $allow_cid = trim($allow_cid); - $updated_fields['allow_cid'] = $allow_cid; - } - - if (!is_null($deny_cid)) { - $deny_cid = trim($deny_cid); - $updated_fields['deny_cid'] = $deny_cid; - } - - if (!is_null($allow_gid)) { - $allow_gid = trim($allow_gid); - $updated_fields['allow_gid'] = $allow_gid; - } - - if (!is_null($deny_gid)) { - $deny_gid = trim($deny_gid); - $updated_fields['deny_gid'] = $deny_gid; - } - - $result = false; - if (count($updated_fields) > 0) { - $nothingtodo = false; - $result = Photo::update($updated_fields, ['uid' => $uid, 'resource-id' => $photo_id, 'album' => $album]); - } else { - $nothingtodo = true; - } - - if (!empty($_FILES['media'])) { - $nothingtodo = false; - $photo = Photo::upload($uid, $_FILES['media'], $album, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc, $photo_id); - if (!empty($photo)) { - $data = prepare_photo_data($type, false, $photo['resource_id'], $uid); - return DI::apiResponse()->formatData("photo_update", $type, $data); - } - } - - // return success of updating or error message - if ($result) { - $answer = ['result' => 'updated', 'message' => 'Image id `' . $photo_id . '` has been updated.']; - return DI::apiResponse()->formatData("photo_update", $type, ['$result' => $answer]); - } else { - if ($nothingtodo) { - $answer = ['result' => 'cancelled', 'message' => 'Nothing to update for image id `' . $photo_id . '`.']; - return DI::apiResponse()->formatData("photo_update", $type, ['$result' => $answer]); - } - throw new HTTPException\InternalServerErrorException("unknown error - update photo entry in database failed"); - } - } - throw new HTTPException\InternalServerErrorException("unknown error - this error on uploading or updating a photo should never happen"); -} - -api_register_func('api/friendica/photo/create', 'api_fr_photo_create_update', true); -api_register_func('api/friendica/photo/update', 'api_fr_photo_create_update', true); - -/** - * 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|array - * @throws HTTPException\BadRequestException - * @throws HTTPException\ForbiddenException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\NotFoundException - */ -function api_fr_photo_detail($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $uid = BaseApi::getCurrentUserID(); - - if (empty($_REQUEST['photo_id'])) { - throw new HTTPException\BadRequestException("No photo id."); - } - - $scale = (!empty($_REQUEST['scale']) ? intval($_REQUEST['scale']) : false); - $photo_id = $_REQUEST['photo_id']; - - // prepare json/xml output with data from database for the requested photo - $data = prepare_photo_data($type, $scale, $photo_id, $uid); - - return DI::apiResponse()->formatData("photo_detail", $type, $data); -} - -api_register_func('api/friendica/photo', 'api_fr_photo_detail', true); - -/** - * 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|array - * @throws HTTPException\BadRequestException - * @throws HTTPException\ForbiddenException - * @throws ImagickException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\NotFoundException - * @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) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - // get mediadata from image or media (Twitter call api/account/update_profile_image provides image) - if (!empty($_FILES['image'])) { - $media = $_FILES['image']; - } elseif (!empty($_FILES['media'])) { - $media = $_FILES['media']; - } - - // error if image data is missing - if (empty($media)) { - throw new HTTPException\BadRequestException("no media data submitted"); - } - - // save new profile image - $resource_id = Photo::uploadAvatar($uid, $media); - if (empty($resource_id)) { - throw new HTTPException\InternalServerErrorException("image upload failed"); - } - - // output for client - $skip_status = $_REQUEST['skip_status'] ?? false; - - $user_info = DI::twitterUser()->createFromUserId($uid, $skip_status)->toArray(); - - // "verified" isn't used here in the standard - unset($user_info["verified"]); - - // "uid" is only needed for some internal stuff, so remove it from here - unset($user_info['uid']); - - return DI::apiResponse()->formatData("user", $type, ['user' => $user_info]); -} - -api_register_func('api/account/update_profile_image', 'api_account_update_profile_image', true); - -/** - * 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 - * @throws HTTPException\BadRequestException - * @throws HTTPException\ForbiddenException - * @throws ImagickException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\UnauthorizedException - */ -function api_friendica_group_show($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); - $uid = BaseApi::getCurrentUserID(); - - // params - $gid = $_REQUEST['gid'] ?? 0; - - // get data of the specified group id or all groups if not specified - if ($gid != 0) { - $groups = DBA::selectToArray('group', [], ['deleted' => false, 'uid' => $uid, 'id' => $gid]); - - // error message if specified gid is not in database - if (!DBA::isResult($groups)) { - throw new HTTPException\BadRequestException("gid not available"); - } - } else { - $groups = DBA::selectToArray('group', [], ['deleted' => false, 'uid' => $uid]); - } - - // loop through all groups and retrieve all members for adding data in the user array - $grps = []; - foreach ($groups as $rr) { - $members = Contact\Group::getById($rr['id']); - $users = []; - - if ($type == "xml") { - $user_element = "users"; - $k = 0; - foreach ($members as $member) { - $user = DI::twitterUser()->createFromContactId($member['contact-id'], $uid, true)->toArray(); - $users[$k++.":user"] = $user; - } - } else { - $user_element = "user"; - foreach ($members as $member) { - $user = DI::twitterUser()->createFromContactId($member['contact-id'], $uid, true)->toArray(); - $users[] = $user; - } - } - $grps[] = ['name' => $rr['name'], 'gid' => $rr['id'], $user_element => $users]; - } - return DI::apiResponse()->formatData("groups", $type, ['group' => $grps]); -} - -api_register_func('api/friendica/group_show', 'api_friendica_group_show', true); - -/** - * Delete a group. - * - * @param string $type Return type (atom, rss, xml, json) - * - * @return array|string - * @throws HTTPException\BadRequestException - * @throws HTTPException\ForbiddenException - * @throws ImagickException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\UnauthorizedException - * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/post-lists-destroy - */ -function api_lists_destroy($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - // params - $gid = $_REQUEST['list_id'] ?? 0; - - // error if no gid specified - if ($gid == 0) { - throw new HTTPException\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 HTTPException\BadRequestException('gid not available'); - } - - if (Group::remove($gid)) { - $list = [ - 'name' => $group['name'], - 'id' => intval($gid), - 'id_str' => (string) $gid, - 'user' => DI::twitterUser()->createFromUserId($uid, true)->toArray() - ]; - - return DI::apiResponse()->formatData("lists", $type, ['lists' => $list]); - } -} - -api_register_func('api/lists/destroy', 'api_lists_destroy', true); - -/** - * Create the specified group with the posted array of contacts. - * - * @param string $type Return type (atom, rss, xml, json) - * - * @return array|string - * @throws HTTPException\BadRequestException - * @throws HTTPException\ForbiddenException - * @throws ImagickException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\UnauthorizedException - */ -function api_friendica_group_create($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - // params - $name = $_REQUEST['name'] ?? ''; - $json = json_decode($_POST['json'], true); - $users = $json['user']; - - // error if no name specified - if ($name == "") { - throw new HTTPException\BadRequestException('group name not specified'); - } - - // error message if specified group name already exists - if (DBA::exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => false])) { - throw new HTTPException\BadRequestException('group name already exists'); - } - - // Check if the group needs to be reactivated - if (DBA::exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => true])) { - $reactivate_group = true; - } - - // create group - $ret = Group::create($uid, $name); - if ($ret) { - $gid = Group::getIdByName($uid, $name); - } else { - throw new HTTPException\BadRequestException('other API error'); - } - - // add members - $erroraddinguser = false; - $errorusers = []; - foreach ($users as $user) { - $cid = $user['cid']; - if (DBA::exists('contact', ['id' => $cid, 'uid' => $uid])) { - Group::addMember($gid, $cid); - } else { - $erroraddinguser = true; - $errorusers[] = $cid; - } - } - - // return success message incl. missing users in array - $status = ($erroraddinguser ? "missing user" : ((isset($reactivate_group) && $reactivate_group) ? "reactivated" : "ok")); - - $result = ['success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers]; - - return DI::apiResponse()->formatData("group_create", $type, ['result' => $result]); -} - -api_register_func('api/friendica/group_create', 'api_friendica_group_create', true); - -/** - * Create a new group. - * - * @param string $type Return type (atom, rss, xml, json) - * - * @return array|string - * @throws HTTPException\BadRequestException - * @throws HTTPException\ForbiddenException - * @throws ImagickException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\UnauthorizedException - * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/post-lists-create - */ -function api_lists_create($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - // params - $name = $_REQUEST['name'] ?? ''; - - if ($name == "") { - throw new HTTPException\BadRequestException('group name not specified'); - } - - // error message if specified group name already exists - if (DBA::exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => false])) { - throw new HTTPException\BadRequestException('group name already exists'); - } - - $ret = Group::create($uid, $name); - if ($ret) { - $gid = Group::getIdByName($uid, $name); - } else { - throw new HTTPException\BadRequestException('other API error'); - } - - $grp = [ - 'name' => $name, - 'id' => intval($gid), - 'id_str' => (string) $gid, - 'user' => DI::twitterUser()->createFromUserId($uid, true)->toArray() - ]; - - return DI::apiResponse()->formatData("lists", $type, ['lists' => $grp]); -} - -api_register_func('api/lists/create', 'api_lists_create', true); - -/** - * Update information about a group. - * - * @param string $type Return type (atom, rss, xml, json) - * - * @return array|string - * @throws HTTPException\BadRequestException - * @throws HTTPException\ForbiddenException - * @throws ImagickException - * @throws HTTPException\InternalServerErrorException - * @throws HTTPException\UnauthorizedException - * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/post-lists-update - */ -function api_lists_update($type) -{ - BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); - $uid = BaseApi::getCurrentUserID(); - - // params - $gid = $_REQUEST['list_id'] ?? 0; - $name = $_REQUEST['name'] ?? ''; - - // error if no gid specified - if ($gid == 0) { - throw new HTTPException\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 HTTPException\BadRequestException('gid not available'); - } - - if (Group::update($gid, $name)) { - $list = [ - 'name' => $name, - 'id' => intval($gid), - 'id_str' => (string) $gid, - 'user' => DI::twitterUser()->createFromUserId($uid, true)->toArray() - ]; - - return DI::apiResponse()->formatData("lists", $type, ['lists' => $list]); - } -} - -api_register_func('api/lists/update', 'api_lists_update', true); diff --git a/src/BaseModule.php b/src/BaseModule.php index 5ac56533ca..c03a77e29e 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -260,19 +260,7 @@ abstract class BaseModule implements ICanHandleRequests $request = []; foreach ($defaults as $parameter => $defaultvalue) { - if (is_string($defaultvalue)) { - $request[$parameter] = $input[$parameter] ?? $defaultvalue; - } elseif (is_int($defaultvalue)) { - $request[$parameter] = (int)($input[$parameter] ?? $defaultvalue); - } elseif (is_float($defaultvalue)) { - $request[$parameter] = (float)($input[$parameter] ?? $defaultvalue); - } elseif (is_array($defaultvalue)) { - $request[$parameter] = $input[$parameter] ?? []; - } elseif (is_bool($defaultvalue)) { - $request[$parameter] = in_array(strtolower($input[$parameter] ?? ''), ['true', '1']); - } else { - $this->logger->notice('Unhandled default value type', ['parameter' => $parameter, 'type' => gettype($defaultvalue)]); - } + $request[$parameter] = $this->getRequestValue($input, $parameter, $defaultvalue); } foreach ($input ?? [] as $parameter => $value) { @@ -288,6 +276,50 @@ abstract class BaseModule implements ICanHandleRequests return $request; } + /** + * Fetch a request value and apply default values and check against minimal and maximal values + * + * @param array $input + * @param string $parameter + * @param mixed $default + * @param mixed $minimal_value + * @param mixed $maximum_value + * @return mixed + */ + public function getRequestValue(array $input, string $parameter, $default = null, $minimal_value = null, $maximum_value = null) + { + if (is_string($default)) { + $value = (string)($input[$parameter] ?? $default); + } elseif (is_int($default)) { + $value = filter_var($input[$parameter] ?? $default, FILTER_VALIDATE_INT); + if (!is_null($minimal_value)) { + $value = max(filter_var($minimal_value, FILTER_VALIDATE_INT), $value); + } + if (!is_null($maximum_value)) { + $value = min(filter_var($maximum_value, FILTER_VALIDATE_INT), $value); + } + } elseif (is_float($default)) { + $value = filter_var($input[$parameter] ?? $default, FILTER_VALIDATE_FLOAT); + if (!is_null($minimal_value)) { + $value = max(filter_var($minimal_value, FILTER_VALIDATE_FLOAT), $value); + } + if (!is_null($maximum_value)) { + $value = min(filter_var($maximum_value, FILTER_VALIDATE_FLOAT), $value); + } + } elseif (is_array($default)) { + $value = filter_var($input[$parameter] ?? $default, FILTER_DEFAULT, ['flags' => FILTER_FORCE_ARRAY]); + } elseif (is_bool($default)) { + $value = filter_var($input[$parameter] ?? $default, FILTER_VALIDATE_BOOLEAN); + } elseif (is_null($default)) { + $value = $input[$parameter] ?? null; + } else { + $this->logger->notice('Unhandled default value type', ['parameter' => $parameter, 'type' => gettype($default)]); + $value = null; + } + + return $value; + } + /* * Functions used to protect against Cross-Site Request Forgery * The security token has to base on at least one value that an attacker can't know - here it's the session ID and the private key. diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index a3244aafc3..1f638c2f52 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1134,7 +1134,7 @@ class BBCode switch ($simplehtml) { case self::API: $text = ($is_quote_share? '
' : '') . - '' . html_entity_decode('♲ ', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ":
\n" . + '' . html_entity_decode('♲', ENT_QUOTES, 'UTF-8') . ' ' . $author_contact['addr'] . ":
\n" . '
' . $content . '
'; break; case self::DIASPORA: diff --git a/src/DI.php b/src/DI.php index 89b102afc3..708fb7d836 100644 --- a/src/DI.php +++ b/src/DI.php @@ -262,14 +262,6 @@ abstract class DI // "Factory" namespace instances // - /** - * @return Factory\Api\Friendica\Activities - */ - public static function friendicaActivities() - { - return self::$dice->create(Factory\Api\Friendica\Activities::class); - } - /** * @return Factory\Api\Mastodon\Account */ diff --git a/src/Factory/Api/Friendica/Activities.php b/src/Factory/Api/Friendica/Activities.php index b4936f3107..7b8f458b04 100644 --- a/src/Factory/Api/Friendica/Activities.php +++ b/src/Factory/Api/Friendica/Activities.php @@ -21,7 +21,6 @@ namespace Friendica\Factory\Api\Friendica; -use Friendica\App\BaseURL; use Friendica\BaseFactory; use Friendica\Database\DBA; use Friendica\Model\Post; @@ -32,17 +31,14 @@ use Friendica\Factory\Api\Twitter\User as TwitterUser; class Activities extends BaseFactory { - /** @var BaseURL */ - private $baseUrl; /** @var twitterUser entity */ private $twitterUser; - public function __construct(LoggerInterface $logger, BaseURL $baseURL, TwitterUser $twitteruser) + public function __construct(LoggerInterface $logger, TwitterUser $twitteruser) { parent::__construct($logger); $this->twitterUser = $twitteruser; - $this->baseUrl = $baseURL; } /** diff --git a/src/Factory/Api/Friendica/Group.php b/src/Factory/Api/Friendica/Group.php new file mode 100644 index 0000000000..868231c98b --- /dev/null +++ b/src/Factory/Api/Friendica/Group.php @@ -0,0 +1,62 @@ +. + * + */ + +namespace Friendica\Factory\Api\Friendica; + +use Friendica\BaseFactory; +use Friendica\Database\Database; +use Friendica\Network\HTTPException; +use Psr\Log\LoggerInterface; +use Friendica\Factory\Api\Twitter\User as TwitterUser; + +class Group extends BaseFactory +{ + /** @var twitterUser entity */ + private $twitterUser; + /** @var Database */ + private $dba; + + public function __construct(LoggerInterface $logger, TwitterUser $twitteruser, Database $dba) + { + parent::__construct($logger); + + $this->twitterUser = $twitteruser; + $this->dba = $dba; + } + + /** + * @param int $id id of the group + * @return array + * @throws HTTPException\InternalServerErrorException + */ + public function createFromId(int $id): array + { + $group = $this->dba->selectFirst('group', [], ['id' => $id, 'deleted' => false]); + if (empty($group)) { + return []; + } + + $user = $this->twitterUser->createFromUserId($group['uid'])->toArray(); + $object = new \Friendica\Object\Api\Friendica\Group($group, $user); + + return $object->toArray(); + } +} diff --git a/src/Factory/Api/Friendica/Photo.php b/src/Factory/Api/Friendica/Photo.php new file mode 100644 index 0000000000..dad90d13f1 --- /dev/null +++ b/src/Factory/Api/Friendica/Photo.php @@ -0,0 +1,156 @@ +. + * + */ + +namespace Friendica\Factory\Api\Friendica; + +use Friendica\App\BaseURL; +use Friendica\BaseFactory; +use Friendica\Database\DBA; +use Friendica\Factory\Api\Twitter\Status; +use Friendica\Model\Photo as ModelPhoto; +use Friendica\Model\Post; +use Friendica\Network\HTTPException; +use Psr\Log\LoggerInterface; +use Friendica\Util\Images; + +class Photo extends BaseFactory +{ + /** @var BaseURL */ + private $baseUrl; + /** @var Status */ + private $status; + /** @var Activities */ + private $activities; + + public function __construct(LoggerInterface $logger, BaseURL $baseURL, Status $status, Activities $activities) + { + parent::__construct($logger); + + $this->activities = $activities; + $this->status = $status; + $this->baseUrl = $baseURL; + } + + /** + * @param string $photo_id + * @param int $scale + * @param int $uid + * @param string $type + * @return Array + */ + public function createFromId(string $photo_id, int $scale = null, int $uid, string $type = 'json', bool $with_posts = true): array + { + $fields = ['resource-id', 'created', 'edited', 'title', 'desc', 'album', 'filename','type', + 'height', 'width', 'datasize', 'profile', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid', + 'backend-class', 'backend-ref', 'id', 'scale']; + + $condition = ['uid' => $uid, 'resource-id' => $photo_id]; + if (is_int($scale)) { + $fields = array_merge(['data'], $fields); + + $condition['scale'] = $scale; + } + + $photos = ModelPhoto::selectToArray($fields, $condition); + if (empty($photos)) { + throw new HTTPException\NotFoundException(); + } + $data = $photos[0]; + $data['id'] = $data['resource-id']; + if (is_int($scale)) { + $data['data'] = base64_encode(ModelPhoto::getImageDataForPhoto($data)); + } else { + unset($data['datasize']); //needed only with scale param + } + + if ($type == 'xml') { + $data['links'] = []; + } else { + $data['link'] = []; + } + + foreach ($photos as $id => $photo) { + $link = $this->baseUrl->get() . '/photo/' . $data['resource-id'] . '-' . $photo['scale'] . Images::getExtensionByMimeType($data['type']); + if ($type == 'xml') { + $data['links'][$photo['scale'] . ':link']['@attributes'] = [ + 'type' => $data['type'], + 'scale' => $photo['scale'], + 'href' => $link + ]; + } else { + $data['link'][$id] = $link; + } + } + + unset($data['backend-class']); + unset($data['backend-ref']); + unset($data['resource-id']); + unset($data['scale']); + + if ($with_posts) { + // retrieve item element for getting activities (like, dislike etc.) related to photo + $condition = ['uid' => $uid, 'resource-id' => $photo_id]; + + $item = Post::selectFirst(['id', 'uid', 'uri', 'uri-id', 'parent', 'allow_cid', 'deny_cid', 'allow_gid', 'deny_gid'], $condition); + } + if (!empty($item)) { + $data['friendica_activities'] = $this->activities->createFromUriId($item['uri-id'], $item['uid'], $type); + + // retrieve comments on photo + $condition = ["`parent` = ? AND `uid` = ? AND `gravity` IN (?, ?)", + $item['parent'], $uid, GRAVITY_PARENT, GRAVITY_COMMENT]; + + $statuses = Post::selectForUser($uid, [], $condition); + + // prepare output of comments + $commentData = []; + while ($status = DBA::fetch($statuses)) { + $commentData[] = $this->status->createFromUriId($status['uri-id'], $status['uid'])->toArray(); + } + DBA::close($statuses); + + $comments = []; + if ($type == 'xml') { + $k = 0; + foreach ($commentData as $comment) { + $comments[$k++ . ':comment'] = $comment; + } + } else { + foreach ($commentData as $comment) { + $comments[] = $comment; + } + } + $data['friendica_comments'] = $comments; + + // include info if rights on photo and rights on item are mismatching + $data['rights_mismatch'] = $data['allow_cid'] != $item['allow_cid'] || + $data['deny_cid'] != $item['deny_cid'] || + $data['allow_gid'] != $item['allow_gid'] || + $data['deny_gid'] != $item['deny_gid']; + } elseif ($with_posts) { + $data['friendica_activities'] = []; + $data['friendica_comments'] = []; + $data['rights_mismatch'] = false; + } + + return $data; + } +} diff --git a/src/Module/Api/Friendica/Activity.php b/src/Module/Api/Friendica/Activity.php index 70aeee613f..3c892b3030 100644 --- a/src/Module/Api/Friendica/Activity.php +++ b/src/Module/Api/Friendica/Activity.php @@ -52,7 +52,7 @@ class Activity extends BaseApi $res = Item::performActivity($request['id'], $this->parameters['verb'], $uid); if ($res) { - if (!empty($this->parameters['extension']) && ($this->parameters['extension'] == 'xml')) { + if (($this->parameters['extension'] ?? '') == 'xml') { $ok = 'true'; } else { $ok = 'ok'; diff --git a/src/Module/Api/Friendica/Group/Create.php b/src/Module/Api/Friendica/Group/Create.php new file mode 100644 index 0000000000..3b0f030135 --- /dev/null +++ b/src/Module/Api/Friendica/Group/Create.php @@ -0,0 +1,87 @@ +. + * + */ + +namespace Friendica\Module\Api\Friendica\Group; + +use Friendica\Database\DBA; +use Friendica\Model\Group; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; + +/** + * API endpoint: /api/friendica/group_create + */ +class Create extends BaseApi +{ + protected function post(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + // params + $name = $this->getRequestValue($request, 'name', ''); + $json = json_decode($request['json'], true); + $users = $json['user']; + + // error if no name specified + if ($name == '') { + throw new HTTPException\BadRequestException('group name not specified'); + } + + // error message if specified group name already exists + if (DBA::exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => false])) { + throw new HTTPException\BadRequestException('group name already exists'); + } + + // Check if the group needs to be reactivated + if (DBA::exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => true])) { + $reactivate_group = true; + } + + // create group + $ret = Group::create($uid, $name); + if ($ret) { + $gid = Group::getIdByName($uid, $name); + } else { + throw new HTTPException\BadRequestException('other API error'); + } + + // add members + $erroraddinguser = false; + $errorusers = []; + foreach ($users as $user) { + $cid = $user['cid']; + if (DBA::exists('contact', ['id' => $cid, 'uid' => $uid])) { + Group::addMember($gid, $cid); + } else { + $erroraddinguser = true; + $errorusers[] = $cid; + } + } + + // return success message incl. missing users in array + $status = ($erroraddinguser ? 'missing user' : ((isset($reactivate_group) && $reactivate_group) ? 'reactivated' : 'ok')); + + $result = ['success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers]; + + $this->response->exit('group_create', ['$result' => $result], $this->parameters['extension'] ?? null); + } +} diff --git a/src/Module/Api/Friendica/Group/Delete.php b/src/Module/Api/Friendica/Group/Delete.php index e845bd7a73..d48e9a4f9d 100644 --- a/src/Module/Api/Friendica/Group/Delete.php +++ b/src/Module/Api/Friendica/Group/Delete.php @@ -22,7 +22,6 @@ namespace Friendica\Module\Api\Friendica\Group; use Friendica\Database\DBA; -use Friendica\DI; use Friendica\Model\Group; use Friendica\Module\BaseApi; use Friendica\Network\HTTPException\BadRequestException; diff --git a/src/Module/Api/Friendica/Group/Show.php b/src/Module/Api/Friendica/Group/Show.php new file mode 100644 index 0000000000..d150827694 --- /dev/null +++ b/src/Module/Api/Friendica/Group/Show.php @@ -0,0 +1,79 @@ +. + * + */ + +namespace Friendica\Module\Api\Friendica\Group; + +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\Contact; +use Friendica\Module\BaseApi; +use Friendica\Network\HTTPException; + +/** + * API endpoint: /api/friendica/group_show + */ +class Show extends BaseApi +{ + protected function post(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); + $type = $this->getRequestValue($this->parameters, 'extension', 'json'); + + // params + $gid = $this->getRequestValue($request, 'gid', 0); + + // get data of the specified group id or all groups if not specified + if ($gid != 0) { + $groups = DBA::selectToArray('group', [], ['deleted' => false, 'uid' => $uid, 'id' => $gid]); + + // error message if specified gid is not in database + if (!DBA::isResult($groups)) { + throw new HTTPException\BadRequestException('gid not available'); + } + } else { + $groups = DBA::selectToArray('group', [], ['deleted' => false, 'uid' => $uid]); + } + + // loop through all groups and retrieve all members for adding data in the user array + $grps = []; + foreach ($groups as $rr) { + $members = Contact\Group::getById($rr['id']); + $users = []; + + if ($type == 'xml') { + $user_element = 'users'; + $k = 0; + foreach ($members as $member) { + $users[$k++.':user'] = DI::twitterUser()->createFromContactId($member['contact-id'], $uid, true)->toArray(); + } + } else { + $user_element = 'user'; + foreach ($members as $member) { + $users[] = DI::twitterUser()->createFromContactId($member['contact-id'], $uid, true)->toArray(); + } + } + $grps[] = ['name' => $rr['name'], 'gid' => $rr['id'], $user_element => $users]; + } + + $this->response->exit('group_update', ['group' => $grps], $this->parameters['extension'] ?? null); + } +} diff --git a/src/Module/Api/Friendica/Group/Update.php b/src/Module/Api/Friendica/Group/Update.php index b17e29c1bf..defbeed1c1 100644 --- a/src/Module/Api/Friendica/Group/Update.php +++ b/src/Module/Api/Friendica/Group/Update.php @@ -38,9 +38,9 @@ class Update extends BaseApi $uid = BaseApi::getCurrentUserID(); // params - $gid = $request['gid'] ?? 0; - $name = $request['name'] ?? ''; - $json = json_decode($_POST['json'], true); + $gid = $this->getRequestValue($request, 'gid', 0); + $name = $this->getRequestValue($request, 'name', ''); + $json = json_decode($request['json'], true); $users = $json['user']; // error if no name specified diff --git a/src/Module/Api/Friendica/Notification.php b/src/Module/Api/Friendica/Notification.php index 1f5ac9b488..46320df153 100644 --- a/src/Module/Api/Friendica/Notification.php +++ b/src/Module/Api/Friendica/Notification.php @@ -43,7 +43,7 @@ class Notification extends BaseApi $notifications[] = new ApiNotification($Notify); } - if (!empty($this->parameters['extension']) && ($this->parameters['extension'] == 'xml')) { + if (($this->parameters['extension'] ?? '') == 'xml') { $xmlnotes = []; foreach ($notifications as $notification) { $xmlnotes[] = ['@attributes' => $notification->toArray()]; diff --git a/src/Module/Api/Friendica/Notification/Seen.php b/src/Module/Api/Friendica/Notification/Seen.php index e293372624..7a52a124df 100644 --- a/src/Module/Api/Friendica/Notification/Seen.php +++ b/src/Module/Api/Friendica/Notification/Seen.php @@ -47,7 +47,9 @@ class Seen extends BaseApi throw new BadRequestException('Invalid argument count'); } - $id = intval($_REQUEST['id'] ?? 0); + $id = intval($request['id'] ?? 0); + + $include_entities = $this->getRequestValue($request, 'include_entities', false); try { $Notify = DI::notify()->selectOneById($id); @@ -65,8 +67,6 @@ class Seen extends BaseApi if ($Notify->otype === Notification\ObjectType::ITEM) { $item = Post::selectFirstForUser($uid, [], ['id' => $Notify->iid, 'uid' => $uid]); if (DBA::isResult($item)) { - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); - // we found the item, return it to the user $ret = [DI::twitterStatus()->createFromUriId($item['uri-id'], $item['uid'], $include_entities)->toArray()]; $data = ['status' => $ret]; diff --git a/src/Module/Api/Friendica/Photo.php b/src/Module/Api/Friendica/Photo.php new file mode 100644 index 0000000000..ba87081d45 --- /dev/null +++ b/src/Module/Api/Friendica/Photo.php @@ -0,0 +1,65 @@ +. + * + */ + +namespace Friendica\Module\Api\Friendica; + +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto; +use Friendica\Module\BaseApi; +use Friendica\Model\Contact; +use Friendica\Module\Api\ApiResponse; +use Friendica\Network\HTTPException; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +class Photo extends BaseApi +{ + /** @var FriendicaPhoto */ + private $friendicaPhoto; + + + public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->friendicaPhoto = $friendicaPhoto; + } + + protected function post(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); + $type = $this->getRequestValue($this->parameters, 'extension', 'json'); + + if (empty($request['photo_id'])) { + throw new HTTPException\BadRequestException('No photo id.'); + } + + $scale = (!empty($request['scale']) ? intval($request['scale']) : false); + $photo_id = $request['photo_id']; + + // prepare json/xml output with data from database for the requested photo + $data = ['photo' => $this->friendicaPhoto->createFromId($photo_id, $scale, $uid, $type)]; + + $this->response->exit('statuses', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid)); + } +} diff --git a/src/Module/Api/Friendica/Photo/Create.php b/src/Module/Api/Friendica/Photo/Create.php new file mode 100644 index 0000000000..c207820b0f --- /dev/null +++ b/src/Module/Api/Friendica/Photo/Create.php @@ -0,0 +1,97 @@ +. + * + */ + +namespace Friendica\Module\Api\Friendica\Photo; + +use Friendica\App; +use Friendica\Core\ACL; +use Friendica\Core\L10n; +use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto; +use Friendica\Module\BaseApi; +use Friendica\Model\Photo; +use Friendica\Module\Api\ApiResponse; +use Friendica\Network\HTTPException; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +/** + * API endpoint: /api/friendica/photo/create + */ +class Create extends BaseApi +{ + /** @var FriendicaPhoto */ + private $friendicaPhoto; + + + public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->friendicaPhoto = $friendicaPhoto; + } + + protected function post(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + $type = $this->getRequestValue($this->parameters, 'extension', 'json'); + + // input params + $desc = $this->getRequestValue($request, 'desc'); + $album = $this->getRequestValue($request, 'album'); + $allow_cid = $this->getRequestValue($request, 'allow_cid'); + $deny_cid = $this->getRequestValue($request, 'deny_cid'); + $allow_gid = $this->getRequestValue($request, 'allow_gid'); + $deny_gid = $this->getRequestValue($request, 'deny_gid'); + + // do several checks on input parameters + // we do not allow calls without album string + if ($album == null) { + throw new HTTPException\BadRequestException('no albumname specified'); + } + + // error if no media posted in create-mode + if (empty($_FILES['media'])) { + // Output error + throw new HTTPException\BadRequestException('no media data submitted'); + } + + // checks on acl strings provided by clients + $acl_input_error = false; + $acl_input_error |= !ACL::isValidContact($allow_cid, $uid); + $acl_input_error |= !ACL::isValidContact($deny_cid, $uid); + $acl_input_error |= !ACL::isValidGroup($allow_gid, $uid); + $acl_input_error |= !ACL::isValidGroup($deny_gid, $uid); + if ($acl_input_error) { + throw new HTTPException\BadRequestException('acl data invalid'); + } + // now let's upload the new media in create-mode + $photo = Photo::upload($uid, $_FILES['media'], $album, trim($allow_cid), trim($allow_gid), trim($deny_cid), trim($deny_gid), $desc); + + // return success of updating or error message + if (!empty($photo)) { + $data = ['photo' => $this->friendicaPhoto->createFromId($photo['resource_id'], null, $uid, $type)]; + $this->response->exit('photo_create', $data, $this->parameters['extension'] ?? null); + } else { + throw new HTTPException\InternalServerErrorException('unknown error - uploading photo failed, see Friendica log for more information'); + } + } +} diff --git a/src/Module/Api/Friendica/Photo/Lists.php b/src/Module/Api/Friendica/Photo/Lists.php new file mode 100644 index 0000000000..350d98a1d7 --- /dev/null +++ b/src/Module/Api/Friendica/Photo/Lists.php @@ -0,0 +1,82 @@ +. + * + */ + +namespace Friendica\Module\Api\Friendica\Photo; + +use Friendica\Database\DBA; +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto; +use Friendica\Module\BaseApi; +use Friendica\Model\Contact; +use Friendica\Model\Photo; +use Friendica\Module\Api\ApiResponse; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +/** + * Returns all lists the user subscribes to. + * + * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/get-lists-list + */ +class Lists extends BaseApi +{ + /** @var FriendicaPhoto */ + private $friendicaPhoto; + + + public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->friendicaPhoto = $friendicaPhoto; + } + + protected function rawContent(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); + $type = $this->getRequestValue($this->parameters, 'extension', 'json'); + + $photos = Photo::selectToArray(['resource-id'], ["`uid` = ? AND NOT `photo-type` IN (?, ?)", $uid, Photo::CONTACT_AVATAR, Photo::CONTACT_BANNER], + ['order' => ['id'], 'group_by' => ['resource-id']]); + + $data = ['photo' => []]; + if (DBA::isResult($photos)) { + foreach ($photos as $photo) { + $element = $this->friendicaPhoto->createFromId($photo['resource-id'], null, $uid, 'json', false); + + $element['thumb'] = end($element['link']); + unset($element['link']); + + if ($type == 'xml') { + $thumb = $element['thumb']; + unset($element['thumb']); + $data['photo'][] = ['@attributes' => $element, '1' => $thumb]; + } else { + $data['photo'][] = $element; + } + } + } + + $this->response->exit('statuses', $data, $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid)); + } +} diff --git a/src/Module/Api/Friendica/Photo/Update.php b/src/Module/Api/Friendica/Photo/Update.php new file mode 100644 index 0000000000..44fd554b09 --- /dev/null +++ b/src/Module/Api/Friendica/Photo/Update.php @@ -0,0 +1,152 @@ +. + * + */ + +namespace Friendica\Module\Api\Friendica\Photo; + +use Friendica\App; +use Friendica\Core\ACL; +use Friendica\Core\L10n; +use Friendica\Factory\Api\Friendica\Photo as FriendicaPhoto; +use Friendica\Module\BaseApi; +use Friendica\Model\Photo; +use Friendica\Module\Api\ApiResponse; +use Friendica\Network\HTTPException; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +/** + * API endpoint: /api/friendica/photo/update + */ +class Update extends BaseApi +{ + /** @var FriendicaPhoto */ + private $friendicaPhoto; + + + public function __construct(FriendicaPhoto $friendicaPhoto, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->friendicaPhoto = $friendicaPhoto; + } + + protected function post(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + $type = $this->getRequestValue($this->parameters, 'extension', 'json'); + + // input params + $photo_id = $this->getRequestValue($request, 'photo_id'); + $desc = $this->getRequestValue($request, 'desc'); + $album = $this->getRequestValue($request, 'album'); + $album_new = $this->getRequestValue($request, 'album_new'); + $allow_cid = $this->getRequestValue($request, 'allow_cid'); + $deny_cid = $this->getRequestValue($request, 'deny_cid'); + $allow_gid = $this->getRequestValue($request, 'allow_gid'); + $deny_gid = $this->getRequestValue($request, 'deny_gid'); + + // do several checks on input parameters + // we do not allow calls without album string + if ($album == null) { + throw new HTTPException\BadRequestException('no albumname specified'); + } + + // check if photo is existing in databasei + if (!Photo::exists(['resource-id' => $photo_id, 'uid' => $uid, 'album' => $album])) { + throw new HTTPException\BadRequestException('photo not available'); + } + + // checks on acl strings provided by clients + $acl_input_error = false; + $acl_input_error |= !ACL::isValidContact($allow_cid, $uid); + $acl_input_error |= !ACL::isValidContact($deny_cid, $uid); + $acl_input_error |= !ACL::isValidGroup($allow_gid, $uid); + $acl_input_error |= !ACL::isValidGroup($deny_gid, $uid); + if ($acl_input_error) { + throw new HTTPException\BadRequestException('acl data invalid'); + } + + $updated_fields = []; + + if (!is_null($desc)) { + $updated_fields['desc'] = $desc; + } + + if (!is_null($album_new)) { + $updated_fields['album'] = $album_new; + } + + if (!is_null($allow_cid)) { + $allow_cid = trim($allow_cid); + $updated_fields['allow_cid'] = $allow_cid; + } + + if (!is_null($deny_cid)) { + $deny_cid = trim($deny_cid); + $updated_fields['deny_cid'] = $deny_cid; + } + + if (!is_null($allow_gid)) { + $allow_gid = trim($allow_gid); + $updated_fields['allow_gid'] = $allow_gid; + } + + if (!is_null($deny_gid)) { + $deny_gid = trim($deny_gid); + $updated_fields['deny_gid'] = $deny_gid; + } + + $result = false; + if (count($updated_fields) > 0) { + $nothingtodo = false; + $result = Photo::update($updated_fields, ['uid' => $uid, 'resource-id' => $photo_id, 'album' => $album]); + } else { + $nothingtodo = true; + } + + if (!empty($_FILES['media'])) { + $nothingtodo = false; + $photo = Photo::upload($uid, $_FILES['media'], $album, $allow_cid, $allow_gid, $deny_cid, $deny_gid, $desc, $photo_id); + if (!empty($photo)) { + $data = ['photo' => $this->friendicaPhoto->createFromId($photo['resource_id'], null, $uid, $type)]; + $this->response->exit('photo_update', $data, $this->parameters['extension'] ?? null); + return; + } + } + + // return success of updating or error message + if ($result) { + $answer = ['result' => 'updated', 'message' => 'Image id `' . $photo_id . '` has been updated.']; + $this->response->exit('photo_update', ['$result' => $answer], $this->parameters['extension'] ?? null); + return; + } else { + if ($nothingtodo) { + $answer = ['result' => 'cancelled', 'message' => 'Nothing to update for image id `' . $photo_id . '`.']; + $this->response->exit('photo_update', ['$result' => $answer], $this->parameters['extension'] ?? null); + return; + } + throw new HTTPException\InternalServerErrorException('unknown error - update photo entry in database failed'); + } + + throw new HTTPException\InternalServerErrorException('unknown error - this error on uploading or updating a photo should never happen'); + } +} diff --git a/src/Module/Api/Friendica/Profile/Show.php b/src/Module/Api/Friendica/Profile/Show.php index 60f4f024b0..2d686d9cae 100644 --- a/src/Module/Api/Friendica/Profile/Show.php +++ b/src/Module/Api/Friendica/Profile/Show.php @@ -48,7 +48,7 @@ class Show extends BaseApi $profile = self::formatProfile($profile, $profileFields); $profiles = []; - if (!empty($this->parameters['extension']) && ($this->parameters['extension'] == 'xml')) { + if (($this->parameters['extension'] ?? '') == 'xml') { $profiles['0:profile'] = $profile; } else { $profiles[] = $profile; diff --git a/src/Module/Api/GNUSocial/Help/Test.php b/src/Module/Api/GNUSocial/Help/Test.php index 4cfeda6aba..ecf99c78bf 100644 --- a/src/Module/Api/GNUSocial/Help/Test.php +++ b/src/Module/Api/GNUSocial/Help/Test.php @@ -31,7 +31,7 @@ class Test extends BaseApi { protected function rawContent(array $request = []) { - if (!empty($this->parameters['extension']) && ($this->parameters['extension'] == 'xml')) { + if (($this->parameters['extension'] ?? '') == 'xml') { $ok = 'true'; } else { $ok = 'ok'; diff --git a/src/Module/Api/GNUSocial/Statusnet/Conversation.php b/src/Module/Api/GNUSocial/Statusnet/Conversation.php index 76bc9b8165..08cfe82fb9 100644 --- a/src/Module/Api/GNUSocial/Statusnet/Conversation.php +++ b/src/Module/Api/GNUSocial/Statusnet/Conversation.php @@ -40,16 +40,17 @@ class Conversation extends BaseApi $uid = BaseApi::getCurrentUserID(); // params - $id = $this->parameters['id'] ?? 0; - $since_id = $_REQUEST['since_id'] ?? 0; - $max_id = $_REQUEST['max_id'] ?? 0; - $count = $_REQUEST['count'] ?? 20; - $page = $_REQUEST['page'] ?? 1; + $id = $this->getRequestValue($this->parameters, 'id', 0); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $page = $this->getRequestValue($request, 'page', 1, 1); + $include_entities = $this->getRequestValue($request, 'include_entities', false); $start = max(0, ($page - 1) * $count); if ($id == 0) { - $id = $_REQUEST['id'] ?? 0; + $id = $request['id'] ?? 0; } Logger::info(BaseApi::LOG_PREFIX . '{subaction}', ['module' => 'api', 'action' => 'conversation', 'subaction' => 'show', 'id' => $id]); @@ -82,8 +83,6 @@ class Conversation extends BaseApi throw new BadRequestException("There is no status with id $id."); } - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); - $ret = []; while ($status = DBA::fetch($statuses)) { $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray(); diff --git a/src/Module/Api/Mastodon/Media.php b/src/Module/Api/Mastodon/Media.php index d6fcba3e41..1253026fdd 100644 --- a/src/Module/Api/Mastodon/Media.php +++ b/src/Module/Api/Mastodon/Media.php @@ -37,7 +37,7 @@ class Media extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - Logger::info('Photo post', ['request' => $_REQUEST, 'files' => $_FILES]); + Logger::info('Photo post', ['request' => $request, 'files' => $_FILES]); if (empty($_FILES['file'])) { DI::mstdnError()->UnprocessableEntity(); diff --git a/src/Module/Api/Twitter/Account/RateLimitStatus.php b/src/Module/Api/Twitter/Account/RateLimitStatus.php index 7a48f4f0e0..c5f052b394 100644 --- a/src/Module/Api/Twitter/Account/RateLimitStatus.php +++ b/src/Module/Api/Twitter/Account/RateLimitStatus.php @@ -31,7 +31,7 @@ class RateLimitStatus extends BaseApi { protected function rawContent(array $request = []) { - if (!empty($this->parameters['extension']) && ($this->parameters['extension'] == 'xml')) { + if (($this->parameters['extension'] ?? '') == 'xml') { $hash = [ 'remaining-hits' => '150', '@attributes' => ["type" => "integer"], diff --git a/src/Module/Api/Twitter/Account/UpdateProfile.php b/src/Module/Api/Twitter/Account/UpdateProfile.php index 079d58f4be..06ebf3db1e 100644 --- a/src/Module/Api/Twitter/Account/UpdateProfile.php +++ b/src/Module/Api/Twitter/Account/UpdateProfile.php @@ -56,7 +56,7 @@ class UpdateProfile extends BaseApi Profile::publishUpdate($uid); - $skip_status = $request['skip_status'] ?? false; + $skip_status = $this->getRequestValue($request, 'skip_status', false); $user_info = DI::twitterUser()->createFromUserId($uid, $skip_status)->toArray(); diff --git a/src/Module/Api/Twitter/Account/UpdateProfileImage.php b/src/Module/Api/Twitter/Account/UpdateProfileImage.php new file mode 100644 index 0000000000..5af835289a --- /dev/null +++ b/src/Module/Api/Twitter/Account/UpdateProfileImage.php @@ -0,0 +1,72 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter\Account; + +use Friendica\Module\BaseApi; +use Friendica\DI; +use Friendica\Model\Photo; +use Friendica\Network\HTTPException; + +/** + * updates the profile image for the user (either a specified profile or the default profile) + * + * @see https://developer.twitter.com/en/docs/accounts-and-users/manage-account-settings/api-reference/post-account-update_profile_image + */ +class UpdateProfileImage extends BaseApi +{ + protected function post(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + // get mediadata from image or media (Twitter call api/account/update_profile_image provides image) + if (!empty($_FILES['image'])) { + $media = $_FILES['image']; + } elseif (!empty($_FILES['media'])) { + $media = $_FILES['media']; + } + + // error if image data is missing + if (empty($media)) { + throw new HTTPException\BadRequestException('no media data submitted'); + } + + // save new profile image + $resource_id = Photo::uploadAvatar($uid, $media); + if (empty($resource_id)) { + throw new HTTPException\InternalServerErrorException('image upload failed'); + } + + // output for client + $skip_status = $this->getRequestValue($request, 'skip_status', false); + + $user_info = DI::twitterUser()->createFromUserId($uid, $skip_status)->toArray(); + + // "verified" isn't used here in the standard + unset($user_info['verified']); + + // "uid" is only needed for some internal stuff, so remove it from here + unset($user_info['uid']); + + $this->response->exit('user', ['user' => $user_info], $this->parameters['extension'] ?? null); + } +} diff --git a/src/Module/Api/Twitter/Account/VerifyCredentials.php b/src/Module/Api/Twitter/Account/VerifyCredentials.php index 592cff4c0c..af9c2a6f70 100644 --- a/src/Module/Api/Twitter/Account/VerifyCredentials.php +++ b/src/Module/Api/Twitter/Account/VerifyCredentials.php @@ -37,7 +37,7 @@ class VerifyCredentials extends BaseApi BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - $skip_status = $_REQUEST['skip_status'] ?? false; + $skip_status = $this->getRequestValue($request, 'skip_status', false); $user_info = DI::twitterUser()->createFromUserId($uid, $skip_status)->toArray(); diff --git a/src/Module/Api/Twitter/Blocks/Ids.php b/src/Module/Api/Twitter/Blocks/Ids.php index e411fc507f..d687b85a5c 100644 --- a/src/Module/Api/Twitter/Blocks/Ids.php +++ b/src/Module/Api/Twitter/Blocks/Ids.php @@ -37,17 +37,14 @@ class Ids extends ContactEndpoint $uid = BaseApi::getCurrentUserID(); // Expected value for user_id parameter: public/user contact id - $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT, ['options' => ['default' => -1]]); - $stringify_ids = filter_input(INPUT_GET, 'stringify_ids', FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => false]]); - $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [ - 'default' => self::DEFAULT_COUNT, - 'min_range' => 1, - 'max_range' => self::MAX_COUNT, - ]]); + $cursor = $this->getRequestValue($request, 'cursor', -1); + $stringify_ids = $this->getRequestValue($request, 'stringify_ids', false); + $count = $this->getRequestValue($request, 'count', self::DEFAULT_COUNT, 1, self::MAX_COUNT); + // Friendica-specific - $since_id = filter_input(INPUT_GET, 'since_id', FILTER_VALIDATE_INT); - $max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT); - $min_id = filter_input(INPUT_GET, 'min_id' , FILTER_VALIDATE_INT); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $min_id = $this->getRequestValue($request, 'min_id', 0, 0); $params = ['order' => ['cid' => true], 'limit' => $count]; diff --git a/src/Module/Api/Twitter/Blocks/Lists.php b/src/Module/Api/Twitter/Blocks/Lists.php index da7b00f43c..823336c087 100644 --- a/src/Module/Api/Twitter/Blocks/Lists.php +++ b/src/Module/Api/Twitter/Blocks/Lists.php @@ -37,18 +37,15 @@ class Lists extends ContactEndpoint $uid = BaseApi::getCurrentUserID(); // Expected value for user_id parameter: public/user contact id - $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT, ['options' => ['default' => -1]]); - $skip_status = filter_input(INPUT_GET, 'skip_status' , FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => false]]); - $include_user_entities = filter_input(INPUT_GET, 'include_user_entities', FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => false]]); - $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [ - 'default' => self::DEFAULT_COUNT, - 'min_range' => 1, - 'max_range' => self::MAX_COUNT, - ]]); + $cursor = $this->getRequestValue($request, 'cursor', -1); + $skip_status = $this->getRequestValue($request, 'skip_status', false); + $include_user_entities = $this->getRequestValue($request, 'include_user_entities', false); + $count = $this->getRequestValue($request, 'count', self::DEFAULT_COUNT, 1, self::MAX_COUNT); + // Friendica-specific - $since_id = filter_input(INPUT_GET, 'since_id', FILTER_VALIDATE_INT); - $max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT); - $min_id = filter_input(INPUT_GET, 'min_id' , FILTER_VALIDATE_INT); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $min_id = $this->getRequestValue($request, 'min_id', 0, 0); $params = ['order' => ['cid' => true], 'limit' => $count]; diff --git a/src/Module/Api/Twitter/DirectMessages/Destroy.php b/src/Module/Api/Twitter/DirectMessages/Destroy.php index 9f9c4de303..9de0982364 100644 --- a/src/Module/Api/Twitter/DirectMessages/Destroy.php +++ b/src/Module/Api/Twitter/DirectMessages/Destroy.php @@ -52,8 +52,10 @@ class Destroy extends BaseApi BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); $uid = BaseApi::getCurrentUserID(); - $id = filter_var($request['id'] ?? 0, FILTER_VALIDATE_INT); - $verbose = filter_var($request['friendica_verbose'] ?? false, FILTER_VALIDATE_BOOLEAN); + $id = $this->getRequestValue($request, 'id', 0); + $id = $this->getRequestValue($this->parameters, 'id', $id); + + $verbose = $this->getRequestValue($request, 'friendica_verbose', false); $parenturi = $request['friendica_parenturi'] ?? ''; @@ -64,11 +66,6 @@ class Destroy extends BaseApi return; } - // BadRequestException if no id specified (for clients using Twitter API) - if ($id == 0) { - throw new BadRequestException('Message id not specified'); - } - // add parent-uri to sql command if specified by calling app $sql_extra = ($parenturi != "" ? " AND `parent-uri` = '" . DBA::escape($parenturi) . "'" : ""); diff --git a/src/Module/Api/Twitter/DirectMessagesEndpoint.php b/src/Module/Api/Twitter/DirectMessagesEndpoint.php index c88e43036a..28b867889f 100644 --- a/src/Module/Api/Twitter/DirectMessagesEndpoint.php +++ b/src/Module/Api/Twitter/DirectMessagesEndpoint.php @@ -58,12 +58,12 @@ abstract class DirectMessagesEndpoint extends BaseApi protected function getMessages(array $request, int $uid, array $condition) { // params - $count = filter_var($request['count'] ?? 20, FILTER_VALIDATE_INT, ['options' => ['max_range' => 100]]); - $page = filter_var($request['page'] ?? 1, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]); - $since_id = filter_var($request['since_id'] ?? 0, FILTER_VALIDATE_INT); - $max_id = filter_var($request['max_id'] ?? 0, FILTER_VALIDATE_INT); - $min_id = filter_var($request['min_id'] ?? 0, FILTER_VALIDATE_INT); - $verbose = filter_var($request['friendica_verbose'] ?? false, FILTER_VALIDATE_BOOLEAN); + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $page = $this->getRequestValue($request, 'page', 1, 1); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $min_id = $this->getRequestValue($request, 'min_id', 0, 0); + $verbose = $this->getRequestValue($request, 'friendica_verbose', false); // pagination $start = max(0, ($page - 1) * $count); @@ -84,7 +84,7 @@ abstract class DirectMessagesEndpoint extends BaseApi $params['order'] = ['id']; } - $cid = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $_REQUEST['user_id'] ?? 0, 0); + $cid = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $request['user_id'] ?? 0, 0); if (!empty($cid)) { $cdata = Contact::getPublicAndUserContactID($cid, $uid); if (!empty($cdata['user'])) { diff --git a/src/Module/Api/Twitter/Favorites.php b/src/Module/Api/Twitter/Favorites.php index 9a7149cab7..828741a195 100644 --- a/src/Module/Api/Twitter/Favorites.php +++ b/src/Module/Api/Twitter/Favorites.php @@ -45,10 +45,11 @@ class Favorites extends BaseApi Logger::info(BaseApi::LOG_PREFIX . 'for {self}', ['module' => 'api', 'action' => 'favorites']); // params - $since_id = $request['since_id'] ?? 0; - $max_id = $request['max_id'] ?? 0; - $count = $request['count'] ?? 20; - $page = $request['page'] ?? 1; + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $page = $this->getRequestValue($request, 'page', 1, 1); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $include_entities = $this->getRequestValue($request, 'include_entities', false); $start = max(0, ($page - 1) * $count); @@ -64,8 +65,6 @@ class Favorites extends BaseApi $statuses = Post::selectForUser($uid, [], $condition, $params); - $include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true'); - $ret = []; while ($status = DBA::fetch($statuses)) { $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray(); diff --git a/src/Module/Api/Twitter/Followers/Ids.php b/src/Module/Api/Twitter/Followers/Ids.php index a618304b21..df7120ab62 100644 --- a/src/Module/Api/Twitter/Followers/Ids.php +++ b/src/Module/Api/Twitter/Followers/Ids.php @@ -37,22 +37,15 @@ class Ids extends ContactEndpoint $uid = BaseApi::getCurrentUserID(); // Expected value for user_id parameter: public/user contact id - $contact_id = filter_input(INPUT_GET, 'user_id' , FILTER_VALIDATE_INT); - $screen_name = filter_input(INPUT_GET, 'screen_name'); - $profile_url = filter_input(INPUT_GET, 'profile_url'); - $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT, ['options' => ['default' => -1]]); - $stringify_ids = filter_input(INPUT_GET, 'stringify_ids', FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => false]]); - $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [ - 'default' => self::DEFAULT_COUNT, - 'min_range' => 1, - 'max_range' => self::MAX_COUNT, - ]]); - // Friendica-specific - $since_id = filter_input(INPUT_GET, 'since_id' , FILTER_VALIDATE_INT); - $max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT); - $min_id = filter_input(INPUT_GET, 'min_id' , FILTER_VALIDATE_INT); + $cid = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $request['user_id'] ?? 0, $uid); + $cursor = $this->getRequestValue($request, 'cursor', -1); + $stringify_ids = $this->getRequestValue($request, 'stringify_ids', false); + $count = $this->getRequestValue($request, 'count', self::DEFAULT_COUNT, 1, self::MAX_COUNT); - $cid = BaseApi::getContactIDForSearchterm($screen_name, $profile_url, $contact_id, $uid); + // Friendica-specific + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $min_id = $this->getRequestValue($request, 'min_id', 0, 0); $params = ['order' => ['relation-cid' => true], 'limit' => $count]; diff --git a/src/Module/Api/Twitter/Followers/Lists.php b/src/Module/Api/Twitter/Followers/Lists.php index a60226d05f..0e9a2aa82a 100644 --- a/src/Module/Api/Twitter/Followers/Lists.php +++ b/src/Module/Api/Twitter/Followers/Lists.php @@ -37,23 +37,16 @@ class Lists extends ContactEndpoint $uid = BaseApi::getCurrentUserID(); // Expected value for user_id parameter: public/user contact id - $contact_id = filter_input(INPUT_GET, 'user_id' , FILTER_VALIDATE_INT); - $screen_name = filter_input(INPUT_GET, 'screen_name'); - $profile_url = filter_input(INPUT_GET, 'profile_url'); - $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT, ['options' => ['default' => -1]]); - $skip_status = filter_input(INPUT_GET, 'skip_status' , FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => false]]); - $include_user_entities = filter_input(INPUT_GET, 'include_user_entities', FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => false]]); - $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [ - 'default' => self::DEFAULT_COUNT, - 'min_range' => 1, - 'max_range' => self::MAX_COUNT, - ]]); - // Friendica-specific - $since_id = filter_input(INPUT_GET, 'since_id', FILTER_VALIDATE_INT); - $max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT); - $min_id = filter_input(INPUT_GET, 'min_id' , FILTER_VALIDATE_INT); + $cid = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $request['user_id'] ?? 0, $uid); + $cursor = $this->getRequestValue($request, 'cursor', -1); + $skip_status = $this->getRequestValue($request, 'skip_status', false); + $include_user_entities = $this->getRequestValue($request, 'include_user_entities', false); + $count = $this->getRequestValue($request, 'count', self::DEFAULT_COUNT, 1, self::MAX_COUNT); - $cid = BaseApi::getContactIDForSearchterm($screen_name, $profile_url, $contact_id, $uid); + // Friendica-specific + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $min_id = $this->getRequestValue($request, 'min_id', 0, 0); $params = ['order' => ['relation-cid' => true], 'limit' => $count]; diff --git a/src/Module/Api/Twitter/Friends/Ids.php b/src/Module/Api/Twitter/Friends/Ids.php index d810ceeb43..b5b571a9f0 100644 --- a/src/Module/Api/Twitter/Friends/Ids.php +++ b/src/Module/Api/Twitter/Friends/Ids.php @@ -37,22 +37,15 @@ class Ids extends ContactEndpoint $uid = BaseApi::getCurrentUserID(); // Expected value for user_id parameter: public/user contact id - $contact_id = filter_input(INPUT_GET, 'user_id' , FILTER_VALIDATE_INT); - $screen_name = filter_input(INPUT_GET, 'screen_name'); - $profile_url = filter_input(INPUT_GET, 'profile_url'); - $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT, ['options' => ['default' => -1]]); - $stringify_ids = filter_input(INPUT_GET, 'stringify_ids', FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => false]]); - $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [ - 'default' => self::DEFAULT_COUNT, - 'min_range' => 1, - 'max_range' => self::MAX_COUNT, - ]]); - // Friendica-specific - $since_id = filter_input(INPUT_GET, 'since_id' , FILTER_VALIDATE_INT); - $max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT); - $min_id = filter_input(INPUT_GET, 'min_id' , FILTER_VALIDATE_INT); + $cid = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $request['user_id'] ?? 0, $uid); + $cursor = $this->getRequestValue($request, 'cursor', -1); + $stringify_ids = $this->getRequestValue($request, 'stringify_ids', false); + $count = $this->getRequestValue($request, 'count', self::DEFAULT_COUNT, 1, self::MAX_COUNT); - $cid = BaseApi::getContactIDForSearchterm($screen_name, $profile_url, $contact_id, $uid); + // Friendica-specific + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $min_id = $this->getRequestValue($request, 'min_id', 0, 0); $params = ['order' => ['cid' => true], 'limit' => $count]; diff --git a/src/Module/Api/Twitter/Friends/Lists.php b/src/Module/Api/Twitter/Friends/Lists.php index dc2c26166d..750ba6db61 100644 --- a/src/Module/Api/Twitter/Friends/Lists.php +++ b/src/Module/Api/Twitter/Friends/Lists.php @@ -37,23 +37,16 @@ class Lists extends ContactEndpoint $uid = BaseApi::getCurrentUserID(); // Expected value for user_id parameter: public/user contact id - $contact_id = filter_input(INPUT_GET, 'user_id' , FILTER_VALIDATE_INT); - $screen_name = filter_input(INPUT_GET, 'screen_name'); - $profile_url = filter_input(INPUT_GET, 'profile_url'); - $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT, ['options' => ['default' => -1]]); - $skip_status = filter_input(INPUT_GET, 'skip_status' , FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => false]]); - $include_user_entities = filter_input(INPUT_GET, 'include_user_entities', FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => false]]); - $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [ - 'default' => self::DEFAULT_COUNT, - 'min_range' => 1, - 'max_range' => self::MAX_COUNT, - ]]); - // Friendica-specific - $since_id = filter_input(INPUT_GET, 'since_id', FILTER_VALIDATE_INT); - $max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT); - $min_id = filter_input(INPUT_GET, 'min_id' , FILTER_VALIDATE_INT); + $cid = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $request['user_id'] ?? 0, $uid); + $cursor = $this->getRequestValue($request, 'cursor', -1); + $skip_status = $this->getRequestValue($request, 'skip_status', false); + $include_user_entities = $this->getRequestValue($request, 'include_user_entities', false); + $count = $this->getRequestValue($request, 'count', self::DEFAULT_COUNT, 1, self::MAX_COUNT); - $cid = BaseApi::getContactIDForSearchterm($screen_name, $profile_url, $contact_id, $uid); + // Friendica-specific + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $min_id = $this->getRequestValue($request, 'min_id', 0, 0); $params = ['order' => ['cid' => true], 'limit' => $count]; diff --git a/src/Module/Api/Twitter/Friendships/Incoming.php b/src/Module/Api/Twitter/Friendships/Incoming.php index 315ef00332..378159c3c2 100644 --- a/src/Module/Api/Twitter/Friendships/Incoming.php +++ b/src/Module/Api/Twitter/Friendships/Incoming.php @@ -37,17 +37,14 @@ class Incoming extends ContactEndpoint $uid = BaseApi::getCurrentUserID(); // Expected value for user_id parameter: public/user contact id - $cursor = filter_input(INPUT_GET, 'cursor' , FILTER_VALIDATE_INT, ['options' => ['default' => -1]]); - $stringify_ids = filter_input(INPUT_GET, 'stringify_ids', FILTER_VALIDATE_BOOLEAN, ['options' => ['default' => false]]); - $count = filter_input(INPUT_GET, 'count' , FILTER_VALIDATE_INT, ['options' => [ - 'default' => self::DEFAULT_COUNT, - 'min_range' => 1, - 'max_range' => self::MAX_COUNT, - ]]); + $cursor = $this->getRequestValue($request, 'cursor', -1); + $stringify_ids = $this->getRequestValue($request, 'stringify_ids', false); + $count = $this->getRequestValue($request, 'count', self::DEFAULT_COUNT, 1, self::MAX_COUNT); + // Friendica-specific - $since_id = filter_input(INPUT_GET, 'since_id', FILTER_VALIDATE_INT); - $max_id = filter_input(INPUT_GET, 'max_id' , FILTER_VALIDATE_INT); - $min_id = filter_input(INPUT_GET, 'min_id' , FILTER_VALIDATE_INT); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $min_id = $this->getRequestValue($request, 'min_id', 0, 0); $params = ['order' => ['cid' => true], 'limit' => $count]; diff --git a/src/Module/Api/Twitter/Friendships/Show.php b/src/Module/Api/Twitter/Friendships/Show.php index 6db48506a3..66605e7265 100644 --- a/src/Module/Api/Twitter/Friendships/Show.php +++ b/src/Module/Api/Twitter/Friendships/Show.php @@ -38,9 +38,9 @@ class Show extends ContactEndpoint self::checkAllowedScope(self::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - $source_cid = BaseApi::getContactIDForSearchterm($_REQUEST['source_screen_name'] ?? '', '', $_REQUEST['source_id'] ?? 0, $uid); + $source_cid = BaseApi::getContactIDForSearchterm($request['source_screen_name'] ?? '', '', $request['source_id'] ?? 0, $uid); - $target_cid = BaseApi::getContactIDForSearchterm($_REQUEST['target_screen_name'] ?? '', '', $_REQUEST['target_id'] ?? 0, $uid); + $target_cid = BaseApi::getContactIDForSearchterm($request['target_screen_name'] ?? '', '', $request['target_id'] ?? 0, $uid); $source = Contact::getById($source_cid); if (empty($source)) { diff --git a/src/Module/Api/Twitter/Lists/Create.php b/src/Module/Api/Twitter/Lists/Create.php new file mode 100644 index 0000000000..26e94eb1ab --- /dev/null +++ b/src/Module/Api/Twitter/Lists/Create.php @@ -0,0 +1,85 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter\Lists; + +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Database\Database; +use Friendica\Factory\Api\Friendica\Group as FriendicaGroup; +use Friendica\Module\BaseApi; +use Friendica\Model\Contact; +use Friendica\Model\Group; +use Friendica\Module\Api\ApiResponse; +use Friendica\Network\HTTPException; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +/** + * Update information about a group. + * + * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/post-lists-update + */ +class Create extends BaseApi +{ + /** @var friendicaGroup */ + private $friendicaGroup; + + /** @var Database */ + private $dba; + + public function __construct(Database $dba, FriendicaGroup $friendicaGroup, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->dba = $dba; + $this->friendicaGroup = $friendicaGroup; + } + + protected function rawContent(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + // params + $name = $this->getRequestValue($request, 'name', ''); + + if ($name == '') { + throw new HTTPException\BadRequestException('group name not specified'); + } + + // error message if specified group name already exists + if ($this->dba->exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => false])) { + throw new HTTPException\BadRequestException('group name already exists'); + } + + $ret = Group::create($uid, $name); + if ($ret) { + $gid = Group::getIdByName($uid, $name); + } else { + throw new HTTPException\BadRequestException('other API error'); + } + + $grp = $this->friendicaGroup->createFromId($gid); + + $this->response->exit('statuses', ['lists' => ['lists' => $grp]], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid)); + } +} diff --git a/src/Module/Api/Twitter/Lists/Destroy.php b/src/Module/Api/Twitter/Lists/Destroy.php new file mode 100644 index 0000000000..8390bab775 --- /dev/null +++ b/src/Module/Api/Twitter/Lists/Destroy.php @@ -0,0 +1,83 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter\Lists; + +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Database\Database; +use Friendica\Factory\Api\Friendica\Group as FriendicaGroup; +use Friendica\Module\BaseApi; +use Friendica\Model\Contact; +use Friendica\Model\Group; +use Friendica\Module\Api\ApiResponse; +use Friendica\Network\HTTPException; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +/** + * Delete a group. + * + * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/post-lists-destroy + */ +class Destroy extends BaseApi +{ + /** @var friendicaGroup */ + private $friendicaGroup; + + /** @var Database */ + private $dba; + + public function __construct(Database $dba, FriendicaGroup $friendicaGroup, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->dba = $dba; + $this->friendicaGroup = $friendicaGroup; + } + + protected function rawContent(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + // params + $gid = $this->getRequestValue($request, 'list_id', 0); + + // error if no gid specified + if ($gid == 0) { + throw new HTTPException\BadRequestException('gid not specified'); + } + + // get data of the specified group id + $group = $this->dba->selectFirst('group', [], ['uid' => $uid, 'id' => $gid]); + // error message if specified gid is not in database + if (!$group) { + throw new HTTPException\BadRequestException('gid not available'); + } + + $list = $this->friendicaGroup->createFromId($gid); + + if (Group::remove($gid)) { + $this->response->exit('statuses', ['lists' => ['lists' => $list]], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid)); + } + } +} diff --git a/src/Module/Api/Friendica/Index.php b/src/Module/Api/Twitter/Lists/Lists.php similarity index 63% rename from src/Module/Api/Friendica/Index.php rename to src/Module/Api/Twitter/Lists/Lists.php index c3eeda23bc..09d740bb68 100644 --- a/src/Module/Api/Friendica/Index.php +++ b/src/Module/Api/Twitter/Lists/Lists.php @@ -19,32 +19,25 @@ * */ -namespace Friendica\Module\Api\Friendica; +namespace Friendica\Module\Api\Twitter\Lists; -use Friendica\DI; use Friendica\Module\BaseApi; -require_once __DIR__ . '/../../../../include/api.php'; +use Friendica\Model\Contact; /** - * api/friendica + * Returns all lists the user subscribes to. * - * @package Friendica\Module\Api\Friendica + * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/get-lists-list */ -class Index extends BaseApi +class Lists extends BaseApi { - protected function post(array $request = []) - { - self::checkAllowedScope(self::SCOPE_WRITE); - } - - protected function delete(array $request = []) - { - self::checkAllowedScope(self::SCOPE_WRITE); - } - protected function rawContent(array $request = []) { - echo api_call(DI::args()->getCommand(), $this->parameters['extension'] ?? 'json'); - exit(); + BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); + + // This is a dummy endpoint + $ret = []; + $this->response->exit('statuses', ["lists_list" => $ret], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid)); } } diff --git a/src/Module/Api/Twitter/Lists/Ownership.php b/src/Module/Api/Twitter/Lists/Ownership.php new file mode 100644 index 0000000000..e5aca1ad5c --- /dev/null +++ b/src/Module/Api/Twitter/Lists/Ownership.php @@ -0,0 +1,69 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter\Lists; + +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Database\Database; +use Friendica\Factory\Api\Friendica\Group as FriendicaGroup; +use Friendica\Module\BaseApi; +use Friendica\Model\Contact; +use Friendica\Module\Api\ApiResponse; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +/** + * Returns all groups the user owns. + * + * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/get-lists-ownerships + */ +class Ownership extends BaseApi +{ + /** @var friendicaGroup */ + private $friendicaGroup; + + /** @var Database */ + private $dba; + + public function __construct(Database $dba, FriendicaGroup $friendicaGroup, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->dba = $dba; + $this->friendicaGroup = $friendicaGroup; + } + protected function rawContent(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); + $uid = BaseApi::getCurrentUserID(); + + $groups = $this->dba->select('group', [], ['deleted' => false, 'uid' => $uid]); + + // loop through all groups + $lists = []; + foreach ($groups as $group) { + $lists[] = $this->friendicaGroup->createFromId($group['id']); + } + + $this->response->exit('statuses', ['lists' => ['lists' => $lists]], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid)); + } +} diff --git a/src/Module/Api/Twitter/Lists/Statuses.php b/src/Module/Api/Twitter/Lists/Statuses.php index 34220fe993..2bf27697b4 100644 --- a/src/Module/Api/Twitter/Lists/Statuses.php +++ b/src/Module/Api/Twitter/Lists/Statuses.php @@ -21,12 +21,18 @@ namespace Friendica\Module\Api\Twitter\Lists; +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Database\Database; use Friendica\Database\DBA; +use Friendica\Factory\Api\Twitter\Status as TwitterStatus; use Friendica\Module\BaseApi; -use Friendica\DI; use Friendica\Model\Contact; use Friendica\Model\Post; -use Friendica\Network\HTTPException\BadRequestException; +use Friendica\Module\Api\ApiResponse; +use Friendica\Network\HTTPException; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; /** * Returns recent statuses from users in the specified group. @@ -35,26 +41,41 @@ use Friendica\Network\HTTPException\BadRequestException; */ class Statuses extends BaseApi { + /** @var TwitterStatus */ + private $twitterStatus; + + /** @var Database */ + private $dba; + + public function __construct(Database $dba, TwitterStatus $twitterStatus, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->dba = $dba; + $this->twitterStatus = $twitterStatus; + } + protected function rawContent(array $request = []) { BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); if (empty($request['list_id'])) { - throw new BadRequestException('list_id not specified'); + throw new HTTPException\BadRequestException('list_id not specified'); } // params - $count = $request['count'] ?? 20; - $page = $request['page'] ?? 1; - $since_id = $request['since_id'] ?? 0; - $max_id = $request['max_id'] ?? 0; - $exclude_replies = (!empty($request['exclude_replies']) ? 1 : 0); - $conversation_id = $request['conversation_id'] ?? 0; + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $page = $this->getRequestValue($request, 'page', 1, 1); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $exclude_replies = $this->getRequestValue($request, 'exclude_replies', false); + $conversation_id = $this->getRequestValue($request, 'conversation_id', 0, 0); + $include_entities = $this->getRequestValue($request, 'include_entities', false); $start = max(0, ($page - 1) * $count); - $groups = DBA::selectToArray('group_member', ['contact-id'], ['gid' => $request['list_id']]); + $groups = $this->dba->selectToArray('group_member', ['contact-id'], ['gid' => $request['list_id']]); $gids = array_column($groups, 'contact-id'); $condition = ['uid' => $uid, 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT], 'contact-id' => $gids]; $condition = DBA::mergeConditions($condition, ["`id` > ?", $since_id]); @@ -63,7 +84,7 @@ class Statuses extends BaseApi $condition[0] .= " AND `id` <= ?"; $condition[] = $max_id; } - if ($exclude_replies > 0) { + if ($exclude_replies) { $condition[0] .= ' AND `gravity` = ?'; $condition[] = GRAVITY_PARENT; } @@ -75,13 +96,11 @@ class Statuses extends BaseApi $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, [], $condition, $params); - $include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true'); - $items = []; - while ($status = DBA::fetch($statuses)) { - $items[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray(); + while ($status = $this->dba->fetch($statuses)) { + $items[] = $this->twitterStatus->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray(); } - DBA::close($statuses); + $this->dba->close($statuses); $this->response->exit('statuses', ['status' => $items], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid)); } diff --git a/src/Module/Api/Twitter/Lists/Update.php b/src/Module/Api/Twitter/Lists/Update.php new file mode 100644 index 0000000000..e7929e77d2 --- /dev/null +++ b/src/Module/Api/Twitter/Lists/Update.php @@ -0,0 +1,84 @@ +. + * + */ + +namespace Friendica\Module\Api\Twitter\Lists; + +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Database\Database; +use Friendica\Factory\Api\Friendica\Group as FriendicaGroup; +use Friendica\Module\BaseApi; +use Friendica\Model\Contact; +use Friendica\Model\Group; +use Friendica\Module\Api\ApiResponse; +use Friendica\Network\HTTPException; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +/** + * Update information about a group. + * + * @see https://developer.twitter.com/en/docs/accounts-and-users/create-manage-lists/api-reference/post-lists-update + */ +class Update extends BaseApi +{ + /** @var friendicaGroup */ + private $friendicaGroup; + + /** @var Database */ + private $dba; + + public function __construct(Database $dba, FriendicaGroup $friendicaGroup, App $app, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, ApiResponse $response, array $server, array $parameters = []) + { + parent::__construct($app, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->dba = $dba; + $this->friendicaGroup = $friendicaGroup; + } + + protected function rawContent(array $request = []) + { + BaseApi::checkAllowedScope(BaseApi::SCOPE_WRITE); + $uid = BaseApi::getCurrentUserID(); + + // params + $gid = $this->getRequestValue($request, 'list_id', 0); + $name = $this->getRequestValue($request, 'name', ''); + + // error if no gid specified + if ($gid == 0) { + throw new HTTPException\BadRequestException('gid not specified'); + } + + // get data of the specified group id + $group = $this->dba->selectFirst('group', [], ['uid' => $uid, 'id' => $gid]); + // error message if specified gid is not in database + if (!$group) { + throw new HTTPException\BadRequestException('gid not available'); + } + + if (Group::update($gid, $name)) { + $list = $this->friendicaGroup->createFromId($gid); + + $this->response->exit('statuses', ['lists' => ['lists' => $list]], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid)); + } + } +} diff --git a/src/Module/Api/Twitter/Search/Tweets.php b/src/Module/Api/Twitter/Search/Tweets.php index 759c797a6e..5da579b7ce 100644 --- a/src/Module/Api/Twitter/Search/Tweets.php +++ b/src/Module/Api/Twitter/Search/Tweets.php @@ -41,26 +41,21 @@ class Tweets extends BaseApi BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - if (empty($_REQUEST['q'])) { + if (empty($request['q'])) { throw new BadRequestException('q parameter is required.'); } - $searchTerm = trim(rawurldecode($_REQUEST['q'])); + $searchTerm = trim(rawurldecode($request['q'])); $data['status'] = []; - $count = 15; - - $exclude_replies = !empty($_REQUEST['exclude_replies']); - if (!empty($_REQUEST['rpp'])) { - $count = $_REQUEST['rpp']; - } elseif (!empty($_REQUEST['count'])) { - $count = $_REQUEST['count']; - } - - $since_id = $_REQUEST['since_id'] ?? 0; - $max_id = $_REQUEST['max_id'] ?? 0; - $page = $_REQUEST['page'] ?? 1; + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $count = $this->getRequestValue($request, 'rpp', $count); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $page = $this->getRequestValue($request, 'page', 1, 1); + $include_entities = $this->getRequestValue($request, 'include_entities', false); + $exclude_replies = $this->getRequestValue($request, 'exclude_replies', false); $start = max(0, ($page - 1) * $count); @@ -115,8 +110,6 @@ class Tweets extends BaseApi $statuses = $statuses ?: Post::selectForUser($uid, [], $condition, $params); - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); - $ret = []; while ($status = DBA::fetch($statuses)) { $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray(); diff --git a/src/Module/Api/Twitter/Statuses/Destroy.php b/src/Module/Api/Twitter/Statuses/Destroy.php index 784b8ef66f..be08483532 100644 --- a/src/Module/Api/Twitter/Statuses/Destroy.php +++ b/src/Module/Api/Twitter/Statuses/Destroy.php @@ -21,7 +21,6 @@ namespace Friendica\Module\Api\Twitter\Statuses; -use Friendica\Core\Logger; use Friendica\Module\BaseApi; use Friendica\DI; use Friendica\Model\Contact; @@ -40,17 +39,15 @@ class Destroy extends BaseApi BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - if (!empty($this->parameters['id'])) { - $id = (int)$this->parameters['id']; - } elseif (!empty($request['id'])) { - $id = (int)$request['id']; - } else { + $id = $this->getRequestValue($request, 'id', 0); + $id = $this->getRequestValue($this->parameters, 'id', $id); + if (empty($id)) { throw new BadRequestException('An id is missing.'); } $this->logger->notice('API: api_statuses_destroy: ' . $id); - $include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true'); + $include_entities = $this->getRequestValue($request, 'include_entities', false); $ret = DI::twitterStatus()->createFromItemId($id, $uid, $include_entities)->toArray(); diff --git a/src/Module/Api/Twitter/Statuses/HomeTimeline.php b/src/Module/Api/Twitter/Statuses/HomeTimeline.php index a9ed6a832b..41314bb1b7 100644 --- a/src/Module/Api/Twitter/Statuses/HomeTimeline.php +++ b/src/Module/Api/Twitter/Statuses/HomeTimeline.php @@ -43,12 +43,13 @@ class HomeTimeline extends BaseApi // get last network messages // params - $count = $_REQUEST['count'] ?? 20; - $page = $_REQUEST['page'] ?? 0; - $since_id = $_REQUEST['since_id'] ?? 0; - $max_id = $_REQUEST['max_id'] ?? 0; - $exclude_replies = !empty($_REQUEST['exclude_replies']); - $conversation_id = $_REQUEST['conversation_id'] ?? 0; + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $page = $this->getRequestValue($request, 'page', 1, 1); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $exclude_replies = $this->getRequestValue($request, 'exclude_replies', false); + $include_entities = $this->getRequestValue($request, 'include_entities', false); + $conversation_id = $this->getRequestValue($request, 'conversation_id', 0, 0); $start = max(0, ($page - 1) * $count); @@ -71,8 +72,6 @@ class HomeTimeline extends BaseApi $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, [], $condition, $params); - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); - $ret = []; $idarray = []; while ($status = DBA::fetch($statuses)) { diff --git a/src/Module/Api/Twitter/Statuses/Mentions.php b/src/Module/Api/Twitter/Statuses/Mentions.php index 31cde911eb..e9bbb93bdf 100644 --- a/src/Module/Api/Twitter/Statuses/Mentions.php +++ b/src/Module/Api/Twitter/Statuses/Mentions.php @@ -42,10 +42,11 @@ class Mentions extends BaseApi // get last network messages // params - $since_id = $_REQUEST['since_id'] ?? 0; - $max_id = $_REQUEST['max_id'] ?? 0; - $count = $_REQUEST['count'] ?? 20; - $page = $_REQUEST['page'] ?? 1; + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $page = $this->getRequestValue($request, 'page', 1, 1); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $include_entities = $this->getRequestValue($request, 'include_entities', false); $start = max(0, ($page - 1) * $count); @@ -72,8 +73,6 @@ class Mentions extends BaseApi $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, [], $condition, $params); - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); - $ret = []; while ($status = DBA::fetch($statuses)) { $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray(); diff --git a/src/Module/Api/Twitter/Statuses/NetworkPublicTimeline.php b/src/Module/Api/Twitter/Statuses/NetworkPublicTimeline.php index 5fa104e88f..af0436b4a2 100644 --- a/src/Module/Api/Twitter/Statuses/NetworkPublicTimeline.php +++ b/src/Module/Api/Twitter/Statuses/NetworkPublicTimeline.php @@ -38,12 +38,11 @@ class NetworkPublicTimeline extends BaseApi BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - $since_id = $_REQUEST['since_id'] ?? 0; - $max_id = $_REQUEST['max_id'] ?? 0; - - // pagination - $count = $_REQUEST['count'] ?? 20; - $page = $_REQUEST['page'] ?? 1; + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $page = $this->getRequestValue($request, 'page', 1, 1); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $include_entities = $this->getRequestValue($request, 'include_entities', false); $start = max(0, ($page - 1) * $count); @@ -58,8 +57,6 @@ class NetworkPublicTimeline extends BaseApi $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, Item::DISPLAY_FIELDLIST, $condition, $params); - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); - $ret = []; while ($status = DBA::fetch($statuses)) { $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray(); diff --git a/src/Module/Api/Twitter/Statuses/PublicTimeline.php b/src/Module/Api/Twitter/Statuses/PublicTimeline.php index 0015d3e08d..aba330a658 100644 --- a/src/Module/Api/Twitter/Statuses/PublicTimeline.php +++ b/src/Module/Api/Twitter/Statuses/PublicTimeline.php @@ -41,12 +41,13 @@ class PublicTimeline extends BaseApi // get last network messages // params - $count = $_REQUEST['count'] ?? 20; - $page = $_REQUEST['page'] ?? 1; - $since_id = $_REQUEST['since_id'] ?? 0; - $max_id = $_REQUEST['max_id'] ?? 0; - $exclude_replies = (!empty($_REQUEST['exclude_replies']) ? 1 : 0); - $conversation_id = $_REQUEST['conversation_id'] ?? 0; + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $page = $this->getRequestValue($request, 'page', 1, 1); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $exclude_replies = $this->getRequestValue($request, 'exclude_replies', false); + $conversation_id = $this->getRequestValue($request, 'conversation_id', 0, 0); + $include_entities = $this->getRequestValue($request, 'include_entities', false); $start = max(0, ($page - 1) * $count); @@ -78,8 +79,6 @@ class PublicTimeline extends BaseApi $statuses = Post::selectForUser($uid, [], $condition, $params); } - $include_entities = strtolower(($_REQUEST['include_entities'] ?? 'false') == 'true'); - $ret = []; while ($status = DBA::fetch($statuses)) { $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray(); diff --git a/src/Module/Api/Twitter/Statuses/Retweet.php b/src/Module/Api/Twitter/Statuses/Retweet.php index 0c2401cf31..1d67443b1c 100644 --- a/src/Module/Api/Twitter/Statuses/Retweet.php +++ b/src/Module/Api/Twitter/Statuses/Retweet.php @@ -44,11 +44,9 @@ class Retweet extends BaseApi self::checkAllowedScope(self::SCOPE_WRITE); $uid = self::getCurrentUserID(); - if (!empty($this->parameters['id'])) { - $id = (int)$this->parameters['id']; - } elseif (!empty($request['id'])) { - $id = (int)$request['id']; - } else { + $id = $this->getRequestValue($request, 'id', 0); + $id = $this->getRequestValue($this->parameters, 'id', $id); + if (empty($id)) { throw new BadRequestException('An id is missing.'); } diff --git a/src/Module/Api/Twitter/Statuses/Show.php b/src/Module/Api/Twitter/Statuses/Show.php index d037697518..64533d0bc9 100644 --- a/src/Module/Api/Twitter/Statuses/Show.php +++ b/src/Module/Api/Twitter/Statuses/Show.php @@ -41,10 +41,10 @@ class Show extends BaseApi BaseApi::checkAllowedScope(BaseApi::SCOPE_READ); $uid = BaseApi::getCurrentUserID(); - if (empty($this->parameters['id'])) { - $id = intval($request['id'] ?? 0); - } else { - $id = (int)$this->parameters['id']; + $id = $this->getRequestValue($request, 'id', 0); + $id = $this->getRequestValue($this->parameters, 'id', $id); + if (empty($id)) { + throw new BadRequestException('An id is missing.'); } Logger::notice('API: api_statuses_show: ' . $id); @@ -79,7 +79,7 @@ class Show extends BaseApi throw new BadRequestException(sprintf("There is no status or conversation with the id %d.", $id)); } - $include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true'); + $include_entities = $this->getRequestValue($request, 'include_entities', false); $ret = []; while ($status = DBA::fetch($statuses)) { diff --git a/src/Module/Api/Twitter/Statuses/UserTimeline.php b/src/Module/Api/Twitter/Statuses/UserTimeline.php index 2c884a6b6c..b6dcd86c91 100644 --- a/src/Module/Api/Twitter/Statuses/UserTimeline.php +++ b/src/Module/Api/Twitter/Statuses/UserTimeline.php @@ -42,15 +42,14 @@ class UserTimeline extends BaseApi Logger::info('api_statuses_user_timeline', ['api_user' => $uid, '_REQUEST' => $request]); - $cid = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $request['user_id'] ?? 0, $uid); - $since_id = $request['since_id'] ?? 0; - $max_id = $request['max_id'] ?? 0; - $exclude_replies = !empty($request['exclude_replies']); - $conversation_id = $request['conversation_id'] ?? 0; - - // pagination - $count = $request['count'] ?? 20; - $page = $request['page'] ?? 1; + $cid = BaseApi::getContactIDForSearchterm($request['screen_name'] ?? '', $request['profileurl'] ?? '', $request['user_id'] ?? 0, $uid); + $count = $this->getRequestValue($request, 'count', 20, 1, 100); + $page = $this->getRequestValue($request, 'page', 1, 1); + $since_id = $this->getRequestValue($request, 'since_id', 0, 0); + $max_id = $this->getRequestValue($request, 'max_id', 0, 0); + $exclude_replies = $this->getRequestValue($request, 'exclude_replies', false); + $conversation_id = $this->getRequestValue($request, 'conversation_id', 0, 0); + $include_entities = $this->getRequestValue($request, 'include_entities', false); $start = max(0, ($page - 1) * $count); @@ -74,8 +73,6 @@ class UserTimeline extends BaseApi $params = ['order' => ['id' => true], 'limit' => [$start, $count]]; $statuses = Post::selectForUser($uid, [], $condition, $params); - $include_entities = strtolower(($request['include_entities'] ?? 'false') == 'true'); - $ret = []; while ($status = DBA::fetch($statuses)) { $ret[] = DI::twitterStatus()->createFromUriId($status['uri-id'], $status['uid'], $include_entities)->toArray(); diff --git a/src/Object/Api/Friendica/Group.php b/src/Object/Api/Friendica/Group.php new file mode 100644 index 0000000000..36dd6df6c9 --- /dev/null +++ b/src/Object/Api/Friendica/Group.php @@ -0,0 +1,59 @@ +. + * + */ + +namespace Friendica\Object\Api\Friendica; + +use Friendica\BaseDataTransferObject; + +/** + * Class Group + * + * + */ +class Group extends BaseDataTransferObject +{ + /** @var string */ + protected $name; + /** @var int */ + protected $id; + /** @var string */ + protected $id_str; + /** @var array */ + protected $user; + /** @var string */ + protected $mode; + + /** + * Creates an Group entity array + * + * @param array $group + * @param array $user + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public function __construct(array $group, array $user) + { + $this->name = $group['name']; + $this->id = $group['id']; + $this->id_str = (string)$group['id']; + $this->user = $user; + $this->mode = $group['visible'] ? 'public' : 'private'; + } +} diff --git a/src/Util/ParseUrl.php b/src/Util/ParseUrl.php index c1fcff28e6..04afc927ba 100644 --- a/src/Util/ParseUrl.php +++ b/src/Util/ParseUrl.php @@ -216,6 +216,7 @@ class ParseUrl $curlResult = DI::httpClient()->get($url, [HttpClientOptions::CONTENT_LENGTH => 1000000]); if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { + Logger::info('Empty body or error when fetching', ['url' => $url, 'success' => $curlResult->isSuccess(), 'code' => $curlResult->getReturnCode()]); return $siteinfo; } diff --git a/static/routes.config.php b/static/routes.config.php index 63c968c6c7..758462ca03 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -42,10 +42,10 @@ $profileRoutes = [ $apiRoutes = [ '/account' => [ - '/verify_credentials[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Account\VerifyCredentials::class, [R::GET ]], - '/rate_limit_status[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Account\RateLimitStatus::class, [R::GET ]], - '/update_profile[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Account\UpdateProfile ::class, [ R::POST]], - '/update_profile_image[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], + '/verify_credentials[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Account\VerifyCredentials::class, [R::GET ]], + '/rate_limit_status[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Account\RateLimitStatus::class, [R::GET ]], + '/update_profile[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Account\UpdateProfile ::class, [ R::POST]], + '/update_profile_image[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Account\UpdateProfileImage::class, [ R::POST]], ], '/blocks/ids[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Blocks\Ids::class, [R::GET ]], @@ -80,18 +80,18 @@ $apiRoutes = [ '/direct_messages_setseen[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\DirectMessages\Setseen::class, [ R::POST]], '/direct_messages_search[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\DirectMessages\Search ::class, [R::GET ]], '/events[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Events\Index::class, [R::GET ]], - '/group_show[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], - '/group_create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], + '/group_show[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Show::class, [R::GET ]], + '/group_create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Create::class, [ R::POST]], '/group_delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Delete::class, [ R::POST]], '/group_update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Group\Update::class, [ R::POST]], '/profile/show[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Profile\Show::class, [R::GET ]], '/photoalbum/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photoalbum\Delete::class, [ R::POST]], '/photoalbum/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photoalbum\Update::class, [ R::POST]], - '/photos/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], - '/photo/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], + '/photos/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photo\Lists::class, [R::GET ]], + '/photo/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photo\Create::class, [ R::POST]], '/photo/delete[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photo\Delete::class, [ R::POST]], - '/photo/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], - '/photo[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], + '/photo/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photo\Update::class, [ R::POST]], + '/photo[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Photo::class, [R::GET ]], ], '/gnusocial/config[.{extension:json|xml|rss|atom}]' => [Module\Api\GNUSocial\GNUSocial\Config::class, [R::GET ]], @@ -99,13 +99,13 @@ $apiRoutes = [ '/help/test[.{extension:json|xml|rss|atom}]' => [Module\Api\GNUSocial\Help\Test::class, [R::GET ]], '/lists' => [ - '/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], - '/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], - '/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], - '/ownerships[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], - '/statuses[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Lists\Statuses::class, [R::GET ]], - '/subscriptions[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [R::GET ]], - '/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Index::class, [ R::POST]], + '/create[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Lists\Create::class, [ R::POST]], + '/destroy[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Lists\Destroy::class, [ R::POST]], + '/list[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Lists\Lists::class, [R::GET ]], + '/ownerships[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Lists\Ownership::class, [R::GET ]], + '/statuses[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Lists\Statuses::class, [R::GET ]], + '/subscriptions[.{extension:json|xml|rss|atom}]' => [Module\Api\Friendica\Lists\Lists::class, [R::GET ]], + '/update[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Lists\Update::class, [ R::POST]], ], '/media/upload[.{extension:json|xml|rss|atom}]' => [Module\Api\Twitter\Media\Upload::class, [ R::POST]], diff --git a/tests/legacy/ApiTest.php b/tests/legacy/ApiTest.php index 075459d0ec..904ae92f6c 100644 --- a/tests/legacy/ApiTest.php +++ b/tests/legacy/ApiTest.php @@ -23,7 +23,6 @@ namespace Friendica\Test\legacy; use Friendica\App; -use Friendica\Core\ACL; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\DI; use Friendica\Module\BaseApi; @@ -33,8 +32,6 @@ use Friendica\Util\Arrays; use Friendica\Util\DateTimeFormat; use Monolog\Handler\TestHandler; -require_once __DIR__ . '/../../include/api.php'; - /** * Tests for the API functions. * @@ -129,37 +126,6 @@ class ApiTest extends FixtureTest BasicAuth::setCurrentUserID($this->selfUser['id']); } - /** - * Assert that a list array contains expected keys. - * - * @param array $list List array - * - * @return void - */ - private function assertList(array $list = []) - { - self::assertIsString($list['name']); - self::assertIsInt($list['id']); - self::assertIsString('string', $list['id_str']); - self::assertContains($list['mode'], ['public', 'private']); - // We could probably do more checks here. - } - - /** - * Assert that the string is XML and contain the root element. - * - * @param string $result XML string - * @param string $root_element Root element name - * - * @return void - */ - private function assertXml($result = '', $root_element = '') - { - self::assertStringStartsWith('', $result); - self::assertStringContainsString('<' . $root_element, $result); - // We could probably do more checks here. - } - /** * Test the api_user() function. * @@ -214,26 +180,6 @@ class ApiTest extends FixtureTest self::assertEquals('Wed Oct 10 00:00:00 +0000 1990', DateTimeFormat::utc('1990-10-10', DateTimeFormat::API)); } - /** - * Test the api_register_func() function. - * - * @return void - */ - public function testApiRegisterFunc() - { - global $API; - self::assertNull( - api_register_func( - 'api_path', - function () { - }, - true, - 'method' - ) - ); - self::assertTrue(is_callable($API['api_path']['func'])); - } - /** * Test the BasicAuth::getCurrentUserID() function without any login. * @@ -312,166 +258,6 @@ class ApiTest extends FixtureTest BasicAuth::getCurrentUserID(true); } - /** - * Test the api_call() function. - * - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testApiCall() - { - global $API; - $API['api_path'] = [ - 'method' => 'method', - 'func' => function () { - return ['data' => ['some_data']]; - } - ]; - $_SERVER['REQUEST_METHOD'] = 'method'; - $_SERVER['QUERY_STRING'] = 'pagename=api_path'; - $_GET['callback'] = 'callback_name'; - - self::assertEquals( - 'callback_name(["some_data"])', - api_call('api_path', 'json') - ); - } - - /** - * Test the api_call() function with the profiled enabled. - * - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testApiCallWithProfiler() - { - global $API; - $API['api_path'] = [ - 'method' => 'method', - 'func' => function () { - return ['data' => ['some_data']]; - } - ]; - - $_SERVER['REQUEST_METHOD'] = 'method'; - $_SERVER['QUERY_STRING'] = 'pagename=api_path'; - - $this->config->set('system', 'profiler', true); - $this->config->set('rendertime', 'callstack', true); - $this->app->callstack = [ - 'database' => ['some_function' => 200], - 'database_write' => ['some_function' => 200], - 'cache' => ['some_function' => 200], - 'cache_write' => ['some_function' => 200], - 'network' => ['some_function' => 200] - ]; - - self::assertEquals( - '["some_data"]', - api_call('api_path', 'json') - ); - } - - /** - * Test the api_call() function with a JSON result. - * - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testApiCallWithJson() - { - global $API; - $API['api_path'] = [ - 'method' => 'method', - 'func' => function () { - return ['data' => ['some_data']]; - } - ]; - $_SERVER['REQUEST_METHOD'] = 'method'; - $_SERVER['QUERY_STRING'] = 'pagename=api_path.json'; - - self::assertEquals( - '["some_data"]', - api_call('api_path.json', 'json') - ); - } - - /** - * Test the api_call() function with an XML result. - * - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testApiCallWithXml() - { - global $API; - $API['api_path'] = [ - 'method' => 'method', - 'func' => function () { - return 'some_data'; - } - ]; - $_SERVER['REQUEST_METHOD'] = 'method'; - $_SERVER['QUERY_STRING'] = 'pagename=api_path.xml'; - - $args = DI::args()->determine($_SERVER, $_GET); - - self::assertEquals( - 'some_data', - api_call('api_path.xml', 'xml') - ); - } - - /** - * Test the api_call() function with an RSS result. - * - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testApiCallWithRss() - { - global $API; - $API['api_path'] = [ - 'method' => 'method', - 'func' => function () { - return 'some_data'; - } - ]; - $_SERVER['REQUEST_METHOD'] = 'method'; - $_SERVER['QUERY_STRING'] = 'pagename=api_path.rss'; - - self::assertEquals( - '' . "\n" . - 'some_data', - api_call('api_path.rss', 'rss') - ); - } - - /** - * Test the api_call() function with an Atom result. - * - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testApiCallWithAtom() - { - global $API; - $API['api_path'] = [ - 'method' => 'method', - 'func' => function () { - return 'some_data'; - } - ]; - $_SERVER['REQUEST_METHOD'] = 'method'; - $_SERVER['QUERY_STRING'] = 'pagename=api_path.atom'; - - self::assertEquals( - '' . "\n" . - 'some_data', - api_call('api_path.atom', 'atom') - ); - } - /** * Test the Arrays::walkRecursive() function. * @@ -511,290 +297,4 @@ class ApiTest extends FixtureTest ) ); } - - /** - * Test the api_lists_list() function. - * - * @return void - */ - public function testApiListsList() - { - $result = api_lists_list('json'); - self::assertEquals(['lists_list' => []], $result); - } - - /** - * Test the api_lists_ownerships() function. - * - * @return void - */ - public function testApiListsOwnerships() - { - $result = api_lists_ownerships('json'); - foreach ($result['lists']['lists'] as $list) { - self::assertList($list); - } - } - - /** - * Test the api_lists_ownerships() function without an authenticated user. - * - * @return void - */ - public function testApiListsOwnershipsWithoutAuthenticatedUser() - { - $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); - BasicAuth::setCurrentUserID(); - $_SESSION['authenticated'] = false; - api_lists_ownerships('json'); - } - - /** - * Test the api_fr_photos_list() function. - * - * @return void - */ - public function testApiFrPhotosList() - { - $result = api_fr_photos_list('json'); - self::assertArrayHasKey('photo', $result); - } - - /** - * Test the api_fr_photos_list() function without an authenticated user. - * - * @return void - */ - public function testApiFrPhotosListWithoutAuthenticatedUser() - { - $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); - BasicAuth::setCurrentUserID(); - $_SESSION['authenticated'] = false; - api_fr_photos_list('json'); - } - - /** - * Test the api_fr_photo_create_update() function. - */ - public function testApiFrPhotoCreateUpdate() - { - $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - api_fr_photo_create_update('json'); - } - - /** - * Test the api_fr_photo_create_update() function without an authenticated user. - * - * @return void - */ - public function testApiFrPhotoCreateUpdateWithoutAuthenticatedUser() - { - $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); - BasicAuth::setCurrentUserID(); - $_SESSION['authenticated'] = false; - api_fr_photo_create_update('json'); - } - - /** - * Test the api_fr_photo_create_update() function with an album name. - * - * @return void - */ - public function testApiFrPhotoCreateUpdateWithAlbum() - { - $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - $_REQUEST['album'] = 'album_name'; - api_fr_photo_create_update('json'); - } - - /** - * Test the api_fr_photo_create_update() function with the update mode. - * - * @return void - */ - public function testApiFrPhotoCreateUpdateWithUpdate() - { - $this->markTestIncomplete('We need to create a dataset for this'); - } - - /** - * Test the api_fr_photo_create_update() function with an uploaded file. - * - * @return void - */ - public function testApiFrPhotoCreateUpdateWithFile() - { - $this->markTestIncomplete(); - } - - /** - * Test the api_fr_photo_detail() function. - * - * @return void - */ - public function testApiFrPhotoDetail() - { - $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - api_fr_photo_detail('json'); - } - - /** - * Test the api_fr_photo_detail() function without an authenticated user. - * - * @return void - */ - public function testApiFrPhotoDetailWithoutAuthenticatedUser() - { - $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); - BasicAuth::setCurrentUserID(); - $_SESSION['authenticated'] = false; - api_fr_photo_detail('json'); - } - - /** - * Test the api_fr_photo_detail() function with a photo ID. - * - * @return void - */ - public function testApiFrPhotoDetailWithPhotoId() - { - $this->expectException(\Friendica\Network\HTTPException\NotFoundException::class); - $_REQUEST['photo_id'] = 1; - api_fr_photo_detail('json'); - } - - /** - * Test the api_fr_photo_detail() function with a correct photo ID. - * - * @return void - */ - public function testApiFrPhotoDetailCorrectPhotoId() - { - $this->markTestIncomplete('We need to create a dataset for this.'); - } - - /** - * Test the api_account_update_profile_image() function. - * - * @return void - */ - public function testApiAccountUpdateProfileImage() - { - $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - api_account_update_profile_image('json'); - } - - /** - * Test the api_account_update_profile_image() function without an authenticated user. - * - * @return void - */ - public function testApiAccountUpdateProfileImageWithoutAuthenticatedUser() - { - $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); - BasicAuth::setCurrentUserID(); - $_SESSION['authenticated'] = false; - api_account_update_profile_image('json'); - } - - /** - * Test the api_account_update_profile_image() function with an uploaded file. - * - * @return void - */ - public function testApiAccountUpdateProfileImageWithUpload() - { - $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - $this->markTestIncomplete(); - } - - /** - * Test the save_media_to_database() function. - * - * @return void - */ - public function testSaveMediaToDatabase() - { - $this->markTestIncomplete(); - } - - /** - * Test the post_photo_item() function. - * - * @return void - */ - public function testPostPhotoItem() - { - $this->markTestIncomplete(); - } - - /** - * Test the prepare_photo_data() function. - * - * @return void - */ - public function testPreparePhotoData() - { - $this->markTestIncomplete(); - } - - /** - * Test the api_friendica_group_show() function. - * - * @return void - */ - public function testApiFriendicaGroupShow() - { - $this->markTestIncomplete(); - } - - /** - * Test the api_lists_destroy() function. - * - * @return void - */ - public function testApiListsDestroy() - { - $this->markTestIncomplete(); - } - - /** - * Test the group_create() function. - * - * @return void - */ - public function testGroupCreate() - { - $this->markTestIncomplete(); - } - - /** - * Test the api_friendica_group_create() function. - * - * @return void - */ - public function testApiFriendicaGroupCreate() - { - $this->markTestIncomplete(); - } - - /** - * Test the api_lists_create() function. - * - * @return void - */ - public function testApiListsCreate() - { - $this->markTestIncomplete(); - } - - /** - * Test the api_lists_update() function. - * - * @return void - */ - public function testApiListsUpdate() - { - $this->markTestIncomplete(); - } } diff --git a/tests/src/Factory/Api/Twitter/ActivitiesTest.php b/tests/src/Factory/Api/Twitter/ActivitiesTest.php index 349537468e..7acdc09556 100644 --- a/tests/src/Factory/Api/Twitter/ActivitiesTest.php +++ b/tests/src/Factory/Api/Twitter/ActivitiesTest.php @@ -36,7 +36,7 @@ class ActivitiesTest extends FixtureTest { $item = ['uid' => 0, 'uri-id' => 1]; - $result = (new Activities(DI::logger(), DI::baseUrl(), DI::twitterUser())) + $result = (new Activities(DI::logger(), DI::twitterUser())) ->createFromUriId($item['uri-id'], $item['uid']); self::assertArrayHasKey('like', $result); @@ -55,7 +55,7 @@ class ActivitiesTest extends FixtureTest { $item = ['uid' => 0, 'uri-id' => 1]; - $result = (new Activities(DI::logger(), DI::baseUrl(), DI::twitterUser())) + $result = (new Activities(DI::logger(), DI::twitterUser())) ->createFromUriId($item['uri-id'], $item['uid'], 'xml'); self::assertArrayHasKey('friendica:like', $result); diff --git a/tests/src/Factory/Api/Twitter/StatusTest.php b/tests/src/Factory/Api/Twitter/StatusTest.php index 337fab086f..f608b66bb1 100644 --- a/tests/src/Factory/Api/Twitter/StatusTest.php +++ b/tests/src/Factory/Api/Twitter/StatusTest.php @@ -48,7 +48,7 @@ class StatusTest extends FixtureTest new Media(DI::logger(), DI::baseUrl()), new Url(DI::logger()), new Mention(DI::logger(), DI::baseUrl()), - new Activities(DI::logger(), DI::baseUrl(), DI::twitterUser()), + new Activities(DI::logger(), DI::twitterUser()), new Attachment(DI::logger())); } diff --git a/tests/src/Module/Api/Twitter/Lists/StatusesTest.php b/tests/src/Module/Api/Twitter/Lists/StatusesTest.php index 1f9226b84a..8bc8257c0c 100644 --- a/tests/src/Module/Api/Twitter/Lists/StatusesTest.php +++ b/tests/src/Module/Api/Twitter/Lists/StatusesTest.php @@ -38,7 +38,7 @@ class StatusesTest extends ApiTest { $this->expectException(BadRequestException::class); - (new Statuses(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) + (new Statuses(DI::dba(), DI::twitterStatus(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run(); } @@ -47,7 +47,7 @@ class StatusesTest extends ApiTest */ public function testApiListsStatusesWithListId() { - $response = (new Statuses(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) + $response = (new Statuses(DI::dba(), DI::twitterStatus(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [])) ->run([ 'list_id' => 1, 'page' => -1, @@ -67,7 +67,7 @@ class StatusesTest extends ApiTest */ public function testApiListsStatusesWithListIdAndRss() { - $response = (new Statuses(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'rss'])) + $response = (new Statuses(DI::dba(), DI::twitterStatus(), DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), [], ['extension' => 'rss'])) ->run([ 'list_id' => 1 ]); diff --git a/view/lang/C/messages.po b/view/lang/C/messages.po index 7860e4b4bb..9273287808 100644 --- a/view/lang/C/messages.po +++ b/view/lang/C/messages.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 2021.12-rc\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2022-01-12 21:28+0000\n" +"POT-Creation-Date: 2022-01-16 08:58+0000\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n"