]> git.mxchange.org Git - friendica.git/blob - mod/message.php
Fix wrong mode for App since local conf file wasn't present
[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 (!empty($_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
145                 // Now check how the user responded to the confirmation query
146                 if (!empty($_REQUEST['canceled'])) {
147                         goaway($_SESSION['return_url']);
148                 }
149
150                 $cmd = $a->argv[1];
151                 if ($cmd === 'drop') {
152                         if (dba::delete('mail', ['id' => $a->argv[2], 'uid' => local_user()])) {
153                                 info(L10n::t('Message deleted.') . EOL);
154                         }
155
156                         //goaway(System::baseUrl(true) . '/message' );
157                         goaway($_SESSION['return_url']);
158                 } else {
159                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
160                                 intval($a->argv[2]),
161                                 intval(local_user())
162                         );
163                         if (DBM::is_result($r)) {
164                                 $parent = $r[0]['parent-uri'];
165                                 $convid = $r[0]['convid'];
166
167                                 if (dba::delete('mail', ['parent-uri' => $parent, 'uid' => local_user()])) {
168                                         info(L10n::t('Conversation removed.') . EOL);
169                                 }
170                         }
171                         //goaway(System::baseUrl(true) . '/message' );
172                         goaway($_SESSION['return_url']);
173                 }
174         }
175
176         if (($a->argc > 1) && ($a->argv[1] === 'new')) {
177                 $o .= $header;
178
179                 $tpl = get_markup_template('msg-header.tpl');
180                 $a->page['htmlhead'] .= replace_macros($tpl, [
181                         '$baseurl' => System::baseUrl(true),
182                         '$nickname' => $a->user['nickname'],
183                         '$linkurl' => L10n::t('Please enter a link URL:')
184                 ]);
185
186                 $tpl = get_markup_template('msg-end.tpl');
187                 $a->page['end'] .= replace_macros($tpl, [
188                         '$baseurl' => System::baseUrl(true),
189                         '$nickname' => $a->user['nickname'],
190                         '$linkurl' => L10n::t('Please enter a link URL:')
191                 ]);
192
193                 $preselect = isset($a->argv[2]) ? [$a->argv[2]] : [];
194
195                 $prename = $preurl = $preid = '';
196
197                 if ($preselect) {
198                         $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
199                                 intval(local_user()),
200                                 intval($a->argv[2])
201                         );
202                         if (!DBM::is_result($r)) {
203                                 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
204                                         intval(local_user()),
205                                         dbesc(normalise_link(base64_decode($a->argv[2])))
206                                 );
207                         }
208
209                         if (!DBM::is_result($r)) {
210                                 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
211                                         intval(local_user()),
212                                         dbesc(base64_decode($a->argv[2]))
213                                 );
214                         }
215
216                         if (DBM::is_result($r)) {
217                                 $prename = $r[0]['name'];
218                                 $preurl = $r[0]['url'];
219                                 $preid = $r[0]['id'];
220                                 $preselect = [$preid];
221                         } else {
222                                 $preselect = [];
223                         }
224                 }
225
226                 $prefill = $preselect ? $prename : '';
227
228                 // the ugly select box
229                 $select = ACL::getMessageContactSelectHTML('messageto', 'message-to-select', $preselect, 4, 10);
230
231                 $tpl = get_markup_template('prv_message.tpl');
232                 $o .= replace_macros($tpl, [
233                         '$header' => L10n::t('Send Private Message'),
234                         '$to' => L10n::t('To:'),
235                         '$showinputs' => 'true',
236                         '$prefill' => $prefill,
237                         '$preid' => $preid,
238                         '$subject' => L10n::t('Subject:'),
239                         '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '',
240                         '$text' => x($_REQUEST, 'body') ? escape_tags(htmlspecialchars($_REQUEST['body'])) : '',
241                         '$readonly' => '',
242                         '$yourmessage' => L10n::t('Your message:'),
243                         '$select' => $select,
244                         '$parent' => '',
245                         '$upload' => L10n::t('Upload photo'),
246                         '$insert' => L10n::t('Insert web link'),
247                         '$wait' => L10n::t('Please wait'),
248                         '$submit' => L10n::t('Submit')
249                 ]);
250                 return $o;
251         }
252
253
254         $_SESSION['return_url'] = $a->query_string;
255
256         if ($a->argc == 1) {
257
258                 // List messages
259
260                 $o .= $header;
261
262                 $r = q("SELECT count(*) AS `total`, ANY_VALUE(`created`) AS `created` FROM `mail`
263                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
264                         intval(local_user())
265                 );
266
267                 if (DBM::is_result($r)) {
268                         $a->set_pager_total($r[0]['total']);
269                 }
270
271                 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
272
273                 if (!DBM::is_result($r)) {
274                         info(L10n::t('No messages.') . EOL);
275                         return $o;
276                 }
277
278                 $o .= render_messages($r, 'mail_list.tpl');
279
280                 $o .= paginate($a);
281
282                 return $o;
283         }
284
285         if (($a->argc > 1) && (intval($a->argv[1]))) {
286
287                 $o .= $header;
288
289                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
290                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
291                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
292                         intval(local_user()),
293                         intval($a->argv[1])
294                 );
295                 if (DBM::is_result($r)) {
296                         $contact_id = $r[0]['contact-id'];
297                         $convid = $r[0]['convid'];
298
299                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
300                         if ($convid)
301                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
302                                         dbesc($r[0]['parent-uri']),
303                                         intval($convid)
304                                 );
305
306                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
307                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
308                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
309                                 intval(local_user())
310                         );
311                 } else {
312                         $messages = false;
313                 }
314                 if (!DBM::is_result($messages)) {
315                         notice(L10n::t('Message not available.') . EOL);
316                         return $o;
317                 }
318
319                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
320                         dbesc($r[0]['parent-uri']),
321                         intval(local_user())
322                 );
323
324                 $tpl = get_markup_template('msg-header.tpl');
325                 $a->page['htmlhead'] .= replace_macros($tpl, [
326                         '$baseurl' => System::baseUrl(true),
327                         '$nickname' => $a->user['nickname'],
328                         '$linkurl' => L10n::t('Please enter a link URL:')
329                 ]);
330
331                 $tpl = get_markup_template('msg-end.tpl');
332                 $a->page['end'] .= replace_macros($tpl, [
333                         '$baseurl' => System::baseUrl(true),
334                         '$nickname' => $a->user['nickname'],
335                         '$linkurl' => L10n::t('Please enter a link URL:')
336                 ]);
337
338                 $mails = [];
339                 $seen = 0;
340                 $unknown = false;
341
342                 foreach ($messages as $message) {
343                         if ($message['unknown'])
344                                 $unknown = true;
345                         if ($message['from-url'] == $myprofile) {
346                                 $from_url = $myprofile;
347                                 $sparkle = '';
348                         } else {
349                                 $from_url = Contact::magicLink($message['from-url']);
350                                 $sparkle = ' sparkle';
351                         }
352
353                         $extracted = item_extract_images($message['body']);
354                         if ($extracted['images']) {
355                                 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
356                         }
357
358                         $from_name_e = $message['from-name'];
359                         $subject_e = $message['title'];
360                         $body_e = Smilies::replace(BBCode::convert($message['body']));
361                         $to_name_e = $message['name'];
362
363                         $contact = Contact::getDetailsByURL($message['from-url']);
364                         if (isset($contact["thumb"])) {
365                                 $from_photo = $contact["thumb"];
366                         } else {
367                                 $from_photo = $message['from-photo'];
368                         }
369
370                         $mails[] = [
371                                 'id' => $message['id'],
372                                 'from_name' => $from_name_e,
373                                 'from_url' => $from_url,
374                                 'from_addr' => $contact['addr'],
375                                 'sparkle' => $sparkle,
376                                 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
377                                 'subject' => $subject_e,
378                                 'body' => $body_e,
379                                 'delete' => L10n::t('Delete message'),
380                                 'to_name' => $to_name_e,
381                                 'date' => DateTimeFormat::local($message['created'], L10n::t('D, d M Y - g:i A')),
382                                 'ago' => Temporal::getRelativeDate($message['created']),
383                         ];
384
385                         $seen = $message['seen'];
386                 }
387
388                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
389                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
390
391                 $tpl = get_markup_template('mail_display.tpl');
392                 $o = replace_macros($tpl, [
393                         '$thread_id' => $a->argv[1],
394                         '$thread_subject' => $message['title'],
395                         '$thread_seen' => $seen,
396                         '$delete' => L10n::t('Delete conversation'),
397                         '$canreply' => (($unknown) ? false : '1'),
398                         '$unknown_text' => L10n::t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
399                         '$mails' => $mails,
400
401                         // reply
402                         '$header' => L10n::t('Send Reply'),
403                         '$to' => L10n::t('To:'),
404                         '$showinputs' => '',
405                         '$subject' => L10n::t('Subject:'),
406                         '$subjtxt' => $message['title'],
407                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
408                         '$yourmessage' => L10n::t('Your message:'),
409                         '$text' => '',
410                         '$select' => $select,
411                         '$parent' => $parent,
412                         '$upload' => L10n::t('Upload photo'),
413                         '$insert' => L10n::t('Insert web link'),
414                         '$submit' => L10n::t('Submit'),
415                         '$wait' => L10n::t('Please wait')
416                 ]);
417
418                 return $o;
419         }
420 }
421
422 function get_messages($user, $lstart, $lend)
423 {
424         //TODO: rewritte with a sub-query to get the first message of each private thread with certainty
425         return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
426                 ANY_VALUE(`mail`.`id`) AS `id`, ANY_VALUE(`mail`.`uid`) AS `uid`, ANY_VALUE(`mail`.`guid`) AS `guid`,
427                 ANY_VALUE(`mail`.`from-name`) AS `from-name`, ANY_VALUE(`mail`.`from-photo`) AS `from-photo`,
428                 ANY_VALUE(`mail`.`from-url`) AS `from-url`, ANY_VALUE(`mail`.`contact-id`) AS `contact-id`,
429                 ANY_VALUE(`mail`.`convid`) AS `convid`, ANY_VALUE(`mail`.`title`) AS `title`, ANY_VALUE(`mail`.`body`) AS `body`,
430                 ANY_VALUE(`mail`.`seen`) AS `seen`, ANY_VALUE(`mail`.`reply`) AS `reply`, ANY_VALUE(`mail`.`replied`) AS `replied`,
431                 ANY_VALUE(`mail`.`unknown`) AS `unknown`, ANY_VALUE(`mail`.`uri`) AS `uri`,
432                 `mail`.`parent-uri`,
433                 ANY_VALUE(`mail`.`created`) AS `created`, ANY_VALUE(`contact`.`name`) AS `name`, ANY_VALUE(`contact`.`url`) AS `url`,
434                 ANY_VALUE(`contact`.`thumb`) AS `thumb`, ANY_VALUE(`contact`.`network`) AS `network`,
435                 count( * ) as `count`
436                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
437                 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC LIMIT %d , %d ",
438                 intval($user), intval($lstart), intval($lend)
439         );
440 }
441
442 function render_messages(array $msg, $t)
443 {
444         $a = get_app();
445
446         $tpl = get_markup_template($t);
447         $rslt = '';
448
449         $myprofile = System::baseUrl() . '/profile/' . $a->user['nickname'];
450
451         foreach ($msg as $rr) {
452                 if ($rr['unknown']) {
453                         $participants = L10n::t("Unknown sender - %s", $rr['from-name']);
454                 } elseif (link_compare($rr['from-url'], $myprofile)) {
455                         $participants = L10n::t("You and %s", $rr['name']);
456                 } else {
457                         $participants = L10n::t("%s and You", $rr['from-name']);
458                 }
459
460                 $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
461                 $body_e = $rr['body'];
462                 $to_name_e = $rr['name'];
463
464                 $contact = Contact::getDetailsByURL($rr['url']);
465                 if (isset($contact["thumb"])) {
466                         $from_photo = $contact["thumb"];
467                 } else {
468                         $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
469                 }
470
471                 $rslt .= replace_macros($tpl, [
472                         '$id' => $rr['id'],
473                         '$from_name' => $participants,
474                         '$from_url' => Contact::magicLink($rr['url']),
475                         '$from_addr' => $contact['addr'],
476                         '$sparkle' => ' sparkle',
477                         '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
478                         '$subject' => $subject_e,
479                         '$delete' => L10n::t('Delete conversation'),
480                         '$body' => $body_e,
481                         '$to_name' => $to_name_e,
482                         '$date' => DateTimeFormat::local($rr['mailcreated'], L10n::t('D, d M Y - g:i A')),
483                         '$ago' => Temporal::getRelativeDate($rr['mailcreated']),
484                         '$seen' => $rr['mailseen'],
485                         '$count' => L10n::tt('%d message', '%d messages', $rr['count']),
486                 ]);
487         }
488
489         return $rslt;
490 }