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