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);
72 $r = dba::selectFirst('conv', ['id'], ['guid' => $conv_guid, 'uid' => local_user()]);
73 if (DBM::is_result($r)) {
79 logger('send message: conversation not found.');
83 if (!strlen($replyto)) {
87 q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
88 `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`)
89 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s' )",
93 dbesc($me[0]['name']),
94 dbesc($me[0]['thumb']),
108 $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
112 if (DBM::is_result($r)) {
113 $post_id = $r[0]['id'];
118 * When a photo was uploaded into the message using the (profile wall) ajax
119 * uploader, The permissions are initially set to disallow anybody but the
120 * owner from seeing it. This is because the permissions may not yet have been
121 * set for the post. If it's private, the photo permissions should be set
122 * appropriately. But we didn't know the final permissions on the post until
123 * now. So now we'll look for links of uploaded messages that are in the
124 * post and set them to the same permissions as the post itself.
128 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
130 if (count($images)) {
131 foreach ($images as $image) {
132 if (!stristr($image, System::baseUrl() . '/photo/')) {
135 $image_uri = substr($image, strrpos($image, '/') + 1);
136 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
137 q("UPDATE `photo` SET `allow_cid` = '%s'
138 WHERE `resource-id` = '%s' AND `album` = '%s' AND `uid` = %d ",
139 dbesc('<' . $recipient . '>'),
141 dbesc( t('Wall Photos')),
149 Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
150 return intval($post_id);
156 function send_wallmessage($recipient = '', $body = '', $subject = '', $replyto = '')
162 if (!strlen($subject)) {
163 $subject = t('[no subject]');
166 $guid = get_guid(32);
167 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
169 $me = Probe::uri($replyto);
175 $conv_guid = get_guid(32);
177 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
179 $sender_nick = basename($replyto);
180 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
181 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
182 $sender_handle = $sender_nick . '@' . $sender_host;
184 $handles = $recip_handle . ';' . $sender_handle;
186 $fields = array('uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
187 'created' => datetime_convert(), 'updated' => datetime_convert(),
188 'subject' => $subject, 'recips' => $handles);
189 dba::insert('conv', $fields);
191 $r = dba::selectFirst('conv', ['id'], ['guid' => $conv_guid, 'uid' => $recipient['uid']]);
192 if (!DBM::is_result($r)) {
193 logger('send message: conversation not found.');
199 q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`,
200 `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`)
201 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )",
202 intval($recipient['uid']),