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