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\Model\Notify\Type;
14 use Friendica\Network\Probe;
15 use Friendica\Protocol\Activity;
16 use Friendica\Util\DateTimeFormat;
17 use Friendica\Worker\Delivery;
20 * Class to handle private messages
25 * Insert received private message
28 * @return int|boolean Message ID or false on error
30 public static function insert($msg)
32 $user = User::getById($msg['uid']);
34 if (!isset($msg['reply'])) {
35 $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
38 if (empty($msg['convid'])) {
39 $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
40 if (DBA::isResult($mail)) {
41 $msg['convid'] = $mail['convid'];
45 if (empty($msg['guid'])) {
46 $host = parse_url($msg['from-url'], PHP_URL_HOST);
47 $msg['guid'] = Item::guidFromUri($msg['uri'], $host);
50 $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());
54 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
56 Logger::info('duplicate message already delivered.');
60 DBA::insert('mail', $msg);
62 $msg['id'] = DBA::lastInsertId();
66 if (!empty($msg['convid'])) {
67 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
70 // send notifications.
73 'notify_flags' => $user['notify-flags'],
74 'language' => $user['language'],
75 'to_name' => $user['username'],
76 'to_email' => $user['email'],
77 'uid' => $user['uid'],
80 'source_name' => $msg['from-name'],
81 'source_link' => $msg['from-url'],
82 'source_photo' => $msg['from-photo'],
83 'verb' => Activity::POST,
87 notification($notif_params);
89 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
95 * Send private message
97 * @param integer $recipient recipient id, default 0
98 * @param string $body message body, default empty
99 * @param string $subject message subject, default empty
100 * @param string $replyto reply to
102 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
104 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
112 if (!strlen($subject)) {
113 $subject = DI::l10n()->t('[no subject]');
116 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
117 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
119 if (!(count($me) && (count($contact)))) {
123 Photo::setPermissionFromBody($body, local_user(), $me['id'], '<' . $contact['id'] . '>', '', '', '');
125 $guid = System::createUUID();
126 $uri = Item::newURI(local_user(), $guid);
131 // look for any existing conversation structure
133 if (strlen($replyto)) {
135 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
136 local_user(), $replyto, $replyto];
137 $mail = DBA::selectFirst('mail', ['convid'], $condition);
138 if (DBA::isResult($mail)) {
139 $convid = $mail['convid'];
145 // create a new conversation
146 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
147 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
149 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
150 $sender_handle = $a->user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
152 $conv_guid = System::createUUID();
153 $convuri = $recip_handle . ':' . $conv_guid;
155 $handles = $recip_handle . ';' . $sender_handle;
157 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
158 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
159 'subject' => $subject, 'recips' => $handles];
160 if (DBA::insert('conv', $fields)) {
161 $convid = DBA::lastInsertId();
166 Logger::log('send message: conversation not found.');
170 if (!strlen($replyto)) {
175 $success = DBA::insert(
178 'uid' => local_user(),
181 'from-name' => $me['name'],
182 'from-photo' => $me['thumb'],
183 'from-url' => $me['url'],
184 'contact-id' => $recipient,
191 'parent-uri' => $replyto,
192 'created' => DateTimeFormat::utcNow()
197 $post_id = DBA::lastInsertId();
202 * When a photo was uploaded into the message using the (profile wall) ajax
203 * uploader, The permissions are initially set to disallow anybody but the
204 * owner from seeing it. This is because the permissions may not yet have been
205 * set for the post. If it's private, the photo permissions should be set
206 * appropriately. But we didn't know the final permissions on the post until
207 * now. So now we'll look for links of uploaded messages that are in the
208 * post and set them to the same permissions as the post itself.
212 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
214 if (count($images)) {
215 foreach ($images as $image) {
216 $image_rid = Photo::ridFromURI($image);
217 if (!empty($image_rid)) {
218 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]);
225 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
226 return intval($post_id);
233 * @param array $recipient recipient, default empty
234 * @param string $body message body, default empty
235 * @param string $subject message subject, default empty
236 * @param string $replyto reply to, default empty
238 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
239 * @throws \ImagickException
241 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
247 if (!strlen($subject)) {
248 $subject = DI::l10n()->t('[no subject]');
251 $guid = System::createUUID();
252 $uri = Item::newURI(local_user(), $guid);
254 $me = Probe::uri($replyto);
260 $conv_guid = System::createUUID();
262 $recip_handle = $recipient['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
264 $sender_nick = basename($replyto);
265 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
266 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
267 $sender_handle = $sender_nick . '@' . $sender_host;
269 $handles = $recip_handle . ';' . $sender_handle;
272 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
273 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
274 'subject' => $subject, 'recips' => $handles];
275 if (DBA::insert('conv', $fields)) {
276 $convid = DBA::lastInsertId();
280 Logger::log('send message: conversation not found.');
287 'uid' => $recipient['uid'],
290 'from-name' => $me['name'],
291 'from-photo' => $me['photo'],
292 'from-url' => $me['url'],
300 'parent-uri' => $replyto,
301 'created' => DateTimeFormat::utcNow(),