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