]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Twitter/Statuses/Update.php
Update src/Module/Api/Twitter/Statuses/Update.php
[friendica.git] / src / Module / Api / Twitter / Statuses / Update.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Twitter\Statuses;
23
24 use Friendica\Content\Text\HTML;
25 use Friendica\Content\Text\Markdown;
26 use Friendica\Core\Protocol;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Item;
31 use Friendica\Model\Photo;
32 use Friendica\Model\Post;
33 use Friendica\Model\User;
34 use Friendica\Module\BaseApi;
35 use Friendica\Protocol\Activity;
36 use Friendica\Util\Images;
37 use HTMLPurifier;
38 use HTMLPurifier_Config;
39
40 /**
41  * Updates the user’s current status.
42  *
43  * @see https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update
44  */
45 class Update extends BaseApi
46 {
47         public function post(array $request = [])
48         {
49                 self::checkAllowedScope(self::SCOPE_WRITE);
50                 $uid = self::getCurrentUserID();
51
52                 $owner = User::getOwnerDataById($uid);
53
54                 $request = self::getRequest([
55                         'htmlstatus'            => '',
56                         'status'                => '',
57                         'title'                 => '',
58                         'in_reply_to_status_id' => 0,
59                         'lat'                   => 0,
60                         'long'                  => 0,
61                         'media_ids'             => '',
62                         'source'                => '',
63                         'include_entities'      => false,
64                         'contact_allow'         => $owner['allow_cid'],
65                         'group_allow'           => $owner['allow_gid'],
66                         'contact_deny'          => $owner['deny_cid'],
67                         'group_deny'            => $owner['deny_gid'],
68                 ], $request);
69
70                 if (!empty($request['htmlstatus'])) {
71                         $body = HTML::toBBCodeVideo($request['htmlstatus']);
72
73                         $config = HTMLPurifier_Config::createDefault();
74                         $config->set('Cache.DefinitionImpl', null);
75
76                         $purifier = new HTMLPurifier($config);
77                         $body     = $purifier->purify($body);
78
79                         $body = HTML::toBBCode($request['htmlstatus']);
80                 } else {
81                         // The imput is defined as text. So we can use Markdown for some enhancements
82                         $body = Markdown::toBBCode($request['status']);
83                 }
84
85                 $item               = [];
86                 $item['network']    = Protocol::DFRN;
87                 $item['uid']        = $uid;
88                 $item['verb']       = Activity::POST;
89                 $item['contact-id'] = $owner['id'];
90                 $item['author-id']  = $item['owner-id'] = Contact::getPublicIdByUserId($uid);
91                 $item['title']      = $request['title'];
92                 $item['body']       = $body;
93                 $item['app']        = $request['source'];
94
95                 if (empty($item['app']) && !empty(self::getCurrentApplication()['name'])) {
96                         $item['app'] = self::getCurrentApplication()['name'];
97                 }
98
99                 if (!empty($request['lat']) && !empty($request['long'])) {
100                         $item['coord'] = sprintf("%s %s", $request['lat'], $request['long']);
101                 }
102
103                 $aclFormatter = DI::aclFormatter();
104                 $item['allow_cid'] = $aclFormatter->toString($request['contact_allow']);
105                 $item['allow_gid'] = $aclFormatter->toString($request['group_allow']);
106                 $item['deny_cid']  = $aclFormatter->toString($request['contact_deny']);
107                 $item['deny_gid']  = $aclFormatter->toString($request['group_deny']);
108
109                 if (!empty($item['allow_cid'] . $item['allow_gid'] . $item['deny_cid'] . $item['deny_gid'])) {
110                         $item['private'] = Item::PRIVATE;
111                 } elseif (DI::pConfig()->get($uid, 'system', 'unlisted')) {
112                         $item['private'] = Item::UNLISTED;
113                 } else {
114                         $item['private'] = Item::PUBLIC;
115                 }
116
117                 if ($request['in_reply_to_status_id']) {
118                         $parent = Post::selectFirst(['uri'], ['uri-id' => $request['in_reply_to_status_id'], 'uid' => [0, $uid]]);
119
120                         $item['thr-parent']  = $parent['uri'];
121                         $item['gravity']     = Item::GRAVITY_COMMENT;
122                         $item['object-type'] = Activity\ObjectType::COMMENT;
123                 } else {
124                         self::checkThrottleLimit();
125
126                         $item['gravity']     = Item::GRAVITY_PARENT;
127                         $item['object-type'] = Activity\ObjectType::NOTE;
128                 }
129
130                 $item = DI::contentItem()->expandTags($item);
131
132                 if (!empty($request['media_ids'])) {
133                         $ids = explode(',', $request['media_ids']);
134                 } elseif (!empty($_FILES['media'])) {
135                         // upload the image if we have one
136                         $picture = Photo::upload($uid, $_FILES['media']);
137                         if (!empty($picture)) {
138                                 $ids[] = $picture['id'];
139                         }
140                 }
141
142                 if (!empty($ids)) {
143                         $item['object-type'] = Activity\ObjectType::IMAGE;
144                         $item['post-type']   = Item::PT_IMAGE;
145                         $item['attachments'] = [];
146
147                         foreach ($ids as $id) {
148                                 $media = DBA::toArray(DBA::p("SELECT `resource-id`, `scale`, `type`, `desc`, `filename`, `datasize`, `width`, `height` FROM `photo`
149                                                 WHERE `resource-id` IN (SELECT `resource-id` FROM `photo` WHERE `id` = ?) AND `photo`.`uid` = ?
150                                                 ORDER BY `photo`.`width` DESC LIMIT 2", $id, $uid));
151
152                                 if (empty($media)) {
153                                         continue;
154                                 }
155
156                                 Photo::setPermissionForRessource($media[0]['resource-id'], $uid, $item['allow_cid'], $item['allow_gid'], $item['deny_cid'], $item['deny_gid']);
157
158                                 $ressources[] = $media[0]['resource-id'];
159                                 $phototypes   = Images::supportedTypes();
160                                 $ext          = $phototypes[$media[0]['type']];
161
162                                 $attachment = [
163                                         'type'        => Post\Media::IMAGE,
164                                         'mimetype'    => $media[0]['type'],
165                                         'url'         => DI::baseUrl() . '/photo/' . $media[0]['resource-id'] . '-' . $media[0]['scale'] . '.' . $ext,
166                                         'size'        => $media[0]['datasize'],
167                                         'name'        => $media[0]['filename'] ?: $media[0]['resource-id'],
168                                         'description' => $media[0]['desc'] ?? '',
169                                         'width'       => $media[0]['width'],
170                                         'height'      => $media[0]['height']
171                                 ];
172
173                                 if (count($media) > 1) {
174                                         $attachment['preview']        = DI::baseUrl() . '/photo/' . $media[1]['resource-id'] . '-' . $media[1]['scale'] . '.' . $ext;
175                                         $attachment['preview-width']  = $media[1]['width'];
176                                         $attachment['preview-height'] = $media[1]['height'];
177                                 }
178                                 $item['attachments'][] = $attachment;
179                         }
180                 }
181
182                 $id = Item::insert($item, true);
183                 if (!empty($id)) {
184                         $item = Post::selectFirst(['uri-id'], ['id' => $id]);
185                         if (!empty($item['uri-id'])) {
186                                 // output the post that we just posted.
187                                 $status_info = DI::twitterStatus()->createFromUriId($item['uri-id'], $uid, $request['include_entities'])->toArray();
188                                 DI::apiResponse()->exit('status', ['status' => $status_info], $this->parameters['extension'] ?? null, Contact::getPublicIdByUserId($uid));
189                                 return;
190                         }
191                 }
192                 DI::mstdnError()->InternalError();
193         }
194 }