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