prepared ZNCServer Class to switch between ZNC Versions
[ZNCAdmin.git] / zncadmin / ZNCServerV0.class.php
1 <?php
2 /* ZNCServerV0.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" => "ZNCServerV0",
21         "version" => 0.000,
22 );
23
24 class ZNCServerV0 {
25         private $host, $port;
26         private $connector;
27         
28         public function ZNCServerV0($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/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['clients'] = $exp2[0];
64                         $exp2 = explode('</td>', $exp[4]);
65                         $userdata['server'] = $exp2[0];
66                         $exp2 = explode('</td>', $exp[5]);
67                         $userdata['nick'] = $exp2[0];
68                         $userlist[] = $userdata;
69                 }
70                 return $userlist;
71         }
72     
73     public function getSeenList() {
74                 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/lastseen/");
75                 $exp = explode('<div class="toptable">', $html);
76                 $exp = explode('</div>', $exp[1]);
77                 $exp = explode('<tbody>', $exp[0]);
78                 $exp = explode('</tbody>', $exp[1]);
79                 $list = explode('<tr class=', $exp[0]);
80                 $seenlist = array();
81                 for($i = 1; $i < count($list); $i++) {
82                         $userdata = array();
83                         $exp = explode('<td>', $list[$i]);
84             
85                         $exp2 = explode('</td>', $exp[1]);
86                         $userdata['user'] = $exp2[0];
87                         $exp2 = explode('</td>', $exp[2]);
88                         $userdata['seen'] = $exp2[0];
89             $userdata['seen_unix'] = strtotime($exp2[0]);
90                         $exp2 = explode('</td>', $exp[3]);
91                         $userdata['info'] = $exp2[0];
92                         $seenlist[] = $userdata;
93                 }
94                 return $seenlist;
95         }
96     
97     /*
98     $settings array elements:
99         nick
100         altnick
101         ident
102     */
103     public function addZNC($username, $password, $settings, $servers, $modules, $others) {
104         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/adduser");
105                 $exp = explode('name="_CSRF_Check" value="', $html);
106         $exp = explode('"', $exp[1]);
107         $csrf = $exp[0];
108         $post = array();
109         $post['_CSRF_Check'] = $csrf;
110         $post['submitted'] = '1';
111         $post['doconnect'] = '1';
112         $post['user'] = $username;
113         $post['password'] = $password;
114         $post['password2'] = $password;
115         $post['nick'] = $settings['nick'];
116         $post['altnick'] = $settings['altnick'];
117         $post['ident'] = $settings['ident'];
118         $post['statusprefix'] = '*';
119         $post['servers'] = implode("\n", $servers);
120         $post['loadmod'] = array();
121         foreach($modules as $name => $args) {
122             $post['loadmod'][] = $name;
123             if($args != "")
124                 $post['modargs_'.$name] = $args;
125         }
126         foreach($others as $name => $value) {
127             $post[$name] = $value;
128         }
129         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/adduser", $post);
130         return !preg_match("/Invalid Submission/i", $html);
131     }
132         
133     public function delZNC($username) {
134         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/deluser?user=".$username);
135                 $exp = explode('name="_CSRF_Check" value="', $html);
136         $exp = explode('"', $exp[1]);
137         $csrf = $exp[0];
138         $post = array();
139         $post['_CSRF_Check'] = $csrf;
140         $post['submitted'] = '1';
141         $post['user'] = $username;
142         $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/deluser", $post);
143     }
144     
145     private function parseHTMLFields($html, &$post) {
146         preg_match_all("<input ([^\>]*)>", $html, $matches);
147         foreach($matches[0] as $input) {
148             $fields = array(0 => "");
149             $esc = false; $str = false;
150             $fieldid = 0;
151             for($i = 0; $i < strlen($input); $i++) {
152                 if($esc) {
153                     $esc = false;
154                     $fields[$fieldid] .= $input[$i];
155                     continue;
156                 }
157                 if($input[$i] == "\\") {
158                     $esc = true;
159                 }
160                 if($input[$i] == "\"") {
161                     $str = !$str;
162                     continue;
163                 }
164                 if($input[$i] == " " && !$str) {
165                     $fields[++$fieldid] = "";
166                     continue;
167                 }
168                 $fields[$fieldid] .= $input[$i];
169             }
170             $cfields = array();
171             foreach($fields as $field) {
172                 $fieldexp = explode("=", $field, 2);
173                 if(count($fieldexp) != 2) continue;
174                 $cfields[$fieldexp[0]] = $fieldexp[1];
175             }
176             $name = $cfields['name'];
177             switch($cfields['type']) {
178                 case "text":
179                 case "hidden":
180                     $value = $cfields['value'];
181                     break;
182                 case "checkbox":
183                     if($cfields['checked']) {
184                         $value = ($cfields['value'] ? $cfields['value'] : "1");
185                     } else 
186                         $value = null;
187                     break;
188                 default:
189                     $value = null;
190             }
191             if($name && $value) {
192                 if(array_key_exists($name, $post)) {
193                     if(!is_array($post[$name])) {
194                         $cvalue = $post[$name];
195                         $post[$name] = array();
196                         $post[$name][] = $cvalue;
197                     }
198                     $post[$name][] = $value;
199                 } else
200                     $post[$name] = $value;
201             }
202         }
203         //textareas
204         $textareas = explode("<textarea ", $html);
205         for($i = 1; $i < count($textareas); $i++) {
206             $exp = explode(">", $textareas[$i]);
207             $name = explode("name=\"", $exp[0]);
208             $name = explode("\"", $name[1]);
209             $name = $name[0];
210             $content = explode("</textarea", $exp[1]);
211             $content = $content[0];
212             $post[$name] = $content;
213         }
214         //select boxes
215         $selectboxes = explode("<select ", $html);
216         for($j = 1; $j < count($selectboxes); $j++) {
217             $exp = explode(">", $selectboxes[$j], 2);
218             $name = explode("name=\"", $exp[0]);
219             $name = explode("\"", $name[1]);
220             $name = $name[0];
221             $content = explode("</select", $exp[1]);
222             $content = $content[0];
223             preg_match_all("<option ([^\>]*)>", $content, $matches);
224             foreach($matches[0] as $input) {
225                 $fields = array(0 => "");
226                 $esc = false; $str = false;
227                 $fieldid = 0;
228                 for($i = 0; $i < strlen($input); $i++) {
229                     if($esc) {
230                         $esc = false;
231                         $fields[$fieldid] .= $input[$i];
232                         continue;
233                     }
234                     if($input[$i] == "\\") {
235                         $esc = true;
236                     }
237                     if($input[$i] == "\"") {
238                         $str = !$str;
239                         continue;
240                     }
241                     if($input[$i] == " " && !$str) {
242                         $fields[++$fieldid] = "";
243                         continue;
244                     }
245                     $fields[$fieldid] .= $input[$i];
246                 }
247                 $cfields = array();
248                 foreach($fields as $field) {
249                     $fieldexp = explode("=", $field, 2);
250                     if(count($fieldexp) != 2) continue;
251                     $cfields[$fieldexp[0]] = $fieldexp[1];
252                 }
253                 if(!$post[$name] || $cfields['selected']) {
254                     $post[$name] = $cfields['value'];
255                 }
256             }
257         }
258     }
259     
260     public function editZNC($username, $password = NULL, $new_servers = NULL, $add_modules = NULL, $del_modules = NULL, $others = NULL) {
261         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username);
262         if(preg_match("/No such username/i", $html)) return false;
263         $post = array();
264         $post['loadmod'] = array();
265         $this->parseHTMLFields($html, $post);
266         if($password) {
267             $post['password'] = $password;
268             $post['password2'] = $password;
269         }
270         if($new_servers) {
271             $post['servers'] = implode("\n", $new_servers);
272         }
273         foreach($add_modules as $name => $args) {
274             $post['loadmod'][] = $name;
275             if($args != "")
276                 $post['modargs_'.$name] = $args;
277         }
278         foreach($del_modules as $name => $args) {
279             foreach($post['loadmod'] as $index => $mod) {
280                 if(strtolower($mod) == strtolower($name))
281                     unset($post['loadmod'][$index]);
282             }
283         }
284         foreach($others as $name => $value) {
285             $post[$name] = $value;
286         }
287         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username, $post);
288         return !preg_match("/Invalid Submission/i", $html);
289     }
290     
291     public function blockZNC($username, $block) {
292         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username);
293         if(preg_match("/No such username/i", $html)) return ERR_USER_NOT_FOUND;
294         $post = array();
295         $post['loadmod'] = array();
296         $this->parseHTMLFields($html, $post);
297         if(!$post['embed_blockuser_presented']) return ERR_MODULE_NOT_FOUND;
298         if(!($post['embed_blockuser_block'] xor $block)) return ERR_OK;
299         $post['embed_blockuser_block'] = ($block ? "1" : null);
300         if(!$block) {
301             $post['doconnect'] = 1;
302         }
303         $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username, $post);
304         return (preg_match("/Invalid Submission/i", $html) ? ERR_UNKNOWN : ERR_OK);
305     }
306     
307     public function simulZNC($username, $raw, &$errmsg, $server = true) {
308         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/send_raw/");
309         if(preg_match("/Not Found/i", $html)) return ERR_MODULE_NOT_FOUND;
310                 $exp = explode('name="_CSRF_Check" value="', $html);
311         $exp = explode('"', $exp[1]);
312         $csrf = $exp[0];
313         $post = array();
314         $post['_CSRF_Check'] = $csrf;
315         $post['submitted'] = '1';
316         $post['user'] = $username;
317         $post['send_to'] = ($server ? "server" : "client");
318         $post['line'] = $raw;
319         $this->connector->post("http://".$this->host.":".$this->port."/mods/send_raw/", $post);
320         return ERR_OK;
321     }
322     
323     public function addChan($username, $channel, $key = NULL, $buffer = NULL) {
324         $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/addchan?user=".$username);
325         if(preg_match("/No such username/i", $html)) return false;
326                 $exp = explode('name="_CSRF_Check" value="', $html);
327         $exp = explode('"', $exp[1]);
328         $csrf = $exp[0];
329         $post = array();
330         $post['_CSRF_Check'] = $csrf;
331         $post['submitted'] = '1';
332         $post['user'] = $username;
333         $post['name'] = $channel;
334         $exp = explode('name="key" value="', $html);
335         $exp = explode('"', $exp[1]);
336         $default_key = $exp[0];
337         $post['key'] = ($key ? $key : $default_key);
338         $exp = explode('name="buffercount" value="', $html);
339         $exp = explode('"', $exp[1]);
340         $default_buffer = $exp[0];
341         $post['buffercount'] = ($buffer ? $buffer : $default_buffer);
342         $post['save'] = 'true';
343         $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/addchan?user=".$username, $post);
344     }
345     
346 }
347
348 ?>