]> git.mxchange.org Git - friendica.git/blob - mod/message.php
Fixes:
[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\Database\DBM;
15 use Friendica\Model\Contact;
16 use Friendica\Model\Mail;
17 use Friendica\Util\DateTimeFormat;
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($_SESSION['return_url']);
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($_SESSION['return_url']);
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(System::baseUrl(true) . '/message' );
173                         goaway($_SESSION['return_url']);
174                 } else {
175                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
176                                 intval($a->argv[2]),
177                                 intval(local_user())
178                         );
179                         if (DBM::is_result($r)) {
180                                 $parent = $r[0]['parent-uri'];
181                                 $convid = $r[0]['convid'];
182
183                                 if (DBA::delete('mail', ['parent-uri' => $parent, 'uid' => local_user()])) {
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]] : [];
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 = [];
239                         }
240                 }
241
242                 $prefill = $preselect ? $prename : '';
243
244                 // the ugly select box
245                 $select = ACL::getMessageContactSelectHTML('messageto', 'message-to-select', $preselect, 4, 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                         '$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                 } else {
328                         $messages = false;
329                 }
330                 if (!DBM::is_result($messages)) {
331                         notice(L10n::t('Message not available.') . EOL);
332                         return $o;
333                 }
334
335                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
336                         dbesc($r[0]['parent-uri']),
337                         intval(local_user())
338                 );
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                         } else {
365                                 $from_url = Contact::magicLink($message['from-url']);
366                                 $sparkle = ' sparkle';
367                         }
368
369                         $extracted = item_extract_images($message['body']);
370                         if ($extracted['images']) {
371                                 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
372                         }
373
374                         $from_name_e = $message['from-name'];
375                         $subject_e = $message['title'];
376                         $body_e = Smilies::replace(BBCode::convert($message['body']));
377                         $to_name_e = $message['name'];
378
379                         $contact = Contact::getDetailsByURL($message['from-url']);
380                         if (isset($contact["thumb"])) {
381                                 $from_photo = $contact["thumb"];
382                         } else {
383                                 $from_photo = $message['from-photo'];
384                         }
385
386                         $mails[] = [
387                                 'id' => $message['id'],
388                                 'from_name' => $from_name_e,
389                                 'from_url' => $from_url,
390                                 'from_addr' => $contact['addr'],
391                                 'sparkle' => $sparkle,
392                                 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
393                                 'subject' => $subject_e,
394                                 'body' => $body_e,
395                                 'delete' => L10n::t('Delete message'),
396                                 'to_name' => $to_name_e,
397                                 'date' => DateTimeFormat::local($message['created'], L10n::t('D, d M Y - g:i A')),
398                                 'ago' => Temporal::getRelativeDate($message['created']),
399                         ];
400
401                         $seen = $message['seen'];
402                 }
403
404                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
405                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
406
407                 $tpl = get_markup_template('mail_display.tpl');
408                 $o = replace_macros($tpl, [
409                         '$thread_id' => $a->argv[1],
410                         '$thread_subject' => $message['title'],
411                         '$thread_seen' => $seen,
412                         '$delete' => L10n::t('Delete conversation'),
413                         '$canreply' => (($unknown) ? false : '1'),
414                         '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
415                         '$mails' => $mails,
416
417                         // reply
418                         '$header' => L10n::t('Send Reply'),
419                         '$to' => L10n::t('To:'),
420                         '$showinputs' => '',
421                         '$subject' => L10n::t('Subject:'),
422                         '$subjtxt' => $message['title'],
423                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
424                         '$yourmessage' => L10n::t('Your message:'),
425                         '$text' => '',
426                         '$select' => $select,
427                         '$parent' => $parent,
428                         '$upload' => L10n::t('Upload photo'),
429                         '$insert' => L10n::t('Insert web link'),
430                         '$submit' => L10n::t('Submit'),
431                         '$wait' => L10n::t('Please wait')
432                 ]);
433
434                 return $o;
435         }
436 }
437
438 function get_messages($user, $lstart, $lend)
439 {
440         //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
441         return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
442                 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
443                 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
444                 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
445                 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
446                 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
447                 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
448                 `mail`.`parent-uri`,
449                 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
450                 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
451                 count( * ) as `count`
452                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
453                 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
454                 intval($user), intval($lstart), intval($lend)
455         );
456 }
457
458 function render_messages(array $msg, $t)
459 {
460         $a = get_app();
461
462         $tpl = get_markup_template($t);
463         $rslt = '';
464
465         $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
466
467         foreach ($msg as $rr) {
468                 if ($rr['unknown']) {
469                         $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
470                 } elseif (link_compare($rr['from-url'], $myprofile)) {
471                         $participants = L10n::t("You and %s", $rr['name']);
472                 } else {
473                         $participants = L10n::t("%s and You", $rr['from-name']);
474                 }
475
476                 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
477                 $body_e = $rr['body'];
478                 $to_name_e = $rr['name'];
479
480                 $contact = Contact::getDetailsByURL($rr['url']);
481                 if (isset($contact["thumb"])) {
482                         $from_photo = $contact["thumb"];
483                 } else {
484                         $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
485                 }
486
487                 $rslt .= replace_macros($tpl, [
488                         '$id' => $rr['id'],
489                         '$from_name' => $participants,
490                         '$from_url' => Contact::magicLink($rr['url']),
491                         '$from_addr' => $contact['addr'],
492                         '$sparkle' => ' sparkle',
493                         '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
494                         '$subject' => $subject_e,
495                         '$delete' => L10n::t('Delete conversation'),
496                         '$body' => $body_e,
497                         '$to_name' => $to_name_e,
498                         '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
499                         '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
500                         '$seen' => $rr['mailseen'],
501                         '$count' => L10n::tt('%d message', '%d messages', $rr['count']),
502                 ]);
503         }
504
505         return $rslt;
506 }