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