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