]> git.mxchange.org Git - friendica.git/blob - mod/message.php
Use short form array syntax everywhere
[friendica.git] / mod / message.php
1 <?php
2 /**
3  * @file mod/message.php
4  */
5 use Friendica\App;
6 use Friendica\Content\Smilies;
7 use Friendica\Core\System;
8 use Friendica\Database\DBM;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Mail;
11
12 require_once 'include/acl_selectors.php';
13 require_once 'include/conversation.php';
14
15 function message_init(App $a)
16 {
17         $tabs = '';
18
19         if ($a->argc > 1 && is_numeric($a->argv[1])) {
20                 $tabs = render_messages(get_messages(local_user(), 0, 5), 'mail_list.tpl');
21         }
22
23         $new = [
24                 'label' => t('New Message'),
25                 'url' => 'message/new',
26                 'sel' => $a->argc > 1 && $a->argv[1] == 'new',
27                 'accesskey' => 'm',
28         ];
29
30         $tpl = get_markup_template('message_side.tpl');
31         $a->page['aside'] = replace_macros($tpl, [
32                 '$tabs' => $tabs,
33                 '$new' => $new,
34         ]);
35         $base = System::baseUrl();
36
37         $head_tpl = get_markup_template('message-head.tpl');
38         $a->page['htmlhead'] .= replace_macros($head_tpl, [
39                 '$baseurl' => System::baseUrl(true),
40                 '$base' => $base
41         ]);
42
43         $end_tpl = get_markup_template('message-end.tpl');
44         $a->page['end'] .= replace_macros($end_tpl, [
45                 '$baseurl' => System::baseUrl(true),
46                 '$base' => $base
47         ]);
48 }
49
50 function message_post(App $a)
51 {
52         if (!local_user()) {
53                 notice(t('Permission denied.') . EOL);
54                 return;
55         }
56
57         $replyto   = x($_REQUEST, 'replyto')   ? notags(trim($_REQUEST['replyto']))   : '';
58         $subject   = x($_REQUEST, 'subject')   ? notags(trim($_REQUEST['subject']))   : '';
59         $body      = x($_REQUEST, 'body')      ? escape_tags(trim($_REQUEST['body'])) : '';
60         $recipient = x($_REQUEST, 'messageto') ? intval($_REQUEST['messageto'])       : 0;
61
62         $ret = Mail::send($recipient, $body, $subject, $replyto);
63         $norecip = false;
64
65         switch ($ret) {
66                 case -1:
67                         notice(t('No recipient selected.') . EOL);
68                         $norecip = true;
69                         break;
70                 case -2:
71                         notice(t('Unable to locate contact information.') . EOL);
72                         break;
73                 case -3:
74                         notice(t('Message could not be sent.') . EOL);
75                         break;
76                 case -4:
77                         notice(t('Message collection failure.') . EOL);
78                         break;
79                 default:
80                         info(t('Message sent.') . EOL);
81         }
82
83         // fake it to go back to the input form if no recipient listed
84         if ($norecip) {
85                 $a->argc = 2;
86                 $a->argv[1] = 'new';
87         } else {
88                 goaway($_SESSION['return_url']);
89         }
90 }
91
92 function message_content(App $a)
93 {
94         $o = '';
95         nav_set_selected('messages');
96
97         if (!local_user()) {
98                 notice(t('Permission denied.') . EOL);
99                 return;
100         }
101
102         $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
103
104         $tpl = get_markup_template('mail_head.tpl');
105         $header = replace_macros($tpl, [
106                 '$messages' => t('Messages'),
107         ]);
108
109         if (($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
110                 if (!intval($a->argv[2])) {
111                         return;
112                 }
113
114                 // Check if we should do HTML-based delete confirmation
115                 if ($_REQUEST['confirm']) {
116                         // <form> can't take arguments in its "action" parameter
117                         // so add any arguments as hidden inputs
118                         $query = explode_querystring($a->query_string);
119                         $inputs = [];
120                         foreach ($query['args'] as $arg) {
121                                 if (strpos($arg, 'confirm=') === false) {
122                                         $arg_parts = explode('=', $arg);
123                                         $inputs[] = ['name' => $arg_parts[0], 'value' => $arg_parts[1]];
124                                 }
125                         }
126
127                         //$a->page['aside'] = '';
128                         return replace_macros(get_markup_template('confirm.tpl'), [
129                                 '$method' => 'get',
130                                 '$message' => t('Do you really want to delete this message?'),
131                                 '$extra_inputs' => $inputs,
132                                 '$confirm' => t('Yes'),
133                                 '$confirm_url' => $query['base'],
134                                 '$confirm_name' => 'confirmed',
135                                 '$cancel' => t('Cancel'),
136                         ]);
137                 }
138                 // Now check how the user responded to the confirmation query
139                 if ($_REQUEST['canceled']) {
140                         goaway($_SESSION['return_url']);
141                 }
142
143                 $cmd = $a->argv[1];
144                 if ($cmd === 'drop') {
145                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
146                                 intval($a->argv[2]),
147                                 intval(local_user())
148                         );
149                         if ($r) {
150                                 info(t('Message deleted.') . EOL);
151                         }
152                         //goaway(System::baseUrl(true) . '/message' );
153                         goaway($_SESSION['return_url']);
154                 } else {
155                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
156                                 intval($a->argv[2]),
157                                 intval(local_user())
158                         );
159                         if (DBM::is_result($r)) {
160                                 $parent = $r[0]['parent-uri'];
161                                 $convid = $r[0]['convid'];
162
163                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
164                                         dbesc($parent),
165                                         intval(local_user())
166                                 );
167
168                                 // remove diaspora conversation pointer
169                                 // Actually if we do this, we can never receive another reply to that conversation,
170                                 // as we will never again have the info we need to re-create it.
171                                 // We'll just have to orphan it.
172                                 //if ($convid) {
173                                 //      q("delete from conv where id = %d limit 1",
174                                 //              intval($convid)
175                                 //      );
176                                 //}
177
178                                 if ($r) {
179                                         info(t('Conversation removed.') . EOL);
180                                 }
181                         }
182                         //goaway(System::baseUrl(true) . '/message' );
183                         goaway($_SESSION['return_url']);
184                 }
185         }
186
187         if (($a->argc > 1) && ($a->argv[1] === 'new')) {
188                 $o .= $header;
189
190                 $tpl = get_markup_template('msg-header.tpl');
191                 $a->page['htmlhead'] .= replace_macros($tpl, [
192                         '$baseurl' => System::baseUrl(true),
193                         '$nickname' => $a->user['nickname'],
194                         '$linkurl' => t('Please enter a link URL:')
195                 ]);
196
197                 $tpl = get_markup_template('msg-end.tpl');
198                 $a->page['end'] .= replace_macros($tpl, [
199                         '$baseurl' => System::baseUrl(true),
200                         '$nickname' => $a->user['nickname'],
201                         '$linkurl' => t('Please enter a link URL:')
202                 ]);
203
204                 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : false;
205
206                 $prename = $preurl = $preid = '';
207
208                 if ($preselect) {
209                         $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
210                                 intval(local_user()),
211                                 intval($a->argv[2])
212                         );
213                         if (!DBM::is_result($r)) {
214                                 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
215                                         intval(local_user()),
216                                         dbesc(normalise_link(base64_decode($a->argv[2])))
217                                 );
218                         }
219
220                         if (!DBM::is_result($r)) {
221                                 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
222                                         intval(local_user()),
223                                         dbesc(base64_decode($a->argv[2]))
224                                 );
225                         }
226
227                         if (DBM::is_result($r)) {
228                                 $prename = $r[0]['name'];
229                                 $preurl = $r[0]['url'];
230                                 $preid = $r[0]['id'];
231                                 $preselect = [$preid];
232                         } else {
233                                 $preselect = false;
234                         }
235                 }
236
237                 $prefill = $preselect ? $prename : '';
238
239                 // the ugly select box
240                 $select = contact_select('messageto', 'message-to-select', $preselect, 4, true, false, false, 10);
241
242                 $tpl = get_markup_template('prv_message.tpl');
243                 $o .= replace_macros($tpl, [
244                         '$header' => t('Send Private Message'),
245                         '$to' => t('To:'),
246                         '$showinputs' => 'true',
247                         '$prefill' => $prefill,
248                         '$autocomp' => $autocomp,
249                         '$preid' => $preid,
250                         '$subject' => t('Subject:'),
251                         '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
252                         '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
253                         '$readonly' => '',
254                         '$yourmessage' => t('Your message:'),
255                         '$select' => $select,
256                         '$parent' => '',
257                         '$upload' => t('Upload photo'),
258                         '$insert' => t('Insert web link'),
259                         '$wait' => t('Please wait'),
260                         '$submit' => t('Submit')
261                 ]);
262                 return $o;
263         }
264
265
266         $_SESSION['return_url'] = $a->query_string;
267
268         if ($a->argc == 1) {
269
270                 // List messages
271
272                 $o .= $header;
273
274                 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
275                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
276                         intval(local_user())
277                 );
278
279                 if (DBM::is_result($r)) {
280                         $a->set_pager_total($r[0]['total']);
281                 }
282
283                 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
284
285                 if (!DBM::is_result($r)) {
286                         info(t('No messages.') . EOL);
287                         return $o;
288                 }
289
290                 $o .= render_messages($r, 'mail_list.tpl');
291
292                 $o .= paginate($a);
293
294                 return $o;
295         }
296
297         if (($a->argc > 1) && (intval($a->argv[1]))) {
298
299                 $o .= $header;
300
301                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
302                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
303                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
304                         intval(local_user()),
305                         intval($a->argv[1])
306                 );
307                 if (DBM::is_result($r)) {
308                         $contact_id = $r[0]['contact-id'];
309                         $convid = $r[0]['convid'];
310
311                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
312                         if ($convid)
313                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
314                                         dbesc($r[0]['parent-uri']),
315                                         intval($convid)
316                                 );
317
318                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
319                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
320                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
321                                 intval(local_user())
322                         );
323                 }
324                 if (!count($messages)) {
325                         notice(t('Message not available.') . EOL);
326                         return $o;
327                 }
328
329                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
330                         dbesc($r[0]['parent-uri']),
331                         intval(local_user())
332                 );
333
334                 require_once("include/bbcode.php");
335
336                 $tpl = get_markup_template('msg-header.tpl');
337                 $a->page['htmlhead'] .= replace_macros($tpl, [
338                         '$baseurl' => System::baseUrl(true),
339                         '$nickname' => $a->user['nickname'],
340                         '$linkurl' => t('Please enter a link URL:')
341                 ]);
342
343                 $tpl = get_markup_template('msg-end.tpl');
344                 $a->page['end'] .= replace_macros($tpl, [
345                         '$baseurl' => System::baseUrl(true),
346                         '$nickname' => $a->user['nickname'],
347                         '$linkurl' => t('Please enter a link URL:')
348                 ]);
349
350                 $mails = [];
351                 $seen = 0;
352                 $unknown = false;
353
354                 foreach ($messages as $message) {
355                         if ($message['unknown'])
356                                 $unknown = true;
357                         if ($message['from-url'] == $myprofile) {
358                                 $from_url = $myprofile;
359                                 $sparkle = '';
360                         } elseif ($message['contact-id'] != 0) {
361                                 $from_url = 'redir/' . $message['contact-id'];
362                                 $sparkle = ' sparkle';
363                         } else {
364                                 $from_url = $message['from-url'] . "?zrl=" . urlencode($myprofile);
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($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' => t('Delete message'),
395                                 'to_name' => $to_name_e,
396                                 'date' => datetime_convert('UTC', date_default_timezone_get(), $message['created'], 'D, d M Y - g:i A'),
397                                 'ago' => relative_date($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' => t('Delete conversation'),
412                         '$canreply' => (($unknown) ? false : '1'),
413                         '$unknown_text' => 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' => t('Send Reply'),
418                         '$to' => t('To:'),
419                         '$showinputs' => '',
420                         '$subject' => t('Subject:'),
421                         '$subjtxt' => $message['title'],
422                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
423                         '$yourmessage' => t('Your message:'),
424                         '$text' => '',
425                         '$select' => $select,
426                         '$parent' => $parent,
427                         '$upload' => t('Upload photo'),
428                         '$insert' => t('Insert web link'),
429                         '$submit' => t('Submit'),
430                         '$wait' => 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 = t("Unknown sender - %s", $rr['from-name']);
469                 } elseif (link_compare($rr['from-url'], $myprofile)) {
470                         $participants = t("You and %s", $rr['name']);
471                 } else {
472                         $participants = 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' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $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' => t('Delete conversation'),
495                         '$body' => $body_e,
496                         '$to_name' => $to_name_e,
497                         '$date' => datetime_convert('UTC', date_default_timezone_get(), $rr['mailcreated'], t('D, d M Y - g:i A')),
498                         '$ago' => relative_date($rr['mailcreated']),
499                         '$seen' => $rr['mailseen'],
500                         '$count' => tt('%d message', '%d messages', $rr['count']),
501                 ]);
502         }
503
504         return $rslt;
505 }