]> git.mxchange.org Git - friendica.git/blob - mod/message.php
email autocomplete
[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                 onSelect: function(value,data) {
30                         $("#recip-complete").val(data);
31                 }                       
32         });
33
34 }); 
35
36 </script>
37 EOT;
38         
39 }
40
41 function message_post(&$a) {
42
43         if(! local_user()) {
44                 notice( t('Permission denied.') . EOL);
45                 return;
46         }
47
48         $replyto   = ((x($_REQUEST,'replyto'))   ? notags(trim($_REQUEST['replyto']))   : '');
49         $subject   = ((x($_REQUEST,'subject'))   ? notags(trim($_REQUEST['subject']))   : '');
50         $body      = ((x($_REQUEST,'body'))      ? escape_tags(trim($_REQUEST['body'])) : '');
51         $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto'])       : 0 );
52
53         // Work around doubled linefeeds in Tinymce 3.5b2
54
55         $plaintext = intval(get_pconfig(local_user(),'system','plaintext'));
56         if(! $plaintext) {
57                 $body = fix_mce_lf($body);
58         }
59         
60         $ret = send_message($recipient, $body, $subject, $replyto);
61         $norecip = false;
62
63         switch($ret){
64                 case -1:
65                         notice( t('No recipient selected.') . EOL );
66                         $norecip = true;
67                         break;
68                 case -2:
69                         notice( t('Unable to locate contact information.') . EOL );
70                         break;
71                 case -3:
72                         notice( t('Message could not be sent.') . EOL );
73                         break;
74                 case -4:
75                         notice( t('Message collection failure.') . EOL );
76                         break;
77                 default:
78                         info( t('Message sent.') . EOL );
79         }
80
81         // fake it to go back to the input form if no recipient listed
82
83         if($norecip) {
84                 $a->argc = 2;
85                 $a->argv[1] = 'new';
86         }
87
88 }
89
90 function message_content(&$a) {
91
92         $o = '';
93         nav_set_selected('messages');
94
95         if(! local_user()) {
96                 notice( t('Permission denied.') . EOL);
97                 return;
98         }
99
100         $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
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
177                 $prename = $preurl = $preid = '';
178
179                 if($preselect) {
180                         $r = q("select name, url, id from contact where uid = %d and id = %d limit 1",
181                                 intval(local_user()),
182                                 intval($a->argv[2])
183                         );
184                         if(count($r)) {
185                                 $prename = $r[0]['name'];
186                                 $preurl = $r[0]['url'];
187                                 $preid = $r[0]['id'];
188                         }
189                 }        
190
191                 $prefill = (($preselect) ? $prename . ' [' . $preurl . ']' : '');
192
193                 // the ugly select box
194                 
195                 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
196
197                 $tpl = get_markup_template('prv_message.tpl');
198                 $o .= replace_macros($tpl,array(
199                         '$header' => t('Send Private Message'),
200                         '$to' => t('To:'),
201                         '$prefill' => $prefill,
202                         '$autocomp' => $autocomp,
203                         '$preid' => $preid,
204                         '$subject' => t('Subject:'),
205                         '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
206                         '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
207                         '$readonly' => '',
208                         '$yourmessage' => t('Your message:'),
209                         '$select' => $select,
210                         '$parent' => '',
211                         '$upload' => t('Upload photo'),
212                         '$insert' => t('Insert web link'),
213                         '$wait' => t('Please wait')
214                 ));
215
216                 return $o;
217         }
218
219         if($a->argc == 1) {
220
221                 // list messages
222
223                 $o .= $header;
224                 
225                 $r = q("SELECT count(*) AS `total` FROM `mail` 
226                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `created` DESC",
227                         intval(local_user()),
228                         dbesc($myprofile)
229                 );
230                 if(count($r))
231                         $a->set_pager_total($r[0]['total']);
232         
233                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
234                         `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`,
235                         count( * ) as count
236                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
237                         WHERE `mail`.`uid` = %d GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
238                         intval(local_user()),
239                         //
240                         intval($a->pager['start']),
241                         intval($a->pager['itemspage'])
242                 );
243                 if(! count($r)) {
244                         info( t('No messages.') . EOL);
245                         return $o;
246                 }
247
248                 $tpl = get_markup_template('mail_list.tpl');
249                 foreach($r as $rr) {
250                         if($rr['unknown']) {
251                                 $partecipants = sprintf( t("Unknown sender - %s"),$rr['from-name']);
252                         }
253                         elseif (link_compare($rr['from-url'],$myprofile)){
254                                 $partecipants = sprintf( t("You and %s"), $rr['name']);
255                         }
256                         else {
257                                 $partecipants = sprintf( t("%s and You"), $rr['from-name']);
258                         }
259                         
260                         $o .= replace_macros($tpl, array(
261                                 '$id' => $rr['id'],
262                                 '$from_name' => $partecipants,
263                                 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
264                                 '$sparkle' => ' sparkle',
265                                 '$from_photo' => (($rr['thumb']) ? $rr['thumb'] : $rr['from-photo']),
266                                 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
267                                 '$delete' => t('Delete conversation'),
268                                 '$body' => template_escape($rr['body']),
269                                 '$to_name' => template_escape($rr['name']),
270                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A')),
271                                 '$seen' => $rr['mailseen'],
272                                 '$count' => sprintf( tt('%d message', '%d messages', $rr['count']), $rr['count']),
273                         ));
274                 }
275                 $o .= paginate($a);     
276                 return $o;
277         }
278
279         if(($a->argc > 1) && (intval($a->argv[1]))) {
280
281                 $o .= $header;
282
283                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
284                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
285                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
286                         intval(local_user()),
287                         intval($a->argv[1])
288                 );
289                 if(count($r)) { 
290                         $contact_id = $r[0]['contact-id'];
291                         $convid = $r[0]['convid'];
292
293                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
294                         if($convid)
295                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
296                                         dbesc($r[0]['parent-uri']),
297                                         intval($convid)
298                                 );  
299
300                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
301                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
302                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
303                                 intval(local_user())
304                         );
305                 }
306                 if(! count($messages)) {
307                         notice( t('Message not available.') . EOL );
308                         return $o;
309                 }
310
311                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
312                         dbesc($r[0]['parent-uri']),
313                         intval(local_user())
314                 );
315
316                 require_once("include/bbcode.php");
317
318                 $tpl = get_markup_template('msg-header.tpl');
319         
320                 $a->page['htmlhead'] .= replace_macros($tpl, array(
321                         '$nickname' => $a->user['nickname'],
322                         '$baseurl' => $a->get_baseurl(true)
323                 ));
324
325
326                 $mails = array();
327                 $seen = 0;
328                 $unknown = false;
329
330                 foreach($messages as $message) {
331                         if($message['unknown'])
332                                 $unknown = true;
333                         if($message['from-url'] == $myprofile) {
334                                 $from_url = $myprofile;
335                                 $sparkle = '';
336                         }
337                         else {
338                                 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
339                                 $sparkle = ' sparkle';
340                         }
341
342
343                         $Text = $message['body'];
344                         $saved_image = '';
345                         $img_start = strpos($Text,'[img]data:');
346                         $img_end = strpos($Text,'[/img]');
347
348                         if($img_start !== false && $img_end !== false && $img_end > $img_start) {
349                                 $start_fragment = substr($Text,0,$img_start);
350                                 $img_start += strlen('[img]');
351                                 $saved_image = substr($Text,$img_start,$img_end - $img_start);
352                                 $end_fragment = substr($Text,$img_end + strlen('[/img]'));              
353                                 $Text = $start_fragment . '[!#saved_image#!]' . $end_fragment;
354                                 $search = '/\[url\=(.*?)\]\[!#saved_image#!\]\[\/url\]' . '/is';
355                                 $replace = '[url=' . z_path() . '/redir/' . $message['contact-id'] 
356                                         . '?f=1&url=' . '$1' . '][!#saved_image#!][/url]' ;
357
358                                 $Text = preg_replace($search,$replace,$Text);
359
360                         if(strlen($saved_image))
361                                 $message['body'] = str_replace('[!#saved_image#!]', '[img]' . $saved_image . '[/img]',$Text);
362                         }
363
364                         $mails[] = array(
365                                 'id' => $message['id'],
366                                 'from_name' => template_escape($message['from-name']),
367                                 'from_url' => $from_url,
368                                 'sparkle' => $sparkle,
369                                 'from_photo' => $message['from-photo'],
370                                 'subject' => template_escape($message['title']),
371                                 'body' => template_escape(smilies(bbcode($message['body']))),
372                                 'delete' => t('Delete message'),
373                                 'to_name' => template_escape($message['name']),
374                                 'date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A'),
375                         );
376                                 
377                         $seen = $message['seen'];
378                 }
379                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
380                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
381                         
382
383                 $tpl = get_markup_template('mail_display.tpl');
384                 $o = replace_macros($tpl, array(
385                         '$thread_id' => $a->argv[1],
386                         '$thread_subject' => $message['title'],
387                         '$thread_seen' => $seen,
388                         '$delete' =>  t('Delete conversation'),
389                         '$canreply' => (($unknown) ? false : '1'),
390                         '$unknown_text' => t("No secure communications available. You <strong>may</strong> be able to respond from the sender's profile page."),                        
391                         '$mails' => $mails,
392                         
393                         // reply
394                         '$header' => t('Send Reply'),
395                         '$to' => t('To:'),
396                         '$subject' => t('Subject:'),
397                         '$subjtxt' => template_escape($message['title']),
398                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
399                         '$yourmessage' => t('Your message:'),
400                         '$text' => '',
401                         '$select' => $select,
402                         '$parent' => $parent,
403                         '$upload' => t('Upload photo'),
404                         '$insert' => t('Insert web link'),
405                         '$wait' => t('Please wait')
406
407                 ));
408
409                 return $o;
410         }
411
412 }