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