]> git.mxchange.org Git - friendica.git/blob - src/Model/Mail.php
8cf9fe32a7ab315e36aab99c4bdd548871449092
[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\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBA;
12 use Friendica\Network\Probe;
13 use Friendica\Util\DateTimeFormat;
14
15 require_once 'include/dba.php';
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::createGUID(32);
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                         $r = q("SELECT `convid` FROM `mail` WHERE `uid` = %d AND (`uri` = '%s' OR `parent-uri` = '%s') LIMIT 1",
60                                 intval(local_user()),
61                                 dbesc($replyto),
62                                 dbesc($replyto)
63                         );
64                         if (DBA::isResult($r)) {
65                                 $convid = $r[0]['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::createGUID(32);
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('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                                         DBA::update('photo', ['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          */
166         public static function sendWall($recipient = '', $body = '', $subject = '', $replyto = '')
167         {
168                 if (!$recipient) {
169                         return -1;
170                 }
171
172                 if (!strlen($subject)) {
173                         $subject = L10n::t('[no subject]');
174                 }
175
176                 $guid = System::createGUID(32);
177                 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
178
179                 $me = Probe::uri($replyto);
180
181                 if (!$me['name']) {
182                         return -2;
183                 }
184
185                 $conv_guid = System::createGUID(32);
186
187                 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
188
189                 $sender_nick = basename($replyto);
190                 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
191                 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
192                 $sender_handle = $sender_nick . '@' . $sender_host;
193
194                 $handles = $recip_handle . ';' . $sender_handle;
195
196                 $convid = null;
197                 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
198                         'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
199                         'subject' => $subject, 'recips' => $handles];
200                 if (DBA::insert('conv', $fields)) {
201                         $convid = DBA::lastInsertId();
202                 }
203
204                 if (!$convid) {
205                         logger('send message: conversation not found.');
206                         return -4;
207                 }
208
209                 DBA::insert(
210                         'mail',
211                         [
212                                 'uid' => $recipient['uid'],
213                                 'guid' => $guid,
214                                 'convid' => $convid,
215                                 'from-name' => $me['name'],
216                                 'from-photo' => $me['photo'],
217                                 'from-url' => $me['url'],
218                                 'contact-id' => 0,
219                                 'title' => $subject,
220                                 'body' => $body,
221                                 'seen' => 0,
222                                 'reply' => 0,
223                                 'replied' => 0,
224                                 'uri' => $uri,
225                                 'parent-uri' => $replyto,
226                                 'created' => DateTimeFormat::utcNow(),
227                                 'unknown' => 1
228                         ]
229                 );
230
231                 return 0;
232         }
233 }