]> git.mxchange.org Git - friendica.git/blob - mod/message.php
Better implementation of "hidewall"
[friendica.git] / mod / message.php
1 <?php
2
3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
5 require_once('include/Smilies.php');
6
7 function message_init(&$a) {
8
9         $tabs = '';
10
11         if ($a->argc >1 && is_numeric($a->argv[1])) {
12          $tabs = render_messages(get_messages(local_user(),0,5), 'mail_list.tpl');
13         }
14
15         $new = array(
16                 'label' => t('New Message'),
17                 'url' => 'message/new',
18                 'sel'=> ($a->argv[1] == 'new'),
19                 'accesskey' => 'm',
20         );
21
22         $tpl = get_markup_template('message_side.tpl');
23         $a->page['aside'] = replace_macros($tpl, array(
24                 '$tabs'=>$tabs,
25                 '$new'=>$new,
26         ));
27         $base = $a->get_baseurl();
28
29         $head_tpl = get_markup_template('message-head.tpl');
30         $a->page['htmlhead'] .= replace_macros($head_tpl,array(
31                 '$baseurl' => $a->get_baseurl(true),
32                 '$base' => $base
33         ));
34
35         $end_tpl = get_markup_template('message-end.tpl');
36         $a->page['end'] .= replace_macros($end_tpl,array(
37                 '$baseurl' => $a->get_baseurl(true),
38                 '$base' => $base
39         ));
40
41 }
42
43 function message_post(&$a) {
44
45         if(! local_user()) {
46                 notice( t('Permission denied.') . EOL);
47                 return;
48         }
49
50         $replyto   = ((x($_REQUEST,'replyto'))   ? notags(trim($_REQUEST['replyto']))   : '');
51         $subject   = ((x($_REQUEST,'subject'))   ? notags(trim($_REQUEST['subject']))   : '');
52         $body      = ((x($_REQUEST,'body'))      ? escape_tags(trim($_REQUEST['body'])) : '');
53         $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto'])       : 0 );
54
55         // Work around doubled linefeeds in Tinymce 3.5b2
56
57 /*      $plaintext = intval(get_pconfig(local_user(),'system','plaintext') && !feature_enabled(local_user(),'richtext'));
58         if(! $plaintext) {
59                 $body = fix_mce_lf($body);
60         }*/
61         $plaintext = intval(!feature_enabled(local_user(),'richtext'));
62         if(! $plaintext) {
63                 $body = fix_mce_lf($body);
64         }
65
66         $ret = send_message($recipient, $body, $subject, $replyto);
67         $norecip = false;
68
69         switch($ret){
70                 case -1:
71                         notice( t('No recipient selected.') . EOL );
72                         $norecip = true;
73                         break;
74                 case -2:
75                         notice( t('Unable to locate contact information.') . EOL );
76                         break;
77                 case -3:
78                         notice( t('Message could not be sent.') . EOL );
79                         break;
80                 case -4:
81                         notice( t('Message collection failure.') . EOL );
82                         break;
83                 default:
84                         info( t('Message sent.') . EOL );
85         }
86
87         // fake it to go back to the input form if no recipient listed
88
89         if($norecip) {
90                 $a->argc = 2;
91                 $a->argv[1] = 'new';
92         }
93         else
94                 goaway($_SESSION['return_url']);
95
96 }
97
98 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
99 // is identical to the code in include/conversation.php
100 if(! function_exists('item_extract_images')) {
101 function item_extract_images($body) {
102
103         $saved_image = array();
104         $orig_body = $body;
105         $new_body = '';
106
107         $cnt = 0;
108         $img_start = strpos($orig_body, '[img');
109         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
110         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
111         while(($img_st_close !== false) && ($img_end !== false)) {
112
113                 $img_st_close++; // make it point to AFTER the closing bracket
114                 $img_end += $img_start;
115
116                 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
117                         // This is an embedded image
118
119                         $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
120                         $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
121
122                         $cnt++;
123                 }
124                 else
125                         $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
126
127                 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
128
129                 if($orig_body === false) // in case the body ends on a closing image tag
130                         $orig_body = '';
131
132                 $img_start = strpos($orig_body, '[img');
133                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
134                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
135         }
136
137         $new_body = $new_body . $orig_body;
138
139         return array('body' => $new_body, 'images' => $saved_image);
140 }}
141
142 if(! function_exists('item_redir_and_replace_images')) {
143 function item_redir_and_replace_images($body, $images, $cid) {
144
145         $origbody = $body;
146         $newbody = '';
147
148         for($i = 0; $i < count($images); $i++) {
149                 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
150                 $replace = '[url=' . z_path() . '/redir/' . $cid
151                            . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
152
153                 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
154                 $process_part = substr($origbody, 0, $img_end);
155                 $origbody = substr($origbody, $img_end);
156
157                 $process_part = preg_replace($search, $replace, $process_part);
158                 $newbody = $newbody . $process_part;
159         }
160         $newbody = $newbody . $origbody;
161
162         $cnt = 0;
163         foreach($images as $image) {
164                 // We're depending on the property of 'foreach' (specified on the PHP website) that
165                 // it loops over the array starting from the first element and going sequentially
166                 // to the last element
167                 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
168                 $cnt++;
169         }
170
171         return $newbody;
172 }}
173
174
175
176 function message_content(&$a) {
177
178         $o = '';
179         nav_set_selected('messages');
180
181         if(! local_user()) {
182                 notice( t('Permission denied.') . EOL);
183                 return;
184         }
185
186         $myprofile = $a->get_baseurl().'/profile/' . $a->user['nickname'];
187
188         $tpl = get_markup_template('mail_head.tpl');
189         $header = replace_macros($tpl, array(
190                 '$messages' => t('Messages'),
191                 '$tab_content' => $tab_content
192         ));
193
194
195         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
196                 if(! intval($a->argv[2]))
197                         return;
198
199                 // Check if we should do HTML-based delete confirmation
200                 if($_REQUEST['confirm']) {
201                         // <form> can't take arguments in its "action" parameter
202                         // so add any arguments as hidden inputs
203                         $query = explode_querystring($a->query_string);
204                         $inputs = array();
205                         foreach($query['args'] as $arg) {
206                                 if(strpos($arg, 'confirm=') === false) {
207                                         $arg_parts = explode('=', $arg);
208                                         $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
209                                 }
210                         }
211
212                         //$a->page['aside'] = '';
213                         return replace_macros(get_markup_template('confirm.tpl'), array(
214                                 '$method' => 'get',
215                                 '$message' => t('Do you really want to delete this message?'),
216                                 '$extra_inputs' => $inputs,
217                                 '$confirm' => t('Yes'),
218                                 '$confirm_url' => $query['base'],
219                                 '$confirm_name' => 'confirmed',
220                                 '$cancel' => t('Cancel'),
221                         ));
222                 }
223                 // Now check how the user responded to the confirmation query
224                 if($_REQUEST['canceled']) {
225                         goaway($_SESSION['return_url']);
226                 }
227
228                 $cmd = $a->argv[1];
229                 if($cmd === 'drop') {
230                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
231                                 intval($a->argv[2]),
232                                 intval(local_user())
233                         );
234                         if($r) {
235                                 info( t('Message deleted.') . EOL );
236                         }
237                         //goaway($a->get_baseurl(true) . '/message' );
238                         goaway($_SESSION['return_url']);
239                 }
240                 else {
241                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
242                                 intval($a->argv[2]),
243                                 intval(local_user())
244                         );
245                         if(count($r)) {
246                                 $parent = $r[0]['parent-uri'];
247                                 $convid = $r[0]['convid'];
248
249                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
250                                         dbesc($parent),
251                                         intval(local_user())
252                                 );
253
254                                 // remove diaspora conversation pointer
255                                 // Actually if we do this, we can never receive another reply to that conversation,
256                                 // as we will never again have the info we need to re-create it.
257                                 // We'll just have to orphan it.
258
259                                 //if($convid) {
260                                 //      q("delete from conv where id = %d limit 1",
261                                 //              intval($convid)
262                                 //      );
263                                 //}
264
265                                 if($r)
266                                         info( t('Conversation removed.') . EOL );
267                         }
268                         //goaway($a->get_baseurl(true) . '/message' );
269                         goaway($_SESSION['return_url']);
270                 }
271
272         }
273
274         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
275
276                 $o .= $header;
277
278 /*              $plaintext = false;
279                 if(intval(get_pconfig(local_user(),'system','plaintext')))
280                         $plaintext = true;*/
281                 $plaintext = true;
282                 if( local_user() && feature_enabled(local_user(),'richtext') )
283                         $plaintext = false;
284
285
286                 $tpl = get_markup_template('msg-header.tpl');
287                 $a->page['htmlhead'] .= replace_macros($tpl, array(
288                         '$baseurl' => $a->get_baseurl(true),
289                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
290                         '$nickname' => $a->user['nickname'],
291                         '$linkurl' => t('Please enter a link URL:')
292                 ));
293
294                 $tpl = get_markup_template('msg-end.tpl');
295                 $a->page['end'] .= replace_macros($tpl, array(
296                         '$baseurl' => $a->get_baseurl(true),
297                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
298                         '$nickname' => $a->user['nickname'],
299                         '$linkurl' => t('Please enter a link URL:')
300                 ));
301
302                 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
303
304
305                 $prename = $preurl = $preid = '';
306
307                 if($preselect) {
308                         $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
309                                 intval(local_user()),
310                                 intval($a->argv[2])
311                         );
312                         if(count($r)) {
313                                 $prename = $r[0]['name'];
314                                 $preurl = $r[0]['url'];
315                                 $preid = $r[0]['id'];
316                         }
317                 }
318
319                 $prefill = (($preselect) ? $prename  : '');
320
321                 // the ugly select box
322
323                 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
324
325                 $tpl = get_markup_template('prv_message.tpl');
326                 $o .= replace_macros($tpl,array(
327                         '$header' => t('Send Private Message'),
328                         '$to' => t('To:'),
329                         '$showinputs' => 'true',
330                         '$prefill' => $prefill,
331                         '$autocomp' => $autocomp,
332                         '$preid' => $preid,
333                         '$subject' => t('Subject:'),
334                         '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
335                         '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
336                         '$readonly' => '',
337                         '$yourmessage' => t('Your message:'),
338                         '$select' => $select,
339                         '$parent' => '',
340                         '$upload' => t('Upload photo'),
341                         '$insert' => t('Insert web link'),
342                         '$wait' => t('Please wait'),
343                         '$submit' => t('Submit')
344                 ));
345
346                 return $o;
347         }
348
349
350         $_SESSION['return_url'] = $a->query_string;
351
352         if($a->argc == 1) {
353
354                 // List messages
355
356                 $o .= $header;
357
358                 $r = q("SELECT count(*) AS `total` FROM `mail`
359                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
360                         intval(local_user())
361                 );
362
363                 if(count($r)) $a->set_pager_total($r[0]['total']);
364
365                 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
366
367                 if(! count($r)) {
368                         info( t('No messages.') . EOL);
369                         return $o;
370                 }
371
372                 $o .= render_messages($r, 'mail_list.tpl');
373
374                 $o .= paginate($a);
375
376                 return $o;
377         }
378
379         if(($a->argc > 1) && (intval($a->argv[1]))) {
380
381                 $o .= $header;
382
383                 $plaintext = true;
384                 if( local_user() && feature_enabled(local_user(),'richtext') )
385                         $plaintext = false;
386
387                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
388                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
389                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
390                         intval(local_user()),
391                         intval($a->argv[1])
392                 );
393                 if(count($r)) {
394                         $contact_id = $r[0]['contact-id'];
395                         $convid = $r[0]['convid'];
396
397                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
398                         if($convid)
399                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
400                                         dbesc($r[0]['parent-uri']),
401                                         intval($convid)
402                                 );
403
404                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
405                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
406                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
407                                 intval(local_user())
408                         );
409                 }
410                 if(! count($messages)) {
411                         notice( t('Message not available.') . EOL );
412                         return $o;
413                 }
414
415                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
416                         dbesc($r[0]['parent-uri']),
417                         intval(local_user())
418                 );
419
420                 require_once("include/bbcode.php");
421
422                 $tpl = get_markup_template('msg-header.tpl');
423                 $a->page['htmlhead'] .= replace_macros($tpl, array(
424                         '$baseurl' => $a->get_baseurl(true),
425                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
426                         '$nickname' => $a->user['nickname'],
427                         '$linkurl' => t('Please enter a link URL:')
428                 ));
429
430                 $tpl = get_markup_template('msg-end.tpl');
431                 $a->page['end'] .= replace_macros($tpl, array(
432                         '$baseurl' => $a->get_baseurl(true),
433                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
434                         '$nickname' => $a->user['nickname'],
435                         '$linkurl' => t('Please enter a link URL:')
436                 ));
437
438
439                 $mails = array();
440                 $seen = 0;
441                 $unknown = false;
442
443                 foreach($messages as $message) {
444                         if($message['unknown'])
445                                 $unknown = true;
446                         if($message['from-url'] == $myprofile) {
447                                 $from_url = $myprofile;
448                                 $sparkle = '';
449                         }
450                         else {
451                                 $from_url = 'redir/' . $message['contact-id'];
452                                 $sparkle = ' sparkle';
453                         }
454
455
456                         $extracted = item_extract_images($message['body']);
457                         if($extracted['images'])
458                                 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
459
460                         if($a->theme['template_engine'] === 'internal') {
461                                 $from_name_e = template_escape($message['from-name']);
462                                 $subject_e = template_escape($message['title']);
463                                 $body_e = template_escape(Smilies::replace(bbcode($message['body'])));
464                                 $to_name_e = template_escape($message['name']);
465                         }
466                         else {
467                                 $from_name_e = $message['from-name'];
468                                 $subject_e = $message['title'];
469                                 $body_e = Smilies::replace(bbcode($message['body']));
470                                 $to_name_e = $message['name'];
471                         }
472
473                         $mails[] = array(
474                                 'id' => $message['id'],
475                                 'from_name' => $from_name_e,
476                                 'from_url' => $from_url,
477                                 'sparkle' => $sparkle,
478                                 'from_photo' => $message['from-photo'],
479                                 'subject' => $subject_e,
480                                 'body' => $body_e,
481                                 'delete' => t('Delete message'),
482                                 'to_name' => $to_name_e,
483                                 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
484                                 'ago' => relative_date($message['created']),
485                         );
486
487                         $seen = $message['seen'];
488                 }
489
490
491                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
492                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
493
494                 $tpl = get_markup_template('mail_display.tpl');
495
496                 if($a->theme['template_engine'] === 'internal') {
497                         $subjtxt_e = template_escape($message['title']);
498                 }
499                 else {
500                         $subjtxt_e = $message['title'];
501                 }
502
503                 $o = replace_macros($tpl, array(
504                         '$thread_id' => $a->argv[1],
505                         '$thread_subject' => $message['title'],
506                         '$thread_seen' => $seen,
507                         '$delete' =>  t('Delete conversation'),
508                         '$canreply' => (($unknown) ? false : '1'),
509                         '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
510                         '$mails' => $mails,
511
512                         // reply
513                         '$header' => t('Send Reply'),
514                         '$to' => t('To:'),
515                         '$showinputs' => '',
516                         '$subject' => t('Subject:'),
517                         '$subjtxt' => $subjtxt_e,
518                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
519                         '$yourmessage' => t('Your message:'),
520                         '$text' => '',
521                         '$select' => $select,
522                         '$parent' => $parent,
523                         '$upload' => t('Upload photo'),
524                         '$insert' => t('Insert web link'),
525                         '$submit' => t('Submit'),
526                         '$wait' => t('Please wait')
527
528                 ));
529
530                 return $o;
531         }
532 }
533
534 function get_messages($user, $lstart, $lend) {
535
536         return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
537                 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
538                 count( * ) as count
539                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
540                 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
541                 intval($user), intval($lstart), intval($lend)
542         );
543 }
544
545 function render_messages($msg, $t) {
546
547         $a = get_app();
548
549         $tpl = get_markup_template($t);
550         $rslt = '';
551
552         $myprofile = $a->get_baseurl().'/profile/' . $a->user['nickname'];
553
554         foreach($msg as $rr) {
555
556                 if($rr['unknown'])
557                         $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
558                 elseif (link_compare($rr['from-url'], $myprofile))
559                         $participants = sprintf( t("You and %s"), $rr['name']);
560                 else
561                         $participants = sprintf(t("%s and You"), $rr['from-name']);
562
563                 if($a->theme['template_engine'] === 'internal') {
564                         $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
565                         $body_e = template_escape($rr['body']);
566                         $to_name_e = template_escape($rr['name']);
567                 }
568                 else {
569                         $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
570                         $body_e = $rr['body'];
571                         $to_name_e = $rr['name'];
572                 }
573
574                 $rslt .= replace_macros($tpl, array(
575                         '$id' => $rr['id'],
576                         '$from_name' => $participants,
577                         '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
578                         '$sparkle' => ' sparkle',
579                         '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
580                         '$subject' => $subject_e,
581                         '$delete' => t('Delete conversation'),
582                         '$body' => $body_e,
583                         '$to_name' => $to_name_e,
584                         '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
585                                                                                                                         '$ago' => relative_date($rr['mailcreated']),
586                         '$seen' => $rr['mailseen'],
587                         '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
588                 ));
589         }
590
591         return $rslt;
592 }