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