4 * @file src/Model/Mail.php
6 namespace Friendica\Model;
8 use Friendica\Core\L10n;
9 use Friendica\Core\System;
10 use Friendica\Core\Worker;
11 use Friendica\Database\DBM;
12 use Friendica\Network\Probe;
13 use Friendica\Util\DateTimeFormat;
16 require_once 'include/dba.php';
19 * Class to handle private messages
24 * Send private message
26 * @param integer $recipient recipient id, default 0
27 * @param string $body message body, default empty
28 * @param string $subject message subject, default empty
29 * @param string $replyto reply to
31 public static function send($recipient = 0, $body = '', $subject = '', $replyto = '')
39 if (!strlen($subject)) {
40 $subject = L10n::t('[no subject]');
43 $me = dba::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
44 $contact = dba::selectFirst('contact', [], ['id' => $recipient, 'uid' => local_user()]);
46 if (!(count($me) && (count($contact)))) {
51 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
56 // look for any existing conversation structure
58 if (strlen($replyto)) {
60 $r = q("SELECT `convid` FROM `mail` WHERE `uid` = %d AND (`uri` = '%s' OR `parent-uri` = '%s') LIMIT 1",
65 if (DBM::is_result($r)) {
66 $convid = $r[0]['convid'];
72 // create a new conversation
73 $recip_host = substr($contact['url'], strpos($contact['url'], '://') + 3);
74 $recip_host = substr($recip_host, 0, strpos($recip_host, '/'));
76 $recip_handle = (($contact['addr']) ? $contact['addr'] : $contact['nick'] . '@' . $recip_host);
77 $sender_handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
79 $conv_guid = get_guid(32);
80 $convuri = $recip_handle . ':' . $conv_guid;
82 $handles = $recip_handle . ';' . $sender_handle;
84 $fields = ['uid' => local_user(), 'guid' => $conv_guid, 'creator' => $sender_handle,
85 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
86 'subject' => $subject, 'recips' => $handles];
87 if (dba::insert('conv', $fields)) {
88 $convid = dba::lastInsertId();
93 logger('send message: conversation not found.');
97 if (!strlen($replyto)) {
102 $success = dba::insert(
105 'uid' => local_user(),
108 'from-name' => $me['name'],
109 'from-photo' => $me['thumb'],
110 'from-url' => $me['url'],
111 'contact-id' => $recipient,
118 'parent-uri' => $replyto,
119 'created' => DateTimeFormat::utcNow()
124 $post_id = dba::lastInsertId();
129 * When a photo was uploaded into the message using the (profile wall) ajax
130 * uploader, The permissions are initially set to disallow anybody but the
131 * owner from seeing it. This is because the permissions may not yet have been
132 * set for the post. If it's private, the photo permissions should be set
133 * appropriately. But we didn't know the final permissions on the post until
134 * now. So now we'll look for links of uploaded messages that are in the
135 * post and set them to the same permissions as the post itself.
139 if (preg_match_all("/\[img\](.*?)\[\/img\]/", $body, $match)) {
141 if (count($images)) {
142 foreach ($images as $image) {
143 if (!stristr($image, System::baseUrl() . '/photo/')) {
146 $image_uri = substr($image, strrpos($image, '/') + 1);
147 $image_uri = substr($image_uri, 0, strpos($image_uri, '-'));
148 dba::update('photo', ['allow-cid' => '<' . $recipient . '>'], ['resource-id' => $image_uri, 'album' => 'Wall Photos', 'uid' => local_user()]);
154 Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
155 return intval($post_id);
162 * @param string $recipient recipient, default empty
163 * @param string $body message body, default empty
164 * @param string $subject message subject, default empty
165 * @param string $replyto reply to, default empty
167 public static function sendWall($recipient = '', $body = '', $subject = '', $replyto = '')
173 if (!strlen($subject)) {
174 $subject = L10n::t('[no subject]');
177 $guid = get_guid(32);
178 $uri = 'urn:X-dfrn:' . System::baseUrl() . ':' . local_user() . ':' . $guid;
180 $me = Probe::uri($replyto);
186 $conv_guid = get_guid(32);
188 $recip_handle = $recipient['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
190 $sender_nick = basename($replyto);
191 $sender_host = substr($replyto, strpos($replyto, '://') + 3);
192 $sender_host = substr($sender_host, 0, strpos($sender_host, '/'));
193 $sender_handle = $sender_nick . '@' . $sender_host;
195 $handles = $recip_handle . ';' . $sender_handle;
198 $fields = ['uid' => $recipient['uid'], 'guid' => $conv_guid, 'creator' => $sender_handle,
199 'created' => DateTimeFormat::utcNow(), 'updated' => DateTimeFormat::utcNow(),
200 'subject' => $subject, 'recips' => $handles];
201 if (dba::insert('conv', $fields)) {
202 $convid = dba::lastInsertId();
206 logger('send message: conversation not found.');
213 'uid' => $recipient['uid'],
216 'from-name' => $me['name'],
217 'from-photo' => $me['photo'],
218 'from-url' => $me['url'],
226 'parent-uri' => $replyto,
227 'created' => DateTimeFormat::utcNow(),