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