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\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.
72 'type' => NOTIFY_MAIL,
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 = 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(System::baseUrl(), strpos(System::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 if (!stristr($image, System::baseUrl() . '/photo/')) {
219 $image_uri = substr($image, strrpos($image, '/') + 1);
220 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
221 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, '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 = 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(System::baseUrl(), strpos(System::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(),