]> git.mxchange.org Git - friendica.git/blob - src/Model/Mail.php
be68f515f1c20c92dc75e844774c6a86f7063626
[friendica.git] / src / Model / Mail.php
1 <?php
2 /**
3  * @file src/Model/Mail.php
4  */
5 namespace Friendica\Model;
6
7 use Friendica\App;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
12 use dba;
13
14 require_once 'include/dba.php';
15 require_once 'include/datetime.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 = get_guid(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 (DBM::is_result($r)) {
65                                 $convid = $r[0]['convid'];
66                         }
67                 }
68
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 = get_guid(32);
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' => datetime_convert(), 'updated' => datetime_convert(),
84                                 'subject' => $subject, 'recips' => $handles];
85                         if (dba::insert('conv', $fields)) {
86                                 $convid = dba::lastInsertId();
87                         }
88                 }
89
90                 if (!$convid) {
91                         logger('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' => datetime_convert()
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 = get_guid(32);
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 = get_guid(32);
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' => datetime_convert(), 'updated' => datetime_convert(),
198                         'subject' => $subject, 'recips' => $handles];
199                 if (dba::insert('conv', $fields)) {
200                         $convid = dba::lastInsertId();
201                 }
202                 
203                 if (!$convid) {
204                         logger('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' => datetime_convert(),
226                                 'unknown' => 1
227                         ]
228                 );
229
230                 return 0;
231         }
232 }