]> git.mxchange.org Git - friendica.git/blob - mod/message.php
reset permissions to forum default when adding forum delivery chain
[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         $a->page['htmlhead'] .= '<script src="' . $a->get_baseurl(true) . '/library/jquery_ac/jquery.autocomplete-min.js" ></script>';
22         $a->page['htmlhead'] .= <<< EOT
23
24 <script>$(document).ready(function() { 
25         var a; 
26         a = $("#recip").autocomplete({ 
27                 serviceUrl: '$base/acl',
28                 width: 350
29         });
30 }); 
31
32 </script>
33 EOT;
34         
35 }
36
37 function message_post(&$a) {
38
39         if(! local_user()) {
40                 notice( t('Permission denied.') . EOL);
41                 return;
42         }
43
44         $replyto   = ((x($_REQUEST,'replyto'))   ? notags(trim($_REQUEST['replyto']))   : '');
45         $subject   = ((x($_REQUEST,'subject'))   ? notags(trim($_REQUEST['subject']))   : '');
46         $body      = ((x($_REQUEST,'body'))      ? escape_tags(trim($_REQUEST['body'])) : '');
47         $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto'])       : 0 );
48
49         // Work around doubled linefeeds in Tinymce 3.5b2
50
51         $plaintext = intval(get_pconfig(local_user(),'system','plaintext'));
52         if(! $plaintext) {
53                 $body = fix_mce_lf($body);
54         }
55         
56         $ret = send_message($recipient, $body, $subject, $replyto);
57         $norecip = false;
58
59         switch($ret){
60                 case -1:
61                         notice( t('No recipient selected.') . EOL );
62                         $norecip = true;
63                         break;
64                 case -2:
65                         notice( t('Unable to locate contact information.') . EOL );
66                         break;
67                 case -3:
68                         notice( t('Message could not be sent.') . EOL );
69                         break;
70                 case -4:
71                         notice( t('Message collection failure.') . EOL );
72                         break;
73                 default:
74                         info( t('Message sent.') . EOL );
75         }
76
77         // fake it to go back to the input form if no recipient listed
78
79         if($norecip) {
80                 $a->argc = 2;
81                 $a->argv[1] = 'new';
82         }
83
84 }
85
86 function message_content(&$a) {
87
88         $o = '';
89         nav_set_selected('messages');
90
91         if(! local_user()) {
92                 notice( t('Permission denied.') . EOL);
93                 return;
94         }
95
96         $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
97
98
99
100
101
102         $tpl = get_markup_template('mail_head.tpl');
103         $header = replace_macros($tpl, array(
104                 '$messages' => t('Messages'),
105                 '$tab_content' => $tab_content
106         ));
107
108
109         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
110                 if(! intval($a->argv[2]))
111                         return;
112                 $cmd = $a->argv[1];
113                 if($cmd === 'drop') {
114                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
115                                 intval($a->argv[2]),
116                                 intval(local_user())
117                         );
118                         if($r) {
119                                 info( t('Message deleted.') . EOL );
120                         }
121                         goaway($a->get_baseurl(true) . '/message' );
122                 }
123                 else {
124                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
125                                 intval($a->argv[2]),
126                                 intval(local_user())
127                         );
128                         if(count($r)) {
129                                 $parent = $r[0]['parent-uri'];
130                                 $convid = $r[0]['convid'];
131
132                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
133                                         dbesc($parent),
134                                         intval(local_user())
135                                 );
136
137                                 // remove diaspora conversation pointer
138                                 // Actually if we do this, we can never receive another reply to that conversation,
139                                 // as we will never again have the info we need to re-create it. 
140                                 // We'll just have to orphan it. 
141
142                                 //if($convid) {
143                                 //      q("delete from conv where id = %d limit 1",
144                                 //              intval($convid)
145                                 //      );
146                                 //}
147
148                                 if($r)
149                                         info( t('Conversation removed.') . EOL );
150                         } 
151                         goaway($a->get_baseurl(true) . '/message' );
152                 }       
153         
154         }
155
156         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
157                 
158                 $o .= $header;
159                 
160                 $plaintext = false;
161                 if(intval(get_pconfig(local_user(),'system','plaintext')))
162                         $plaintext = true;
163
164
165                 $tpl = get_markup_template('msg-header.tpl');
166
167                 $a->page['htmlhead'] .= replace_macros($tpl, array(
168                         '$baseurl' => $a->get_baseurl(true),
169                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
170                         '$nickname' => $a->user['nickname'],
171                         '$linkurl' => t('Please enter a link URL:')
172                 ));
173         
174                 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
175         
176                 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
177
178 // here's sort of where we want to do contact autocomplete
179 // comment out the contact selector line just above and use the following one instead,
180 // then figure out how to make it do the right thing
181 // pictures would be nice, but that didn't seem to work when I tried it 
182 // (the json backend is found in mod/acl.php)
183
184 //              $select = '<input type="text" id="recip" name="messageto" value="' . $preselect .'" />';
185
186                 $tpl = get_markup_template('prv_message.tpl');
187                 $o .= replace_macros($tpl,array(
188                         '$header' => t('Send Private Message'),
189                         '$to' => t('To:'),
190                         '$subject' => t('Subject:'),
191                         '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
192                         '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
193                         '$readonly' => '',
194                         '$yourmessage' => t('Your message:'),
195                         '$select' => $select,
196                         '$parent' => '',
197                         '$upload' => t('Upload photo'),
198                         '$insert' => t('Insert web link'),
199                         '$wait' => t('Please wait')
200                 ));
201
202                 return $o;
203         }
204
205         if($a->argc == 1) {
206
207                 // list messages
208
209                 $o .= $header;
210                 
211                 $r = q("SELECT count(*) AS `total` FROM `mail` 
212                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
213                         intval(local_user()),
214                         dbesc($myprofile)
215                 );
216                 if(count($r))
217                         $a->set_pager_total($r[0]['total']);
218         
219                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
220                         `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
221                         count( * ) as count
222                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
223                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
224                         intval(local_user()),
225                         //
226                         intval($a->pager['start']),
227                         intval($a->pager['itemspage'])
228                 );
229                 if(! count($r)) {
230                         info( t('No messages.') . EOL);
231                         return $o;
232                 }
233
234                 $tpl = get_markup_template('mail_list.tpl');
235                 foreach($r as $rr) {
236                         if($rr['unknown']) {
237                                 $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
238                         }
239                         elseif (link_compare($rr['from-url'],$myprofile)){
240                                 $partecipants = sprintf( t("You and %s"), $rr['name']);
241                         }
242                         else {
243                                 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
244                         }
245                         
246                         $o .= replace_macros($tpl, array(
247                                 '$id' => $rr['id'],
248                                 '$from_name' => $partecipants,
249                                 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
250                                 '$sparkle' => ' sparkle',
251                                 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
252                                 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
253                                 '$delete' => t('Delete conversation'),
254                                 '$body' => template_escape($rr['body']),
255                                 '$to_name' => template_escape($rr['name']),
256                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
257                                 '$seen' => $rr['mailseen'],
258                                 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
259                         ));
260                 }
261                 $o .= paginate($a);     
262                 return $o;
263         }
264
265         if(($a->argc > 1) && (intval($a->argv[1]))) {
266
267                 $o .= $header;
268
269                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
270                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
271                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
272                         intval(local_user()),
273                         intval($a->argv[1])
274                 );
275                 if(count($r)) { 
276                         $contact_id = $r[0]['contact-id'];
277                         $convid = $r[0]['convid'];
278
279                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
280                         if($convid)
281                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
282                                         dbesc($r[0]['parent-uri']),
283                                         intval($convid)
284                                 );  
285
286                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
287                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
288                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
289                                 intval(local_user())
290                         );
291                 }
292                 if(! count($messages)) {
293                         notice( t('Message not available.') . EOL );
294                         return $o;
295                 }
296
297                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
298                         dbesc($r[0]['parent-uri']),
299                         intval(local_user())
300                 );
301
302                 require_once("include/bbcode.php");
303
304                 $tpl = get_markup_template('msg-header.tpl');
305         
306                 $a->page['htmlhead'] .= replace_macros($tpl, array(
307                         '$nickname' => $a->user['nickname'],
308                         '$baseurl' => $a->get_baseurl(true)
309                 ));
310
311
312                 $mails = array();
313                 $seen = 0;
314                 $unknown = false;
315
316                 foreach($messages as $message) {
317                         if($message['unknown'])
318                                 $unknown = true;
319                         if($message['from-url'] == $myprofile) {
320                                 $from_url = $myprofile;
321                                 $sparkle = '';
322                         }
323                         else {
324                                 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
325                                 $sparkle = ' sparkle';
326                         }
327
328
329                         $Text = $message['body'];
330                         $saved_image = '';
331                         $img_start = strpos($Text,'[img]data:');
332                         $img_end = strpos($Text,'[/img]');
333
334                         if($img_start !== false && $img_end !== false && $img_end > $img_start) {
335                                 $start_fragment = substr($Text,0,$img_start);
336                                 $img_start += strlen('[img]');
337                                 $saved_image = substr($Text,$img_start,$img_end - $img_start);
338                                 $end_fragment = substr($Text,$img_end + strlen('[/img]'));              
339                                 $Text = $start_fragment . '[!#saved_image#!]' . $end_fragment;
340                                 $search = '/\[url\=(.*?)\]\[!#saved_image#!\]\[\/url\]' . '/is';
341                                 $replace = '[url=' . z_path() . '/redir/' . $message['contact-id'] 
342                                         . '?f=1&url=' . '$1' . '][!#saved_image#!][/url]' ;
343
344                                 $Text = preg_replace($search,$replace,$Text);
345
346                         if(strlen($saved_image))
347                                 $message['body'] = str_replace('[!#saved_image#!]', '[img]' . $saved_image . '[/img]',$Text);
348                         }
349
350                         $mails[] = array(
351                                 'id' => $message['id'],
352                                 'from_name' => template_escape($message['from-name']),
353                                 'from_url' => $from_url,
354                                 'sparkle' => $sparkle,
355                                 'from_photo' => $message['from-photo'],
356                                 'subject' => template_escape($message['title']),
357                                 'body' => template_escape(smilies(bbcode($message['body']))),
358                                 'delete' => t('Delete message'),
359                                 'to_name' => template_escape($message['name']),
360                                 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
361                         );
362                                 
363                         $seen = $message['seen'];
364                 }
365                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
366                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
367                         
368
369                 $tpl = get_markup_template('mail_display.tpl');
370                 $o = replace_macros($tpl, array(
371                         '$thread_id' => $a->argv[1],
372                         '$thread_subject' => $message['title'],
373                         '$thread_seen' => $seen,
374                         '$delete' =>  t('Delete conversation'),
375                         '$canreply' => (($unknown) ? false : '1'),
376                         '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),                        
377                         '$mails' => $mails,
378                         
379                         // reply
380                         '$header' => t('Send Reply'),
381                         '$to' => t('To:'),
382                         '$subject' => t('Subject:'),
383                         '$subjtxt' => template_escape($message['title']),
384                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
385                         '$yourmessage' => t('Your message:'),
386                         '$text' => '',
387                         '$select' => $select,
388                         '$parent' => $parent,
389                         '$upload' => t('Upload photo'),
390                         '$insert' => t('Insert web link'),
391                         '$wait' => t('Please wait')
392
393                 ));
394
395                 return $o;
396         }
397
398 }