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