]> git.mxchange.org Git - friendica.git/blob - mod/message.php
Replace remaining functions in include/datetime by Temporal methods
[friendica.git] / mod / message.php
1 <?php
2 /**
3  * @file mod/message.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Nav;
8 use Friendica\Content\Smilies;
9 use Friendica\Core\L10n;
10 use Friendica\Core\System;
11 use Friendica\Database\DBM;
12 use Friendica\Model\Contact;
13 use Friendica\Model\Mail;
14 use Friendica\Util\DateTimeFormat;
15 use Friendica\Util\Temporal;
16
17 require_once 'include/acl_selectors.php';
18 require_once 'include/conversation.php';
19
20 function message_init(App $a)
21 {
22         $tabs = '';
23
24         if ($a->argc > 1 && is_numeric($a->argv[1])) {
25                 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
26         }
27
28         $new = [
29                 'label' => L10n::t('New Message'),
30                 'url' => 'message/new',
31                 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
32                 'accesskey' => 'm',
33         ];
34
35         $tpl = get_markup_template('message_side.tpl');
36         $a->page['aside'] = replace_macros($tpl, [
37                 '$tabs' => $tabs,
38                 '$new' => $new,
39         ]);
40         $base = System::baseUrl();
41
42         $head_tpl = get_markup_template('message-head.tpl');
43         $a->page['htmlhead'] .= replace_macros($head_tpl, [
44                 '$baseurl' => System::baseUrl(true),
45                 '$base' => $base
46         ]);
47
48         $end_tpl = get_markup_template('message-end.tpl');
49         $a->page['end'] .= replace_macros($end_tpl, [
50                 '$baseurl' => System::baseUrl(true),
51                 '$base' => $base
52         ]);
53 }
54
55 function message_post(App $a)
56 {
57         if (!local_user()) {
58                 notice(L10n::t('Permission denied.') . EOL);
59                 return;
60         }
61
62         $replyto   = x($_REQUEST, 'replyto')   ? notags(trim($_REQUEST['replyto']))   : '';
63         $subject   = x($_REQUEST, 'subject')   ? notags(trim($_REQUEST['subject']))   : '';
64         $body      = x($_REQUEST, 'body')      ? escape_tags(trim($_REQUEST['body'])) : '';
65         $recipient = x($_REQUEST, 'messageto') ? intval($_REQUEST['messageto'])       : 0;
66
67         $ret = Mail::send($recipient, $body, $subject, $replyto);
68         $norecip = false;
69
70         switch ($ret) {
71                 case -1:
72                         notice(L10n::t('No recipient selected.') . EOL);
73                         $norecip = true;
74                         break;
75                 case -2:
76                         notice(L10n::t('Unable to locate contact information.') . EOL);
77                         break;
78                 case -3:
79                         notice(L10n::t('Message could not be sent.') . EOL);
80                         break;
81                 case -4:
82                         notice(L10n::t('Message collection failure.') . EOL);
83                         break;
84                 default:
85                         info(L10n::t('Message sent.') . EOL);
86         }
87
88         // fake it to go back to the input form if no recipient listed
89         if ($norecip) {
90                 $a->argc = 2;
91                 $a->argv[1] = 'new';
92         } else {
93                 goaway($_SESSION['return_url']);
94         }
95 }
96
97 function message_content(App $a)
98 {
99         $o = '';
100         Nav::setSelected('messages');
101
102         if (!local_user()) {
103                 notice(L10n::t('Permission denied.') . EOL);
104                 return;
105         }
106
107         $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
108
109         $tpl = get_markup_template('mail_head.tpl');
110         $header = replace_macros($tpl, [
111                 '$messages' => L10n::t('Messages'),
112         ]);
113
114         if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
115                 if (!intval($a->argv[2])) {
116                         return;
117                 }
118
119                 // Check if we should do HTML-based delete confirmation
120                 if ($_REQUEST['confirm']) {
121                         // <form> can't take arguments in its "action" parameter
122                         // so add any arguments as hidden inputs
123                         $query = explode_querystring($a->query_string);
124                         $inputs = [];
125                         foreach ($query['args'] as $arg) {
126                                 if (strpos($arg, 'confirm=') === false) {
127                                         $arg_parts = explode('=', $arg);
128                                         $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
129                                 }
130                         }
131
132                         //$a->page['aside'] = '';
133                         return replace_macros(get_markup_template('confirm.tpl'), [
134                                 '$method' => 'get',
135                                 '$message' => L10n::t('Do you really want to delete this message?'),
136                                 '$extra_inputs' => $inputs,
137                                 '$confirm' => L10n::t('Yes'),
138                                 '$confirm_url' => $query['base'],
139                                 '$confirm_name' => 'confirmed',
140                                 '$cancel' => L10n::t('Cancel'),
141                         ]);
142                 }
143                 // Now check how the user responded to the confirmation query
144                 if ($_REQUEST['canceled']) {
145                         goaway($_SESSION['return_url']);
146                 }
147
148                 $cmd = $a->argv[1];
149                 if ($cmd === 'drop') {
150                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
151                                 intval($a->argv[2]),
152                                 intval(local_user())
153                         );
154                         if ($r) {
155                                 info(L10n::t('Message deleted.') . EOL);
156                         }
157                         //goaway(System::baseUrl(true) . '/message' );
158                         goaway($_SESSION['return_url']);
159                 } else {
160                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
161                                 intval($a->argv[2]),
162                                 intval(local_user())
163                         );
164                         if (DBM::is_result($r)) {
165                                 $parent = $r[0]['parent-uri'];
166                                 $convid = $r[0]['convid'];
167
168                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
169                                         dbesc($parent),
170                                         intval(local_user())
171                                 );
172
173                                 // remove diaspora conversation pointer
174                                 // Actually if we do this, we can never receive another reply to that conversation,
175                                 // as we will never again have the info we need to re-create it.
176                                 // We'll just have to orphan it.
177                                 //if ($convid) {
178                                 //      q("delete from conv where id = %d limit 1",
179                                 //              intval($convid)
180                                 //      );
181                                 //}
182
183                                 if ($r) {
184                                         info(L10n::t('Conversation removed.') . EOL);
185                                 }
186                         }
187                         //goaway(System::baseUrl(true) . '/message' );
188                         goaway($_SESSION['return_url']);
189                 }
190         }
191
192         if (($a->argc > 1) && ($a->argv[1] === 'new')) {
193                 $o .= $header;
194
195                 $tpl = get_markup_template('msg-header.tpl');
196                 $a->page['htmlhead'] .= replace_macros($tpl, [
197                         '$baseurl' => System::baseUrl(true),
198                         '$nickname' => $a->user['nickname'],
199                         '$linkurl' => L10n::t('Please enter a link URL:')
200                 ]);
201
202                 $tpl = get_markup_template('msg-end.tpl');
203                 $a->page['end'] .= replace_macros($tpl, [
204                         '$baseurl' => System::baseUrl(true),
205                         '$nickname' => $a->user['nickname'],
206                         '$linkurl' => L10n::t('Please enter a link URL:')
207                 ]);
208
209                 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : false;
210
211                 $prename = $preurl = $preid = '';
212
213                 if ($preselect) {
214                         $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
215                                 intval(local_user()),
216                                 intval($a->argv[2])
217                         );
218                         if (!DBM::is_result($r)) {
219                                 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
220                                         intval(local_user()),
221                                         dbesc(normalise_link(base64_decode($a->argv[2])))
222                                 );
223                         }
224
225                         if (!DBM::is_result($r)) {
226                                 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
227                                         intval(local_user()),
228                                         dbesc(base64_decode($a->argv[2]))
229                                 );
230                         }
231
232                         if (DBM::is_result($r)) {
233                                 $prename = $r[0]['name'];
234                                 $preurl = $r[0]['url'];
235                                 $preid = $r[0]['id'];
236                                 $preselect = [$preid];
237                         } else {
238                                 $preselect = false;
239                         }
240                 }
241
242                 $prefill = $preselect ? $prename : '';
243
244                 // the ugly select box
245                 $select = contact_select('messageto', 'message-to-select', $preselect, 4, true, false, false, 10);
246
247                 $tpl = get_markup_template('prv_message.tpl');
248                 $o .= replace_macros($tpl, [
249                         '$header' => L10n::t('Send Private Message'),
250                         '$to' => L10n::t('To:'),
251                         '$showinputs' => 'true',
252                         '$prefill' => $prefill,
253                         '$autocomp' => $autocomp,
254                         '$preid' => $preid,
255                         '$subject' => L10n::t('Subject:'),
256                         '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
257                         '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
258                         '$readonly' => '',
259                         '$yourmessage' => L10n::t('Your message:'),
260                         '$select' => $select,
261                         '$parent' => '',
262                         '$upload' => L10n::t('Upload photo'),
263                         '$insert' => L10n::t('Insert web link'),
264                         '$wait' => L10n::t('Please wait'),
265                         '$submit' => L10n::t('Submit')
266                 ]);
267                 return $o;
268         }
269
270
271         $_SESSION['return_url'] = $a->query_string;
272
273         if ($a->argc == 1) {
274
275                 // List messages
276
277                 $o .= $header;
278
279                 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
280                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
281                         intval(local_user())
282                 );
283
284                 if (DBM::is_result($r)) {
285                         $a->set_pager_total($r[0]['total']);
286                 }
287
288                 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
289
290                 if (!DBM::is_result($r)) {
291                         info(L10n::t('No messages.') . EOL);
292                         return $o;
293                 }
294
295                 $o .= render_messages($r, 'mail_list.tpl');
296
297                 $o .= paginate($a);
298
299                 return $o;
300         }
301
302         if (($a->argc > 1) && (intval($a->argv[1]))) {
303
304                 $o .= $header;
305
306                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
307                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
308                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
309                         intval(local_user()),
310                         intval($a->argv[1])
311                 );
312                 if (DBM::is_result($r)) {
313                         $contact_id = $r[0]['contact-id'];
314                         $convid = $r[0]['convid'];
315
316                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
317                         if ($convid)
318                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
319                                         dbesc($r[0]['parent-uri']),
320                                         intval($convid)
321                                 );
322
323                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
324                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
325                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
326                                 intval(local_user())
327                         );
328                 }
329                 if (!count($messages)) {
330                         notice(L10n::t('Message not available.') . EOL);
331                         return $o;
332                 }
333
334                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
335                         dbesc($r[0]['parent-uri']),
336                         intval(local_user())
337                 );
338
339                 require_once("include/bbcode.php");
340
341                 $tpl = get_markup_template('msg-header.tpl');
342                 $a->page['htmlhead'] .= replace_macros($tpl, [
343                         '$baseurl' => System::baseUrl(true),
344                         '$nickname' => $a->user['nickname'],
345                         '$linkurl' => L10n::t('Please enter a link URL:')
346                 ]);
347
348                 $tpl = get_markup_template('msg-end.tpl');
349                 $a->page['end'] .= replace_macros($tpl, [
350                         '$baseurl' => System::baseUrl(true),
351                         '$nickname' => $a->user['nickname'],
352                         '$linkurl' => L10n::t('Please enter a link URL:')
353                 ]);
354
355                 $mails = [];
356                 $seen = 0;
357                 $unknown = false;
358
359                 foreach ($messages as $message) {
360                         if ($message['unknown'])
361                                 $unknown = true;
362                         if ($message['from-url'] == $myprofile) {
363                                 $from_url = $myprofile;
364                                 $sparkle = '';
365                         } elseif ($message['contact-id'] != 0) {
366                                 $from_url = 'redir/' . $message['contact-id'];
367                                 $sparkle = ' sparkle';
368                         } else {
369                                 $from_url = $message['from-url'] . "?zrl=" . urlencode($myprofile);
370                                 $sparkle = ' sparkle';
371                         }
372
373                         $extracted = item_extract_images($message['body']);
374                         if ($extracted['images']) {
375                                 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
376                         }
377
378                         $from_name_e = $message['from-name'];
379                         $subject_e = $message['title'];
380                         $body_e = Smilies::replace(bbcode($message['body']));
381                         $to_name_e = $message['name'];
382
383                         $contact = Contact::getDetailsByURL($message['from-url']);
384                         if (isset($contact["thumb"])) {
385                                 $from_photo = $contact["thumb"];
386                         } else {
387                                 $from_photo = $message['from-photo'];
388                         }
389
390                         $mails[] = [
391                                 'id' => $message['id'],
392                                 'from_name' => $from_name_e,
393                                 'from_url' => $from_url,
394                                 'from_addr' => $contact['addr'],
395                                 'sparkle' => $sparkle,
396                                 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
397                                 'subject' => $subject_e,
398                                 'body' => $body_e,
399                                 'delete' => L10n::t('Delete message'),
400                                 'to_name' => $to_name_e,
401                                 'date' => DateTimeFormat::local($message['created'], 'D, d M Y - g:i A'),
402                                 'ago' => Temporal::getRelativeDate($message['created']),
403                         ];
404
405                         $seen = $message['seen'];
406                 }
407
408                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
409                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
410
411                 $tpl = get_markup_template('mail_display.tpl');
412                 $o = replace_macros($tpl, [
413                         '$thread_id' => $a->argv[1],
414                         '$thread_subject' => $message['title'],
415                         '$thread_seen' => $seen,
416                         '$delete' => L10n::t('Delete conversation'),
417                         '$canreply' => (($unknown) ? false : '1'),
418                         '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
419                         '$mails' => $mails,
420
421                         // reply
422                         '$header' => L10n::t('Send Reply'),
423                         '$to' => L10n::t('To:'),
424                         '$showinputs' => '',
425                         '$subject' => L10n::t('Subject:'),
426                         '$subjtxt' => $message['title'],
427                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
428                         '$yourmessage' => L10n::t('Your message:'),
429                         '$text' => '',
430                         '$select' => $select,
431                         '$parent' => $parent,
432                         '$upload' => L10n::t('Upload photo'),
433                         '$insert' => L10n::t('Insert web link'),
434                         '$submit' => L10n::t('Submit'),
435                         '$wait' => L10n::t('Please wait')
436                 ]);
437
438                 return $o;
439         }
440 }
441
442 function get_messages($user, $lstart, $lend)
443 {
444         //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
445         return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
446                 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
447                 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
448                 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
449                 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
450                 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
451                 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
452                 `mail`.`parent-uri`,
453                 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
454                 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
455                 count( * ) as `count`
456                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
457                 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
458                 intval($user), intval($lstart), intval($lend)
459         );
460 }
461
462 function render_messages(array $msg, $t)
463 {
464         $a = get_app();
465
466         $tpl = get_markup_template($t);
467         $rslt = '';
468
469         $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
470
471         foreach ($msg as $rr) {
472                 if ($rr['unknown']) {
473                         $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
474                 } elseif (link_compare($rr['from-url'], $myprofile)) {
475                         $participants = L10n::t("You and %s", $rr['name']);
476                 } else {
477                         $participants = L10n::t("%s and You", $rr['from-name']);
478                 }
479
480                 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
481                 $body_e = $rr['body'];
482                 $to_name_e = $rr['name'];
483
484                 $contact = Contact::getDetailsByURL($rr['url']);
485                 if (isset($contact["thumb"])) {
486                         $from_photo = $contact["thumb"];
487                 } else {
488                         $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
489                 }
490
491                 $rslt .= replace_macros($tpl, [
492                         '$id' => $rr['id'],
493                         '$from_name' => $participants,
494                         '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
495                         '$from_addr' => $contact['addr'],
496                         '$sparkle' => ' sparkle',
497                         '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
498                         '$subject' => $subject_e,
499                         '$delete' => L10n::t('Delete conversation'),
500                         '$body' => $body_e,
501                         '$to_name' => $to_name_e,
502                         '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
503                         '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
504                         '$seen' => $rr['mailseen'],
505                         '$count' => L10n::tt('%d message', '%d messages', $rr['count']),
506                 ]);
507         }
508
509         return $rslt;
510 }