]> git.mxchange.org Git - friendica.git/blob - mod/message.php
report failure to generate keys
[friendica.git] / mod / message.php
1 <?php
2
3 require_once('include/acl_selectors.php');
4
5 function message_init(&$a) {
6
7
8 }
9
10 function message_post(&$a) {
11
12         if(! local_user()) {
13                 notice( t('Permission denied.') . EOL);
14                 return;
15         }
16
17         $replyto   = ((x($_POST,'replyto'))   ? notags(trim($_POST['replyto']))   : '');
18         $subject   = ((x($_POST,'subject'))   ? notags(trim($_POST['subject']))   : '');
19         $body      = ((x($_POST,'body'))      ? escape_tags(trim($_POST['body'])) : '');
20         $recipient = ((x($_POST,'messageto')) ? intval($_POST['messageto'])       : 0 );
21
22         if(! $recipient) {
23                 notice( t('No recipient selected.') . EOL );
24                 return;
25         }
26
27         if(! strlen($subject))
28                 $subject = t('[no subject]');
29
30         $me = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
31                 intval(local_user())
32         );
33         $contact = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
34                         intval($recipient),
35                         intval(local_user())
36         );
37
38         if(! (count($me) && (count($contact)))) {
39                 notice( t('Unable to locate contact information.') . EOL );
40                 return;
41         }
42
43         $hash = random_string();
44         $uri = 'urn:X-dfrn:' . $a->get_baseurl() . ':' . local_user() . ':' . $hash ;
45
46         if(! strlen($replyto))
47                 $replyto = $uri;
48
49         $r = q("INSERT INTO `mail` ( `uid`, `from-name`, `from-photo`, `from-url`, 
50                 `contact-id`, `title`, `body`, `seen`, `replied`, `uri`, `parent-uri`, `created`)
51                 VALUES ( %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, '%s', '%s', '%s' )",
52                 intval(local_user()),
53                 dbesc($me[0]['name']),
54                 dbesc($me[0]['thumb']),
55                 dbesc($me[0]['url']),
56                 intval($recipient),
57                 dbesc($subject),
58                 dbesc($body),
59                 1,
60                 0,
61                 dbesc($uri),
62                 dbesc($replyto),
63                 datetime_convert()
64         );
65         $r = q("SELECT * FROM `mail` WHERE `uri` = '%s' and `uid` = %d LIMIT 1",
66                 dbesc($uri),
67                 intval(local_user())
68         );
69         if(count($r))
70                 $post_id = $r[0]['id'];
71
72         $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
73         
74         if($post_id) {
75                 proc_close(proc_open("\"$php_path\" \"include/notifier.php\" \"mail\" \"$post_id\" &",
76                         array(),$foo));
77                 notice( t('Message sent.') . EOL );
78         }
79         else {
80                 notice( t('Message could not be sent.') . EOL );
81         }
82         return;
83
84 }
85
86 function message_content(&$a) {
87
88         $o = '';
89         $o .= '<script> $(document).ready(function() { $(\'#nav-messages-link\').addClass(\'nav-selected\'); });</script>';
90
91         if(! local_user()) {
92                 notice( t('Permission denied.') . EOL);
93                 return;
94         }
95
96         $myprofile = $a->get_baseurl() . '/profile/' . $a->user['nickname'];
97
98
99         $tpl = load_view_file('view/mail_head.tpl');
100         $header = replace_macros($tpl, array(
101                 '$messages' => t('Messages'),
102                 '$inbox' => t('Inbox'),
103                 '$outbox' => t('Outbox'),
104                 '$new' => t('New Message')
105         ));
106
107
108         if(($a->argc == 3) && ($a->argv[1] === 'drop' || $a->argv[1] === 'dropconv')) {
109                 if(! intval($a->argv[2]))
110                         return;
111                 $cmd = $a->argv[1];
112                 if($cmd === 'drop') {
113                         $r = q("DELETE FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
114                                 intval($a->argv[2]),
115                                 intval(local_user())
116                         );
117                         if($r) {
118                                 notice( t('Message deleted.') . EOL );
119                         }
120                         goaway($a->get_baseurl() . '/message' );
121                 }
122                 else {
123                         $r = q("SELECT `parent-uri` FROM `mail` WHERE `id` = %d AND `uid` = %d LIMIT 1",
124                                 intval($a->argv[2]),
125                                 intval(local_user())
126                         );
127                         if(count($r)) {
128                                 $parent = $r[0]['parent-uri'];
129                                 $r = q("DELETE FROM `mail` WHERE `parent-uri` = '%s' AND `uid` = %d ",
130                                         dbesc($parent),
131                                         intval(local_user())
132                                 );
133                                 if($r)
134                                         notice( t('Conversation removed.') . EOL );
135                         } 
136                         goaway($a->get_baseurl() . '/message' );
137                 }       
138         
139         }
140
141         if(($a->argc > 1) && ($a->argv[1] === 'new')) {
142                 
143                 $tpl = load_view_file('view/msg-header.tpl');
144         
145                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
146
147                 $select = contact_select('messageto','message-to-select', false, 4, true);
148                 $tpl = load_view_file('view/prv_message.tpl');
149                 $o .= replace_macros($tpl,array(
150                         '$header' => t('Send Private Message'),
151                         '$to' => t('To:'),
152                         '$subject' => t('Subject:'),
153                         '$subjtxt' => '',
154                         '$readonly' => '',
155                         '$yourmessage' => t('Your message:'),
156                         '$select' => $select,
157                         '$parent' => '',
158                         '$upload' => t('Upload photo'),
159                         '$insert' => t('Insert web link'),
160                         '$wait' => t('Please wait')
161
162                 ));
163
164                 return $o;
165         }
166
167         if(($a->argc == 1) || ($a->argc == 2 && $a->argv[1] === 'sent')) {
168
169                 $o .= $header;
170                 
171                 if($a->argc == 2)
172                         $eq = '='; // I'm not going to bother escaping this.
173                 else
174                         $eq = '!='; // or this.
175
176                 $r = q("SELECT count(*) AS `total` FROM `mail` 
177                         WHERE `mail`.`uid` = %d AND `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC",
178                         intval(local_user()),
179                         dbesc($myprofile)
180                 );
181                 if(count($r))
182                         $a->set_pager_total($r[0]['total']);
183         
184                 $r = q("SELECT max(`mail`.`created`) AS `mailcreated`, min(`mail`.`seen`) AS `mailseen`, 
185                         `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 `from-url` $eq '%s' GROUP BY `parent-uri` ORDER BY `created` DESC  LIMIT %d , %d ",
188                         intval(local_user()),
189                         dbesc($myprofile),
190                         intval($a->pager['start']),
191                         intval($a->pager['itemspage'])
192                 );
193                 if(! count($r)) {
194                         notice( t('No messages.') . EOL);
195                         return $o;
196                 }
197
198                 $tpl = load_view_file('view/mail_list.tpl');
199                 foreach($r as $rr) {
200                         $o .= replace_macros($tpl, array(
201                                 '$id' => $rr['id'],
202                                 '$from_name' =>$rr['from-name'],
203                                 '$from_url' => $a->get_baseurl() . '/redir/' . $rr['contact-id'],
204                                 '$sparkle' => ' sparkle',
205                                 '$from_photo' => $rr['from-photo'],
206                                 '$subject' => (($rr['mailseen']) ? $rr['title'] : '<strong>' . $rr['title'] . '</strong>'),
207                                 '$delete' => t('Delete conversation'),
208                                 '$body' => $rr['body'],
209                                 '$to_name' => $rr['name'],
210                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$rr['mailcreated'],'D, d M Y - g:i A')
211                         ));
212                 }
213                 $o .= paginate($a);     
214                 return $o;
215         }
216
217         if(($a->argc > 1) && (intval($a->argv[1]))) {
218
219                 $o .= $header;
220
221                 $r = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
222                         FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
223                         WHERE `mail`.`uid` = %d AND `mail`.`id` = %d LIMIT 1",
224                         intval(local_user()),
225                         intval($a->argv[1])
226                 );
227                 if(count($r)) { 
228                         $contact_id = $r[0]['contact-id'];
229                         $messages = q("SELECT `mail`.*, `contact`.`name`, `contact`.`url`, `contact`.`thumb` 
230                                 FROM `mail` LEFT JOIN `contact` ON `mail`.`contact-id` = `contact`.`id` 
231                                 WHERE `mail`.`uid` = %d AND `mail`.`parent-uri` = '%s' ORDER BY `mail`.`created` ASC",
232                                 intval(local_user()),
233                                 dbesc($r[0]['parent-uri'])
234                         );
235                 }
236                 if(! count($messages)) {
237                         notice( t('Message not available.') . EOL );
238                         return $o;
239                 }
240
241                 $r = q("UPDATE `mail` SET `seen` = 1 WHERE `parent-uri` = '%s' AND `uid` = %d",
242                         dbesc($r[0]['parent-uri']),
243                         intval(local_user())
244                 );
245
246                 require_once("include/bbcode.php");
247
248                 $tpl = load_view_file('view/msg-header.tpl');
249         
250                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
251
252
253                 $tpl = load_view_file('view/mail_conv.tpl');
254                 foreach($messages as $message) {
255                         if($message['from-url'] == $myprofile) {
256                                 $from_url = $myprofile;
257                                 $sparkle = '';
258                         }
259                         else {
260                                 $from_url = $a->get_baseurl() . '/redir/' . $message['contact-id'];
261                                 $sparkle = ' sparkle';
262                         }
263                         $o .= replace_macros($tpl, array(
264                                 '$id' => $message['id'],
265                                 '$from_name' =>$message['from-name'],
266                                 '$from_url' => $from_url,
267                                 '$sparkle' => $sparkle,
268                                 '$from_photo' => $message['from-photo'],
269                                 '$subject' => $message['title'],
270                                 '$body' => bbcode($message['body']),
271                                 '$delete' => t('Delete message'),
272                                 '$to_name' => $message['name'],
273                                 '$date' => datetime_convert('UTC',date_default_timezone_get(),$message['created'],'D, d M Y - g:i A')
274                         ));
275                                 
276                 }
277                 $select = $message['name'] . '<input type="hidden" name="messageto" value="' . $contact_id . '" />';
278                 $parent = '<input type="hidden" name="replyto" value="' . $message['parent-uri'] . '" />';
279                 $tpl = load_view_file('view/prv_message.tpl');
280                 $o .= replace_macros($tpl,array(
281                         '$header' => t('Send Reply'),
282                         '$to' => t('To:'),
283                         '$subject' => t('Subject:'),
284                         '$subjtxt' => $message['title'],
285                         '$readonly' => ' readonly="readonly" style="background: #BBBBBB;" ',
286                         '$yourmessage' => t('Your message:'),
287                         '$select' => $select,
288                         '$parent' => $parent,
289                         '$upload' => t('Upload photo'),
290                         '$insert' => t('Insert web link'),
291                         '$wait' => t('Please wait')
292
293                 ));
294
295                 return $o;
296         }
297
298 }