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