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\Model\Photo;
14 use Friendica\Database\DBA;
15 use Friendica\Network\Probe;
16 use Friendica\Protocol\Activity;
17 use Friendica\Util\DateTimeFormat;
18 use Friendica\Worker\Delivery;
21 * Class to handle private messages
26 * Insert received private message
29 * @return int|boolean Message ID or false on error
31 public static function insert($msg)
33 $user = User::getById($msg['uid']);
35 if (!isset($msg['reply'])) {
36 $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
39 if (empty($msg['convid'])) {
40 $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
41 if (DBA::isResult($mail)) {
42 $msg['convid'] = $mail['convid'];
46 if (empty($msg['guid'])) {
47 $host = parse_url($msg['from-url'], PHP_URL_HOST);
48 $msg['guid'] = Item::guidFromUri($msg['uri'], $host);
51 $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());
55 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
57 Logger::info('duplicate message already delivered.');
61 DBA::insert('mail', $msg);
63 $msg['id'] = DBA::lastInsertId();
67 if (!empty($msg['convid'])) {
68 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
71 // send notifications.
73 'type' => NOTIFY_MAIL,
74 'notify_flags' => $user['notify-flags'],
75 'language' => $user['language'],
76 'to_name' => $user['username'],
77 'to_email' => $user['email'],
78 'uid' => $user['uid'],
81 'source_name' => $msg['from-name'],
82 'source_link' => $msg['from-url'],
83 'source_photo' => $msg['from-photo'],
84 'verb' => Activity::POST,
88 notification($notif_params);
90 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
96 * Send private message
98 * @param integer $recipient recipient id, default 0
99 * @param string $body message body, default empty
100 * @param string $subject message subject, default empty
101 * @param string $replyto reply to
103 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
105 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
113 if (!strlen($subject)) {
114 $subject = L10n::t('[no subject]');
117 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
118 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
120 if (!(count($me) && (count($contact)))) {
124 Photo::setPermissionFromBody($body, local_user(), $me['id'], '<' . $contact['id'] . '>', '', '', '');
126 $guid = System::createUUID();
127 $uri = Item::newURI(local_user(), $guid);
132 // look for any existing conversation structure
134 if (strlen($replyto)) {
136 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
137 local_user(), $replyto, $replyto];
138 $mail = DBA::selectFirst('mail', ['convid'], $condition);
139 if (DBA::isResult($mail)) {
140 $convid = $mail['convid'];
146 // create a new conversation
147 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
148 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
150 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
151 $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
153 $conv_guid = System::createUUID();
154 $convuri = $recip_handle . ':' . $conv_guid;
156 $handles = $recip_handle . ';' . $sender_handle;
158 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
159 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
160 'subject' => $subject, 'recips' => $handles];
161 if (DBA::insert('conv', $fields)) {
162 $convid = DBA::lastInsertId();
167 Logger::log('send message: conversation not found.');
171 if (!strlen($replyto)) {
176 $success = DBA::insert(
179 'uid' => local_user(),
182 'from-name' => $me['name'],
183 'from-photo' => $me['thumb'],
184 'from-url' => $me['url'],
185 'contact-id' => $recipient,
192 'parent-uri' => $replyto,
193 'created' => DateTimeFormat::utcNow()
198 $post_id = DBA::lastInsertId();
203 * When a photo was uploaded into the message using the (profile wall) ajax
204 * uploader, The permissions are initially set to disallow anybody but the
205 * owner from seeing it. This is because the permissions may not yet have been
206 * set for the post. If it's private, the photo permissions should be set
207 * appropriately. But we didn't know the final permissions on the post until
208 * now. So now we'll look for links of uploaded messages that are in the
209 * post and set them to the same permissions as the post itself.
213 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
215 if (count($images)) {
216 foreach ($images as $image) {
217 if (!stristr($image, System::baseUrl() . '/photo/')) {
220 $image_uri = substr($image, strrpos($image, '/') + 1);
221 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
222 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
228 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
229 return intval($post_id);
236 * @param array $recipient recipient, default empty
237 * @param string $body message body, default empty
238 * @param string $subject message subject, default empty
239 * @param string $replyto reply to, default empty
241 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
242 * @throws \ImagickException
244 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
250 if (!strlen($subject)) {
251 $subject = L10n::t('[no subject]');
254 $guid = System::createUUID();
255 $uri = Item::newURI(local_user(), $guid);
257 $me = Probe::uri($replyto);
263 $conv_guid = System::createUUID();
265 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
267 $sender_nick = basename($replyto);
268 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
269 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
270 $sender_handle = $sender_nick . '@' . $sender_host;
272 $handles = $recip_handle . ';' . $sender_handle;
275 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
276 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
277 'subject' => $subject, 'recips' => $handles];
278 if (DBA::insert('conv', $fields)) {
279 $convid = DBA::lastInsertId();
283 Logger::log('send message: conversation not found.');
290 'uid' => $recipient['uid'],
293 'from-name' => $me['name'],
294 'from-photo' => $me['photo'],
295 'from-url' => $me['url'],
303 'parent-uri' => $replyto,
304 'created' => DateTimeFormat::utcNow(),