4 * @file src/Model/Mail.php
6 namespace Friendica\Model;
8 use Friendica\Core\Logger;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
12 use Friendica\Database\DBA;
13 use Friendica\Network\Probe;
14 use Friendica\Protocol\Activity;
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);
49 $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());
53 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
55 Logger::info('duplicate message already delivered.');
59 DBA::insert('mail', $msg);
61 $msg['id'] = DBA::lastInsertId();
65 if (!empty($msg['convid'])) {
66 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
69 // send notifications.
71 'type' => NOTIFY_MAIL,
72 'notify_flags' => $user['notify-flags'],
73 'language' => $user['language'],
74 'to_name' => $user['username'],
75 'to_email' => $user['email'],
76 'uid' => $user['uid'],
79 'source_name' => $msg['from-name'],
80 'source_link' => $msg['from-url'],
81 'source_photo' => $msg['from-photo'],
82 'verb' => Activity::POST,
86 notification($notif_params);
88 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
94 * Send private message
96 * @param integer $recipient recipient id, default 0
97 * @param string $body message body, default empty
98 * @param string $subject message subject, default empty
99 * @param string $replyto reply to
101 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
103 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
111 if (!strlen($subject)) {
112 $subject = DI::l10n()->t('[no subject]');
115 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
116 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
118 if (!(count($me) && (count($contact)))) {
122 Photo::setPermissionFromBody($body, local_user(), $me['id'], '<' . $contact['id'] . '>', '', '', '');
124 $guid = System::createUUID();
125 $uri = Item::newURI(local_user(), $guid);
130 // look for any existing conversation structure
132 if (strlen($replyto)) {
134 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
135 local_user(), $replyto, $replyto];
136 $mail = DBA::selectFirst('mail', ['convid'], $condition);
137 if (DBA::isResult($mail)) {
138 $convid = $mail['convid'];
144 // create a new conversation
145 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
146 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
148 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
149 $sender_handle = $a->user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
151 $conv_guid = System::createUUID();
152 $convuri = $recip_handle . ':' . $conv_guid;
154 $handles = $recip_handle . ';' . $sender_handle;
156 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
157 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
158 'subject' => $subject, 'recips' => $handles];
159 if (DBA::insert('conv', $fields)) {
160 $convid = DBA::lastInsertId();
165 Logger::log('send message: conversation not found.');
169 if (!strlen($replyto)) {
174 $success = DBA::insert(
177 'uid' => local_user(),
180 'from-name' => $me['name'],
181 'from-photo' => $me['thumb'],
182 'from-url' => $me['url'],
183 'contact-id' => $recipient,
190 'parent-uri' => $replyto,
191 'created' => DateTimeFormat::utcNow()
196 $post_id = DBA::lastInsertId();
201 * When a photo was uploaded into the message using the (profile wall) ajax
202 * uploader, The permissions are initially set to disallow anybody but the
203 * owner from seeing it. This is because the permissions may not yet have been
204 * set for the post. If it's private, the photo permissions should be set
205 * appropriately. But we didn't know the final permissions on the post until
206 * now. So now we'll look for links of uploaded messages that are in the
207 * post and set them to the same permissions as the post itself.
211 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
213 if (count($images)) {
214 foreach ($images as $image) {
215 $image_rid = Photo::ridFromURI($image);
216 if (!empty($image_rid)) {
217 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]);
224 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
225 return intval($post_id);
232 * @param array $recipient recipient, default empty
233 * @param string $body message body, default empty
234 * @param string $subject message subject, default empty
235 * @param string $replyto reply to, default empty
237 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
238 * @throws \ImagickException
240 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
246 if (!strlen($subject)) {
247 $subject = DI::l10n()->t('[no subject]');
250 $guid = System::createUUID();
251 $uri = Item::newURI(local_user(), $guid);
253 $me = Probe::uri($replyto);
259 $conv_guid = System::createUUID();
261 $recip_handle = $recipient['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
263 $sender_nick = basename($replyto);
264 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
265 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
266 $sender_handle = $sender_nick . '@' . $sender_host;
268 $handles = $recip_handle . ';' . $sender_handle;
271 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
272 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
273 'subject' => $subject, 'recips' => $handles];
274 if (DBA::insert('conv', $fields)) {
275 $convid = DBA::lastInsertId();
279 Logger::log('send message: conversation not found.');
286 'uid' => $recipient['uid'],
289 'from-name' => $me['name'],
290 'from-photo' => $me['photo'],
291 'from-url' => $me['url'],
299 'parent-uri' => $replyto,
300 'created' => DateTimeFormat::utcNow(),