4 * @file src/Model/Mail.php
6 namespace Friendica\Model;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Logger;
10 use Friendica\Core\System;
11 use Friendica\Core\Worker;
12 use Friendica\Database\DBA;
13 use Friendica\Network\Probe;
14 use Friendica\Util\DateTimeFormat;
17 * Class to handle private messages
22 * Send private message
24 * @param integer $recipient recipient id, default 0
25 * @param string $body message body, default empty
26 * @param string $subject message subject, default empty
27 * @param string $replyto reply to
29 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
37 if (!strlen($subject)) {
38 $subject = L10n::t('[no subject]');
41 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
42 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
44 if (!(count($me) && (count($contact)))) {
48 $guid = System::createUUID();
49 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
54 // look for any existing conversation structure
56 if (strlen($replyto)) {
58 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
59 local_user(), $replyto, $replyto];
60 $mail = DBA::selectFirst('mail', ['convid'], $condition);
61 if (DBA::isResult($mail)) {
62 $convid = $mail['convid'];
68 // create a new conversation
69 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
70 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
72 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
73 $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
75 $conv_guid = System::createUUID();
76 $convuri = $recip_handle . ':' . $conv_guid;
78 $handles = $recip_handle . ';' . $sender_handle;
80 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
81 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
82 'subject' => $subject, 'recips' => $handles];
83 if (DBA::insert('conv', $fields)) {
84 $convid = DBA::lastInsertId();
89 Logger::log('send message: conversation not found.');
93 if (!strlen($replyto)) {
98 $success = DBA::insert(
101 'uid' => local_user(),
104 'from-name' => $me['name'],
105 'from-photo' => $me['thumb'],
106 'from-url' => $me['url'],
107 'contact-id' => $recipient,
114 'parent-uri' => $replyto,
115 'created' => DateTimeFormat::utcNow()
120 $post_id = DBA::lastInsertId();
125 * When a photo was uploaded into the message using the (profile wall) ajax
126 * uploader, The permissions are initially set to disallow anybody but the
127 * owner from seeing it. This is because the permissions may not yet have been
128 * set for the post. If it's private, the photo permissions should be set
129 * appropriately. But we didn't know the final permissions on the post until
130 * now. So now we'll look for links of uploaded messages that are in the
131 * post and set them to the same permissions as the post itself.
135 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
137 if (count($images)) {
138 foreach ($images as $image) {
139 if (!stristr($image, System::baseUrl() . '/photo/')) {
142 $image_uri = substr($image, strrpos($image, '/') + 1);
143 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
144 DBA::update('photo', ['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
150 Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
151 return intval($post_id);
158 * @param string $recipient recipient, default empty
159 * @param string $body message body, default empty
160 * @param string $subject message subject, default empty
161 * @param string $replyto reply to, default empty
163 public static function sendWall($recipient = '', $body = '', $subject = '', $replyto = '')
169 if (!strlen($subject)) {
170 $subject = L10n::t('[no subject]');
173 $guid = System::createUUID();
174 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
176 $me = Probe::uri($replyto);
182 $conv_guid = System::createUUID();
184 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
186 $sender_nick = basename($replyto);
187 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
188 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
189 $sender_handle = $sender_nick . '@' . $sender_host;
191 $handles = $recip_handle . ';' . $sender_handle;
194 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
195 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
196 'subject' => $subject, 'recips' => $handles];
197 if (DBA::insert('conv', $fields)) {
198 $convid = DBA::lastInsertId();
202 Logger::log('send message: conversation not found.');
209 'uid' => $recipient['uid'],
212 'from-name' => $me['name'],
213 'from-photo' => $me['photo'],
214 'from-url' => $me['url'],
222 'parent-uri' => $replyto,
223 'created' => DateTimeFormat::utcNow(),