Merge branch 'ZNCv1' into MixedVersions
[ZNCAdmin.git] / zncadmin / ZNCServerV1.class.php
1 <?php
2 /* ZNCServerV1.class.php - ZNC Server Class - ZNCAdmin
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License 
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
17  */
18
19 $classinfo = array(
20         "name" => "ZNCServerV1",
21         "version" => 1.000,
22 );
23
24 class ZNCServerV1 {
25         private $host, $port;
26         private $connector;
27         
28         public function ZNCServerV1($host, $port, $connector = NULL) {
29                 $this->host = $host;
30                 $this->port = $port;
31                 if($connector == NULL)
32                         $connector = new HTTPConnector();
33                 $this->connector = $connector;
34         }
35
36     public function login($user, $pass) {
37         $post = array(
38             "user" => $user,
39             "pass" => $pass,
40             "submitted" => "1"
41         );
42         $this->connector->post("http://".$this->host.":".$this->port."/login", $post);
43         $http = $this->connector->get("http://".$this->host.":".$this->port."/?cookie_check=true");
44         $logged_in = !preg_match("/errorbar/i", $http);
45
46         return $logged_in;
47     }
48
49     public function getUserList() {
50         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/listusers");
51         $exp = explode('<div class="toptable">', $html);
52         $exp = explode('</div>', $exp[1]);
53         $exp = explode('<tbody>', $exp[0]);
54         $exp = explode('</tbody>', $exp[1]);
55         $list = explode('<tr class=', $exp[0]);
56         $userlist = array();
57         for($i = 1; $i < count($list); $i++) {
58             $userdata = array();
59             $exp = explode('<td>', $list[$i]);
60             $exp2 = explode('</td>', $exp[2]);
61             $userdata['user'] = $exp2[0];
62             $exp2 = explode('</td>', $exp[3]);
63             $userdata['server'] = $exp2[0];
64             $exp2 = explode('</td>', $exp[4]);
65             $userdata['clients'] = $exp2[0];
66             $userlist[] = $userdata;
67         }
68         return $userlist;
69     }
70
71     public function getSeenList() {
72         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/lastseen/");
73         $exp = explode('<div class="toptable">', $html);
74         $exp = explode('</div>', $exp[1]);
75         $exp = explode('<tbody>', $exp[0]);
76         $exp = explode('</tbody>', $exp[1]);
77         $list = explode('<tr class=', $exp[0]);
78         $seenlist = array();
79         for($i = 1; $i < count($list); $i++) {
80             $userdata = array();
81             $exp = explode('<td>', $list[$i]);
82
83             $exp2 = explode('</td>', $exp[1]);
84             $userdata['user'] = $exp2[0];
85             $exp2 = explode('</td>', $exp[2]);
86             $userdata['seen'] = $exp2[0];
87             $userdata['seen_unix'] = strtotime($exp2[0]);
88             $exp2 = explode('</td>', $exp[3]);
89             $userdata['info'] = $exp2[0];
90             $seenlist[] = $userdata;
91         }
92         return $seenlist;
93     }
94
95     /*
96     $settings array elements:
97         nick
98         altnick
99         ident
100     */
101     public function addZNC($username, $password, $settings, $servers, $modules, $others) {
102         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/adduser");
103         $exp = explode('name="_CSRF_Check" value="', $html);
104         $exp = explode('"', $exp[1]);
105         $csrf = $exp[0];
106         $post = array();
107         $post['_CSRF_Check'] = $csrf;
108         $post['submitted'] = '1';
109         //$post['doconnect'] = '1';
110         $post['user'] = $username;
111         $post['password'] = $password;
112         $post['password2'] = $password;
113         $post['nick'] = $settings['nick'];
114         $post['altnick'] = $settings['altnick'];
115         $post['ident'] = $settings['ident'];
116         $post['statusprefix'] = '*';
117         $post['loadmod'] = array();
118         foreach($modules as $name => $args) {
119             $post['loadmod'][] = $name;
120             if($args != "")
121                 $post['modargs_'.$name] = $args;
122         }
123
124         foreach($others as $name => $value) {
125             $post[$name] = $value;
126         }
127
128         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/adduser", $post);
129
130         if(!preg_match("/Invalid Submission/i", $html)) {
131             if(!$this->addNetwork($username, $settings, $servers, $other)) {
132                 return false;
133             }
134             return !preg_match("/Invalid Submission/i", $html);
135         }
136         return !preg_match("/Invalid Submission/i", $html);
137     }
138
139     public function addNetwork($username, $settings, $servers, $other) {
140         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/addnetwork?user=".$username);
141         $exp = explode('name="_CSRF_Check" value="', $html);
142         $exp = explode('"', $exp[1]);
143         $csrf = $exp[0];
144         $post = array();
145         $post['_CSRF_Check'] = $csrf;
146         $post['submitted'] = '1';
147         $post['network'] = 'default';
148         $post['nick'] = $settings['nick'];
149         $post['altnick'] = $settings['altnick'];
150         $post['ident'] = $settings['ident'];
151         $post['servers'] = implode("\n", $servers);
152         $post['doconnect'] = '1';
153         $post['floodprotection'] = '1';
154         $post['floodrate'] = '1.0';
155         $post['floodburst'] = '4';
156         $post['user'] = $username;
157
158         foreach($others as $name => $value) {
159             $post[$name] = $value;
160         }
161
162         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/addnetwork", $post);
163         return !preg_match("/Invalid Submission/i", $html);
164     }
165
166     public function delZNC($username) {
167         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/deluser?user=".$username);
168         $exp = explode('name="_CSRF_Check" value="', $html);
169         $exp = explode('"', $exp[1]);
170         $csrf = $exp[0];
171         $post = array();
172         $post['_CSRF_Check'] = $csrf;
173         $post['submitted'] = '1';
174         $post['user'] = $username;
175         $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/deluser", $post);
176     }
177
178     private function parseHTMLFields($html, &$post) {
179         preg_match_all("<input ([^\>]*)>", $html, $matches);
180         foreach($matches[0] as $input) {
181             $fields = array(0 => "");
182             $esc = false; $str = false;
183             $fieldid = 0;
184             for($i = 0; $i < strlen($input); $i++) {
185                 if($esc) {
186                     $esc = false;
187                     $fields[$fieldid] .= $input[$i];
188                     continue;
189                 }
190                 if($input[$i] == "\\") {
191                     $esc = true;
192                 }
193                 if($input[$i] == "\"") {
194                     $str = !$str;
195                     continue;
196                 }
197                 if($input[$i] == " " && !$str) {
198                     $fields[++$fieldid] = "";
199                     continue;
200                 }
201                 $fields[$fieldid] .= $input[$i];
202             }
203             $cfields = array();
204             foreach($fields as $field) {
205                 $fieldexp = explode("=", $field, 2);
206                 if(count($fieldexp) != 2) continue;
207                 $cfields[$fieldexp[0]] = $fieldexp[1];
208             }
209             $name = $cfields['name'];
210             switch($cfields['type']) {
211                 case "text":
212                 case "hidden":
213                     $value = $cfields['value'];
214                     break;
215                 case "checkbox":
216                     if($cfields['checked']) {
217                         $value = ($cfields['value'] ? $cfields['value'] : "1");
218                     } else
219                         $value = null;
220                     break;
221                 default:
222                     $value = null;
223             }
224             if($name && $value) {
225                 if(array_key_exists($name, $post)) {
226                     if(!is_array($post[$name])) {
227                         $cvalue = $post[$name];
228                         $post[$name] = array();
229                         $post[$name][] = $cvalue;
230                     }
231                     $post[$name][] = $value;
232                 } else
233                     $post[$name] = $value;
234             }
235         }
236         //textareas
237         $textareas = explode("<textarea ", $html);
238         for($i = 1; $i < count($textareas); $i++) {
239             $exp = explode(">", $textareas[$i]);
240             $name = explode("name=\"", $exp[0]);
241             $name = explode("\"", $name[1]);
242             $name = $name[0];
243             $content = explode("</textarea", $exp[1]);
244             $content = $content[0];
245             $post[$name] = $content;
246         }
247         //select boxes
248         $selectboxes = explode("<select ", $html);
249         for($j = 1; $j < count($selectboxes); $j++) {
250             $exp = explode(">", $selectboxes[$j], 2);
251             $name = explode("name=\"", $exp[0]);
252             $name = explode("\"", $name[1]);
253             $name = $name[0];
254             $content = explode("</select", $exp[1]);
255             $content = $content[0];
256             preg_match_all("<option ([^\>]*)>", $content, $matches);
257             foreach($matches[0] as $input) {
258                 $fields = array(0 => "");
259                 $esc = false; $str = false;
260                 $fieldid = 0;
261                 for($i = 0; $i < strlen($input); $i++) {
262                     if($esc) {
263                         $esc = false;
264                         $fields[$fieldid] .= $input[$i];
265                         continue;
266                     }
267                     if($input[$i] == "\\") {
268                         $esc = true;
269                     }
270                     if($input[$i] == "\"") {
271                         $str = !$str;
272                         continue;
273                     }
274                     if($input[$i] == " " && !$str) {
275                         $fields[++$fieldid] = "";
276                         continue;
277                     }
278                     $fields[$fieldid] .= $input[$i];
279                 }
280                 $cfields = array();
281                 foreach($fields as $field) {
282                     $fieldexp = explode("=", $field, 2);
283                     if(count($fieldexp) != 2) continue;
284                     $cfields[$fieldexp[0]] = $fieldexp[1];
285                 }
286                 if(!$post[$name] || $cfields['selected']) {
287                     $post[$name] = $cfields['value'];
288                 }
289             }
290         }
291     }
292
293     public function editZNC($username, $password = NULL, $new_servers = NULL, $add_modules = NULL, $del_modules = NULL, $others = NULL) {
294         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username);
295         if(preg_match("/No such username/i", $html)) return false;
296         $post = array();
297         $post['loadmod'] = array();
298         $this->parseHTMLFields($html, $post);
299         if($password) {
300             $post['password'] = $password;
301             $post['password2'] = $password;
302         }
303         if($new_servers) {
304             $post['servers'] = implode("\n", $new_servers);
305         }
306         foreach($add_modules as $name => $args) {
307             $post['loadmod'][] = $name;
308             if($args != "")
309                 $post['modargs_'.$name] = $args;
310         }
311         foreach($del_modules as $name => $args) {
312             foreach($post['loadmod'] as $index => $mod) {
313                 if(strtolower($mod) == strtolower($name))
314                     unset($post['loadmod'][$index]);
315             }
316         }
317         foreach($others as $name => $value) {
318             $post[$name] = $value;
319         }
320         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username, $post);
321         return !preg_match("/Invalid Submission/i", $html);
322     }
323
324     public function blockZNC($username, $block) {
325         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username);
326         $handle = fopen('/home/srvx/neonserv/log.txt', 'a');
327         fwrite($handle, "$html\r\n");
328         fclose($handle);
329         if(preg_match("/No such username/i", $html)) return ERR_USER_NOT_FOUND;
330         $post = array();
331         $post['user'] = $username;
332         $post['loadmod'] = array();
333         $this->parseHTMLFields($html, $post);
334         if(!$post['embed_blockuser_presented']) return ERR_MODULE_NOT_FOUND;
335         if(!($post['embed_blockuser_block'] xor $block)) return ERR_OK;
336         $post['embed_blockuser_block'] = ($block ? "1" : null);
337         if(!$block) {
338             $post['doconnect'] = 1;
339         }
340         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser", $post);
341         $handle = fopen('/home/srvx/neonserv/log.txt', 'a');
342         fwrite($handle, "$html\r\n");
343         fclose($handle);
344         return (preg_match("/Invalid Submission/i", $html) ? ERR_UNKNOWN : ERR_OK);
345     }
346
347     public function simulZNC($username, $raw, &$errmsg, $server = true) {
348         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/user/send_raw/");
349         if(preg_match("/Not Found/i", $html)) return ERR_MODULE_NOT_FOUND;
350         $exp = explode('name="_CSRF_Check" value="', $html);
351         $exp = explode('"', $exp[1]);
352         $csrf = $exp[0];
353         $post = array();
354         $post['_CSRF_Check'] = $csrf;
355         $post['submitted'] = '1';
356         $post['network'] = $username.'/default';
357         $post['send_to'] = ($server ? "server" : "client");
358         $post['line'] = $raw;
359         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/user/send_raw/", $post);
360         return ERR_OK;
361     }
362
363     public function addChan($username, $channel, $key = NULL, $buffer = NULL) {
364         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username.'&network=default');
365         if(preg_match("/No such username/i", $html)) return false;
366         $exp = explode('name="_CSRF_Check" value="', $html);
367         $exp = explode('"', $exp[1]);
368         $csrf = $exp[0];
369         $post = array();
370         $post['_CSRF_Check'] = $csrf;
371         $post['submitted'] = '1';
372         $post['network'] = 'default';
373         $post['user'] = $username;
374         $post['name'] = $channel;
375         $exp = explode('name="key" value="', $html);
376         $exp = explode('"', $exp[1]);
377         $default_key = $exp[0];
378         $post['key'] = ($key ? $key : $default_key);
379         $exp = explode('name="buffercount" value="', $html);
380         $exp = explode('"', $exp[1]);
381         $default_buffer = $exp[0];
382         $post['buffercount'] = ($buffer ? $buffer : $default_buffer);
383         $post['defmodes'] = '';
384         $post['save'] = 'true';
385         $post['autoclearchanbuffer'] = 'true';
386         $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username.'&network=default', $post);
387     }
388         
389 }
390
391 ?>