From 223cb8f0375d5e0ba803e3a5636d644bda032830 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 30 Jul 2019 05:02:26 +0000 Subject: [PATCH 1/4] We can now set image descriptions via API --- include/api.php | 64 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/include/api.php b/include/api.php index 39a61c9838..0e190b84c1 100644 --- a/include/api.php +++ b/include/api.php @@ -1158,15 +1158,16 @@ function api_statuses_update($type) // To-Do: Multiple IDs if (requestdata('media_ids')) { $r = q( - "SELECT `resource-id`, `scale`, `nickname`, `type` FROM `photo` INNER JOIN `user` ON `user`.`uid` = `photo`.`uid` WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = %d) AND `scale` > 0 AND `photo`.`uid` = %d ORDER BY `photo`.`width` DESC LIMIT 1", + "SELECT `resource-id`, `scale`, `nickname`, `type`, `desc` FROM `photo` INNER JOIN `user` ON `user`.`uid` = `photo`.`uid` WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = %d) AND `scale` > 0 AND `photo`.`uid` = %d ORDER BY `photo`.`width` DESC LIMIT 1", intval(requestdata('media_ids')), api_user() ); if (DBA::isResult($r)) { $phototypes = Image::supportedTypes(); $ext = $phototypes[$r[0]['type']]; + $description = $r[0]['desc'] ?? ''; $_REQUEST['body'] .= "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $r[0]['nickname'] . '/image/' . $r[0]['resource-id'] . ']'; - $_REQUEST['body'] .= '[img]' . System::baseUrl() . '/photo/' . $r[0]['resource-id'] . '-' . $r[0]['scale'] . '.' . $ext . '[/img][/url]'; + $_REQUEST['body'] .= '[img=' . System::baseUrl() . '/photo/' . $r[0]['resource-id'] . '-' . $r[0]['scale'] . '.' . $ext . ']' . $description . '[/img][/url]'; } } @@ -1239,6 +1240,65 @@ function api_media_upload() /// @TODO move to top of file or somewhere better api_register_func('api/media/upload', 'api_media_upload', true, API_METHOD_POST); +/** + * Updates media meta data (picture descriptions) + * + * @param string $type Return type (atom, rss, xml, json) + * + * @return array|string + * @throws BadRequestException + * @throws ForbiddenException + * @throws ImagickException + * @throws InternalServerErrorException + * @throws TooManyRequestsException + * @throws UnauthorizedException + * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update + * + * @todo Compare the corresponding Twitter function for correct return values + */ +function api_media_metadata_create($type) +{ + $a = \get_app(); + + if (api_user() === false) { + Logger::info('no user'); + throw new ForbiddenException(); + } + + api_get_user($a); + + $postdata = file_get_contents('php://input'); + + if (empty($postdata)) { + throw new BadRequestException("No post data"); + } + + $data = json_decode($postdata, true); + if (empty($data)) { + throw new BadRequestException("Invalid post data"); + } + + if (empty($data['media_id']) || empty($data['alt_text'])) { + throw new BadRequestException("Missing post data values"); + } + + if (empty($data['alt_text']['text'])) { + throw new BadRequestException("No alt text."); + } + + Logger::info('Updating metadata', ['media_id' => $data['media_id']]); + + $condition = ['id' => $data['media_id'], 'uid' => api_user()]; + $photo = DBA::selectFirst('photo', ['resource-id'], $condition); + if (!DBA::isResult($photo)) { + throw new BadRequestException("Metadata not found."); + } + + DBA::update('photo', ['desc' => $data['alt_text']['text']], ['resource-id' => $photo['resource-id']]); +} + +api_register_func('api/media/metadata/create', 'api_media_metadata_create', true, API_METHOD_POST); + /** * @param string $type Return format (atom, rss, xml, json) * @param int $item_id From cf67b8aa3788de9ebd1739e2c502127c328a1f2c Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 30 Jul 2019 05:36:06 +0000 Subject: [PATCH 2/4] Added documentation --- doc/api.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/doc/api.md b/doc/api.md index ccd2e7a7ee..9d0dc8095b 100644 --- a/doc/api.md +++ b/doc/api.md @@ -393,6 +393,27 @@ Object of: --- +### media/metadata/create (POST,PUT; AUTH) + +#### Parameters + +Parameters are sent as JSON object: + +``` +{ + "media_id":"1234", + "alt_text": { + "text":"Here comes the description" + } +} +``` + +#### Return values + +None + +--- + ### oauth/request_token (*) #### Parameters From 79bc773d020175255b2957b54849d686e48373da Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 30 Jul 2019 05:38:10 +0000 Subject: [PATCH 3/4] Use tabs --- doc/api.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/api.md b/doc/api.md index 9d0dc8095b..d522767fd8 100644 --- a/doc/api.md +++ b/doc/api.md @@ -401,10 +401,10 @@ Parameters are sent as JSON object: ``` { - "media_id":"1234", - "alt_text": { - "text":"Here comes the description" - } + "media_id":"1234", + "alt_text": { + "text":"Here comes the description" + } } ``` From b4c673a6203a5b31599f23dbb35617a712304958 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 30 Jul 2019 22:26:01 +0000 Subject: [PATCH 4/4] We now use a central function for fetching the postdata --- include/api.php | 2 +- mod/dfrn_notify.php | 3 ++- mod/pubsub.php | 3 ++- mod/receive.php | 3 ++- mod/salmon.php | 3 ++- src/Module/Inbox.php | 3 ++- src/Util/Network.php | 10 ++++++++++ 7 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/api.php b/include/api.php index 0e190b84c1..5665a7fbdd 100644 --- a/include/api.php +++ b/include/api.php @@ -1267,7 +1267,7 @@ function api_media_metadata_create($type) api_get_user($a); - $postdata = file_get_contents('php://input'); + $postdata = Network::postdata(); if (empty($postdata)) { throw new BadRequestException("No post data"); diff --git a/mod/dfrn_notify.php b/mod/dfrn_notify.php index e75d975a82..3f0ecba005 100644 --- a/mod/dfrn_notify.php +++ b/mod/dfrn_notify.php @@ -16,11 +16,12 @@ use Friendica\Model\User; use Friendica\Protocol\DFRN; use Friendica\Protocol\Diaspora; use Friendica\Util\Strings; +use Friendica\Util\Network; function dfrn_notify_post(App $a) { Logger::log(__function__, Logger::TRACE); - $postdata = file_get_contents('php://input'); + $postdata = Network::postdata(); if (empty($_POST) || !empty($postdata)) { $data = json_decode($postdata); diff --git a/mod/pubsub.php b/mod/pubsub.php index e5ede6c80a..d10d7031d6 100644 --- a/mod/pubsub.php +++ b/mod/pubsub.php @@ -7,6 +7,7 @@ use Friendica\Database\DBA; use Friendica\Model\Contact; use Friendica\Protocol\OStatus; use Friendica\Util\Strings; +use Friendica\Util\Network; use Friendica\Core\System; function hub_return($valid, $body) @@ -83,7 +84,7 @@ function pubsub_init(App $a) function pubsub_post(App $a) { - $xml = file_get_contents('php://input'); + $xml = Network::postdata(); Logger::log('Feed arrived from ' . $_SERVER['REMOTE_ADDR'] . ' for ' . $a->cmd . ' with user-agent: ' . $_SERVER['HTTP_USER_AGENT']); Logger::log('Data: ' . $xml, Logger::DATA); diff --git a/mod/receive.php b/mod/receive.php index db1287ea6f..182a1df8c5 100644 --- a/mod/receive.php +++ b/mod/receive.php @@ -10,6 +10,7 @@ use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Protocol\Diaspora; +use Friendica\Util\Network; /** * @param App $a App @@ -47,7 +48,7 @@ function receive_post(App $a) Logger::log('mod-diaspora: receiving post', Logger::DEBUG); if (empty($_POST['xml'])) { - $postdata = file_get_contents("php://input"); + $postdata = Network::postdata(); if ($postdata == '') { throw new \Friendica\Network\HTTPException\InternalServerErrorException(); } diff --git a/mod/salmon.php b/mod/salmon.php index ba1bc8d465..67e467a73f 100644 --- a/mod/salmon.php +++ b/mod/salmon.php @@ -13,11 +13,12 @@ use Friendica\Protocol\OStatus; use Friendica\Protocol\Salmon; use Friendica\Util\Crypto; use Friendica\Util\Strings; +use Friendica\Util\Network; function salmon_post(App $a, $xml = '') { if (empty($xml)) { - $xml = file_get_contents('php://input'); + $xml = Network::postdata(); } Logger::log('new salmon ' . $xml, Logger::DATA); diff --git a/src/Module/Inbox.php b/src/Module/Inbox.php index 5367adb7e1..2cc273b139 100644 --- a/src/Module/Inbox.php +++ b/src/Module/Inbox.php @@ -12,6 +12,7 @@ use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Protocol\ActivityPub; use Friendica\Util\HTTPSignature; +use Friendica\Util\Network; /** * ActivityPub Inbox @@ -22,7 +23,7 @@ class Inbox extends BaseModule { $a = self::getApp(); - $postdata = file_get_contents('php://input'); + $postdata = Network::postdata(); if (empty($postdata)) { throw new \Friendica\Network\HTTPException\BadRequestException(); diff --git a/src/Util/Network.php b/src/Util/Network.php index 75b928bea2..cd66abe0b2 100644 --- a/src/Util/Network.php +++ b/src/Util/Network.php @@ -342,6 +342,16 @@ class Network return $curlResponse; } + /** + * Return raw post data from a post request + * + * @return string post data + */ + public static function postdata() + { + return file_get_contents('php://input'); + } + /** * @brief Check URL to see if it's real *