3 // send a private message
8 function send_message($recipient=0, $body='', $subject='', $replyto=''){
12 if(! $recipient) return -1;
14 if(! strlen($subject))
15 $subject = t('[no subject]');
17 $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
20 $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
25 if(! (count($me) && (count($contact)))) {
29 $hash = random_string();
30 $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash ;
35 // look for any existing conversation structure
37 if(strlen($replyto)) {
39 $r = q("select convid from mail where uid = %d and ( uri = '%s' or `parent-uri` = '%s' ) limit 1",
45 $convid = $r[0]['convid'];
50 // create a new conversation
52 $conv_guid = get_guid();
54 $recip_host = substr($contact[0]['url'],strpos($contact[0]['url'],'://')+3);
55 $recip_host = substr($recip_host,0,strpos($recip_host,'/'));
57 $recip_handle = (($contact[0]['addr']) ? $contact[0]['addr'] : $contact[0]['nick'] . '@' . $recip_host);
58 $sender_handle = $a->user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
60 $handles = $recip_handle . ';' . $sender_handle;
62 $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
65 dbesc($sender_handle),
66 dbesc(datetime_convert()),
67 dbesc(datetime_convert()),
72 $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
77 $convid = $r[0]['id'];
81 logger('send message: conversation not found.');
85 if(! strlen($replyto)) {
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' )",
96 dbesc($me[0]['name']),
97 dbesc($me[0]['thumb']),
111 $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
116 $post_id = $r[0]['id'];
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.
132 if(preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
135 foreach($images as $image) {
136 if(! stristr($image,$a->get_baseurl() . '/photo/'))
138 $image_uri = substr($image,strrpos($image,'/') + 1);
139 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
140 $r = q("UPDATE `photo` SET `allow_cid` = '%s'
141 WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
142 dbesc('<' . $recipient . '>'),
144 dbesc( t('Wall Photos')),
152 proc_run('php',"include/notifier.php","mail","$post_id");
153 return intval($post_id);
164 function send_wallmessage($recipient='', $body='', $subject='', $replyto=''){
168 if(! $recipient) return -1;
170 if(! strlen($subject))
171 $subject = t('[no subject]');
173 $hash = random_string();
174 $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash ;
179 require_once('include/Scrape.php');
181 $me = probe_url($replyto);
186 $conv_guid = get_guid();
188 $recip_handle = $recipient['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
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;
195 $handles = $recip_handle . ';' . $sender_handle;
197 $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
198 intval(local_user()),
200 dbesc($sender_handle),
201 dbesc(datetime_convert()),
202 dbesc(datetime_convert()),
207 $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
209 intval($recipient['uid'])
212 $convid = $r[0]['id'];
215 logger('send message: conversation not found.');
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']),