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