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