2 /* ZNCServerV1.class.php - ZNC Server Class - ZNCAdmin
3 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
20 "name" => "ZNCServerV1",
28 public function ZNCServerV1($host, $port, $connector = NULL) {
31 if($connector == NULL)
32 $connector = new HTTPConnector();
33 $this->connector = $connector;
36 public function login($user, $pass) {
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);
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]);
57 for($i = 1; $i < count($list); $i++) {
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;
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]);
79 for($i = 1; $i < count($list); $i++) {
81 $exp = explode('<td>', $list[$i]);
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;
95 public function getTrafficStats() {
96 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/traffic");
100 $settings array elements:
105 public function addZNC($username, $password, $settings, $servers, $modules, $others) {
106 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/adduser");
107 $exp = explode('name="_CSRF_Check" value="', $html);
108 $exp = explode('"', $exp[1]);
111 $post['_CSRF_Check'] = $csrf;
112 $post['submitted'] = '1';
113 //$post['doconnect'] = '1';
114 $post['user'] = $username;
115 $post['password'] = $password;
116 $post['password2'] = $password;
117 $post['nick'] = $settings['nick'];
118 $post['altnick'] = $settings['altnick'];
119 $post['ident'] = $settings['ident'];
120 $post['statusprefix'] = '*';
121 $post['loadmod'] = array();
122 foreach($modules as $name => $args) {
123 $post['loadmod'][] = $name;
125 $post['modargs_'.$name] = $args;
128 foreach($others as $name => $value) {
129 $post[$name] = $value;
132 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/adduser", $post);
134 if(!preg_match("/Invalid Submission/i", $html)) {
135 if(!$this->addNetwork($username, $settings, $servers, $others)) {
138 return !preg_match("/Invalid Submission/i", $html);
140 return !preg_match("/Invalid Submission/i", $html);
143 public function addNetwork($username, $settings, $servers, $others) {
144 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/addnetwork?user=".$username);
145 $exp = explode('name="_CSRF_Check" value="', $html);
146 $exp = explode('"', $exp[1]);
149 $post['_CSRF_Check'] = $csrf;
150 $post['submitted'] = '1';
151 $post['network'] = 'default';
152 $post['nick'] = $settings['nick'];
153 $post['altnick'] = $settings['altnick'];
154 $post['ident'] = $settings['ident'];
155 $post['servers'] = implode("\n", $servers);
156 $post['doconnect'] = '1';
157 $post['floodprotection'] = '1';
158 $post['floodrate'] = '1.0';
159 $post['floodburst'] = '4';
160 $post['user'] = $username;
162 foreach($others as $name => $value) {
163 $post[$name] = $value;
166 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/addnetwork", $post);
167 return !preg_match("/Invalid Submission/i", $html);
170 public function delZNC($username) {
171 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/deluser?user=".$username);
172 $exp = explode('name="_CSRF_Check" value="', $html);
173 $exp = explode('"', $exp[1]);
176 $post['_CSRF_Check'] = $csrf;
177 $post['submitted'] = '1';
178 $post['user'] = $username;
179 $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/deluser", $post);
182 private function parseHTMLFields($html, &$post) {
183 preg_match_all("<input ([^\>]*)>", $html, $matches);
184 foreach($matches[0] as $input) {
185 $fields = array(0 => "");
186 $esc = false; $str = false;
188 for($i = 0; $i < strlen($input); $i++) {
191 $fields[$fieldid] .= $input[$i];
194 if($input[$i] == "\\") {
197 if($input[$i] == "\"") {
201 if($input[$i] == " " && !$str) {
202 $fields[++$fieldid] = "";
205 $fields[$fieldid] .= $input[$i];
208 foreach($fields as $field) {
209 $fieldexp = explode("=", $field, 2);
210 if(count($fieldexp) != 2) continue;
211 $cfields[$fieldexp[0]] = $fieldexp[1];
213 $name = $cfields['name'];
214 switch($cfields['type']) {
217 $value = $cfields['value'];
220 if($cfields['checked']) {
221 $value = ($cfields['value'] ? $cfields['value'] : "1");
228 if($name && $value) {
229 if(array_key_exists($name, $post)) {
230 if(!is_array($post[$name])) {
231 $cvalue = $post[$name];
232 $post[$name] = array();
233 $post[$name][] = $cvalue;
235 $post[$name][] = $value;
237 $post[$name] = $value;
241 $textareas = explode("<textarea ", $html);
242 for($i = 1; $i < count($textareas); $i++) {
243 $exp = explode(">", $textareas[$i]);
244 $name = explode("name=\"", $exp[0]);
245 $name = explode("\"", $name[1]);
247 $content = explode("</textarea", $exp[1]);
248 $content = $content[0];
249 $post[$name] = $content;
252 $selectboxes = explode("<select ", $html);
253 for($j = 1; $j < count($selectboxes); $j++) {
254 $exp = explode(">", $selectboxes[$j], 2);
255 $name = explode("name=\"", $exp[0]);
256 $name = explode("\"", $name[1]);
258 $content = explode("</select", $exp[1]);
259 $content = $content[0];
260 preg_match_all("<option ([^\>]*)>", $content, $matches);
261 foreach($matches[0] as $input) {
262 $fields = array(0 => "");
263 $esc = false; $str = false;
265 for($i = 0; $i < strlen($input); $i++) {
268 $fields[$fieldid] .= $input[$i];
271 if($input[$i] == "\\") {
274 if($input[$i] == "\"") {
278 if($input[$i] == " " && !$str) {
279 $fields[++$fieldid] = "";
282 $fields[$fieldid] .= $input[$i];
285 foreach($fields as $field) {
286 $fieldexp = explode("=", $field, 2);
287 if(count($fieldexp) != 2) continue;
288 $cfields[$fieldexp[0]] = $fieldexp[1];
290 if(!$post[$name] || $cfields['selected']) {
291 $post[$name] = $cfields['value'];
297 public function editZNC($username, $password = NULL, $new_servers = NULL, $add_modules = NULL, $del_modules = NULL, $others = NULL) {
298 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username);
299 if(preg_match("/No such username/i", $html)) return false;
301 $post['loadmod'] = array();
302 $this->parseHTMLFields($html, $post);
304 $post['password'] = $password;
305 $post['password2'] = $password;
308 $post['servers'] = implode("\n", $new_servers);
311 foreach($add_modules as $name => $args) {
312 $post['loadmod'][] = $name;
314 $post['modargs_'.$name] = $args;
318 foreach($del_modules as $name => $args) {
319 foreach($post['loadmod'] as $index => $mod) {
320 if(strtolower($mod) == strtolower($name))
321 unset($post['loadmod'][$index]);
326 foreach($others as $name => $value) {
327 $post[$name] = $value;
330 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username, $post);
331 return !preg_match("/Invalid Submission/i", $html);
334 public function blockZNC($username, $block) {
335 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username);
336 $handle = fopen('/home/srvx/neonserv/log.txt', 'a');
337 fwrite($handle, "$html\r\n");
339 if(preg_match("/No such username/i", $html)) return ERR_USER_NOT_FOUND;
341 $post['user'] = $username;
342 $post['loadmod'] = array();
343 $this->parseHTMLFields($html, $post);
344 if(!$post['embed_blockuser_presented']) return ERR_MODULE_NOT_FOUND;
345 if(!($post['embed_blockuser_block'] xor $block)) return ERR_OK;
346 $post['embed_blockuser_block'] = ($block ? "1" : null);
348 $post['doconnect'] = 1;
350 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser", $post);
351 $handle = fopen('/home/srvx/neonserv/log.txt', 'a');
352 fwrite($handle, "$html\r\n");
354 return (preg_match("/Invalid Submission/i", $html) ? ERR_UNKNOWN : ERR_OK);
357 public function simulZNC($username, $raw, &$errmsg, $server = true) {
358 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/user/send_raw/");
359 if(preg_match("/Not Found/i", $html)) return ERR_MODULE_NOT_FOUND;
360 $exp = explode('name="_CSRF_Check" value="', $html);
361 $exp = explode('"', $exp[1]);
364 $post['_CSRF_Check'] = $csrf;
365 $post['submitted'] = '1';
366 $post['network'] = $username.'/default';
367 $post['send_to'] = ($server ? "server" : "client");
368 $post['line'] = $raw;
369 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/user/send_raw/", $post);
373 public function addChan($username, $channel, $key = NULL, $buffer = NULL) {
374 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username.'&network=default');
375 if(preg_match("/No such username/i", $html)) return false;
376 $exp = explode('name="_CSRF_Check" value="', $html);
377 $exp = explode('"', $exp[1]);
380 $post['_CSRF_Check'] = $csrf;
381 $post['submitted'] = '1';
382 $post['network'] = 'default';
383 $post['user'] = $username;
384 $post['name'] = $channel;
385 $exp = explode('name="key" value="', $html);
386 $exp = explode('"', $exp[1]);
387 $default_key = $exp[0];
388 $post['key'] = ($key ? $key : $default_key);
389 $exp = explode('name="buffercount" value="', $html);
390 $exp = explode('"', $exp[1]);
391 $default_buffer = $exp[0];
392 $post['buffercount'] = ($buffer ? $buffer : $default_buffer);
393 $post['defmodes'] = '';
394 $post['save'] = 'true';
395 $post['autoclearchanbuffer'] = 'true';
396 $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username.'&network=default', $post);