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