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