]> git.mxchange.org Git - friendica.git/blob - mod/message.php
fbb1391699490b056ad77032132f5d479dc6e1e7
[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\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         if ($a->argc > 1 && $a->argv[1] == 'new') {
112                 $button = [
113                         'label' => L10n::t('Discard'),
114                         'url' => '/message',
115                         'sel' => 'close',
116                 ];
117         } else {
118                 $button = [
119                         'label' => L10n::t('New Message'),
120                         'url' => '/message/new',
121                         'sel' => 'new',
122                         'accesskey' => 'm',
123                 ];
124         }
125         $header = replace_macros($tpl, [
126                 '$messages' => L10n::t('Messages'),
127                 '$button' => $button,
128         ]);
129
130         if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
131                 if (!intval($a->argv[2])) {
132                         return;
133                 }
134
135                 // Check if we should do HTML-based delete confirmation
136                 if (!empty($_REQUEST['confirm'])) {
137                         // <form> can't take arguments in its "action" parameter
138                         // so add any arguments as hidden inputs
139                         $query = explode_querystring($a->query_string);
140                         $inputs = [];
141                         foreach ($query['args'] as $arg) {
142                                 if (strpos($arg, 'confirm=') === false) {
143                                         $arg_parts = explode('=', $arg);
144                                         $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
145                                 }
146                         }
147
148                         //$a->page['aside'] = '';
149                         return replace_macros(get_markup_template('confirm.tpl'), [
150                                 '$method' => 'get',
151                                 '$message' => L10n::t('Do you really want to delete this message?'),
152                                 '$extra_inputs' => $inputs,
153                                 '$confirm' => L10n::t('Yes'),
154                                 '$confirm_url' => $query['base'],
155                                 '$confirm_name' => 'confirmed',
156                                 '$cancel' => L10n::t('Cancel'),
157                         ]);
158                 }
159
160                 // Now check how the user responded to the confirmation query
161                 if (!empty($_REQUEST['canceled'])) {
162                         goaway($_SESSION['return_url']);
163                 }
164
165                 $cmd = $a->argv[1];
166                 if ($cmd === 'drop') {
167                         if (DBA::delete('mail', ['id' => $a->argv[2], 'uid' => local_user()])) {
168                                 info(L10n::t('Message deleted.') . EOL);
169                         }
170
171                         //goaway(System::baseUrl(true) . '/message' );
172                         goaway($_SESSION['return_url']);
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(System::baseUrl(true) . '/message' );
187                         goaway($_SESSION['return_url']);
188                 }
189         }
190
191         if (($a->argc > 1) && ($a->argv[1] === 'new')) {
192                 $o .= $header;
193
194                 $tpl = get_markup_template('msg-header.tpl');
195                 $a->page['htmlhead'] .= replace_macros($tpl, [
196                         '$baseurl' => System::baseUrl(true),
197                         '$nickname' => $a->user['nickname'],
198                         '$linkurl' => L10n::t('Please enter a link URL:')
199                 ]);
200
201                 $tpl = get_markup_template('msg-end.tpl');
202                 $a->page['end'] .= replace_macros($tpl, [
203                         '$baseurl' => System::baseUrl(true),
204                         '$nickname' => $a->user['nickname'],
205                         '$linkurl' => L10n::t('Please enter a link URL:')
206                 ]);
207
208                 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : [];
209
210                 $prename = $preurl = $preid = '';
211
212                 if ($preselect) {
213                         $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
214                                 intval(local_user()),
215                                 intval($a->argv[2])
216                         );
217                         if (!DBA::isResult($r)) {
218                                 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
219                                         intval(local_user()),
220                                         dbesc(normalise_link(base64_decode($a->argv[2])))
221                                 );
222                         }
223
224                         if (!DBA::isResult($r)) {
225                                 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
226                                         intval(local_user()),
227                                         dbesc(base64_decode($a->argv[2]))
228                                 );
229                         }
230
231                         if (DBA::isResult($r)) {
232                                 $prename = $r[0]['name'];
233                                 $preurl = $r[0]['url'];
234                                 $preid = $r[0]['id'];
235                                 $preselect = [$preid];
236                         } else {
237                                 $preselect = [];
238                         }
239                 }
240
241                 $prefill = $preselect ? $prename : '';
242
243                 // the ugly select box
244                 $select = ACL::getMessageContactSelectHTML('messageto', 'message-to-select', $preselect, 4, 10);
245
246                 $tpl = get_markup_template('prv_message.tpl');
247                 $o .= replace_macros($tpl, [
248                         '$header' => L10n::t('Send Private Message'),
249                         '$to' => L10n::t('To:'),
250                         '$showinputs' => 'true',
251                         '$prefill' => $prefill,
252                         '$preid' => $preid,
253                         '$subject' => L10n::t('Subject:'),
254                         '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
255                         '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
256                         '$readonly' => '',
257                         '$yourmessage' => L10n::t('Your message:'),
258                         '$select' => $select,
259                         '$parent' => '',
260                         '$upload' => L10n::t('Upload photo'),
261                         '$insert' => L10n::t('Insert web link'),
262                         '$wait' => L10n::t('Please wait'),
263                         '$submit' => L10n::t('Submit')
264                 ]);
265                 return $o;
266         }
267
268
269         $_SESSION['return_url'] = $a->query_string;
270
271         if ($a->argc == 1) {
272
273                 // List messages
274
275                 $o .= $header;
276
277                 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
278                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
279                         intval(local_user())
280                 );
281
282                 if (DBA::isResult($r)) {
283                         $a->set_pager_total($r[0]['total']);
284                 }
285
286                 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
287
288                 if (!DBA::isResult($r)) {
289                         info(L10n::t('No messages.') . EOL);
290                         return $o;
291                 }
292
293                 $o .= render_messages($r, 'mail_list.tpl');
294
295                 $o .= paginate($a);
296
297                 return $o;
298         }
299
300         if (($a->argc > 1) && (intval($a->argv[1]))) {
301
302                 $o .= $header;
303
304                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
305                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
306                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
307                         intval(local_user()),
308                         intval($a->argv[1])
309                 );
310                 if (DBA::isResult($r)) {
311                         $contact_id = $r[0]['contact-id'];
312                         $convid = $r[0]['convid'];
313
314                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
315                         if ($convid)
316                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
317                                         dbesc($r[0]['parent-uri']),
318                                         intval($convid)
319                                 );
320
321                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
322                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
323                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
324                                 intval(local_user())
325                         );
326                 } else {
327                         $messages = false;
328                 }
329                 if (!DBA::isResult($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                         } else {
364                                 $from_url = Contact::magicLink($message['from-url']);
365                                 $sparkle = ' sparkle';
366                         }
367
368                         $extracted = item_extract_images($message['body']);
369                         if ($extracted['images']) {
370                                 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
371                         }
372
373                         $from_name_e = $message['from-name'];
374                         $subject_e = $message['title'];
375                         $body_e = Smilies::replace(BBCode::convert($message['body']));
376                         $to_name_e = $message['name'];
377
378                         $contact = Contact::getDetailsByURL($message['from-url']);
379                         if (isset($contact["thumb"])) {
380                                 $from_photo = $contact["thumb"];
381                         } else {
382                                 $from_photo = $message['from-photo'];
383                         }
384
385                         $mails[] = [
386                                 'id' => $message['id'],
387                                 'from_name' => $from_name_e,
388                                 'from_url' => $from_url,
389                                 'from_addr' => $contact['addr'],
390                                 'sparkle' => $sparkle,
391                                 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
392                                 'subject' => $subject_e,
393                                 'body' => $body_e,
394                                 'delete' => L10n::t('Delete message'),
395                                 'to_name' => $to_name_e,
396                                 'date' => DateTimeFormat::local($message['created'], L10n::t('D, d M Y - g:i A')),
397                                 'ago' => Temporal::getRelativeDate($message['created']),
398                         ];
399
400                         $seen = $message['seen'];
401                 }
402
403                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
404                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
405
406                 $tpl = get_markup_template('mail_display.tpl');
407                 $o = replace_macros($tpl, [
408                         '$thread_id' => $a->argv[1],
409                         '$thread_subject' => $message['title'],
410                         '$thread_seen' => $seen,
411                         '$delete' => L10n::t('Delete conversation'),
412                         '$canreply' => (($unknown) ? false : '1'),
413                         '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
414                         '$mails' => $mails,
415
416                         // reply
417                         '$header' => L10n::t('Send Reply'),
418                         '$to' => L10n::t('To:'),
419                         '$showinputs' => '',
420                         '$subject' => L10n::t('Subject:'),
421                         '$subjtxt' => $message['title'],
422                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
423                         '$yourmessage' => L10n::t('Your message:'),
424                         '$text' => '',
425                         '$select' => $select,
426                         '$parent' => $parent,
427                         '$upload' => L10n::t('Upload photo'),
428                         '$insert' => L10n::t('Insert web link'),
429                         '$submit' => L10n::t('Submit'),
430                         '$wait' => L10n::t('Please wait')
431                 ]);
432
433                 return $o;
434         }
435 }
436
437 function get_messages($user, $lstart, $lend)
438 {
439         //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
440         return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
441                 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
442                 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
443                 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
444                 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
445                 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
446                 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
447                 `mail`.`parent-uri`,
448                 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
449                 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
450                 count( * ) as `count`
451                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
452                 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
453                 intval($user), intval($lstart), intval($lend)
454         );
455 }
456
457 function render_messages(array $msg, $t)
458 {
459         $a = get_app();
460
461         $tpl = get_markup_template($t);
462         $rslt = '';
463
464         $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
465
466         foreach ($msg as $rr) {
467                 if ($rr['unknown']) {
468                         $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
469                 } elseif (link_compare($rr['from-url'], $myprofile)) {
470                         $participants = L10n::t("You and %s", $rr['name']);
471                 } else {
472                         $participants = L10n::t("%s and You", $rr['from-name']);
473                 }
474
475                 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
476                 $body_e = $rr['body'];
477                 $to_name_e = $rr['name'];
478
479                 $contact = Contact::getDetailsByURL($rr['url']);
480                 if (isset($contact["thumb"])) {
481                         $from_photo = $contact["thumb"];
482                 } else {
483                         $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
484                 }
485
486                 $rslt .= replace_macros($tpl, [
487                         '$id' => $rr['id'],
488                         '$from_name' => $participants,
489                         '$from_url' => Contact::magicLink($rr['url']),
490                         '$from_addr' => $contact['addr'],
491                         '$sparkle' => ' sparkle',
492                         '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
493                         '$subject' => $subject_e,
494                         '$delete' => L10n::t('Delete conversation'),
495                         '$body' => $body_e,
496                         '$to_name' => $to_name_e,
497                         '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
498                         '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
499                         '$seen' => $rr['mailseen'],
500                         '$count' => L10n::tt('%d message', '%d messages', $rr['count']),
501                 ]);
502         }
503
504         return $rslt;
505 }