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)))) {
30 $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $guid;
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 $recip_host = substr($contact[0]['url'],strpos($contact[0]['url'],'://')+3);
53 $recip_host = substr($recip_host,0,strpos($recip_host,'/'));
55 $recip_handle = (($contact[0]['addr']) ? $contact[0]['addr'] : $contact[0]['nick'] . '@' . $recip_host);
56 $sender_handle = $a->user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
58 $conv_guid = get_guid(32);
59 $convuri = $recip_handle.':'.$conv_guid;
61 $handles = $recip_handle . ';' . $sender_handle;
63 $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
66 dbesc($sender_handle),
67 dbesc(datetime_convert()),
68 dbesc(datetime_convert()),
73 $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
78 $convid = $r[0]['id'];
82 logger('send message: conversation not found.');
86 if(! strlen($replyto)) {
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' )",
97 dbesc($me[0]['name']),
98 dbesc($me[0]['thumb']),
112 $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
117 $post_id = $r[0]['id'];
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.
133 if(preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
136 foreach($images as $image) {
137 if(! stristr($image,$a->get_baseurl() . '/photo/'))
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 . '>'),
145 dbesc( t('Wall Photos')),
153 proc_run(PRIORITY_HIGH, "include/notifier.php", "mail", $post_id);
154 return intval($post_id);
165 function send_wallmessage($recipient='', $body='', $subject='', $replyto=''){
170 if(! $recipient) return -1;
172 if(! strlen($subject))
173 $subject = t('[no subject]');
175 $guid = get_guid(32);
176 $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $guid;
181 require_once('include/Scrape.php');
183 $me = probe_url($replyto);
188 $conv_guid = get_guid(32);
190 $recip_handle = $recipient['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
192 $sender_nick = basename($replyto);
193 $sender_host = substr($replyto,strpos($replyto,'://')+3);
194 $sender_host = substr($sender_host,0,strpos($sender_host,'/'));
195 $sender_handle = $sender_nick . '@' . $sender_host;
197 $handles = $recip_handle . ';' . $sender_handle;
199 $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
200 intval($recipient['uid']),
202 dbesc($sender_handle),
203 dbesc(datetime_convert()),
204 dbesc(datetime_convert()),
209 $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
211 intval($recipient['uid'])
214 $convid = $r[0]['id'];
217 logger('send message: conversation not found.');
221 $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
222 `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`)
223 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )",
224 intval($recipient['uid']),