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