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;
16 require_once 'include/dba.php';
19 * Class to handle private messages
24 * Send private message
26 * @param integer $recipient recipient id, default 0
27 * @param string $body message body, default empty
28 * @param string $subject message subject, default empty
29 * @param string $replyto reply to
31 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
39 if (!strlen($subject)) {
40 $subject = L10n::t('[no subject]');
43 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
44 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
46 if (!(count($me) && (count($contact)))) {
50 $guid = System::createUUID();
51 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
56 // look for any existing conversation structure
58 if (strlen($replyto)) {
60 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
61 local_user(), $replyto, $replyto];
62 $mail = DBA::selectFirst('mail', ['convid'], $condition);
63 if (DBA::isResult($mail)) {
64 $convid = $mail['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 = System::createUUID();
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' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
84 'subject' => $subject, 'recips' => $handles];
85 if (DBA::insert('conv', $fields)) {
86 $convid = DBA::lastInsertId();
91 Logger::log('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' => DateTimeFormat::utcNow()
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 = System::createUUID();
176 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
178 $me = Probe::uri($replyto);
184 $conv_guid = System::createUUID();
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' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
198 'subject' => $subject, 'recips' => $handles];
199 if (DBA::insert('conv', $fields)) {
200 $convid = DBA::lastInsertId();
204 Logger::log('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' => DateTimeFormat::utcNow(),