]> git.mxchange.org Git - friendica.git/blob - src/Model/Mail.php
Use rawContent for Special Options to avoid a protected options() method
[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\Logger;
25 use Friendica\Core\System;
26 use Friendica\Core\Worker;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Protocol\Activity;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Worker\Delivery;
32
33 /**
34  * Class to handle private messages
35  */
36 class Mail
37 {
38         /**
39          * Insert private message
40          *
41          * @param array $msg
42          * @param bool  $notifiction
43          * @return int|boolean Message ID or false on error
44          */
45         public static function insert($msg, $notifiction = true)
46         {
47                 if (!isset($msg['reply'])) {
48                         $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
49                 }
50
51                 if (empty($msg['convid'])) {
52                         $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
53                         if (DBA::isResult($mail)) {
54                                 $msg['convid'] = $mail['convid'];
55                         }
56                 }
57
58                 if (empty($msg['guid'])) {
59                         $host = parse_url($msg['from-url'], PHP_URL_HOST);
60                         $msg['guid'] = Item::guidFromUri($msg['uri'], $host);
61                 }
62
63                 $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());
64
65                 $msg['author-id']     = Contact::getIdForURL($msg['from-url'], 0, false);
66                 $msg['uri-id']        = ItemURI::insert(['uri' => $msg['uri'], 'guid' => $msg['guid']]);
67                 $msg['parent-uri-id'] = ItemURI::getIdByURI($msg['parent-uri']);
68
69                 DBA::lock('mail');
70
71                 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
72                         DBA::unlock();
73                         Logger::info('duplicate message already delivered.');
74                         return false;
75                 }
76
77                 if ($msg['reply'] && DBA::isResult($reply = DBA::selectFirst('mail', ['uri', 'uri-id'], ['parent-uri' => $msg['parent-uri'], 'reply' => false]))) {
78                         $msg['thr-parent']    = $reply['uri'];
79                         $msg['thr-parent-id'] = $reply['uri-id'];
80                 } else {
81                         $msg['thr-parent']    = $msg['uri'];
82                         $msg['thr-parent-id'] = $msg['uri-id'];
83                 }
84
85                 DBA::insert('mail', $msg);
86
87                 $msg['id'] = DBA::lastInsertId();
88
89                 DBA::unlock();
90
91                 if (!empty($msg['convid'])) {
92                         DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
93                 }
94
95                 if ($notifiction) {
96                         $user = User::getById($msg['uid']);
97                         // send notifications.
98                         $notif_params = [
99                                 'type'  => Notification\Type::MAIL,
100                                 'otype' => Notification\ObjectType::MAIL,
101                                 'verb'  => Activity::POST,
102                                 'uid'   => $user['uid'],
103                                 'cid'   => $msg['contact-id'],
104                                 'link'  => DI::baseUrl() . '/message/' . $msg['id'],
105                         ];
106
107                         DI::notify()->createFromArray($notif_params);
108
109                         Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
110                 }
111
112                 return $msg['id'];
113         }
114
115         /**
116          * Send private message
117          *
118          * @param integer $recipient recipient id, default 0
119          * @param string  $body      message body, default empty
120          * @param string  $subject   message subject, default empty
121          * @param string  $replyto   reply to
122          * @return int
123          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
124          */
125         public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
126         {
127                 $a = DI::app();
128
129                 if (!$recipient) {
130                         return -1;
131                 }
132
133                 if (!strlen($subject)) {
134                         $subject = DI::l10n()->t('[no subject]');
135                 }
136
137                 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
138                 if (!DBA::isResult($me)) {
139                         return -2;
140                 }
141
142                 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
143                 if (!DBA::isResult($contact)) {
144                         return -2;
145                 }
146
147                 Photo::setPermissionFromBody($body, local_user(), $me['id'],  '<' . $contact['id'] . '>', '', '', '');
148
149                 $guid = System::createUUID();
150                 $uri = Item::newURI(local_user(), $guid);
151
152                 $convid = 0;
153                 $reply = false;
154
155                 // look for any existing conversation structure
156
157                 if (strlen($replyto)) {
158                         $reply = true;
159                         $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
160                                 local_user(), $replyto, $replyto];
161                         $mail = DBA::selectFirst('mail', ['convid'], $condition);
162                         if (DBA::isResult($mail)) {
163                                 $convid = $mail['convid'];
164                         }
165                 }
166
167                 $convuri = '';
168                 if (!$convid) {
169                         // create a new conversation
170                         $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
171                         $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
172
173                         $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
174                         $sender_handle = $a->getLoggedInUserNickname() . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
175
176                         $conv_guid = System::createUUID();
177                         $convuri = $recip_handle . ':' . $conv_guid;
178
179                         $handles = $recip_handle . ';' . $sender_handle;
180
181                         $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
182                                 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
183                                 'subject' => $subject, 'recips' => $handles];
184                         if (DBA::insert('conv', $fields)) {
185                                 $convid = DBA::lastInsertId();
186                         }
187                 }
188
189                 if (!$convid) {
190                         Logger::notice('send message: conversation not found.');
191                         return -4;
192                 }
193
194                 if (!strlen($replyto)) {
195                         $replyto = $convuri;
196                 }
197
198                 $post_id = self::insert(
199                         [
200                                 'uid' => local_user(),
201                                 'guid' => $guid,
202                                 'convid' => $convid,
203                                 'from-name' => $me['name'],
204                                 'from-photo' => $me['thumb'],
205                                 'from-url' => $me['url'],
206                                 'contact-id' => $recipient,
207                                 'title' => $subject,
208                                 'body' => $body,
209                                 'seen' => 1,
210                                 'reply' => $reply,
211                                 'replied' => 0,
212                                 'uri' => $uri,
213                                 'parent-uri' => $replyto,
214                                 'created' => DateTimeFormat::utcNow()
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' => local_user()]);
237                                         }
238                                 }
239                         }
240                 }
241
242                 if ($post_id) {
243                         Worker::add(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 = [], $body = '', $subject = '', $replyto = '')
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(local_user(), $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::notice('send message: 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                         ], false
317                 );
318
319                 return 0;
320         }
321 }