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;
16 use Friendica\Worker\Delivery;
19 * Class to handle private messages
24 * Insert received private message
27 * @return int|boolean Message ID or false on error
29 public static function insert($msg)
31 $user = User::getById($msg['uid']);
33 if (!isset($msg['reply'])) {
34 $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
37 if (empty($msg['convid'])) {
38 $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
39 if (DBA::isResult($mail)) {
40 $msg['convid'] = $mail['convid'];
44 if (empty($msg['guid'])) {
45 $host = parse_url($msg['from-url'], PHP_URL_HOST);
46 $msg['guid'] = Item::guidFromUri($msg['uri'], $host);
51 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
53 Logger::info('duplicate message already delivered.');
57 DBA::insert('mail', $msg);
59 $msg['id'] = DBA::lastInsertId();
63 if (!empty($msg['convid'])) {
64 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
67 // send notifications.
69 'type' => NOTIFY_MAIL,
70 'notify_flags' => $user['notify-flags'],
71 'language' => $user['language'],
72 'to_name' => $user['username'],
73 'to_email' => $user['email'],
74 'uid' => $user['uid'],
77 'source_name' => $msg['from-name'],
78 'source_link' => $msg['from-url'],
79 'source_photo' => $msg['from-photo'],
80 'verb' => ACTIVITY_POST,
84 notification($notif_params);
86 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
92 * Send private message
94 * @param integer $recipient recipient id, default 0
95 * @param string $body message body, default empty
96 * @param string $subject message subject, default empty
97 * @param string $replyto reply to
99 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
101 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
109 if (!strlen($subject)) {
110 $subject = L10n::t('[no subject]');
113 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
114 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
116 if (!(count($me) && (count($contact)))) {
120 $guid = System::createUUID();
121 $uri = Item::newURI(local_user(), $guid);
126 // look for any existing conversation structure
128 if (strlen($replyto)) {
130 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
131 local_user(), $replyto, $replyto];
132 $mail = DBA::selectFirst('mail', ['convid'], $condition);
133 if (DBA::isResult($mail)) {
134 $convid = $mail['convid'];
140 // create a new conversation
141 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
142 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
144 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
145 $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
147 $conv_guid = System::createUUID();
148 $convuri = $recip_handle . ':' . $conv_guid;
150 $handles = $recip_handle . ';' . $sender_handle;
152 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
153 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
154 'subject' => $subject, 'recips' => $handles];
155 if (DBA::insert('conv', $fields)) {
156 $convid = DBA::lastInsertId();
161 Logger::log('send message: conversation not found.');
165 if (!strlen($replyto)) {
170 $success = DBA::insert(
173 'uid' => local_user(),
176 'from-name' => $me['name'],
177 'from-photo' => $me['thumb'],
178 'from-url' => $me['url'],
179 'contact-id' => $recipient,
186 'parent-uri' => $replyto,
187 'created' => DateTimeFormat::utcNow()
192 $post_id = DBA::lastInsertId();
197 * When a photo was uploaded into the message using the (profile wall) ajax
198 * uploader, The permissions are initially set to disallow anybody but the
199 * owner from seeing it. This is because the permissions may not yet have been
200 * set for the post. If it's private, the photo permissions should be set
201 * appropriately. But we didn't know the final permissions on the post until
202 * now. So now we'll look for links of uploaded messages that are in the
203 * post and set them to the same permissions as the post itself.
207 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
209 if (count($images)) {
210 foreach ($images as $image) {
211 if (!stristr($image, System::baseUrl() . '/photo/')) {
214 $image_uri = substr($image, strrpos($image, '/') + 1);
215 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
216 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
222 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
223 return intval($post_id);
230 * @param array $recipient recipient, default empty
231 * @param string $body message body, default empty
232 * @param string $subject message subject, default empty
233 * @param string $replyto reply to, default empty
235 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
236 * @throws \ImagickException
238 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
244 if (!strlen($subject)) {
245 $subject = L10n::t('[no subject]');
248 $guid = System::createUUID();
249 $uri = Item::newURI(local_user(), $guid);
251 $me = Probe::uri($replyto);
257 $conv_guid = System::createUUID();
259 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
261 $sender_nick = basename($replyto);
262 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
263 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
264 $sender_handle = $sender_nick . '@' . $sender_host;
266 $handles = $recip_handle . ';' . $sender_handle;
269 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
270 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
271 'subject' => $subject, 'recips' => $handles];
272 if (DBA::insert('conv', $fields)) {
273 $convid = DBA::lastInsertId();
277 Logger::log('send message: conversation not found.');
284 'uid' => $recipient['uid'],
287 'from-name' => $me['name'],
288 'from-photo' => $me['photo'],
289 'from-url' => $me['url'],
297 'parent-uri' => $replyto,
298 'created' => DateTimeFormat::utcNow(),