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