3 * @file src/Model/Mail.php
5 namespace Friendica\Model;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
14 require_once 'include/dba.php';
15 require_once 'include/datetime.php';
18 * Class to handle private messages
23 * Send private message
25 * @param integer $recipient recipient id, default 0
26 * @param string $body message body, default empty
27 * @param string $subject message subject, default empty
28 * @param string $replyto reply to
30 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
38 if (!strlen($subject)) {
39 $subject = L10n::t('[no subject]');
42 $me = dba::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
43 $contact = dba::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
45 if (!(count($me) && (count($contact)))) {
50 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
55 // look for any existing conversation structure
57 if (strlen($replyto)) {
59 $r = q("SELECT `convid` FROM `mail` WHERE `uid` = %d AND (`uri` = '%s' OR `parent-uri` = '%s') LIMIT 1",
64 if (DBM::is_result($r)) {
65 $convid = $r[0]['convid'];
70 // create a new conversation
71 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
72 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
74 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
75 $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
77 $conv_guid = get_guid(32);
78 $convuri = $recip_handle . ':' . $conv_guid;
80 $handles = $recip_handle . ';' . $sender_handle;
82 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
83 'created' => datetime_convert(), 'updated' => datetime_convert(),
84 'subject' => $subject, 'recips' => $handles];
85 if (dba::insert('conv', $fields)) {
86 $convid = dba::lastInsertId();
91 logger('send message: conversation not found.');
95 if (!strlen($replyto)) {
100 $success = dba::insert(
103 'uid' => local_user(),
106 'from-name' => $me['name'],
107 'from-photo' => $me['thumb'],
108 'from-url' => $me['url'],
109 'contact-id' => $recipient,
116 'parent-uri' => $replyto,
117 'created' => datetime_convert()
122 $post_id = dba::lastInsertId();
127 * When a photo was uploaded into the message using the (profile wall) ajax
128 * uploader, The permissions are initially set to disallow anybody but the
129 * owner from seeing it. This is because the permissions may not yet have been
130 * set for the post. If it's private, the photo permissions should be set
131 * appropriately. But we didn't know the final permissions on the post until
132 * now. So now we'll look for links of uploaded messages that are in the
133 * post and set them to the same permissions as the post itself.
137 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
139 if (count($images)) {
140 foreach ($images as $image) {
141 if (!stristr($image, System::baseUrl() . '/photo/')) {
144 $image_uri = substr($image, strrpos($image, '/') + 1);
145 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
146 dba::update('photo', ['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
152 Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
153 return intval($post_id);
160 * @param string $recipient recipient, default empty
161 * @param string $body message body, default empty
162 * @param string $subject message subject, default empty
163 * @param string $replyto reply to, default empty
165 public static function sendWall($recipient = '', $body = '', $subject = '', $replyto = '')
171 if (!strlen($subject)) {
172 $subject = L10n::t('[no subject]');
175 $guid = get_guid(32);
176 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
178 $me = Probe::uri($replyto);
184 $conv_guid = get_guid(32);
186 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
188 $sender_nick = basename($replyto);
189 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
190 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
191 $sender_handle = $sender_nick . '@' . $sender_host;
193 $handles = $recip_handle . ';' . $sender_handle;
196 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
197 'created' => datetime_convert(), 'updated' => datetime_convert(),
198 'subject' => $subject, 'recips' => $handles];
199 if (dba::insert('conv', $fields)) {
200 $convid = dba::lastInsertId();
204 logger('send message: conversation not found.');
211 'uid' => $recipient['uid'],
214 'from-name' => $me['name'],
215 'from-photo' => $me['photo'],
216 'from-url' => $me['url'],
224 'parent-uri' => $replyto,
225 'created' => datetime_convert(),