]> git.mxchange.org Git - friendica.git/blob - src/Model/Mail.php
b2e9e1a48734770b4b2ec5a0b9fcf05287eb0eda
[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\DBM;
12 use Friendica\Network\Probe;
13 use Friendica\Util\DateTimeFormat;
14 use dba;
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 = get_guid(32);
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                         $r = q("SELECT `convid` FROM `mail` WHERE `uid` = %d AND (`uri` = '%s' OR `parent-uri` = '%s') LIMIT 1",
61                                 intval(local_user()),
62                                 dbesc($replyto),
63                                 dbesc($replyto)
64                         );
65                         if (DBM::is_result($r)) {
66                                 $convid = $r[0]['convid'];
67                         }
68                 }
69
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 = get_guid(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 = get_guid(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 = get_guid(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 }