]> git.mxchange.org Git - friendica.git/blob - mod/message.php
missing salmon key? report it.
[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                 default:
31                         info( t('Message sent.') . EOL );
32         }
33
34 }
35
36 function message_content(&$a) {
37
38         $o = '';
39         $o .= '<script> $(document).ready(function() { $(\'#nav-messages-link\').addClass(\'nav-selected\'); });</script>';
40
41         if(! local_user()) {
42                 notice( t('Permission denied.') . EOL);
43                 return;
44         }
45
46         $myprofile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
47
48
49         $tpl = get_markup_template('mail_head.tpl');
50         $header = replace_macros($tpl, array(
51                 '$messages' => t('Messages'),
52                 '$inbox' => t('Inbox'),
53                 '$outbox' => t('Outbox'),
54                 '$new' => t('New Message')
55         ));
56
57
58         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
59                 if(! intval($a->argv[2]))
60                         return;
61                 $cmd = $a->argv[1];
62                 if($cmd === 'drop') {
63                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
64                                 intval($a->argv[2]),
65                                 intval(local_user())
66                         );
67                         if($r) {
68                                 info( t('Message deleted.') . EOL );
69                         }
70                         goaway($a->get_baseurl() . '/message' );
71                 }
72                 else {
73                         $r = q("SELECT `parent-uri` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
74                                 intval($a->argv[2]),
75                                 intval(local_user())
76                         );
77                         if(count($r)) {
78                                 $parent = $r[0]['parent-uri'];
79                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
80                                         dbesc($parent),
81                                         intval(local_user())
82                                 );
83                                 if($r)
84                                         info( t('Conversation removed.') . EOL );
85                         } 
86                         goaway($a->get_baseurl() . '/message' );
87                 }       
88         
89         }
90
91         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
92                 
93                 $tpl = get_markup_template('msg-header.tpl');
94
95                 $a->page['htmlhead'] .= replace_macros($tpl, array(
96                         '$baseurl' => $a->get_baseurl(),
97                         '$nickname' => $a->user['nickname'],
98                         '$linkurl' => t('Please enter a link URL:')
99                 ));
100         
101                 $preselect = (isset($a->argv[2])?array($a->argv[2]):false);
102         
103                 $select = contact_select('messageto','message-to-select', $preselect, 4, true);
104                 $tpl = get_markup_template('prv_message.tpl');
105                 $o .= replace_macros($tpl,array(
106                         '$header' => t('Send Private Message'),
107                         '$to' => t('To:'),
108                         '$subject' => t('Subject:'),
109                         '$subjtxt' => '',
110                         '$readonly' => '',
111                         '$yourmessage' => t('Your message:'),
112                         '$select' => $select,
113                         '$parent' => '',
114                         '$upload' => t('Upload photo'),
115                         '$insert' => t('Insert web link'),
116                         '$wait' => t('Please wait')
117
118                 ));
119
120                 return $o;
121         }
122
123         if(($a->argc == 1) || ($a->argc == 2 && $a->argv[1] === 'sent')) {
124
125                 $o .= $header;
126                 
127                 if($a->argc == 2)
128                         $eq = '='; // I'm not going to bother escaping this.
129                 else
130                         $eq = '!='; // or this.
131
132                 $r = q("SELECT count(*) AS `total` FROM `mail` 
133                         WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC",
134                         intval(local_user()),
135                         dbesc($myprofile)
136                 );
137                 if(count($r))
138                         $a->set_pager_total($r[0]['total']);
139         
140                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
141                         `mail`.* , `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
142                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
143                         WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC  LIMIT %d , %d ",
144                         intval(local_user()),
145                         dbesc($myprofile),
146                         intval($a->pager['start']),
147                         intval($a->pager['itemspage'])
148                 );
149                 if(! count($r)) {
150                         info( t('No messages.') . EOL);
151                         return $o;
152                 }
153
154                 $tpl = get_markup_template('mail_list.tpl');
155                 foreach($r as $rr) {
156                         $o .= replace_macros($tpl, array(
157                                 '$id' => $rr['id'],
158                                 '$from_name' =>$rr['from-name'],
159                                 '$from_url' => $a->get_baseurl() . '/redir/' . $rr['contact-id'],
160                                 '$sparkle' => ' sparkle',
161                                 '$from_photo' => $rr['thumb'],
162                                 '$subject' => (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'),
163                                 '$delete' => t('Delete conversation'),
164                                 '$body' => $rr['body'],
165                                 '$to_name' => $rr['name'],
166                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'], t('D, d M Y - g:i A'))
167                         ));
168                 }
169                 $o .= paginate($a);     
170                 return $o;
171         }
172
173         if(($a->argc > 1) && (intval($a->argv[1]))) {
174
175                 $o .= $header;
176
177                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
178                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
179                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
180                         intval(local_user()),
181                         intval($a->argv[1])
182                 );
183                 if(count($r)) { 
184                         $contact_id = $r[0]['contact-id'];
185                         $messages = q("SELECT `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 `mail`.`parent-uri` = '%s' ORDER BY `mail`.`created` ASC",
188                                 intval(local_user()),
189                                 dbesc($r[0]['parent-uri'])
190                         );
191                 }
192                 if(! count($messages)) {
193                         notice( t('Message not available.') . EOL );
194                         return $o;
195                 }
196
197                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
198                         dbesc($r[0]['parent-uri']),
199                         intval(local_user())
200                 );
201
202                 require_once("include/bbcode.php");
203
204                 $tpl = get_markup_template('msg-header.tpl');
205         
206                 $a->page['htmlhead'] .= replace_macros($tpl, array(
207                         '$nickname' => $a->user['nickname'],
208                         '$baseurl' => $a->get_baseurl()
209                 ));
210
211
212                 $tpl = get_markup_template('mail_conv.tpl');
213                 foreach($messages as $message) {
214                         if($message['from-url'] == $myprofile) {
215                                 $from_url = $myprofile;
216                                 $sparkle = '';
217                         }
218                         else {
219                                 $from_url = $a->get_baseurl() . '/redir/' . $message['contact-id'];
220                                 $sparkle = ' sparkle';
221                         }
222                         $o .= replace_macros($tpl, array(
223                                 '$id' => $message['id'],
224                                 '$from_name' =>$message['from-name'],
225                                 '$from_url' => $from_url,
226                                 '$sparkle' => $sparkle,
227                                 '$from_photo' => $message['from-photo'],
228                                 '$subject' => $message['title'],
229                                 '$body' => smilies(bbcode($message['body'])),
230                                 '$delete' => t('Delete message'),
231                                 '$to_name' => $message['name'],
232                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A')
233                         ));
234                                 
235                 }
236                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
237                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
238                 $tpl = get_markup_template('prv_message.tpl');
239                 $o .= replace_macros($tpl,array(
240                         '$header' => t('Send Reply'),
241                         '$to' => t('To:'),
242                         '$subject' => t('Subject:'),
243                         '$subjtxt' => $message['title'],
244                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
245                         '$yourmessage' => t('Your message:'),
246                         '$select' => $select,
247                         '$parent' => $parent,
248                         '$upload' => t('Upload photo'),
249                         '$insert' => t('Insert web link'),
250                         '$wait' => t('Please wait')
251
252                 ));
253
254                 return $o;
255         }
256
257 }