]> git.mxchange.org Git - friendica.git/blob - mod/message.php
Continued:
[friendica.git] / mod / message.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Content\Nav;
24 use Friendica\Content\Pager;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\ACL;
27 use Friendica\Core\Renderer;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Mail;
32 use Friendica\Module\Security\Login;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Strings;
35 use Friendica\Util\Temporal;
36
37 function message_init(App $a)
38 {
39         $tabs = '';
40
41         if (DI::args()->getArgc() > 1 && is_numeric(DI::args()->getArgv()[1])) {
42                 $tabs = render_messages(get_messages(DI::userSession()->getLocalUserId(), 0, 5), 'mail_list.tpl');
43         }
44
45         $new = [
46                 'label'     => DI::l10n()->t('New Message'),
47                 'url'       => 'message/new',
48                 'sel'       => DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] == 'new',
49                 'accesskey' => 'm',
50         ];
51
52         $tpl = Renderer::getMarkupTemplate('message_side.tpl');
53         DI::page()['aside'] = Renderer::replaceMacros($tpl, [
54                 '$tabs' => $tabs,
55                 '$new'  => $new,
56         ]);
57
58         $head_tpl = Renderer::getMarkupTemplate('message-head.tpl');
59         DI::page()['htmlhead'] .= Renderer::replaceMacros($head_tpl, [
60                 '$base' => (string)DI::baseUrl()
61         ]);
62 }
63
64 function message_post(App $a)
65 {
66         if (!DI::userSession()->getLocalUserId()) {
67                 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
68                 return;
69         }
70
71         $sender_id = DI::userSession()->getLocalUserId();
72         $replyto   = !empty($_REQUEST['replyto'])   ? trim($_REQUEST['replyto'])                   : '';
73         $subject   = !empty($_REQUEST['subject'])   ? trim($_REQUEST['subject'])                   : '';
74         $body      = !empty($_REQUEST['body'])      ? Strings::escapeHtml(trim($_REQUEST['body'])) : '';
75         $recipient = !empty($_REQUEST['recipient']) ? intval($_REQUEST['recipient'])               : 0;
76
77         $ret = Mail::send($sender_id, $recipient, $body, $subject, $replyto);
78         $norecip = false;
79
80         switch ($ret) {
81                 case -1:
82                         DI::sysmsg()->addNotice(DI::l10n()->t('No recipient selected.'));
83                         $norecip = true;
84                         break;
85
86                 case -2:
87                         DI::sysmsg()->addNotice(DI::l10n()->t('Unable to locate contact information.'));
88                         break;
89
90                 case -3:
91                         DI::sysmsg()->addNotice(DI::l10n()->t('Message could not be sent.'));
92                         break;
93
94                 case -4:
95                         DI::sysmsg()->addNotice(DI::l10n()->t('Message collection failure.'));
96                         break;
97         }
98
99         // fake it to go back to the input form if no recipient listed
100         if ($norecip) {
101                 DI::args()->setArgv(['message', 'new']);
102         } else {
103                 DI::baseUrl()->redirect(DI::args()->getCommand() . '/' . $ret);
104         }
105 }
106
107 function message_content(App $a)
108 {
109         $o = '';
110         Nav::setSelected('messages');
111
112         if (!DI::userSession()->getLocalUserId()) {
113                 DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
114                 return Login::form();
115         }
116
117         $myprofile = DI::baseUrl() . '/profile/' . $a->getLoggedInUserNickname();
118
119         $tpl = Renderer::getMarkupTemplate('mail_head.tpl');
120         if (DI::args()->getArgc() > 1 && DI::args()->getArgv()[1] == 'new') {
121                 $button = [
122                         'label' => DI::l10n()->t('Discard'),
123                         'url'   => '/message',
124                         'sel'   => 'close',
125                 ];
126         } else {
127                 $button = [
128                         'label'     => DI::l10n()->t('New Message'),
129                         'url'       => '/message/new',
130                         'sel'       => 'new',
131                         'accesskey' => 'm',
132                 ];
133         }
134         $header = Renderer::replaceMacros($tpl, [
135                 '$messages' => DI::l10n()->t('Messages'),
136                 '$button'   => $button,
137         ]);
138
139         if ((DI::args()->getArgc() == 3) && (DI::args()->getArgv()[1] === 'drop' || DI::args()->getArgv()[1] === 'dropconv')) {
140                 if (!intval(DI::args()->getArgv()[2])) {
141                         return;
142                 }
143
144                 $cmd = DI::args()->getArgv()[1];
145                 if ($cmd === 'drop') {
146                         $message = DBA::selectFirst('mail', ['convid'], ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId()]);
147                         if(!DBA::isResult($message)){
148                                 DI::sysmsg()->addNotice(DI::l10n()->t('Conversation not found.'));
149                                 DI::baseUrl()->redirect('message');
150                         }
151
152                         if (!DBA::delete('mail', ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId()])) {
153                                 DI::sysmsg()->addNotice(DI::l10n()->t('Message was not deleted.'));
154                         }
155
156                         $conversation = DBA::selectFirst('mail', ['id'], ['convid' => $message['convid'], 'uid' => DI::userSession()->getLocalUserId()]);
157                         if(!DBA::isResult($conversation)){
158                                 DI::baseUrl()->redirect('message');
159                         }
160
161                         DI::baseUrl()->redirect('message/' . $conversation['id'] );
162                 } else {
163                         $parentmail = DBA::selectFirst('mail', ['parent-uri'], ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId()]);
164                         if (DBA::isResult($parentmail)) {
165                                 $parent = $parentmail['parent-uri'];
166
167                                 if (!DBA::delete('mail', ['parent-uri' => $parent, 'uid' => DI::userSession()->getLocalUserId()])) {
168                                         DI::sysmsg()->addNotice(DI::l10n()->t('Conversation was not removed.'));
169                                 }
170                         }
171                         DI::baseUrl()->redirect('message');
172                 }
173         }
174
175         if ((DI::args()->getArgc() > 1) && (DI::args()->getArgv()[1] === 'new')) {
176                 $o .= $header;
177
178                 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
179                 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
180                         '$nickname' => $a->getLoggedInUserNickname(),
181                         '$linkurl' => DI::l10n()->t('Please enter a link URL:')
182                 ]);
183
184                 $recipientId = DI::args()->getArgv()[2] ?? null;
185
186                 $select = ACL::getMessageContactSelectHTML($recipientId);
187
188                 $tpl = Renderer::getMarkupTemplate('prv_message.tpl');
189                 $o .= Renderer::replaceMacros($tpl, [
190                         '$header'      => DI::l10n()->t('Send Private Message'),
191                         '$to'          => DI::l10n()->t('To:'),
192                         '$subject'     => DI::l10n()->t('Subject:'),
193                         '$subjtxt'     => $_REQUEST['subject'] ?? '',
194                         '$text'        => $_REQUEST['body'] ?? '',
195                         '$readonly'    => '',
196                         '$yourmessage' => DI::l10n()->t('Your message:'),
197                         '$select'      => $select,
198                         '$parent'      => '',
199                         '$upload'      => DI::l10n()->t('Upload photo'),
200                         '$insert'      => DI::l10n()->t('Insert web link'),
201                         '$wait'        => DI::l10n()->t('Please wait'),
202                         '$submit'      => DI::l10n()->t('Submit')
203                 ]);
204                 return $o;
205         }
206
207
208         $_SESSION['return_path'] = DI::args()->getQueryString();
209
210         if (DI::args()->getArgc() == 1) {
211
212                 // List messages
213
214                 $o .= $header;
215
216                 $total = DBA::count('mail', ['uid' => DI::userSession()->getLocalUserId()], ['distinct' => true, 'expression' => 'parent-uri']);
217
218                 $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
219
220                 $r = get_messages(DI::userSession()->getLocalUserId(), $pager->getStart(), $pager->getItemsPerPage());
221
222                 if (!DBA::isResult($r)) {
223                         DI::sysmsg()->addNotice(DI::l10n()->t('No messages.'));
224                         return $o;
225                 }
226
227                 $o .= render_messages($r, 'mail_list.tpl');
228
229                 $o .= $pager->renderFull($total);
230
231                 return $o;
232         }
233
234         if ((DI::args()->getArgc() > 1) && (intval(DI::args()->getArgv()[1]))) {
235
236                 $o .= $header;
237
238                 $message = DBA::fetchFirst("
239                         SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
240                         FROM `mail`
241                         LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
242                         WHERE `mail`.`uid` = ? AND `mail`.`id` = ?
243                         LIMIT 1",
244                         DI::userSession()->getLocalUserId(),
245                         DI::args()->getArgv()[1]
246                 );
247                 if (DBA::isResult($message)) {
248                         $contact_id = $message['contact-id'];
249
250                         $params = [
251                                 DI::userSession()->getLocalUserId(),
252                                 $message['parent-uri']
253                         ];
254
255                         if ($message['convid']) {
256                                 $sql_extra = "AND (`mail`.`parent-uri` = ? OR `mail`.`convid` = ?)";
257                                 $params[] = $message['convid'];
258                         } else {
259                                 $sql_extra = "AND `mail`.`parent-uri` = ?";
260                         }
261                         $messages_stmt = DBA::p("
262                                 SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
263                                 FROM `mail`
264                                 LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
265                                 WHERE `mail`.`uid` = ?
266                                 $sql_extra
267                                 ORDER BY `mail`.`created` ASC",
268                                 ...$params
269                         );
270
271                         $messages = DBA::toArray($messages_stmt);
272
273                         DBA::update('mail', ['seen' => 1], ['parent-uri' => $message['parent-uri'], 'uid' => DI::userSession()->getLocalUserId()]);
274                 } else {
275                         $messages = false;
276                 }
277
278                 if (!DBA::isResult($messages)) {
279                         DI::sysmsg()->addNotice(DI::l10n()->t('Message not available.'));
280                         return $o;
281                 }
282
283                 $tpl = Renderer::getMarkupTemplate('msg-header.tpl');
284                 DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
285                         '$nickname' => $a->getLoggedInUserNickname(),
286                         '$linkurl' => DI::l10n()->t('Please enter a link URL:')
287                 ]);
288
289                 $mails = [];
290                 $seen = 0;
291                 $unknown = false;
292
293                 foreach ($messages as $message) {
294                         if ($message['unknown']) {
295                                 $unknown = true;
296                         }
297
298                         if ($message['from-url'] == $myprofile) {
299                                 $from_url = $myprofile;
300                                 $sparkle = '';
301                         } else {
302                                 $from_url = Contact::magicLink($message['from-url']);
303                                 $sparkle = ' sparkle';
304                         }
305
306                         $from_name_e = $message['from-name'];
307                         $subject_e = $message['title'];
308                         $body_e = BBCode::convertForUriId($message['uri-id'], $message['body']);
309                         $to_name_e = $message['name'];
310
311                         $contact = Contact::getByURL($message['from-url'], false, ['thumb', 'addr', 'id', 'avatar', 'url']);
312                         $from_photo = Contact::getThumb($contact);
313
314                         $mails[] = [
315                                 'id'         => $message['id'],
316                                 'from_name'  => $from_name_e,
317                                 'from_url'   => $from_url,
318                                 'from_addr'  => $contact['addr'] ?? $from_url,
319                                 'sparkle'    => $sparkle,
320                                 'from_photo' => $from_photo,
321                                 'subject'    => $subject_e,
322                                 'body'       => $body_e,
323                                 'delete'     => DI::l10n()->t('Delete message'),
324                                 'to_name'    => $to_name_e,
325                                 'date'       => DateTimeFormat::local($message['created'], DI::l10n()->t('D, d M Y - g:i A')),
326                                 'ago'        => Temporal::getRelativeDate($message['created']),
327                         ];
328
329                         $seen = $message['seen'];
330                 }
331
332                 $select = $message['name'] . '<input type="hidden" name="recipient" value="' . $contact_id . '" />';
333                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
334
335                 $tpl = Renderer::getMarkupTemplate('mail_display.tpl');
336                 $o = Renderer::replaceMacros($tpl, [
337                         '$thread_id'      => DI::args()->getArgv()[1],
338                         '$thread_subject' => $message['title'],
339                         '$thread_seen'    => $seen,
340                         '$delete'         => DI::l10n()->t('Delete conversation'),
341                         '$canreply'       => (($unknown) ? false : '1'),
342                         '$unknown_text'   => DI::l10n()->t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
343                         '$mails'          => $mails,
344                         // reply
345                         '$header'         => DI::l10n()->t('Send Reply'),
346                         '$to'             => DI::l10n()->t('To:'),
347                         '$subject'        => DI::l10n()->t('Subject:'),
348                         '$subjtxt'        => $message['title'],
349                         '$readonly'       => ' readonly="readonly" style="background: #BBBBBB;" ',
350                         '$yourmessage'    => DI::l10n()->t('Your message:'),
351                         '$text'           => '',
352                         '$select'         => $select,
353                         '$parent'         => $parent,
354                         '$upload'         => DI::l10n()->t('Upload photo'),
355                         '$insert'         => DI::l10n()->t('Insert web link'),
356                         '$submit'         => DI::l10n()->t('Submit'),
357                         '$wait'           => DI::l10n()->t('Please wait')
358                 ]);
359
360                 return $o;
361         }
362 }
363
364 /**
365  * @param int $uid
366  * @param int $start
367  * @param int $limit
368  * @return array
369  */
370 function get_messages(int $uid, int $start, int $limit): array
371 {
372         return DBA::toArray(DBA::p('SELECT
373                         m.`id`,
374                         m.`uid`,
375                         m.`guid`,
376                         m.`from-name`,
377                         m.`from-photo`,
378                         m.`from-url`,
379                         m.`contact-id`,
380                         m.`convid`,
381                         m.`title`,
382                         m.`body`,
383                         m.`seen`,
384                         m.`reply`,
385                         m.`replied`,
386                         m.`unknown`,
387                         m.`uri`,
388                         m.`parent-uri`,
389                         m.`created`,
390                         c.`name`,
391                         c.`url`,
392                         c.`thumb`,
393                         c.`network`,
394                         m2.`count`,
395                         m2.`mailcreated`,
396                         m2.`mailseen`
397                 FROM `mail` m
398                 JOIN (
399                         SELECT
400                                 `parent-uri`,
401                                 MIN(`id`)      AS `id`,
402                                 COUNT(*)       AS `count`,
403                                 MAX(`created`) AS `mailcreated`,
404                                 MIN(`seen`)    AS `mailseen`
405                         FROM `mail`
406                         WHERE `uid` = ?
407                         GROUP BY `parent-uri`
408                 ) m2 ON m.`parent-uri` = m2.`parent-uri` AND m.`id` = m2.`id`
409                 LEFT JOIN `contact` c ON m.`contact-id` = c.`id`
410                 WHERE m.`uid` = ?
411                 ORDER BY m2.`mailcreated` DESC
412                 LIMIT ?, ?'
413                 , $uid, $uid, $start, $limit));
414 }
415
416 function render_messages(array $msg, string $t): string
417 {
418         $a = DI::app();
419
420         $tpl = Renderer::getMarkupTemplate($t);
421         $rslt = '';
422
423         $myprofile = DI::baseUrl() . '/profile/' . $a->getLoggedInUserNickname();
424
425         foreach ($msg as $rr) {
426                 if ($rr['unknown']) {
427                         $participants = DI::l10n()->t("Unknown sender - %s", $rr['from-name']);
428                 } elseif (Strings::compareLink($rr['from-url'], $myprofile)) {
429                         $participants = DI::l10n()->t("You and %s", $rr['name']);
430                 } else {
431                         $participants = DI::l10n()->t("%s and You", $rr['from-name']);
432                 }
433
434                 $body_e = $rr['body'];
435                 $to_name_e = $rr['name'];
436
437                 if (is_null($rr['url'])) {
438                         // contact-id is pointing to a nonexistent contact
439                         continue;
440                 }
441
442                 $contact = Contact::getByURL($rr['url'], false, ['thumb', 'addr', 'id', 'avatar', 'url']);
443                 $from_photo = Contact::getThumb($contact);
444
445                 $rslt .= Renderer::replaceMacros($tpl, [
446                         '$id'         => $rr['id'],
447                         '$from_name'  => $participants,
448                         '$from_url'   => Contact::magicLink($rr['url']),
449                         '$from_addr'  => $contact['addr'] ?? '',
450                         '$sparkle'    => ' sparkle',
451                         '$from_photo' => $from_photo,
452                         '$subject'    => $rr['title'],
453                         '$delete'     => DI::l10n()->t('Delete conversation'),
454                         '$body'       => $body_e,
455                         '$to_name'    => $to_name_e,
456                         '$date'       => DateTimeFormat::local($rr['mailcreated'], DI::l10n()->t('D, d M Y - g:i A')),
457                         '$ago'        => Temporal::getRelativeDate($rr['mailcreated']),
458                         '$seen'       => $rr['mailseen'],
459                         '$count'      => DI::l10n()->tt('%d message', '%d messages', $rr['count']),
460                 ]);
461         }
462
463         return $rslt;
464 }