]> git.mxchange.org Git - friendica.git/blob - src/Model/Mail.php
Use the owner, not the author
[friendica.git] / src / Model / Mail.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Protocol\Delivery;
32 use Friendica\Util\DateTimeFormat;
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 $sender_uid the user id of the sender
121          * @param integer $recipient recipient id, default 0
122          * @param string  $body      message body, default empty
123          * @param string  $subject   message subject, default empty
124          * @param string  $replyto   reply to
125          * @return int
126          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
127          */
128         public static function send(int $sender_uid, int $recipient = 0, string $body = '', string $subject = '', string $replyto = ''): int
129         {
130                 $a = DI::app();
131
132                 if (!$recipient) {
133                         return -1;
134                 }
135
136                 if (!strlen($subject)) {
137                         $subject = DI::l10n()->t('[no subject]');
138                 }
139
140                 $me = DBA::selectFirst('contact', [], ['uid' => $sender_uid, 'self' => true]);
141                 if (!DBA::isResult($me)) {
142                         return -2;
143                 }
144
145                 $contacts = ACL::getValidMessageRecipientsForUser($sender_uid);
146
147                 $contactIndex = array_search($recipient, array_column($contacts, 'id'));
148                 if ($contactIndex === false) {
149                         return -2;
150                 }
151
152                 $contact = $contacts[$contactIndex];
153
154                 Photo::setPermissionFromBody($body, $sender_uid, $me['id'], '<' . $contact['id'] . '>', '', '', '');
155
156                 $guid = System::createUUID();
157                 $uri = Item::newURI($guid);
158
159                 $convid = 0;
160                 $reply = false;
161
162                 // look for any existing conversation structure
163
164                 if (strlen($replyto)) {
165                         $reply = true;
166                         $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
167                                 $sender_uid, $replyto, $replyto];
168                         $mail = DBA::selectFirst('mail', ['convid'], $condition);
169                         if (DBA::isResult($mail)) {
170                                 $convid = $mail['convid'];
171                         }
172                 }
173
174                 $convuri = '';
175                 if (!$convid) {
176                         // create a new conversation
177                         $conv_guid = System::createUUID();
178                         $convuri = $contact['addr'] . ':' . $conv_guid;
179
180                         $fields = ['uid' => $sender_uid, 'guid' => $conv_guid, 'creator' => $me['addr'],
181                                 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
182                                 'subject' => $subject, 'recips' => $contact['addr'] . ';' . $me['addr']];
183                         if (DBA::insert('conv', $fields)) {
184                                 $convid = DBA::lastInsertId();
185                         }
186                 }
187
188                 if (!$convid) {
189                         Logger::warning('conversation not found.');
190                         return -4;
191                 }
192
193                 if (!strlen($replyto)) {
194                         $replyto = $convuri;
195                 }
196
197                 $post_id = self::insert(
198                         [
199                                 'uid' => $sender_uid,
200                                 'guid' => $guid,
201                                 'convid' => $convid,
202                                 'from-name' => $me['name'],
203                                 'from-photo' => $me['thumb'],
204                                 'from-url' => $me['url'],
205                                 'contact-id' => $recipient,
206                                 'title' => $subject,
207                                 'body' => $body,
208                                 'seen' => 1,
209                                 'reply' => $reply,
210                                 'replied' => 0,
211                                 'uri' => $uri,
212                                 'parent-uri' => $replyto,
213                                 'created' => DateTimeFormat::utcNow()
214                         ],
215                         false
216                 );
217
218                 /**
219                  *
220                  * When a photo was uploaded into the message using the (profile wall) ajax
221                  * uploader, The permissions are initially set to disallow anybody but the
222                  * owner from seeing it. This is because the permissions may not yet have been
223                  * set for the post. If it's private, the photo permissions should be set
224                  * appropriately. But we didn't know the final permissions on the post until
225                  * now. So now we'll look for links of uploaded messages that are in the
226                  * post and set them to the same permissions as the post itself.
227                  *
228                  */
229                 $match = null;
230                 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
231                         $images = $match[1];
232                         if (count($images)) {
233                                 foreach ($images as $image) {
234                                         $image_rid = Photo::ridFromURI($image);
235                                         if (!empty($image_rid)) {
236                                                 Photo::update(['allow_cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => $sender_uid]);
237                                         }
238                                 }
239                         }
240                 }
241
242                 if ($post_id) {
243                         Worker::add(Worker::PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
244                         return intval($post_id);
245                 } else {
246                         return -3;
247                 }
248         }
249
250         /**
251          * @param array  $recipient recipient, default empty
252          * @param string $body      message body, default empty
253          * @param string $subject   message subject, default empty
254          * @param string $replyto   reply to, default empty
255          * @return int
256          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
257          * @throws \ImagickException
258          */
259         public static function sendWall(array $recipient = [], string $body = '', string $subject = '', string $replyto = ''): int
260         {
261                 if (!$recipient) {
262                         return -1;
263                 }
264
265                 if (!strlen($subject)) {
266                         $subject = DI::l10n()->t('[no subject]');
267                 }
268
269                 $guid = System::createUUID();
270                 $uri = Item::newURI($guid);
271
272                 $me = Contact::getByURL($replyto);
273                 if (!$me['name']) {
274                         return -2;
275                 }
276
277                 $conv_guid = System::createUUID();
278
279                 $recip_handle = $recipient['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
280
281                 $sender_handle = $me['addr'];
282
283                 $handles = $recip_handle . ';' . $sender_handle;
284
285                 $convid = null;
286                 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
287                         'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
288                         'subject' => $subject, 'recips' => $handles];
289                 if (DBA::insert('conv', $fields)) {
290                         $convid = DBA::lastInsertId();
291                 }
292
293                 if (!$convid) {
294                         Logger::warning('conversation not found.');
295                         return -4;
296                 }
297
298                 self::insert(
299                         [
300                                 'uid' => $recipient['uid'],
301                                 'guid' => $guid,
302                                 'convid' => $convid,
303                                 'from-name' => $me['name'],
304                                 'from-photo' => $me['photo'],
305                                 'from-url' => $me['url'],
306                                 'contact-id' => 0,
307                                 'title' => $subject,
308                                 'body' => $body,
309                                 'seen' => 0,
310                                 'reply' => 0,
311                                 'replied' => 0,
312                                 'uri' => $uri,
313                                 'parent-uri' => $me['url'],
314                                 'created' => DateTimeFormat::utcNow(),
315                                 'unknown' => 1
316                         ],
317                         false
318                 );
319
320                 return 0;
321         }
322 }