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