2 /* ZNCServerV0.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" => "ZNCServerV0",
28 public function ZNCServerV0($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/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['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;
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]);
81 for($i = 1; $i < count($list); $i++) {
83 $exp = explode('<td>', $list[$i]);
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;
98 $settings array elements:
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]);
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;
124 $post['modargs_'.$name] = $args;
126 foreach($others as $name => $value) {
127 $post[$name] = $value;
129 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/adduser", $post);
130 return !preg_match("/Invalid Submission/i", $html);
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]);
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);
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;
151 for($i = 0; $i < strlen($input); $i++) {
154 $fields[$fieldid] .= $input[$i];
157 if($input[$i] == "\\") {
160 if($input[$i] == "\"") {
164 if($input[$i] == " " && !$str) {
165 $fields[++$fieldid] = "";
168 $fields[$fieldid] .= $input[$i];
171 foreach($fields as $field) {
172 $fieldexp = explode("=", $field, 2);
173 if(count($fieldexp) != 2) continue;
174 $cfields[$fieldexp[0]] = $fieldexp[1];
176 $name = $cfields['name'];
177 switch($cfields['type']) {
180 $value = $cfields['value'];
183 if($cfields['checked']) {
184 $value = ($cfields['value'] ? $cfields['value'] : "1");
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;
198 $post[$name][] = $value;
200 $post[$name] = $value;
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]);
210 $content = explode("</textarea", $exp[1]);
211 $content = $content[0];
212 $post[$name] = $content;
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]);
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;
228 for($i = 0; $i < strlen($input); $i++) {
231 $fields[$fieldid] .= $input[$i];
234 if($input[$i] == "\\") {
237 if($input[$i] == "\"") {
241 if($input[$i] == " " && !$str) {
242 $fields[++$fieldid] = "";
245 $fields[$fieldid] .= $input[$i];
248 foreach($fields as $field) {
249 $fieldexp = explode("=", $field, 2);
250 if(count($fieldexp) != 2) continue;
251 $cfields[$fieldexp[0]] = $fieldexp[1];
253 if(!$post[$name] || $cfields['selected']) {
254 $post[$name] = $cfields['value'];
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;
264 $post['loadmod'] = array();
265 $this->parseHTMLFields($html, $post);
267 $post['password'] = $password;
268 $post['password2'] = $password;
271 $post['servers'] = implode("\n", $new_servers);
273 foreach($add_modules as $name => $args) {
274 $post['loadmod'][] = $name;
276 $post['modargs_'.$name] = $args;
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]);
284 foreach($others as $name => $value) {
285 $post[$name] = $value;
287 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username, $post);
288 return !preg_match("/Invalid Submission/i", $html);
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;
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);
301 $post['doconnect'] = 1;
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);
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]);
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);
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]);
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);