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;
96 $settings array elements:
101 public function addZNC($username, $password, $settings, $servers, $modules, $netmodules, $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]);
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;
121 $post['modargs_'.$name] = $args;
124 foreach($others as $name => $value) {
125 $post[$name] = $value;
128 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/adduser", $post);
130 if(!preg_match("/Invalid Submission/i", $html)) {
131 if(!$this->addNetwork($username, $settings, $servers, $netmodules, $others)) {
134 return !preg_match("/Invalid Submission/i", $html);
136 return !preg_match("/Invalid Submission/i", $html);
139 public function addNetwork($username, $settings, $servers, $modules, $others) {
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]);
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;
158 $post['loadmod'] = array();
159 foreach($modules as $name => $args) {
160 $post['loadmod'][] = $name;
162 $post['modargs_'.$name] = $args;
165 foreach($others as $name => $value) {
166 $post[$name] = $value;
169 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/addnetwork", $post);
170 return !preg_match("/Invalid Submission/i", $html);
173 public function delZNC($username) {
174 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/deluser?user=".$username);
175 $exp = explode('name="_CSRF_Check" value="', $html);
176 $exp = explode('"', $exp[1]);
179 $post['_CSRF_Check'] = $csrf;
180 $post['submitted'] = '1';
181 $post['user'] = $username;
182 $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/deluser", $post);
185 private function parseHTMLFields($html, &$post) {
186 preg_match_all("<input ([^\>]*)>", $html, $matches);
187 foreach($matches[0] as $input) {
188 $fields = array(0 => "");
189 $esc = false; $str = false;
191 for($i = 0; $i < strlen($input); $i++) {
194 $fields[$fieldid] .= $input[$i];
197 if($input[$i] == "\\") {
200 if($input[$i] == "\"") {
204 if($input[$i] == " " && !$str) {
205 $fields[++$fieldid] = "";
208 $fields[$fieldid] .= $input[$i];
211 foreach($fields as $field) {
212 $fieldexp = explode("=", $field, 2);
213 if(count($fieldexp) != 2) continue;
214 $cfields[$fieldexp[0]] = $fieldexp[1];
216 $name = $cfields['name'];
217 switch($cfields['type']) {
220 $value = $cfields['value'];
223 if($cfields['checked']) {
224 $value = ($cfields['value'] ? $cfields['value'] : "1");
231 if($name && $value) {
232 if(array_key_exists($name, $post)) {
233 if(!is_array($post[$name])) {
234 $cvalue = $post[$name];
235 $post[$name] = array();
236 $post[$name][] = $cvalue;
238 $post[$name][] = $value;
240 $post[$name] = $value;
244 $textareas = explode("<textarea ", $html);
245 for($i = 1; $i < count($textareas); $i++) {
246 $exp = explode(">", $textareas[$i]);
247 $name = explode("name=\"", $exp[0]);
248 $name = explode("\"", $name[1]);
250 $content = explode("</textarea", $exp[1]);
251 $content = $content[0];
252 $post[$name] = $content;
255 $selectboxes = explode("<select ", $html);
256 for($j = 1; $j < count($selectboxes); $j++) {
257 $exp = explode(">", $selectboxes[$j], 2);
258 $name = explode("name=\"", $exp[0]);
259 $name = explode("\"", $name[1]);
261 $content = explode("</select", $exp[1]);
262 $content = $content[0];
263 preg_match_all("<option ([^\>]*)>", $content, $matches);
264 foreach($matches[0] as $input) {
265 $fields = array(0 => "");
266 $esc = false; $str = false;
268 for($i = 0; $i < strlen($input); $i++) {
271 $fields[$fieldid] .= $input[$i];
274 if($input[$i] == "\\") {
277 if($input[$i] == "\"") {
281 if($input[$i] == " " && !$str) {
282 $fields[++$fieldid] = "";
285 $fields[$fieldid] .= $input[$i];
288 foreach($fields as $field) {
289 $fieldexp = explode("=", $field, 2);
290 if(count($fieldexp) != 2) continue;
291 $cfields[$fieldexp[0]] = $fieldexp[1];
293 if(!$post[$name] || $cfields['selected']) {
294 $post[$name] = $cfields['value'];
300 public function editZNC($username, $password = NULL, $new_servers = NULL, $add_modules = NULL, $del_modules = NULL, $others = NULL) {
301 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username);
302 if(preg_match("/No such username/i", $html)) return false;
304 $post['loadmod'] = array();
305 $this->parseHTMLFields($html, $post);
307 $post['password'] = $password;
308 $post['password2'] = $password;
311 $post['servers'] = implode("\n", $new_servers);
314 foreach($add_modules as $name => $args) {
315 $post['loadmod'][] = $name;
317 $post['modargs_'.$name] = $args;
321 foreach($del_modules as $name => $args) {
322 foreach($post['loadmod'] as $index => $mod) {
323 if(strtolower($mod) == strtolower($name))
324 unset($post['loadmod'][$index]);
329 foreach($others as $name => $value) {
330 $post[$name] = $value;
333 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username, $post);
334 return !preg_match("/Invalid Submission/i", $html);
337 public function blockZNC($username, $block) {
338 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser?user=".$username);
339 $handle = fopen('/home/srvx/neonserv/log.txt', 'a');
340 fwrite($handle, "$html\r\n");
342 if(preg_match("/No such username/i", $html)) return ERR_USER_NOT_FOUND;
344 $post['user'] = $username;
345 $post['loadmod'] = array();
346 $this->parseHTMLFields($html, $post);
347 if(!$post['embed_blockuser_presented']) return ERR_MODULE_NOT_FOUND;
348 if(!($post['embed_blockuser_block'] xor $block)) return ERR_OK;
349 $post['embed_blockuser_block'] = ($block ? "1" : null);
351 $post['doconnect'] = 1;
353 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/edituser", $post);
354 $handle = fopen('/home/srvx/neonserv/log.txt', 'a');
355 fwrite($handle, "$html\r\n");
357 return (preg_match("/Invalid Submission/i", $html) ? ERR_UNKNOWN : ERR_OK);
360 public function simulZNC($username, $raw, &$errmsg, $server = true) {
361 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/user/send_raw/");
362 if(preg_match("/Not Found/i", $html)) return ERR_MODULE_NOT_FOUND;
363 $exp = explode('name="_CSRF_Check" value="', $html);
364 $exp = explode('"', $exp[1]);
367 $post['_CSRF_Check'] = $csrf;
368 $post['submitted'] = '1';
369 $post['network'] = $username.'/default';
370 $post['send_to'] = ($server ? "server" : "client");
371 $post['line'] = $raw;
372 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/user/send_raw/", $post);
376 public function addChan($username, $channel, $key = NULL, $buffer = NULL) {
377 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username.'&network=default');
378 if(preg_match("/No such username/i", $html)) return false;
379 $exp = explode('name="_CSRF_Check" value="', $html);
380 $exp = explode('"', $exp[1]);
383 $post['_CSRF_Check'] = $csrf;
384 $post['submitted'] = '1';
385 $post['network'] = 'default';
386 $post['user'] = $username;
387 $post['name'] = $channel;
388 $exp = explode('name="key" value="', $html);
389 $exp = explode('"', $exp[1]);
390 $default_key = $exp[0];
391 $post['key'] = ($key ? $key : $default_key);
392 $exp = explode('name="buffercount" value="', $html);
393 $exp = explode('"', $exp[1]);
394 $default_buffer = $exp[0];
395 $post['buffercount'] = ($buffer ? $buffer : $default_buffer);
396 $post['defmodes'] = '';
397 $post['save'] = 'true';
398 $post['autoclearchanbuffer'] = 'true';
399 $this->connector->post("http://".$this->host.":".$this->port."/mods/global/webadmin/addchan?user=".$username.'&network=default', $post);