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\Database\DBA;
14 use Friendica\Network\Probe;
15 use Friendica\Util\DateTimeFormat;
18 * Class to handle private messages
23 * Insert received private message
26 * @return int|boolean Message ID or false on error
28 public static function insert($msg)
30 $user = User::getById($msg['uid']);
32 if (empty($msg['convid'])) {
33 $mail = DBA::selectFirst('mail', ['convid'], ["`convid` != 0 AND `parent-uri` = ?", $msg['parent-uri']]);
34 if (DBA::isResult($mail)) {
35 $msg['convid'] = $mail['convid'];
39 if (empty($msg['guid'])) {
40 $host = parse_url($msg['from-url'], PHP_URL_HOST);
41 $msg['guid'] = Item::guidFromUri($msg['uri'], $host);
46 if (DBA::exists('mail', ['uri' => $msg['uri'], 'uid' => $msg['uid']])) {
48 Logger::info('duplicate message already delivered.');
52 DBA::insert('mail', $msg);
54 $msg['id'] = DBA::lastInsertId();
58 if (!empty($msg['convid'])) {
59 DBA::update('conv', ['updated' => DateTimeFormat::utcNow()], ['id' => $msg['convid']]);
62 // send notifications.
64 'type' => NOTIFY_MAIL,
65 'notify_flags' => $user['notify-flags'],
66 'language' => $user['language'],
67 'to_name' => $user['username'],
68 'to_email' => $user['email'],
69 'uid' => $user['uid'],
71 'parent' => $msg['parent-uri'],
72 'source_name' => $msg['from-name'],
73 'source_link' => $msg['from-url'],
74 'source_photo' => $msg['from-photo'],
75 'verb' => ACTIVITY_POST,
79 notification($notif_params);
81 Logger::info('Mail is processed, notification was sent.', ['id' => $msg['id'], 'uri' => $msg['uri']]);
87 * Send private message
89 * @param integer $recipient recipient id, default 0
90 * @param string $body message body, default empty
91 * @param string $subject message subject, default empty
92 * @param string $replyto reply to
94 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
96 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
104 if (!strlen($subject)) {
105 $subject = L10n::t('[no subject]');
108 $me = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
109 $contact = DBA::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
111 if (!(count($me) && (count($contact)))) {
115 $guid = System::createUUID();
116 $uri = Item::newURI(local_user(), $guid);
121 // look for any existing conversation structure
123 if (strlen($replyto)) {
125 $condition = ["`uid` = ? AND (`uri` = ? OR `parent-uri` = ?)",
126 local_user(), $replyto, $replyto];
127 $mail = DBA::selectFirst('mail', ['convid'], $condition);
128 if (DBA::isResult($mail)) {
129 $convid = $mail['convid'];
135 // create a new conversation
136 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
137 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
139 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
140 $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
142 $conv_guid = System::createUUID();
143 $convuri = $recip_handle . ':' . $conv_guid;
145 $handles = $recip_handle . ';' . $sender_handle;
147 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
148 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
149 'subject' => $subject, 'recips' => $handles];
150 if (DBA::insert('conv', $fields)) {
151 $convid = DBA::lastInsertId();
156 Logger::log('send message: conversation not found.');
160 if (!strlen($replyto)) {
165 $success = DBA::insert(
168 'uid' => local_user(),
171 'from-name' => $me['name'],
172 'from-photo' => $me['thumb'],
173 'from-url' => $me['url'],
174 'contact-id' => $recipient,
181 'parent-uri' => $replyto,
182 'created' => DateTimeFormat::utcNow()
187 $post_id = DBA::lastInsertId();
192 * When a photo was uploaded into the message using the (profile wall) ajax
193 * uploader, The permissions are initially set to disallow anybody but the
194 * owner from seeing it. This is because the permissions may not yet have been
195 * set for the post. If it's private, the photo permissions should be set
196 * appropriately. But we didn't know the final permissions on the post until
197 * now. So now we'll look for links of uploaded messages that are in the
198 * post and set them to the same permissions as the post itself.
202 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
204 if (count($images)) {
205 foreach ($images as $image) {
206 if (!stristr($image, System::baseUrl() . '/photo/')) {
209 $image_uri = substr($image, strrpos($image, '/') + 1);
210 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
211 Photo::update(['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
217 Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
218 return intval($post_id);
225 * @param array $recipient recipient, default empty
226 * @param string $body message body, default empty
227 * @param string $subject message subject, default empty
228 * @param string $replyto reply to, default empty
230 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
231 * @throws \ImagickException
233 public static function sendWall(array $recipient = [], $body = '', $subject = '', $replyto = '')
239 if (!strlen($subject)) {
240 $subject = L10n::t('[no subject]');
243 $guid = System::createUUID();
244 $uri = Item::newURI(local_user(), $guid);
246 $me = Probe::uri($replyto);
252 $conv_guid = System::createUUID();
254 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
256 $sender_nick = basename($replyto);
257 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
258 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
259 $sender_handle = $sender_nick . '@' . $sender_host;
261 $handles = $recip_handle . ';' . $sender_handle;
264 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
265 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
266 'subject' => $subject, 'recips' => $handles];
267 if (DBA::insert('conv', $fields)) {
268 $convid = DBA::lastInsertId();
272 Logger::log('send message: conversation not found.');
279 'uid' => $recipient['uid'],
282 'from-name' => $me['name'],
283 'from-photo' => $me['photo'],
284 'from-url' => $me['url'],
292 'parent-uri' => $replyto,
293 'created' => DateTimeFormat::utcNow(),