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