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