]> git.mxchange.org Git - friendica.git/blob - mod/message.php
Merge pull request #2151 from annando/1512-misconfigured-friendica
[friendica.git] / mod / message.php
1 <?php
2
3 require_once('include/acl_selectors.php');
4 require_once('include/message.php');
5
6 function message_init(&$a) {
7
8         $tabs = '';
9
10         if ($a->argc >1 && is_numeric($a->argv[1])) {
11          $tabs = render_messages(get_messages(local_user(),0,5), 'mail_list.tpl');
12         }
13
14         $new = array(
15                 'label' => t('New Message'),
16                 'url' => $a->get_baseurl(true) . '/message/new',
17                 'sel'=> ($a->argv[1] == 'new'),
18                 'accesskey' => 'm',
19         );
20
21         $tpl = get_markup_template('message_side.tpl');
22         $a->page['aside'] = replace_macros($tpl, array(
23                 '$tabs'=>$tabs,
24                 '$new'=>$new,
25         ));
26         $base = $a->get_baseurl();
27
28         $head_tpl = get_markup_template('message-head.tpl');
29         $a->page['htmlhead'] .= replace_macros($head_tpl,array(
30                 '$baseurl' => $a->get_baseurl(true),
31                 '$base' => $base
32         ));
33
34         $end_tpl = get_markup_template('message-end.tpl');
35         $a->page['end'] .= replace_macros($end_tpl,array(
36                 '$baseurl' => $a->get_baseurl(true),
37                 '$base' => $base
38         ));
39
40 }
41
42 function message_post(&$a) {
43
44         if(! local_user()) {
45                 notice( t('Permission denied.') . EOL);
46                 return;
47         }
48
49         $replyto   = ((x($_REQUEST,'replyto'))   ? notags(trim($_REQUEST['replyto']))   : '');
50         $subject   = ((x($_REQUEST,'subject'))   ? notags(trim($_REQUEST['subject']))   : '');
51         $body      = ((x($_REQUEST,'body'))      ? escape_tags(trim($_REQUEST['body'])) : '');
52         $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto'])       : 0 );
53
54         // Work around doubled linefeeds in Tinymce 3.5b2
55
56 /*      $plaintext = intval(get_pconfig(local_user(),'system','plaintext') && !feature_enabled(local_user(),'richtext'));
57         if(! $plaintext) {
58                 $body = fix_mce_lf($body);
59         }*/
60         $plaintext = intval(!feature_enabled(local_user(),'richtext'));
61         if(! $plaintext) {
62                 $body = fix_mce_lf($body);
63         }
64
65         $ret = send_message($recipient, $body, $subject, $replyto);
66         $norecip = false;
67
68         switch($ret){
69                 case -1:
70                         notice( t('No recipient selected.') . EOL );
71                         $norecip = true;
72                         break;
73                 case -2:
74                         notice( t('Unable to locate contact information.') . EOL );
75                         break;
76                 case -3:
77                         notice( t('Message could not be sent.') . EOL );
78                         break;
79                 case -4:
80                         notice( t('Message collection failure.') . EOL );
81                         break;
82                 default:
83                         info( t('Message sent.') . EOL );
84         }
85
86         // fake it to go back to the input form if no recipient listed
87
88         if($norecip) {
89                 $a->argc = 2;
90                 $a->argv[1] = 'new';
91         }
92         else
93                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
94
95 }
96
97 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
98 // is identical to the code in include/conversation.php
99 if(! function_exists('item_extract_images')) {
100 function item_extract_images($body) {
101
102         $saved_image = array();
103         $orig_body = $body;
104         $new_body = '';
105
106         $cnt = 0;
107         $img_start = strpos($orig_body, '[img');
108         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
109         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
110         while(($img_st_close !== false) && ($img_end !== false)) {
111
112                 $img_st_close++; // make it point to AFTER the closing bracket
113                 $img_end += $img_start;
114
115                 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
116                         // This is an embedded image
117
118                         $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
119                         $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
120
121                         $cnt++;
122                 }
123                 else
124                         $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
125
126                 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
127
128                 if($orig_body === false) // in case the body ends on a closing image tag
129                         $orig_body = '';
130
131                 $img_start = strpos($orig_body, '[img');
132                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
133                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
134         }
135
136         $new_body = $new_body . $orig_body;
137
138         return array('body' => $new_body, 'images' => $saved_image);
139 }}
140
141 if(! function_exists('item_redir_and_replace_images')) {
142 function item_redir_and_replace_images($body, $images, $cid) {
143
144         $origbody = $body;
145         $newbody = '';
146
147         for($i = 0; $i < count($images); $i++) {
148                 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
149                 $replace = '[url=' . z_path() . '/redir/' . $cid
150                            . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
151
152                 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
153                 $process_part = substr($origbody, 0, $img_end);
154                 $origbody = substr($origbody, $img_end);
155
156                 $process_part = preg_replace($search, $replace, $process_part);
157                 $newbody = $newbody . $process_part;
158         }
159         $newbody = $newbody . $origbody;
160
161         $cnt = 0;
162         foreach($images as $image) {
163                 // We're depending on the property of 'foreach' (specified on the PHP website) that
164                 // it loops over the array starting from the first element and going sequentially
165                 // to the last element
166                 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
167                 $cnt++;
168         }
169
170         return $newbody;
171 }}
172
173
174
175 function message_content(&$a) {
176
177         $o = '';
178         nav_set_selected('messages');
179
180         if(! local_user()) {
181                 notice( t('Permission denied.') . EOL);
182                 return;
183         }
184
185         $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
186
187         $tpl = get_markup_template('mail_head.tpl');
188         $header = replace_macros($tpl, array(
189                 '$messages' => t('Messages'),
190                 '$tab_content' => $tab_content
191         ));
192
193
194         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
195                 if(! intval($a->argv[2]))
196                         return;
197
198                 // Check if we should do HTML-based delete confirmation
199                 if($_REQUEST['confirm']) {
200                         // <form> can't take arguments in its "action" parameter
201                         // so add any arguments as hidden inputs
202                         $query = explode_querystring($a->query_string);
203                         $inputs = array();
204                         foreach($query['args'] as $arg) {
205                                 if(strpos($arg, 'confirm=') === false) {
206                                         $arg_parts = explode('=', $arg);
207                                         $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
208                                 }
209                         }
210
211                         //$a->page['aside'] = '';
212                         return replace_macros(get_markup_template('confirm.tpl'), array(
213                                 '$method' => 'get',
214                                 '$message' => t('Do you really want to delete this message?'),
215                                 '$extra_inputs' => $inputs,
216                                 '$confirm' => t('Yes'),
217                                 '$confirm_url' => $query['base'],
218                                 '$confirm_name' => 'confirmed',
219                                 '$cancel' => t('Cancel'),
220                         ));
221                 }
222                 // Now check how the user responded to the confirmation query
223                 if($_REQUEST['canceled']) {
224                         goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
225                 }
226
227                 $cmd = $a->argv[1];
228                 if($cmd === 'drop') {
229                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
230                                 intval($a->argv[2]),
231                                 intval(local_user())
232                         );
233                         if($r) {
234                                 info( t('Message deleted.') . EOL );
235                         }
236                         //goaway($a->get_baseurl(true) . '/message' );
237                         goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
238                 }
239                 else {
240                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
241                                 intval($a->argv[2]),
242                                 intval(local_user())
243                         );
244                         if(count($r)) {
245                                 $parent = $r[0]['parent-uri'];
246                                 $convid = $r[0]['convid'];
247
248                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
249                                         dbesc($parent),
250                                         intval(local_user())
251                                 );
252
253                                 // remove diaspora conversation pointer
254                                 // Actually if we do this, we can never receive another reply to that conversation,
255                                 // as we will never again have the info we need to re-create it.
256                                 // We'll just have to orphan it.
257
258                                 //if($convid) {
259                                 //      q("delete from conv where id = %d limit 1",
260                                 //              intval($convid)
261                                 //      );
262                                 //}
263
264                                 if($r)
265                                         info( t('Conversation removed.') . EOL );
266                         }
267                         //goaway($a->get_baseurl(true) . '/message' );
268                         goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
269                 }
270
271         }
272
273         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
274
275                 $o .= $header;
276
277 /*              $plaintext = false;
278                 if(intval(get_pconfig(local_user(),'system','plaintext')))
279                         $plaintext = true;*/
280                 $plaintext = true;
281                 if( local_user() && feature_enabled(local_user(),'richtext') )
282                         $plaintext = false;
283
284
285                 $tpl = get_markup_template('msg-header.tpl');
286                 $a->page['htmlhead'] .= replace_macros($tpl, array(
287                         '$baseurl' => $a->get_baseurl(true),
288                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
289                         '$nickname' => $a->user['nickname'],
290                         '$linkurl' => t('Please enter a link URL:')
291                 ));
292
293                 $tpl = get_markup_template('msg-end.tpl');
294                 $a->page['end'] .= replace_macros($tpl, array(
295                         '$baseurl' => $a->get_baseurl(true),
296                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
297                         '$nickname' => $a->user['nickname'],
298                         '$linkurl' => t('Please enter a link URL:')
299                 ));
300
301                 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
302
303
304                 $prename = $preurl = $preid = '';
305
306                 if($preselect) {
307                         $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
308                                 intval(local_user()),
309                                 intval($a->argv[2])
310                         );
311                         if(count($r)) {
312                                 $prename = $r[0]['name'];
313                                 $preurl = $r[0]['url'];
314                                 $preid = $r[0]['id'];
315                         }
316                 }
317
318                 $prefill = (($preselect) ? $prename  : '');
319
320                 // the ugly select box
321
322                 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
323
324                 $tpl = get_markup_template('prv_message.tpl');
325                 $o .= replace_macros($tpl,array(
326                         '$header' => t('Send Private Message'),
327                         '$to' => t('To:'),
328                         '$showinputs' => 'true',
329                         '$prefill' => $prefill,
330                         '$autocomp' => $autocomp,
331                         '$preid' => $preid,
332                         '$subject' => t('Subject:'),
333                         '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
334                         '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
335                         '$readonly' => '',
336                         '$yourmessage' => t('Your message:'),
337                         '$select' => $select,
338                         '$parent' => '',
339                         '$upload' => t('Upload photo'),
340                         '$insert' => t('Insert web link'),
341                         '$wait' => t('Please wait'),
342                         '$submit' => t('Submit')
343                 ));
344
345                 return $o;
346         }
347
348
349         $_SESSION['return_url'] = $a->query_string;
350
351         if($a->argc == 1) {
352
353                 // List messages
354
355                 $o .= $header;
356
357                 $r = q("SELECT count(*) AS `total` FROM `mail`
358                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
359                         intval(local_user()),
360                         dbesc($myprofile)
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 = $a->get_baseurl(true) . '/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(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(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         foreach($msg as $rr) {
553
554                 if($rr['unknown']) {
555                         $participants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
556                 }
557                 elseif (link_compare($rr['from-url'], $myprofile)){
558                         $participants = sprintf( t("You and %s"), $rr['name']);
559                 }
560                 else {
561                         $participants = sprintf( t("%s and You"), $rr['from-name']);
562                 }
563
564                 if($a->theme['template_engine'] === 'internal') {
565                         $subject_e = template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'));
566                         $body_e = template_escape($rr['body']);
567                         $to_name_e = template_escape($rr['name']);
568                 }
569                 else {
570                         $subject_e = (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>');
571                         $body_e = $rr['body'];
572                         $to_name_e = $rr['name'];
573                 }
574
575                 $rslt .= replace_macros($tpl, array(
576                         '$id' => $rr['id'],
577                         '$from_name' => $participants,
578                         '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
579                         '$sparkle' => ' sparkle',
580                         '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
581                         '$subject' => $subject_e,
582                         '$delete' => t('Delete conversation'),
583                         '$body' => $body_e,
584                         '$to_name' => $to_name_e,
585                         '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
586                                                                                                                         '$ago' => relative_date($rr['mailcreated']),
587                         '$seen' => $rr['mailseen'],
588                         '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
589                 ));
590         }
591
592         return $rslt;
593 }