]> git.mxchange.org Git - friendica.git/blob - src/Model/Mail.php
Merge branch 'friendica:2022.12-rc' into new_image_presentation
[friendica.git] / src / Model / Mail.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\Model;
23
24 use Friendica\Core\ACL;
25 use Friendica\Core\Logger;
26 use Friendica\Core\System;
27 use Friendica\Core\Worker;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Protocol\Activity;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Worker\Delivery;
33
34 /**
35  * Class to handle private messages
36  */
37 class Mail
38 {
39         /**
40          * Insert private message
41          *
42          * @param array $msg
43          * @param bool  $notification
44          * @return int|boolean Message ID or false on error
45          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
46          * @throws \ImagickException
47          */
48         public static function insert(array $msg, bool $notification = true)
49         {
50                 if (!isset($msg['reply'])) {
51                         $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
52                 }
53
54                 if (empty($msg['convid'])) {
55                         $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
56                         if (DBA::isResult($mail)) {
57                                 $msg['convid'] = $mail['convid'];
58                         }
59                 }
60
61                 if (empty($msg['guid'])) {
62                         $msg['guid'] = Item::guidFromUri($msg['uri'], parse_url($msg['from-url'], PHP_URL_HOST));
63                 }
64
65                 $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());
66
67                 $msg['author-id']     = Contact::getIdForURL($msg['from-url'], 0, false);
68                 $msg['uri-id']        = ItemURI::insert(['uri' => $msg['uri'], 'guid' => $msg['guid']]);
69                 $msg['parent-uri-id'] = ItemURI::getIdByURI($msg['parent-uri']);
70
71                 DBA::lock('mail');
72
73                 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
74                         DBA::unlock();
75                         Logger::info('duplicate message already delivered.');
76                         return false;
77                 }
78
79                 if ($msg['reply'] && DBA::isResult($reply = DBA::selectFirst('mail', ['uri', 'uri-id'], ['parent-uri' => $msg['parent-uri'], 'reply' => false]))) {
80                         $msg['thr-parent']    = $reply['uri'];
81                         $msg['thr-parent-id'] = $reply['uri-id'];
82                 } else {
83                         $msg['thr-parent']    = $msg['uri'];
84                         $msg['thr-parent-id'] = $msg['uri-id'];
85                 }
86
87                 DBA::insert('mail', $msg);
88
89                 $msg['id'] = DBA::lastInsertId();
90
91                 DBA::unlock();
92
93                 if (!empty($msg['convid'])) {
94                         DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
95                 }
96
97                 if ($notification) {
98                         $user = User::getById($msg['uid']);
99                         // send notifications.
100                         $notif_params = [
101                                 'type'  => Notification\Type::MAIL,
102                                 'otype' => Notification\ObjectType::MAIL,
103                                 'verb'  => Activity::POST,
104                                 'uid'   => $user['uid'],
105                                 'cid'   => $msg['contact-id'],
106                                 'link'  => DI::baseUrl() . '/message/' . $msg['id'],
107                         ];
108
109                         DI::notify()->createFromArray($notif_params);
110
111                         Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
112                 }
113
114                 return $msg['id'];
115         }
116
117         /**
118          * Send private message
119          *
120          * @param integer $recipient recipient id, default 0
121          * @param string  $body      message body, default empty
122          * @param string  $subject   message subject, default empty
123          * @param string  $replyto   reply to
124          * @return int
125          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
126          */
127         public static function send(int $recipient = 0, string $body = '', string $subject = '', string $replyto = ''): int
128         {
129                 $a = DI::app();
130
131                 if (!$recipient) {
132                         return -1;
133                 }
134
135                 if (!strlen($subject)) {
136                         $subject = DI::l10n()->t('[no subject]');
137                 }
138
139                 $me = DBA::selectFirst('contact', [], ['uid' => DI::userSession()->getLocalUserId(), 'self' => true]);
140                 if (!DBA::isResult($me)) {
141                         return -2;
142                 }
143
144                 $contacts = ACL::getValidMessageRecipientsForUser(DI::userSession()->getLocalUserId());
145
146                 $contactIndex = array_search($recipient, array_column($contacts, 'id'));
147                 if ($contactIndex === false) {
148                         return -2;
149                 }
150
151                 $contact = $contacts[$contactIndex];
152
153                 Photo::setPermissionFromBody($body, DI::userSession()->getLocalUserId(), $me['id'],  '<' . $contact['id'] . '>', '', '', '');
154
155                 $guid = System::createUUID();
156                 $uri = Item::newURI($guid);
157
158                 $convid = 0;
159                 $reply = false;
160
161                 // look for any existing conversation structure
162
163                 if (strlen($replyto)) {
164                         $reply = true;
165                         $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
166                                 DI::userSession()->getLocalUserId(), $replyto, $replyto];
167                         $mail = DBA::selectFirst('mail', ['convid'], $condition);
168                         if (DBA::isResult($mail)) {
169                                 $convid = $mail['convid'];
170                         }
171                 }
172
173                 $convuri = '';
174                 if (!$convid) {
175                         // create a new conversation
176                         $conv_guid = System::createUUID();
177                         $convuri = $contact['addr'] . ':' . $conv_guid;
178
179                         $fields = ['uid' => DI::userSession()->getLocalUserId(), 'guid' => $conv_guid, 'creator' => $me['addr'],
180                                 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
181                                 'subject' => $subject, 'recips' => $contact['addr'] . ';' . $me['addr']];
182                         if (DBA::insert('conv', $fields)) {
183                                 $convid = DBA::lastInsertId();
184                         }
185                 }
186
187                 if (!$convid) {
188                         Logger::warning('conversation not found.');
189                         return -4;
190                 }
191
192                 if (!strlen($replyto)) {
193                         $replyto = $convuri;
194                 }
195
196                 $post_id = self::insert(
197                         [
198                                 'uid' => DI::userSession()->getLocalUserId(),
199                                 'guid' => $guid,
200                                 'convid' => $convid,
201                                 'from-name' => $me['name'],
202                                 'from-photo' => $me['thumb'],
203                                 'from-url' => $me['url'],
204                                 'contact-id' => $recipient,
205                                 'title' => $subject,
206                                 'body' => $body,
207                                 'seen' => 1,
208                                 'reply' => $reply,
209                                 'replied' => 0,
210                                 'uri' => $uri,
211                                 'parent-uri' => $replyto,
212                                 'created' => DateTimeFormat::utcNow()
213                         ], false
214                 );
215
216                 /**
217                  *
218                  * When a photo was uploaded into the message using the (profile wall) ajax
219                  * uploader, The permissions are initially set to disallow anybody but the
220                  * owner from seeing it. This is because the permissions may not yet have been
221                  * set for the post. If it's private, the photo permissions should be set
222                  * appropriately. But we didn't know the final permissions on the post until
223                  * now. So now we'll look for links of uploaded messages that are in the
224                  * post and set them to the same permissions as the post itself.
225                  *
226                  */
227                 $match = null;
228                 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
229                         $images = $match[1];
230                         if (count($images)) {
231                                 foreach ($images as $image) {
232                                         $image_rid = Photo::ridFromURI($image);
233                                         if (!empty($image_rid)) {
234                                                 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => DI::userSession()->getLocalUserId()]);
235                                         }
236                                 }
237                         }
238                 }
239
240                 if ($post_id) {
241                         Worker::add(Worker::PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
242                         return intval($post_id);
243                 } else {
244                         return -3;
245                 }
246         }
247
248         /**
249          * @param array  $recipient recipient, default empty
250          * @param string $body      message body, default empty
251          * @param string $subject   message subject, default empty
252          * @param string $replyto   reply to, default empty
253          * @return int
254          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
255          * @throws \ImagickException
256          */
257         public static function sendWall(array $recipient = [], string $body = '', string $subject = '', string $replyto = ''): int
258         {
259                 if (!$recipient) {
260                         return -1;
261                 }
262
263                 if (!strlen($subject)) {
264                         $subject = DI::l10n()->t('[no subject]');
265                 }
266
267                 $guid = System::createUUID();
268                 $uri = Item::newURI($guid);
269
270                 $me = Contact::getByURL($replyto);
271                 if (!$me['name']) {
272                         return -2;
273                 }
274
275                 $conv_guid = System::createUUID();
276
277                 $recip_handle = $recipient['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
278
279                 $sender_handle = $me['addr'];
280
281                 $handles = $recip_handle . ';' . $sender_handle;
282
283                 $convid = null;
284                 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
285                         'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
286                         'subject' => $subject, 'recips' => $handles];
287                 if (DBA::insert('conv', $fields)) {
288                         $convid = DBA::lastInsertId();
289                 }
290
291                 if (!$convid) {
292                         Logger::warning('conversation not found.');
293                         return -4;
294                 }
295
296                 self::insert(
297                         [
298                                 'uid' => $recipient['uid'],
299                                 'guid' => $guid,
300                                 'convid' => $convid,
301                                 'from-name' => $me['name'],
302                                 'from-photo' => $me['photo'],
303                                 'from-url' => $me['url'],
304                                 'contact-id' => 0,
305                                 'title' => $subject,
306                                 'body' => $body,
307                                 'seen' => 0,
308                                 'reply' => 0,
309                                 'replied' => 0,
310                                 'uri' => $uri,
311                                 'parent-uri' => $me['url'],
312                                 'created' => DateTimeFormat::utcNow(),
313                                 'unknown' => 1
314                         ], false
315                 );
316
317                 return 0;
318         }
319 }