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