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\Model\Item;
13 use Friendica\Database\DBA;
14 use Friendica\Network\Probe;
15 use Friendica\Util\DateTimeFormat;
18 * Class to handle private messages
23 * Insert received private message
26 * @return int|boolean Message ID or false on error
28 public static function insert($msg)
30 $user = User::getById($msg['uid']);
32 if (!isset($msg['reply'])) {
33 $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
36 if (empty($msg['convid'])) {
37 $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
38 if (DBA::isResult($mail)) {
39 $msg['convid'] = $mail['convid'];
43 if (empty($msg['guid'])) {
44 $host = parse_url($msg['from-url'], PHP_URL_HOST);
45 $msg['guid'] = Item::guidFromUri($msg['uri'], $host);
50 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
52 Logger::info('duplicate message already delivered.');
56 DBA::insert('mail', $msg);
58 $msg['id'] = DBA::lastInsertId();
62 if (!empty($msg['convid'])) {
63 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
66 // send notifications.
68 'type' => NOTIFY_MAIL,
69 'notify_flags' => $user['notify-flags'],
70 'language' => $user['language'],
71 'to_name' => $user['username'],
72 'to_email' => $user['email'],
73 'uid' => $user['uid'],
75 'parent' => $msg['parent-uri'],
76 'source_name' => $msg['from-name'],
77 'source_link' => $msg['from-url'],
78 'source_photo' => $msg['from-photo'],
79 'verb' => ACTIVITY_POST,
83 notification($notif_params);
85 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
91 * Send private message
93 * @param integer $recipient recipient id, default 0
94 * @param string $body message body, default empty
95 * @param string $subject message subject, default empty
96 * @param string $replyto reply to
98 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
100 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
108 if (!strlen($subject)) {
109 $subject = L10n::t('[no subject]');
112 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
113 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
115 if (!(count($me) && (count($contact)))) {
119 $guid = System::createUUID();
120 $uri = Item::newURI(local_user(), $guid);
125 // look for any existing conversation structure
127 if (strlen($replyto)) {
129 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
130 local_user(), $replyto, $replyto];
131 $mail = DBA::selectFirst('mail', ['convid'], $condition);
132 if (DBA::isResult($mail)) {
133 $convid = $mail['convid'];
139 // create a new conversation
140 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
141 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
143 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
144 $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
146 $conv_guid = System::createUUID();
147 $convuri = $recip_handle . ':' . $conv_guid;
149 $handles = $recip_handle . ';' . $sender_handle;
151 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
152 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
153 'subject' => $subject, 'recips' => $handles];
154 if (DBA::insert('conv', $fields)) {
155 $convid = DBA::lastInsertId();
160 Logger::log('send message: conversation not found.');
164 if (!strlen($replyto)) {
169 $success = DBA::insert(
172 'uid' => local_user(),
175 'from-name' => $me['name'],
176 'from-photo' => $me['thumb'],
177 'from-url' => $me['url'],
178 'contact-id' => $recipient,
185 'parent-uri' => $replyto,
186 'created' => DateTimeFormat::utcNow()
191 $post_id = DBA::lastInsertId();
196 * When a photo was uploaded into the message using the (profile wall) ajax
197 * uploader, The permissions are initially set to disallow anybody but the
198 * owner from seeing it. This is because the permissions may not yet have been
199 * set for the post. If it's private, the photo permissions should be set
200 * appropriately. But we didn't know the final permissions on the post until
201 * now. So now we'll look for links of uploaded messages that are in the
202 * post and set them to the same permissions as the post itself.
206 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
208 if (count($images)) {
209 foreach ($images as $image) {
210 if (!stristr($image, System::baseUrl() . '/photo/')) {
213 $image_uri = substr($image, strrpos($image, '/') + 1);
214 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
215 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
221 Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
222 return intval($post_id);
229 * @param array $recipient recipient, default empty
230 * @param string $body message body, default empty
231 * @param string $subject message subject, default empty
232 * @param string $replyto reply to, default empty
234 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
235 * @throws \ImagickException
237 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
243 if (!strlen($subject)) {
244 $subject = L10n::t('[no subject]');
247 $guid = System::createUUID();
248 $uri = Item::newURI(local_user(), $guid);
250 $me = Probe::uri($replyto);
256 $conv_guid = System::createUUID();
258 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
260 $sender_nick = basename($replyto);
261 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
262 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
263 $sender_handle = $sender_nick . '@' . $sender_host;
265 $handles = $recip_handle . ';' . $sender_handle;
268 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
269 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
270 'subject' => $subject, 'recips' => $handles];
271 if (DBA::insert('conv', $fields)) {
272 $convid = DBA::lastInsertId();
276 Logger::log('send message: conversation not found.');
283 'uid' => $recipient['uid'],
286 'from-name' => $me['name'],
287 'from-photo' => $me['photo'],
288 'from-url' => $me['url'],
296 'parent-uri' => $replyto,
297 'created' => DateTimeFormat::utcNow(),