]> git.mxchange.org Git - friendica.git/blob - mod/message.php
- Remove tinyMCE mentions or convert to addeditortext()
[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(App $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 = App::get_baseurl();
28
29         $head_tpl = get_markup_template('message-head.tpl');
30         $a->page['htmlhead'] .= replace_macros($head_tpl,array(
31                 '$baseurl' => App::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' => App::get_baseurl(true),
38                 '$base' => $base
39         ));
40
41 }
42
43 function message_post(App $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(App $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 = App::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(App::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 (dbm::is_result($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(App::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                 $tpl = get_markup_template('msg-header.tpl');
279                 $a->page['htmlhead'] .= replace_macros($tpl, array(
280                         '$baseurl' => App::get_baseurl(true),
281                         '$nickname' => $a->user['nickname'],
282                         '$linkurl' => t('Please enter a link URL:')
283                 ));
284
285                 $tpl = get_markup_template('msg-end.tpl');
286                 $a->page['end'] .= replace_macros($tpl, array(
287                         '$baseurl' => App::get_baseurl(true),
288                         '$nickname' => $a->user['nickname'],
289                         '$linkurl' => t('Please enter a link URL:')
290                 ));
291
292                 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
293
294
295                 $prename = $preurl = $preid = '';
296
297                 if($preselect) {
298                         $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
299                                 intval(local_user()),
300                                 intval($a->argv[2])
301                         );
302                         if (!dbm::is_result($r)) {
303                                 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
304                                         intval(local_user()),
305                                         dbesc(normalise_link(base64_decode($a->argv[2])))
306                                 );
307                         }
308
309                         if (!dbm::is_result($r)) {
310                                 $r = q("SELECT `name`, `url`, `id` FROM `contact` WHERE `uid` = %d AND `addr` = '%s' LIMIT 1",
311                                         intval(local_user()),
312                                         dbesc(base64_decode($a->argv[2]))
313                                 );
314                         }
315
316                         if (dbm::is_result($r)) {
317                                 $prename = $r[0]['name'];
318                                 $preurl = $r[0]['url'];
319                                 $preid = $r[0]['id'];
320                                 $preselect = array($preid);
321                         } else
322                                 $preselect = false;
323                 }
324
325                 $prefill = (($preselect) ? $prename  : '');
326
327                 // the ugly select box
328
329                 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
330
331                 $tpl = get_markup_template('prv_message.tpl');
332                 $o .= replace_macros($tpl,array(
333                         '$header' => t('Send Private Message'),
334                         '$to' => t('To:'),
335                         '$showinputs' => 'true',
336                         '$prefill' => $prefill,
337                         '$autocomp' => $autocomp,
338                         '$preid' => $preid,
339                         '$subject' => t('Subject:'),
340                         '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
341                         '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
342                         '$readonly' => '',
343                         '$yourmessage' => t('Your message:'),
344                         '$select' => $select,
345                         '$parent' => '',
346                         '$upload' => t('Upload photo'),
347                         '$insert' => t('Insert web link'),
348                         '$wait' => t('Please wait'),
349                         '$submit' => t('Submit')
350                 ));
351                 return $o;
352         }
353
354
355         $_SESSION['return_url'] = $a->query_string;
356
357         if ($a->argc == 1) {
358
359                 // List messages
360
361                 $o .= $header;
362
363                 $r = q("SELECT count(*) AS `total` FROM `mail`
364                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
365                         intval(local_user())
366                 );
367
368                 if (dbm::is_result($r)) {
369                         $a->set_pager_total($r[0]['total']);
370                 }
371
372                 $r = get_messages(local_user(), $a->pager['start'], $a->pager['itemspage']);
373
374                 if (! dbm::is_result($r)) {
375                         info( t('No messages.') . EOL);
376                         return $o;
377                 }
378
379                 $o .= render_messages($r, 'mail_list.tpl');
380
381                 $o .= paginate($a);
382
383                 return $o;
384         }
385
386         if(($a->argc > 1) && (intval($a->argv[1]))) {
387
388                 $o .= $header;
389
390                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
391                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
392                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
393                         intval(local_user()),
394                         intval($a->argv[1])
395                 );
396                 if (dbm::is_result($r)) {
397                         $contact_id = $r[0]['contact-id'];
398                         $convid = $r[0]['convid'];
399
400                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
401                         if($convid)
402                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
403                                         dbesc($r[0]['parent-uri']),
404                                         intval($convid)
405                                 );
406
407                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb`
408                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
409                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
410                                 intval(local_user())
411                         );
412                 }
413                 if(! count($messages)) {
414                         notice( t('Message not available.') . EOL );
415                         return $o;
416                 }
417
418                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
419                         dbesc($r[0]['parent-uri']),
420                         intval(local_user())
421                 );
422
423                 require_once("include/bbcode.php");
424
425                 $tpl = get_markup_template('msg-header.tpl');
426                 $a->page['htmlhead'] .= replace_macros($tpl, array(
427                         '$baseurl' => App::get_baseurl(true),
428                         '$nickname' => $a->user['nickname'],
429                         '$linkurl' => t('Please enter a link URL:')
430                 ));
431
432                 $tpl = get_markup_template('msg-end.tpl');
433                 $a->page['end'] .= replace_macros($tpl, array(
434                         '$baseurl' => App::get_baseurl(true),
435                         '$nickname' => $a->user['nickname'],
436                         '$linkurl' => t('Please enter a link URL:')
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                         } elseif ($message['contact-id'] != 0) {
450                                 $from_url = 'redir/'.$message['contact-id'];
451                                 $sparkle = ' sparkle';
452                         } else {
453                                 $from_url = $message['from-url']."?zrl=".urlencode($myprofile);
454                                 $sparkle = ' sparkle';
455                         }
456
457
458                         $extracted = item_extract_images($message['body']);
459                         if($extracted['images'])
460                                 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
461
462                         if($a->theme['template_engine'] === 'internal') {
463                                 $from_name_e = template_escape($message['from-name']);
464                                 $subject_e = template_escape($message['title']);
465                                 $body_e = template_escape(Smilies::replace(bbcode($message['body'])));
466                                 $to_name_e = template_escape($message['name']);
467                         } else {
468                                 $from_name_e = $message['from-name'];
469                                 $subject_e = $message['title'];
470                                 $body_e = Smilies::replace(bbcode($message['body']));
471                                 $to_name_e = $message['name'];
472                         }
473
474                         $contact = get_contact_details_by_url($message['from-url']);
475                         if (isset($contact["thumb"]))
476                                 $from_photo = $contact["thumb"];
477                         else
478                                 $from_photo = $message['from-photo'];
479
480                         $mails[] = array(
481                                 'id' => $message['id'],
482                                 'from_name' => $from_name_e,
483                                 'from_url' => $from_url,
484                                 'sparkle' => $sparkle,
485                                 'from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
486                                 'subject' => $subject_e,
487                                 'body' => $body_e,
488                                 'delete' => t('Delete message'),
489                                 'to_name' => $to_name_e,
490                                 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
491                                 'ago' => relative_date($message['created']),
492                         );
493
494                         $seen = $message['seen'];
495                 }
496
497
498                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
499                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
500
501                 $tpl = get_markup_template('mail_display.tpl');
502
503                 if($a->theme['template_engine'] === 'internal') {
504                         $subjtxt_e = template_escape($message['title']);
505                 }
506                 else {
507                         $subjtxt_e = $message['title'];
508                 }
509
510                 $o = replace_macros($tpl, array(
511                         '$thread_id' => $a->argv[1],
512                         '$thread_subject' => $message['title'],
513                         '$thread_seen' => $seen,
514                         '$delete' =>  t('Delete conversation'),
515                         '$canreply' => (($unknown) ? false : '1'),
516                         '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),
517                         '$mails' => $mails,
518
519                         // reply
520                         '$header' => t('Send Reply'),
521                         '$to' => t('To:'),
522                         '$showinputs' => '',
523                         '$subject' => t('Subject:'),
524                         '$subjtxt' => $subjtxt_e,
525                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
526                         '$yourmessage' => t('Your message:'),
527                         '$text' => '',
528                         '$select' => $select,
529                         '$parent' => $parent,
530                         '$upload' => t('Upload photo'),
531                         '$insert' => t('Insert web link'),
532                         '$submit' => t('Submit'),
533                         '$wait' => t('Please wait')
534
535                 ));
536
537                 return $o;
538         }
539 }
540
541 function get_messages($user, $lstart, $lend) {
542
543         return q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`,
544                 `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
545                 count( * ) as count
546                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id`
547                 WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
548                 intval($user), intval($lstart), intval($lend)
549         );
550 }
551
552 function render_messages(array $msg, $t) {
553
554         $a = get_app();
555
556         $tpl = get_markup_template($t);
557         $rslt = '';
558
559         $myprofile = App::get_baseurl().'/profile/' . $a->user['nickname'];
560
561         foreach($msg as $rr) {
562
563                 if($rr['unknown'])
564                         $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
565                 elseif (link_compare($rr['from-url'], $myprofile))
566                         $participants = sprintf( t("You and %s"), $rr['name']);
567                 else
568                         $participants = sprintf(t("%s and You"), $rr['from-name']);
569
570                 if($a->theme['template_engine'] === 'internal') {
571                         $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
572                         $body_e = template_escape($rr['body']);
573                         $to_name_e = template_escape($rr['name']);
574                 }
575                 else {
576                         $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
577                         $body_e = $rr['body'];
578                         $to_name_e = $rr['name'];
579                 }
580
581                 $contact = get_contact_details_by_url($rr['url']);
582                 if (isset($contact["thumb"]))
583                         $from_photo = $contact["thumb"];
584                 else
585                         $from_photo = (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']);
586
587                 $rslt .= replace_macros($tpl, array(
588                         '$id' => $rr['id'],
589                         '$from_name' => $participants,
590                         '$from_url' => (($rr['network'] === NETWORK_DFRN) ? 'redir/' . $rr['contact-id'] : $rr['url']),
591                         '$sparkle' => ' sparkle',
592                         '$from_photo' => proxy_url($from_photo, false, PROXY_SIZE_THUMB),
593                         '$subject' => $subject_e,
594                         '$delete' => t('Delete conversation'),
595                         '$body' => $body_e,
596                         '$to_name' => $to_name_e,
597                         '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
598                                                                                                                         '$ago' => relative_date($rr['mailcreated']),
599                         '$seen' => $rr['mailseen'],
600                         '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
601                 ));
602         }
603
604         return $rslt;
605 }