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