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