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