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