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