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