4 * @file src/Model/Mail.php
6 namespace Friendica\Model;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBA;
12 use Friendica\Network\Probe;
13 use Friendica\Util\DateTimeFormat;
15 require_once 'include/dba.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)))) {
49 $guid = System::createGUID(32);
50 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
55 // look for any existing conversation structure
57 if (strlen($replyto)) {
59 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
60 local_user(), $replyto, $replyto];
61 $mail = DBA::selectFirst('mail', ['convid'], $condition);
62 if (DBA::isResult($mail)) {
63 $convid = $mail['convid'];
69 // create a new conversation
70 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
71 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
73 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
74 $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
76 $conv_guid = System::createGUID(32);
77 $convuri = $recip_handle . ':' . $conv_guid;
79 $handles = $recip_handle . ';' . $sender_handle;
81 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
82 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
83 'subject' => $subject, 'recips' => $handles];
84 if (DBA::insert('conv', $fields)) {
85 $convid = DBA::lastInsertId();
90 logger('send message: conversation not found.');
94 if (!strlen($replyto)) {
99 $success = DBA::insert(
102 'uid' => local_user(),
105 'from-name' => $me['name'],
106 'from-photo' => $me['thumb'],
107 'from-url' => $me['url'],
108 'contact-id' => $recipient,
115 'parent-uri' => $replyto,
116 'created' => DateTimeFormat::utcNow()
121 $post_id = DBA::lastInsertId();
126 * When a photo was uploaded into the message using the (profile wall) ajax
127 * uploader, The permissions are initially set to disallow anybody but the
128 * owner from seeing it. This is because the permissions may not yet have been
129 * set for the post. If it's private, the photo permissions should be set
130 * appropriately. But we didn't know the final permissions on the post until
131 * now. So now we'll look for links of uploaded messages that are in the
132 * post and set them to the same permissions as the post itself.
136 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
138 if (count($images)) {
139 foreach ($images as $image) {
140 if (!stristr($image, System::baseUrl() . '/photo/')) {
143 $image_uri = substr($image, strrpos($image, '/') + 1);
144 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
145 DBA::update('photo', ['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
151 Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
152 return intval($post_id);
159 * @param string $recipient recipient, default empty
160 * @param string $body message body, default empty
161 * @param string $subject message subject, default empty
162 * @param string $replyto reply to, default empty
164 public static function sendWall($recipient = '', $body = '', $subject = '', $replyto = '')
170 if (!strlen($subject)) {
171 $subject = L10n::t('[no subject]');
174 $guid = System::createGUID(32);
175 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
177 $me = Probe::uri($replyto);
183 $conv_guid = System::createGUID(32);
185 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
187 $sender_nick = basename($replyto);
188 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
189 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
190 $sender_handle = $sender_nick . '@' . $sender_host;
192 $handles = $recip_handle . ';' . $sender_handle;
195 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
196 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
197 'subject' => $subject, 'recips' => $handles];
198 if (DBA::insert('conv', $fields)) {
199 $convid = DBA::lastInsertId();
203 logger('send message: conversation not found.');
210 'uid' => $recipient['uid'],
213 'from-name' => $me['name'],
214 'from-photo' => $me['photo'],
215 'from-url' => $me['url'],
223 'parent-uri' => $replyto,
224 'created' => DateTimeFormat::utcNow(),