2 // send a private message
7 function send_message($recipient=0, $body='', $subject='', $replyto=''){
10 if(! $recipient) return -1;
12 if(! strlen($subject))
13 $subject = t('[no subject]');
15 $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
18 $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
23 if(! (count($me) && (count($contact)))) {
27 $hash = random_string();
28 $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash ;
32 // look for any existing conversation structure
34 if(strlen($replyto)) {
35 $r = q("select convid from mail where uid = %d and uri = '%s' limit 1",
40 $convid = $r[0]['convid'];
43 if(! strlen($replyto))
49 // create a new conversation
51 $conv_guid = get_guid();
53 $recip_host = substr($contact[0]['url'],strpos($contact[0]['url'],'://')+3);
54 $recip_host = substr($recip_host,0,strpos($recip_host,'/'));
56 $recip_handle = (($contact[0]['addr']) ? $contact[0]['addr'] : $contact[0]['nick'] . '@' . $recip_host);
57 $sender_handle = $a->user['nickname'] . '@' . substr($a->get_baseurl(), strpos($a->get_baseurl(),'://') + 3);
59 $handles = $recip_handle . ';' . $sender_handle;
61 $r = q("insert into conv (uid,guid,recips) values (%d, '%s', '%s') ",
66 $r = q("select * from conv where guid = '%s' and uid = %d limit 1",
71 $convid = $r[0]['id'];
75 logger('send message: conversation not found.');
79 $r = q("INSERT INTO `mail` ( `uid`, `convid`, `from-name`, `from-photo`, `from-url`,
80 `contact-id`, `title`, `body`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
81 VALUES ( %d, %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s', '%s', '%s' )",
84 dbesc($me[0]['name']),
85 dbesc($me[0]['thumb']),
96 $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
101 $post_id = $r[0]['id'];
105 * When a photo was uploaded into the message using the (profile wall) ajax
106 * uploader, The permissions are initially set to disallow anybody but the
107 * owner from seeing it. This is because the permissions may not yet have been
108 * set for the post. If it's private, the photo permissions should be set
109 * appropriately. But we didn't know the final permissions on the post until
110 * now. So now we'll look for links of uploaded messages that are in the
111 * post and set them to the same permissions as the post itself.
117 if(preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) {
120 foreach($images as $image) {
121 if(! stristr($image,$a->get_baseurl() . '/photo/'))
123 $image_uri = substr($image,strrpos($image,'/') + 1);
124 $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
125 $r = q("UPDATE `photo` SET `allow_cid` = '%s'
126 WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
127 dbesc('<' . $recipient . '>'),
129 dbesc( t('Wall Photos')),
137 proc_run('php',"include/notifier.php","mail","$post_id");
138 return intval($post_id);