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