3 // send a private message
6 use Friendica\Core\System;
7 use Friendica\Core\Worker;
8 use Friendica\Database\DBM;
10 function send_message($recipient = 0, $body = '', $subject = '', $replyto = '')
18 if (!strlen($subject)) {
19 $subject = t('[no subject]');
22 $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
25 $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
30 if (!(count($me) && (count($contact)))) {
35 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
40 // look for any existing conversation structure
42 if (strlen($replyto)) {
44 $r = q("SELECT `convid` FROM `mail` WHERE `uid` = %d AND (`uri` = '%s' OR `parent-uri` = '%s') LIMIT 1",
49 if (DBM::is_result($r)) {
50 $convid = $r[0]['convid'];
55 // create a new conversation
56 $recip_host = substr($contact[0]['url'], strpos($contact[0]['url'], '://') + 3);
57 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
59 $recip_handle = (($contact[0]['addr']) ? $contact[0]['addr'] : $contact[0]['nick'] . '@' . $recip_host);
60 $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
62 $conv_guid = get_guid(32);
63 $convuri = $recip_handle . ':' . $conv_guid;
65 $handles = $recip_handle . ';' . $sender_handle;
67 $fields = array('uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
68 'created' => datetime_convert(), 'updated' => datetime_convert(),
69 'subject' => $subject, 'recips' => $handles);
70 dba::insert('conv', $fields);
71 $convid = dba::lastInsertId();
75 logger('send message: conversation not found.');
79 if (!strlen($replyto)) {
83 q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
84 `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`)
85 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
89 dbesc($me[0]['name']),
90 dbesc($me[0]['thumb']),
102 $post_id = dba::lastInsertId();
106 * When a photo was uploaded into the message using the (profile wall) ajax
107 * uploader, The permissions are initially set to disallow anybody but the
108 * owner from seeing it. This is because the permissions may not yet have been
109 * set for the post. If it's private, the photo permissions should be set
110 * appropriately. But we didn't know the final permissions on the post until
111 * now. So now we'll look for links of uploaded messages that are in the
112 * post and set them to the same permissions as the post itself.
116 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
118 if (count($images)) {
119 foreach ($images as $image) {
120 if (!stristr($image, System::baseUrl() . '/photo/')) {
123 $image_uri = substr($image, strrpos($image, '/') + 1);
124 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
125 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 Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
138 return intval($post_id);
144 function send_wallmessage($recipient = '', $body = '', $subject = '', $replyto = '')
150 if (!strlen($subject)) {
151 $subject = t('[no subject]');
154 $guid = get_guid(32);
155 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
157 $me = Probe::uri($replyto);
163 $conv_guid = get_guid(32);
165 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
167 $sender_nick = basename($replyto);
168 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
169 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
170 $sender_handle = $sender_nick . '@' . $sender_host;
172 $handles = $recip_handle . ';' . $sender_handle;
174 $fields = array('uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
175 'created' => datetime_convert(), 'updated' => datetime_convert(),
176 'subject' => $subject, 'recips' => $handles);
177 dba::insert('conv', $fields);
178 $convid = dba::lastInsertId();
181 logger('send message: conversation not found.');
185 q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
186 `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`)
187 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )",
188 intval($recipient['uid']),