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