]> git.mxchange.org Git - friendica.git/blob - mod/message.php
744a3eb3f7c8a6a50e584c0cb2a0a03d17b6dc6e
[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
86 }
87
88 // Note: the code in 'item_extract_images' and 'item_redir_and_replace_images'
89 // is identical to the code in include/conversation.php
90 if(! function_exists('item_extract_images')) {
91 function item_extract_images($body) {
92
93         $saved_image = array();
94         $orig_body = $body;
95         $new_body = '';
96
97         $cnt = 0;
98         $img_start = strpos($orig_body, '[img');
99         $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
100         $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
101         while(($img_st_close !== false) && ($img_end !== false)) {
102
103                 $img_st_close++; // make it point to AFTER the closing bracket
104                 $img_end += $img_start;
105
106                 if(! strcmp(substr($orig_body, $img_start + $img_st_close, 5), 'data:')) {
107                         // This is an embedded image
108
109                         $saved_image[$cnt] = substr($orig_body, $img_start + $img_st_close, $img_end - ($img_start + $img_st_close));
110                         $new_body = $new_body . substr($orig_body, 0, $img_start) . '[!#saved_image' . $cnt . '#!]';
111
112                         $cnt++;
113                 }
114                 else
115                         $new_body = $new_body . substr($orig_body, 0, $img_end + strlen('[/img]'));
116
117                 $orig_body = substr($orig_body, $img_end + strlen('[/img]'));
118
119                 if($orig_body === false) // in case the body ends on a closing image tag
120                         $orig_body = '';
121
122                 $img_start = strpos($orig_body, '[img');
123                 $img_st_close = ($img_start !== false ? strpos(substr($orig_body, $img_start), ']') : false);
124                 $img_end = ($img_start !== false ? strpos(substr($orig_body, $img_start), '[/img]') : false);
125         }
126
127         $new_body = $new_body . $orig_body;
128
129         return array('body' => $new_body, 'images' => $saved_image);
130 }}
131
132 if(! function_exists('item_redir_and_replace_images')) {
133 function item_redir_and_replace_images($body, $images, $cid) {
134
135         $origbody = $body;
136         $newbody = '';
137
138         for($i = 0; $i < count($images); $i++) {
139                 $search = '/\[url\=(.*?)\]\[!#saved_image' . $i . '#!\]\[\/url\]' . '/is';
140                 $replace = '[url=' . z_path() . '/redir/' . $cid 
141                            . '?f=1&url=' . '$1' . '][!#saved_image' . $i . '#!][/url]' ;
142
143                 $img_end = strpos($origbody, '[!#saved_image' . $i . '#!][/url]') + strlen('[!#saved_image' . $i . '#!][/url]');
144                 $process_part = substr($origbody, 0, $img_end);
145                 $origbody = substr($origbody, $img_end);
146
147                 $process_part = preg_replace($search, $replace, $process_part);
148                 $newbody = $newbody . $process_part;
149         }
150         $newbody = $newbody . $origbody;
151
152         $cnt = 0;
153         foreach($images as $image) {
154                 // We're depending on the property of 'foreach' (specified on the PHP website) that
155                 // it loops over the array starting from the first element and going sequentially
156                 // to the last element
157                 $newbody = str_replace('[!#saved_image' . $cnt . '#!]', '[img]' . $image . '[/img]', $newbody);
158                 $cnt++;
159         }
160
161         return $newbody;
162 }}
163
164
165
166 function message_content(&$a) {
167
168         $o = '';
169         nav_set_selected('messages');
170
171         if(! local_user()) {
172                 notice( t('Permission denied.') . EOL);
173                 return;
174         }
175
176         $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
177
178         $tpl = get_markup_template('mail_head.tpl');
179         $header = replace_macros($tpl, array(
180                 '$messages' => t('Messages'),
181                 '$tab_content' => $tab_content
182         ));
183
184
185         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
186                 if(! intval($a->argv[2]))
187                         return;
188                 $cmd = $a->argv[1];
189                 if($cmd === 'drop') {
190                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
191                                 intval($a->argv[2]),
192                                 intval(local_user())
193                         );
194                         if($r) {
195                                 info( t('Message deleted.') . EOL );
196                         }
197                         goaway($a->get_baseurl(true) . '/message' );
198                 }
199                 else {
200                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
201                                 intval($a->argv[2]),
202                                 intval(local_user())
203                         );
204                         if(count($r)) {
205                                 $parent = $r[0]['parent-uri'];
206                                 $convid = $r[0]['convid'];
207
208                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
209                                         dbesc($parent),
210                                         intval(local_user())
211                                 );
212
213                                 // remove diaspora conversation pointer
214                                 // Actually if we do this, we can never receive another reply to that conversation,
215                                 // as we will never again have the info we need to re-create it. 
216                                 // We'll just have to orphan it. 
217
218                                 //if($convid) {
219                                 //      q("delete from conv where id = %d limit 1",
220                                 //              intval($convid)
221                                 //      );
222                                 //}
223
224                                 if($r)
225                                         info( t('Conversation removed.') . EOL );
226                         } 
227                         goaway($a->get_baseurl(true) . '/message' );
228                 }       
229         
230         }
231
232         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
233                 
234                 $o .= $header;
235                 
236 /*              $plaintext = false;
237                 if(intval(get_pconfig(local_user(),'system','plaintext')))
238                         $plaintext = true;*/
239                 $plaintext = true;
240                 if( local_user() && feature_enabled(local_user(),'richtext') )
241                         $plaintext = false;
242
243
244                 $tpl = get_markup_template('msg-header.tpl');
245                 $a->page['htmlhead'] .= replace_macros($tpl, array(
246                         '$baseurl' => $a->get_baseurl(true),
247                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
248                         '$nickname' => $a->user['nickname'],
249                         '$linkurl' => t('Please enter a link URL:')
250                 ));
251         
252                 $tpl = get_markup_template('msg-end.tpl');
253                 $a->page['end'] .= replace_macros($tpl, array(
254                         '$baseurl' => $a->get_baseurl(true),
255                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
256                         '$nickname' => $a->user['nickname'],
257                         '$linkurl' => t('Please enter a link URL:')
258                 ));
259         
260                 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
261                         
262
263                 $prename = $preurl = $preid = '';
264
265                 if($preselect) {
266                         $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
267                                 intval(local_user()),
268                                 intval($a->argv[2])
269                         );
270                         if(count($r)) {
271                                 $prename = $r[0]['name'];
272                                 $preurl = $r[0]['url'];
273                                 $preid = $r[0]['id'];
274                         }
275                 }        
276
277                 $prefill = (($preselect) ? $prename  : '');
278
279                 // the ugly select box
280                 
281                 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
282
283                 $tpl = get_markup_template('prv_message.tpl');
284                 $o .= replace_macros($tpl,array(
285                         '$header' => t('Send Private Message'),
286                         '$to' => t('To:'),
287                         '$showinputs' => 'true', 
288                         '$prefill' => $prefill,
289                         '$autocomp' => $autocomp,
290                         '$preid' => $preid,
291                         '$subject' => t('Subject:'),
292                         '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
293                         '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
294                         '$readonly' => '',
295                         '$yourmessage' => t('Your message:'),
296                         '$select' => $select,
297                         '$parent' => '',
298                         '$upload' => t('Upload photo'),
299                         '$insert' => t('Insert web link'),
300                         '$wait' => t('Please wait'),
301                         '$submit' => t('Submit')
302                 ));
303
304                 return $o;
305         }
306
307         if($a->argc == 1) {
308
309                 // list messages
310
311                 $o .= $header;
312
313                 
314                 $r = q("SELECT count(*) AS `total` FROM `mail` 
315                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
316                         intval(local_user()),
317                         dbesc($myprofile)
318                 );
319                 if(count($r))
320                         $a->set_pager_total($r[0]['total']);
321
322                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
323                         `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
324                         count( * ) as count
325                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
326                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
327                         intval(local_user()),
328                         //
329                         intval($a->pager['start']),
330                         intval($a->pager['itemspage'])
331                 );
332
333                 if(! count($r)) {
334                         info( t('No messages.') . EOL);
335                         return $o;
336                 }
337
338                 $tpl = get_markup_template('mail_list.tpl');
339                 foreach($r as $rr) {
340                         if($rr['unknown']) {
341                                 $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
342                         }
343                         elseif (link_compare($rr['from-url'],$myprofile)){
344                                 $partecipants = sprintf( t("You and %s"), $rr['name']);
345                         }
346                         else {
347                                 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
348                         }
349                         
350                         $o .= replace_macros($tpl, array(
351                                 '$id' => $rr['id'],
352                                 '$from_name' => $partecipants,
353                                 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
354                                 '$sparkle' => ' sparkle',
355                                 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
356                                 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
357                                 '$delete' => t('Delete conversation'),
358                                 '$body' => template_escape($rr['body']),
359                                 '$to_name' => template_escape($rr['name']),
360                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
361                                 '$ago' => relative_date($rr['mailcreated']),
362                                 '$seen' => $rr['mailseen'],
363                                 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
364                         ));
365                 }
366                 $o .= paginate($a);     
367                 return $o;
368         }
369
370         if(($a->argc > 1) && (intval($a->argv[1]))) {
371
372                 $o .= $header;
373
374                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
375                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
376                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
377                         intval(local_user()),
378                         intval($a->argv[1])
379                 );
380                 if(count($r)) { 
381                         $contact_id = $r[0]['contact-id'];
382                         $convid = $r[0]['convid'];
383
384                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
385                         if($convid)
386                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
387                                         dbesc($r[0]['parent-uri']),
388                                         intval($convid)
389                                 );  
390
391                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
392                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
393                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
394                                 intval(local_user())
395                         );
396                 }
397                 if(! count($messages)) {
398                         notice( t('Message not available.') . EOL );
399                         return $o;
400                 }
401
402                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
403                         dbesc($r[0]['parent-uri']),
404                         intval(local_user())
405                 );
406
407                 require_once("include/bbcode.php");
408
409                 $tpl = get_markup_template('msg-header.tpl');
410                 $a->page['htmlhead'] .= replace_macros($tpl, array(
411                         '$nickname' => $a->user['nickname'],
412                         '$baseurl' => $a->get_baseurl(true)
413                 ));
414
415                 $tpl = get_markup_template('msg-end.tpl');
416                 $a->page['end'] .= replace_macros($tpl, array(
417                         '$nickname' => $a->user['nickname'],
418                         '$baseurl' => $a->get_baseurl(true)
419                 ));
420
421
422                 $mails = array();
423                 $seen = 0;
424                 $unknown = false;
425
426                 foreach($messages as $message) {
427                         if($message['unknown'])
428                                 $unknown = true;
429                         if($message['from-url'] == $myprofile) {
430                                 $from_url = $myprofile;
431                                 $sparkle = '';
432                         }
433                         else {
434                                 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
435                                 $sparkle = ' sparkle';
436                         }
437
438
439                         $extracted = item_extract_images($message['body']);
440                         if($extracted['images'])
441                                 $message['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $message['contact-id']);
442
443                         $mails[] = array(
444                                 'id' => $message['id'],
445                                 'from_name' => template_escape($message['from-name']),
446                                 'from_url' => $from_url,
447                                 'sparkle' => $sparkle,
448                                 'from_photo' => $message['from-photo'],
449                                 'subject' => template_escape($message['title']),
450                                 'body' => template_escape(smilies(bbcode($message['body']))),
451                                 'delete' => t('Delete message'),
452                                 'to_name' => template_escape($message['name']),
453                                 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
454                                 'ago' => relative_date($message['created']),
455                         );
456                                 
457                         $seen = $message['seen'];
458                 }
459
460
461                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
462                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
463
464                 $tpl = get_markup_template('mail_display.tpl');
465                 $o = replace_macros($tpl, array(
466                         '$thread_id' => $a->argv[1],
467                         '$thread_subject' => $message['title'],
468                         '$thread_seen' => $seen,
469                         '$delete' =>  t('Delete conversation'),
470                         '$canreply' => (($unknown) ? false : '1'),
471                         '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),                        
472                         '$mails' => $mails,
473                         
474                         // reply
475                         '$header' => t('Send Reply'),
476                         '$to' => t('To:'),
477                         '$showinputs' => '',
478                         '$subject' => t('Subject:'),
479                         '$subjtxt' => template_escape($message['title']),
480                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
481                         '$yourmessage' => t('Your message:'),
482                         '$text' => '',
483                         '$select' => $select,
484                         '$parent' => $parent,
485                         '$upload' => t('Upload photo'),
486                         '$insert' => t('Insert web link'),
487                         '$submit' => t('Submit'),
488                         '$wait' => t('Please wait')
489
490                 ));
491
492                 return $o;
493         }
494
495 }