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;
13 use Friendica\Model\Item;
14 use Friendica\Model\Photo;
15 use Friendica\Database\DBA;
16 use Friendica\Network\Probe;
17 use Friendica\Protocol\Activity;
18 use Friendica\Util\DateTimeFormat;
19 use Friendica\Worker\Delivery;
22 * Class to handle private messages
27 * Insert received private message
30 * @return int|boolean Message ID or false on error
32 public static function insert($msg)
34 $user = User::getById($msg['uid']);
36 if (!isset($msg['reply'])) {
37 $msg['reply'] = DBA::exists('mail', ['parent-uri' => $msg['parent-uri']]);
40 if (empty($msg['convid'])) {
41 $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
42 if (DBA::isResult($mail)) {
43 $msg['convid'] = $mail['convid'];
47 if (empty($msg['guid'])) {
48 $host = parse_url($msg['from-url'], PHP_URL_HOST);
49 $msg['guid'] = Item::guidFromUri($msg['uri'], $host);
52 $msg['created'] = (!empty($msg['created']) ? DateTimeFormat::utc($msg['created']) : DateTimeFormat::utcNow());
56 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
58 Logger::info('duplicate message already delivered.');
62 DBA::insert('mail', $msg);
64 $msg['id'] = DBA::lastInsertId();
68 if (!empty($msg['convid'])) {
69 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
72 // send notifications.
74 'type' => NOTIFY_MAIL,
75 'notify_flags' => $user['notify-flags'],
76 'language' => $user['language'],
77 'to_name' => $user['username'],
78 'to_email' => $user['email'],
79 'uid' => $user['uid'],
82 'source_name' => $msg['from-name'],
83 'source_link' => $msg['from-url'],
84 'source_photo' => $msg['from-photo'],
85 'verb' => Activity::POST,
89 notification($notif_params);
91 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
97 * Send private message
99 * @param integer $recipient recipient id, default 0
100 * @param string $body message body, default empty
101 * @param string $subject message subject, default empty
102 * @param string $replyto reply to
104 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
106 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
114 if (!strlen($subject)) {
115 $subject = DI::l10n()->t('[no subject]');
118 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
119 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
121 if (!(count($me) && (count($contact)))) {
125 Photo::setPermissionFromBody($body, local_user(), $me['id'], '<' . $contact['id'] . '>', '', '', '');
127 $guid = System::createUUID();
128 $uri = Item::newURI(local_user(), $guid);
133 // look for any existing conversation structure
135 if (strlen($replyto)) {
137 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
138 local_user(), $replyto, $replyto];
139 $mail = DBA::selectFirst('mail', ['convid'], $condition);
140 if (DBA::isResult($mail)) {
141 $convid = $mail['convid'];
147 // create a new conversation
148 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
149 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
151 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
152 $sender_handle = $a->user['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
154 $conv_guid = System::createUUID();
155 $convuri = $recip_handle . ':' . $conv_guid;
157 $handles = $recip_handle . ';' . $sender_handle;
159 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
160 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
161 'subject' => $subject, 'recips' => $handles];
162 if (DBA::insert('conv', $fields)) {
163 $convid = DBA::lastInsertId();
168 Logger::log('send message: conversation not found.');
172 if (!strlen($replyto)) {
177 $success = DBA::insert(
180 'uid' => local_user(),
183 'from-name' => $me['name'],
184 'from-photo' => $me['thumb'],
185 'from-url' => $me['url'],
186 'contact-id' => $recipient,
193 'parent-uri' => $replyto,
194 'created' => DateTimeFormat::utcNow()
199 $post_id = DBA::lastInsertId();
204 * When a photo was uploaded into the message using the (profile wall) ajax
205 * uploader, The permissions are initially set to disallow anybody but the
206 * owner from seeing it. This is because the permissions may not yet have been
207 * set for the post. If it's private, the photo permissions should be set
208 * appropriately. But we didn't know the final permissions on the post until
209 * now. So now we'll look for links of uploaded messages that are in the
210 * post and set them to the same permissions as the post itself.
214 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
216 if (count($images)) {
217 foreach ($images as $image) {
218 $image_rid = Photo::ridFromURI($image);
219 if (!empty($image_rid)) {
220 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_rid, 'album' => 'Wall Photos', 'uid' => local_user()]);
227 Worker::add(PRIORITY_HIGH, "Notifier", Delivery::MAIL, $post_id);
228 return intval($post_id);
235 * @param array $recipient recipient, default empty
236 * @param string $body message body, default empty
237 * @param string $subject message subject, default empty
238 * @param string $replyto reply to, default empty
240 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
241 * @throws \ImagickException
243 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
249 if (!strlen($subject)) {
250 $subject = DI::l10n()->t('[no subject]');
253 $guid = System::createUUID();
254 $uri = Item::newURI(local_user(), $guid);
256 $me = Probe::uri($replyto);
262 $conv_guid = System::createUUID();
264 $recip_handle = $recipient['nickname'] . '@' . substr(DI::baseUrl(), strpos(DI::baseUrl(), '://') + 3);
266 $sender_nick = basename($replyto);
267 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
268 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
269 $sender_handle = $sender_nick . '@' . $sender_host;
271 $handles = $recip_handle . ';' . $sender_handle;
274 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
275 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
276 'subject' => $subject, 'recips' => $handles];
277 if (DBA::insert('conv', $fields)) {
278 $convid = DBA::lastInsertId();
282 Logger::log('send message: conversation not found.');
289 'uid' => $recipient['uid'],
292 'from-name' => $me['name'],
293 'from-photo' => $me['photo'],
294 'from-url' => $me['url'],
302 'parent-uri' => $replyto,
303 'created' => DateTimeFormat::utcNow(),