2 /* ZNCServer.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 define("ERR_UNKNOWN", 1);
21 define("ERR_MODULE_NOT_FOUND", 2);
22 define("ERR_USER_NOT_FOUND", 3);
28 public function ZNCServer($host, $port) {
31 $this->connector = new HTTPConnector();
34 public function login($user, $pass) {
40 $this->connector->post("http://".$this->host.":".$this->port."/login", $post);
41 $http = $this->connector->get("http://".$this->host.":".$this->port."/?cookie_check=true");
42 $logged_in = !preg_match("/errorbar/i", $http);
47 public function getUserList() {
48 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/listusers");
49 $exp = explode('<div class="toptable">', $html);
50 $exp = explode('</div>', $exp[1]);
51 $exp = explode('<tbody>', $exp[0]);
52 $exp = explode('</tbody>', $exp[1]);
53 $list = explode('<tr class=', $exp[0]);
55 for($i = 1; $i < count($list); $i++) {
57 $exp = explode('<td>', $list[$i]);
58 $exp2 = explode('</td>', $exp[2]);
59 $userdata['user'] = $exp2[0];
60 $exp2 = explode('</td>', $exp[3]);
61 $userdata['clients'] = $exp2[0];
62 $exp2 = explode('</td>', $exp[4]);
63 $userdata['server'] = $exp2[0];
64 $exp2 = explode('</td>', $exp[5]);
65 $userdata['nick'] = $exp2[0];
66 $userlist[] = $userdata;
71 public function getSeenList() {
72 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/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, $others) {
102 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/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['servers'] = implode("\n", $servers);
118 $post['loadmod'] = array();
119 foreach($modules as $name => $args) {
120 $post['loadmod'][] = $name;
122 $post['modargs_'.$name] = $args;
124 foreach($others as $name => $value) {
125 $post[$name] = $value;
127 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/adduser", $post);
128 return !preg_match("/Invalid Submission/i", $html);
131 public function delZNC($username) {
132 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/deluser?user=".$username);
133 $exp = explode('name="_CSRF_Check" value="', $html);
134 $exp = explode('"', $exp[1]);
137 $post['_CSRF_Check'] = $csrf;
138 $post['submitted'] = '1';
139 $post['user'] = $username;
140 $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/deluser", $post);
143 private function parseHTMLFields($html, &$post) {
144 preg_match_all("<input ([^\>]*)>", $html, $matches);
145 foreach($matches[0] as $input) {
146 $fields = array(0 => "");
147 $esc = false; $str = false;
149 for($i = 0; $i < strlen($input); $i++) {
152 $fields[$fieldid] .= $input[$i];
155 if($input[$i] == "\\") {
158 if($input[$i] == "\"") {
162 if($input[$i] == " " && !$str) {
163 $fields[++$fieldid] = "";
166 $fields[$fieldid] .= $input[$i];
169 foreach($fields as $field) {
170 $fieldexp = explode("=", $field, 2);
171 if(count($fieldexp) != 2) continue;
172 $cfields[$fieldexp[0]] = $fieldexp[1];
174 $name = $cfields['name'];
175 switch($cfields['type']) {
178 $value = $cfields['value'];
181 if($cfields['checked']) {
182 $value = ($cfields['value'] ? $cfields['value'] : "1");
189 if($name && $value) {
190 if(array_key_exists($name, $post)) {
191 if(!is_array($post[$name])) {
192 $cvalue = $post[$name];
193 $post[$name] = array();
194 $post[$name][] = $cvalue;
196 $post[$name][] = $value;
198 $post[$name] = $value;
202 $textareas = explode("<textarea ", $html);
203 for($i = 1; $i < count($textareas); $i++) {
204 $exp = explode(">", $textareas[$i]);
205 $name = explode("name=\"", $exp[0]);
206 $name = explode("\"", $name[1]);
208 $content = explode("</textarea", $exp[1]);
209 $content = $content[0];
210 $post[$name] = $content;
214 public function editZNC($username, $password = NULL, $new_servers = NULL, $add_modules = NULL, $del_modules = NULL, $others = NULL) {
215 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username);
216 if(preg_match("/No such username/i", $html)) return false;
218 $post['loadmod'] = array();
219 $this->parseHTMLFields($html, $post);
221 $post['password'] = $password;
222 $post['password2'] = $password;
225 $post['servers'] = implode("\n", $new_servers);
227 foreach($add_modules as $name => $args) {
228 $post['loadmod'][] = $name;
230 $post['modargs_'.$name] = $args;
232 foreach($del_modules as $name => $args) {
233 foreach($post['loadmod'] as $index => $mod) {
234 if(strtolower($mod) == strtolower($name))
235 unset($post['loadmod'][$index]);
238 foreach($others as $name => $value) {
239 $post[$name] = $value;
241 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username, $post);
242 return !preg_match("/Invalid Submission/i", $html);
245 public function blockZNC($username, $block) {
246 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username);
247 if(preg_match("/No such username/i", $html)) return ERR_USER_NOT_FOUND;
249 $post['loadmod'] = array();
250 $this->parseHTMLFields($html, $post);
251 if(!$post['embed_blockuser_presented']) return ERR_MODULE_NOT_FOUND;
252 if(!($post['embed_blockuser_block'] xor $block)) return ERR_OK;
253 $post['embed_blockuser_block'] = ($block ? "1" : null);
255 $post['doconnect'] = 1;
257 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username, $post);
258 return (preg_match("/Invalid Submission/i", $html) ? ERR_UNKNOWN : ERR_OK);
261 public function simulZNC($username, $raw, &$errmsg, $server = true) {
262 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/send_raw/");
263 if(preg_match("/Not Found/i", $html)) return ERR_MODULE_NOT_FOUND;
264 $exp = explode('name="_CSRF_Check" value="', $html);
265 $exp = explode('"', $exp[1]);
268 $post['_CSRF_Check'] = $csrf;
269 $post['submitted'] = '1';
270 $post['user'] = $username;
271 $post['send_to'] = ($server ? "server" : "client");
272 $post['line'] = $raw;
273 $this->connector->post("http://".$this->host.":".$this->port."/mods/send_raw/", $post);
277 public function addChan($username, $channel, $key = NULL, $buffer = NULL) {
278 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/addchan?user=".$username);
279 if(preg_match("/No such username/i", $html)) return false;
280 $exp = explode('name="_CSRF_Check" value="', $html);
281 $exp = explode('"', $exp[1]);
284 $post['_CSRF_Check'] = $csrf;
285 $post['submitted'] = '1';
286 $post['user'] = $username;
287 $post['name'] = $channel;
288 $exp = explode('name="key" value="', $html);
289 $exp = explode('"', $exp[1]);
290 $default_key = $exp[0];
291 $post['key'] = ($key ? $key : $default_key);
292 $exp = explode('name="buffercount" value="', $html);
293 $exp = explode('"', $exp[1]);
294 $default_buffer = $exp[0];
295 $post['buffercount'] = ($buffer ? $buffer : $default_buffer);
296 $post['save'] = 'true';
297 $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/addchan?user=".$username, $post);