]> git.mxchange.org Git - friendica.git/blob - src/Model/Mail.php
b5841d17855e640e2855d5117b16a8db565de0c3
[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\Database\DBA;
13 use Friendica\Model\Photo;
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          * Send private message
24          *
25          * @param integer $recipient recipient id, default 0
26          * @param string  $body      message body, default empty
27          * @param string  $subject   message subject, default empty
28          * @param string  $replyto   reply to
29          * @return int
30          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
31          */
32         public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
33         {
34                 $a = \get_app();
35
36                 if (!$recipient) {
37                         return -1;
38                 }
39
40                 if (!strlen($subject)) {
41                         $subject = L10n::t('[no subject]');
42                 }
43
44                 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
45                 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
46
47                 if (!(count($me) && (count($contact)))) {
48                         return -2;
49                 }
50
51                 $guid = System::createUUID();
52                 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
53
54                 $convid = 0;
55                 $reply = false;
56
57                 // look for any existing conversation structure
58
59                 if (strlen($replyto)) {
60                         $reply = true;
61                         $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
62                                 local_user(), $replyto, $replyto];
63                         $mail = DBA::selectFirst('mail', ['convid'], $condition);
64                         if (DBA::isResult($mail)) {
65                                 $convid = $mail['convid'];
66                         }
67                 }
68
69                 $convuri = '';
70                 if (!$convid) {
71                         // create a new conversation
72                         $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
73                         $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
74
75                         $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
76                         $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
77
78                         $conv_guid = System::createUUID();
79                         $convuri = $recip_handle . ':' . $conv_guid;
80
81                         $handles = $recip_handle . ';' . $sender_handle;
82
83                         $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
84                                 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
85                                 'subject' => $subject, 'recips' => $handles];
86                         if (DBA::insert('conv', $fields)) {
87                                 $convid = DBA::lastInsertId();
88                         }
89                 }
90
91                 if (!$convid) {
92                         Logger::log('send message: conversation not found.');
93                         return -4;
94                 }
95
96                 if (!strlen($replyto)) {
97                         $replyto = $convuri;
98                 }
99
100                 $post_id = null;
101                 $success = DBA::insert(
102                         'mail',
103                         [
104                                 'uid' => local_user(),
105                                 'guid' => $guid,
106                                 'convid' => $convid,
107                                 'from-name' => $me['name'],
108                                 'from-photo' => $me['thumb'],
109                                 'from-url' => $me['url'],
110                                 'contact-id' => $recipient,
111                                 'title' => $subject,
112                                 'body' => $body,
113                                 'seen' => 1,
114                                 'reply' => $reply,
115                                 'replied' => 0,
116                                 'uri' => $uri,
117                                 'parent-uri' => $replyto,
118                                 'created' => DateTimeFormat::utcNow()
119                         ]
120                 );
121
122                 if ($success) {
123                         $post_id = DBA::lastInsertId();
124                 }
125
126                 /**
127                  *
128                  * When a photo was uploaded into the message using the (profile wall) ajax
129                  * uploader, The permissions are initially set to disallow anybody but the
130                  * owner from seeing it. This is because the permissions may not yet have been
131                  * set for the post. If it's private, the photo permissions should be set
132                  * appropriately. But we didn't know the final permissions on the post until
133                  * now. So now we'll look for links of uploaded messages that are in the
134                  * post and set them to the same permissions as the post itself.
135                  *
136                  */
137                 $match = null;
138                 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
139                         $images = $match[1];
140                         if (count($images)) {
141                                 foreach ($images as $image) {
142                                         if (!stristr($image, System::baseUrl() . '/photo/')) {
143                                                 continue;
144                                         }
145                                         $image_uri = substr($image, strrpos($image, '/') + 1);
146                                         $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
147                                         Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
148                                 }
149                         }
150                 }
151
152                 if ($post_id) {
153                         Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
154                         return intval($post_id);
155                 } else {
156                         return -3;
157                 }
158         }
159
160         /**
161          * @param string $recipient recipient, default empty
162          * @param string $body      message body, default empty
163          * @param string $subject   message subject, default empty
164          * @param string $replyto   reply to, default empty
165          * @return int
166          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
167          * @throws \ImagickException
168          */
169         public static function sendWall($recipient = '', $body = '', $subject = '', $replyto = '')
170         {
171                 if (!$recipient) {
172                         return -1;
173                 }
174
175                 if (!strlen($subject)) {
176                         $subject = L10n::t('[no subject]');
177                 }
178
179                 $guid = System::createUUID();
180                 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
181
182                 $me = Probe::uri($replyto);
183
184                 if (!$me['name']) {
185                         return -2;
186                 }
187
188                 $conv_guid = System::createUUID();
189
190                 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
191
192                 $sender_nick = basename($replyto);
193                 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
194                 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
195                 $sender_handle = $sender_nick . '@' . $sender_host;
196
197                 $handles = $recip_handle . ';' . $sender_handle;
198
199                 $convid = null;
200                 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
201                         'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
202                         'subject' => $subject, 'recips' => $handles];
203                 if (DBA::insert('conv', $fields)) {
204                         $convid = DBA::lastInsertId();
205                 }
206
207                 if (!$convid) {
208                         Logger::log('send message: conversation not found.');
209                         return -4;
210                 }
211
212                 DBA::insert(
213                         'mail',
214                         [
215                                 'uid' => $recipient['uid'],
216                                 'guid' => $guid,
217                                 'convid' => $convid,
218                                 'from-name' => $me['name'],
219                                 'from-photo' => $me['photo'],
220                                 'from-url' => $me['url'],
221                                 'contact-id' => 0,
222                                 'title' => $subject,
223                                 'body' => $body,
224                                 'seen' => 0,
225                                 'reply' => 0,
226                                 'replied' => 0,
227                                 'uri' => $uri,
228                                 'parent-uri' => $replyto,
229                                 'created' => DateTimeFormat::utcNow(),
230                                 'unknown' => 1
231                         ]
232                 );
233
234                 return 0;
235         }
236 }