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