]> git.mxchange.org Git - friendica.git/blob - mod/message.php
Merge branch 'pull'
[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_post(&$a) {
7
8         if(! local_user()) {
9                 notice( t('Permission denied.') . EOL);
10                 return;
11         }
12
13         $replyto   = ((x($_REQUEST,'replyto'))   ? notags(trim($_REQUEST['replyto']))   : '');
14         $subject   = ((x($_REQUEST,'subject'))   ? notags(trim($_REQUEST['subject']))   : '');
15         $body      = ((x($_REQUEST,'body'))      ? escape_tags(trim($_REQUEST['body'])) : '');
16         $recipient = ((x($_REQUEST,'messageto')) ? intval($_REQUEST['messageto'])       : 0 );
17
18         // Work around doubled linefeeds in Tinymce 3.5b2
19
20         $plaintext = intval(get_pconfig(local_user(),'system','plaintext'));
21         if(! $plaintext) {
22                 $body = str_replace("\r\n","\n",$body);
23                 $body = str_replace("\n\n","\n",$body);
24         }
25         
26         $ret = send_message($recipient, $body, $subject, $replyto);
27         $norecip = false;
28
29         switch($ret){
30                 case -1:
31                         notice( t('No recipient selected.') . EOL );
32                         $norecip = true;
33                         break;
34                 case -2:
35                         notice( t('Unable to locate contact information.') . EOL );
36                         break;
37                 case -3:
38                         notice( t('Message could not be sent.') . EOL );
39                         break;
40                 case -4:
41                         notice( t('Message collection failure.') . EOL );
42                         break;
43                 default:
44                         info( t('Message sent.') . EOL );
45         }
46
47         // fake it to go back to the input form if no recipient listed
48
49         if($norecip) {
50                 $a->argc = 2;
51                 $a->argv[1] = 'new';
52         }
53
54 }
55
56 function message_content(&$a) {
57
58         $o = '';
59         nav_set_selected('messages');
60
61         if(! local_user()) {
62                 notice( t('Permission denied.') . EOL);
63                 return;
64         }
65
66         $myprofile = $a->get_baseurl(true) . '/profile/' . $a->user['nickname'];
67
68
69         $tabs = array(
70                 array(
71                         'label' => t('Inbox'),
72                         'url'=> $a->get_baseurl(true) . '/message',
73                         'sel'=> (($a->argc == 1) ? 'active' : ''),
74                 ),
75                 array(
76                         'label' => t('Outbox'),
77                         'url' => $a->get_baseurl(true) . '/message/sent',
78                         'sel'=> (($a->argv[1] == 'sent') ? 'active' : ''),
79                 ),
80                 array(
81                         'label' => t('New Message'),
82                         'url' => $a->get_baseurl(true) . '/message/new',
83                         'sel'=> (($a->argv[1] == 'new') ? 'active' : ''),
84                 ),
85         );
86         $tpl = get_markup_template('common_tabs.tpl');
87         $tab_content = replace_macros($tpl, array('$tabs'=>$tabs));
88
89
90         $tpl = get_markup_template('mail_head.tpl');
91         $header = replace_macros($tpl, array(
92                 '$messages' => t('Messages'),
93                 '$tab_content' => $tab_content
94         ));
95
96
97         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
98                 if(! intval($a->argv[2]))
99                         return;
100                 $cmd = $a->argv[1];
101                 if($cmd === 'drop') {
102                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
103                                 intval($a->argv[2]),
104                                 intval(local_user())
105                         );
106                         if($r) {
107                                 info( t('Message deleted.') . EOL );
108                         }
109                         goaway($a->get_baseurl(true) . '/message' );
110                 }
111                 else {
112                         $r = q("SELECT `parent-uri`,`convid` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
113                                 intval($a->argv[2]),
114                                 intval(local_user())
115                         );
116                         if(count($r)) {
117                                 $parent = $r[0]['parent-uri'];
118                                 $convid = $r[0]['convid'];
119
120                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
121                                         dbesc($parent),
122                                         intval(local_user())
123                                 );
124
125                                 // remove diaspora conversation pointer
126                                 // Actually if we do this, we can never receive another reply to that conversation,
127                                 // as we will never again have the info we need to re-create it. 
128                                 // We'll just have to orphan it. 
129
130                                 //if($convid) {
131                                 //      q("delete from conv where id = %d limit 1",
132                                 //              intval($convid)
133                                 //      );
134                                 //}
135
136                                 if($r)
137                                         info( t('Conversation removed.') . EOL );
138                         } 
139                         goaway($a->get_baseurl(true) . '/message' );
140                 }       
141         
142         }
143
144         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
145                 
146                 $o .= $header;
147                 
148                 $plaintext = false;
149                 if(intval(get_pconfig(local_user(),'system','plaintext')))
150                         $plaintext = true;
151
152
153                 $tpl = get_markup_template('msg-header.tpl');
154
155                 $a->page['htmlhead'] .= replace_macros($tpl, array(
156                         '$baseurl' => $a->get_baseurl(true),
157                         '$editselect' => (($plaintext) ? 'none' : '/(profile-jot-text|prvmail-text)/'),
158                         '$nickname' => $a->user['nickname'],
159                         '$linkurl' => t('Please enter a link URL:')
160                 ));
161         
162                 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
163         
164                 $select = contact_select('messageto','message-to-select', $preselect, 4, true, false, false, 10);
165                 $tpl = get_markup_template('prv_message.tpl');
166                 $o .= replace_macros($tpl,array(
167                         '$header' => t('Send Private Message'),
168                         '$to' => t('To:'),
169                         '$subject' => t('Subject:'),
170                         '$subjtxt' => ((x($_REQUEST,'subject')) ? strip_tags($_REQUEST['subject']) : ''),
171                         '$text' => ((x($_REQUEST,'body')) ? escape_tags(htmlspecialchars($_REQUEST['body'])) : ''),
172                         '$readonly' => '',
173                         '$yourmessage' => t('Your message:'),
174                         '$select' => $select,
175                         '$parent' => '',
176                         '$upload' => t('Upload photo'),
177                         '$insert' => t('Insert web link'),
178                         '$wait' => t('Please wait')
179                 ));
180
181                 return $o;
182         }
183
184         if(($a->argc == 1) || ($a->argc == 2 && $a->argv[1] === 'sent')) {
185
186                 $o .= $header;
187                 
188                 if($a->argc == 2)
189                         $eq = '='; // I'm not going to bother escaping this.
190                 else
191                         $eq = '!='; // or this.
192
193                 $r = q("SELECT count(*) AS `total` FROM `mail` 
194                         WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC",
195                         intval(local_user()),
196                         dbesc($myprofile)
197                 );
198                 if(count($r))
199                         $a->set_pager_total($r[0]['total']);
200         
201                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
202                         `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` , `contact`.`network`  
203                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
204                         WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `mailcreated` DESC  LIMIT %d , %d ",
205                         intval(local_user()),
206                         dbesc($myprofile),
207                         intval($a->pager['start']),
208                         intval($a->pager['itemspage'])
209                 );
210                 if(! count($r)) {
211                         info( t('No messages.') . EOL);
212                         return $o;
213                 }
214
215                 $tpl = get_markup_template('mail_list.tpl');
216                 foreach($r as $rr) {
217                         $o .= replace_macros($tpl, array(
218                                 '$id' => $rr['id'],
219                                 '$from_name' =>$rr['from-name'],
220                                 '$from_url' => (($rr['network'] === NETWORK_DFRN) ? $a->get_baseurl(true) . '/redir/' . $rr['contact-id'] : $rr['url']),
221                                 '$sparkle' => ' sparkle',
222                                 '$from_photo' => $rr['thumb'],
223                                 '$subject' => template_escape((($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>')),
224                                 '$delete' => t('Delete conversation'),
225                                 '$body' => template_escape($rr['body']),
226                                 '$to_name' => template_escape($rr['name']),
227                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A'))
228                         ));
229                 }
230                 $o .= paginate($a);     
231                 return $o;
232         }
233
234         if(($a->argc > 1) && (intval($a->argv[1]))) {
235
236                 $o .= $header;
237
238                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
239                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
240                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
241                         intval(local_user()),
242                         intval($a->argv[1])
243                 );
244                 if(count($r)) { 
245                         $contact_id = $r[0]['contact-id'];
246                         $convid = $r[0]['convid'];
247
248                         $sql_extra = sprintf(" and `mail`.`parent-uri` = '%s' ", dbesc($r[0]['parent-uri']));
249                         if($convid)
250                                 $sql_extra = sprintf(" and ( `mail`.`parent-uri` = '%s' OR `mail`.`convid` = '%d' ) ",
251                                         dbesc($r[0]['parent-uri']),
252                                         intval($convid)
253                                 );  
254
255                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
256                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
257                                 WHERE `mail`.`uid` = %d $sql_extra ORDER BY `mail`.`created` ASC",
258                                 intval(local_user())
259                         );
260                 }
261                 if(! count($messages)) {
262                         notice( t('Message not available.') . EOL );
263                         return $o;
264                 }
265
266                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
267                         dbesc($r[0]['parent-uri']),
268                         intval(local_user())
269                 );
270
271                 require_once("include/bbcode.php");
272
273                 $tpl = get_markup_template('msg-header.tpl');
274         
275                 $a->page['htmlhead'] .= replace_macros($tpl, array(
276                         '$nickname' => $a->user['nickname'],
277                         '$baseurl' => $a->get_baseurl(true)
278                 ));
279
280
281                 $tpl = get_markup_template('mail_conv.tpl');
282                 foreach($messages as $message) {
283                         if($message['from-url'] == $myprofile) {
284                                 $from_url = $myprofile;
285                                 $sparkle = '';
286                         }
287                         else {
288                                 $from_url = $a->get_baseurl(true) . '/redir/' . $message['contact-id'];
289                                 $sparkle = ' sparkle';
290                         }
291                         $o .= replace_macros($tpl, array(
292                                 '$id' => $message['id'],
293                                 '$from_name' => template_escape($message['from-name']),
294                                 '$from_url' => $from_url,
295                                 '$sparkle' => $sparkle,
296                                 '$from_photo' => $message['from-photo'],
297                                 '$subject' => template_escape($message['title']),
298                                 '$body' => template_escape(smilies(bbcode($message['body']))),
299                                 '$delete' => t('Delete message'),
300                                 '$to_name' => template_escape($message['name']),
301                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A')
302                         ));
303                                 
304                 }
305                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
306                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
307                 $tpl = get_markup_template('prv_message.tpl');
308                 $o .= replace_macros($tpl,array(
309                         '$header' => t('Send Reply'),
310                         '$to' => t('To:'),
311                         '$subject' => t('Subject:'),
312                         '$subjtxt' => template_escape($message['title']),
313                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
314                         '$yourmessage' => t('Your message:'),
315                         '$text' => '',
316                         '$select' => $select,
317                         '$parent' => $parent,
318                         '$upload' => t('Upload photo'),
319                         '$insert' => t('Insert web link'),
320                         '$wait' => t('Please wait')
321                 ));
322
323                 return $o;
324         }
325
326 }