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