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/>.
23 public function ZNCServer($host, $port) {
26 $this->connector = new HTTPConnector();
29 public function login($user, $pass) {
35 $this->connector->post("http://".$this->host.":".$this->port."/login", $post);
36 $http = $this->connector->get("http://".$this->host.":".$this->port."/?cookie_check=true");
37 $logged_in = !preg_match("/errorbar/i", $http);
42 public function getUserList() {
43 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/listusers");
44 $exp = explode('<div class="toptable">', $html);
45 $exp = explode('</div>', $exp[1]);
46 $exp = explode('<tbody>', $exp[0]);
47 $exp = explode('</tbody>', $exp[1]);
48 $list = explode('<tr class=', $exp[0]);
50 for($i = 1; $i < count($list); $i++) {
52 $exp = explode('<td>', $list[$i]);
53 $exp2 = explode('</td>', $exp[2]);
54 $userdata['user'] = $exp2[0];
55 $exp2 = explode('</td>', $exp[3]);
56 $userdata['clients'] = $exp2[0];
57 $exp2 = explode('</td>', $exp[4]);
58 $userdata['server'] = $exp2[0];
59 $exp2 = explode('</td>', $exp[5]);
60 $userdata['nick'] = $exp2[0];
61 $userlist[] = $userdata;
66 public function getSeenList() {
67 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/lastseen/");
68 $exp = explode('<div class="toptable">', $html);
69 $exp = explode('</div>', $exp[1]);
70 $exp = explode('<tbody>', $exp[0]);
71 $exp = explode('</tbody>', $exp[1]);
72 $list = explode('<tr class=', $exp[0]);
74 for($i = 1; $i < count($list); $i++) {
76 $exp = explode('<td>', $list[$i]);
78 $exp2 = explode('</td>', $exp[1]);
79 $userdata['user'] = $exp2[0];
80 $exp2 = explode('</td>', $exp[2]);
81 $userdata['seen'] = $exp2[0];
82 $userdata['seen_unix'] = strtotime($exp2[0]);
83 $exp2 = explode('</td>', $exp[3]);
84 $userdata['info'] = $exp2[0];
85 $seenlist[] = $userdata;
91 $settings array elements:
96 public function addZNC($username, $password, $settings, $servers, $modules, $others) {
97 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/adduser");
98 $exp = explode('name="_CSRF_Check" value="', $html);
99 $exp = explode('"', $exp[1]);
102 $post['_CSRF_Check'] = $csrf;
103 $post['submitted'] = '1';
104 $post['doconnect'] = '1';
105 $post['user'] = $username;
106 $post['password'] = $password;
107 $post['password2'] = $password;
108 $post['nick'] = $settings['nick'];
109 $post['altnick'] = $settings['altnick'];
110 $post['ident'] = $settings['ident'];
111 $post['statusprefix'] = '*';
112 $post['servers'] = implode("\n", $servers);
113 $post['loadmod'] = array();
114 foreach($modules as $name => $args) {
115 $post['loadmod'][] = $name;
117 $post['modargs_'.$name] = $args;
119 foreach($others as $name => $value) {
120 $post[$name] = $value;
122 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/adduser", $post);
123 return !preg_match("/Invalid Submission/i", $html);
126 public function delZNC($username) {
127 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/deluser?user=".$username);
128 $exp = explode('name="_CSRF_Check" value="', $html);
129 $exp = explode('"', $exp[1]);
132 $post['_CSRF_Check'] = $csrf;
133 $post['submitted'] = '1';
134 $post['user'] = $username;
135 $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/deluser", $post);
138 public function editZNC($username, $password = NULL, $new_servers = NULL, $add_modules = NULL, $del_modules = NULL, $others = NULL) {
139 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username);
140 if(preg_match("/No such username/i", $html)) return false;
142 $post['loadmod'] = array();
143 preg_match_all("<input ([^\>]*)>", $html, $matches);
144 foreach($matches[0] as $input) {
145 $fields = array(0 => "");
146 $esc = false; $str = false;
148 for($i = 0; $i < strlen($input); $i++) {
151 $fields[$fieldid] .= $input[$i];
154 if($input[$i] == "\\") {
157 if($input[$i] == "\"") {
161 if($input[$i] == " " && !$str) {
162 $fields[++$fieldid] = "";
165 $fields[$fieldid] .= $input[$i];
168 foreach($fields as $field) {
169 $fieldexp = explode("=", $field, 2);
170 if(count($fieldexp) != 2) continue;
171 $cfields[$fieldexp[0]] = $fieldexp[1];
173 $name = $cfields['name'];
174 switch($cfields['type']) {
177 $value = $cfields['value'];
180 if($cfields['checked']) {
181 $value = ($cfields['value'] ? $cfields['value'] : "1");
188 if($name && $value) {
189 if(array_key_exists($name, $post)) {
190 if(!is_array($post[$name])) {
191 $cvalue = $post[$name];
192 $post[$name] = array();
193 $post[$name][] = $cvalue;
195 $post[$name][] = $value;
197 $post[$name] = $value;
201 $textareas = explode("<textarea ", $html);
202 for($i = 1; $i < count($textareas); $i++) {
203 $exp = explode(">", $textareas[$i]);
204 $name = explode("name=\"", $exp[0]);
205 $name = explode("\"", $name[1]);
207 $content = explode("</textarea", $exp[1]);
208 $content = $content[0];
209 $post[$name] = $content;
212 $post['password'] = $password;
213 $post['password2'] = $password;
216 $post['servers'] = implode("\n", $new_servers);
218 foreach($add_modules as $name => $args) {
219 $post['loadmod'][] = $name;
221 $post['modargs_'.$name] = $args;
223 foreach($del_modules as $name => $args) {
224 foreach($post['loadmod'] as $index => $mod) {
225 if(strtolower($mod) == strtolower($name))
226 unset($post['loadmod'][$index]);
229 foreach($others as $name => $value) {
230 $post[$name] = $value;
232 $html = $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/edituser?user=".$username, $post);
233 return !preg_match("/Invalid Submission/i", $html);
236 public function simulZNC($username, $raw, $server = true) {
237 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/send_raw/");
238 if(preg_match("/Not Found/i", $html)) return false;
239 $exp = explode('name="_CSRF_Check" value="', $html);
240 $exp = explode('"', $exp[1]);
243 $post['_CSRF_Check'] = $csrf;
244 $post['submitted'] = '1';
245 $post['user'] = $username;
246 $post['send_to'] = ($server ? "server" : "client");
247 $post['line'] = $raw;
248 $this->connector->post("http://".$this->host.":".$this->port."/mods/send_raw/", $post);
251 public function addChan($username, $channel, $key = NULL, $buffer = NULL) {
252 $html = $this->connector->get("http://".$this->host.":".$this->port."/mods/webadmin/addchan?user=".$username);
253 if(preg_match("/No such username/i", $html)) return false;
254 $exp = explode('name="_CSRF_Check" value="', $html);
255 $exp = explode('"', $exp[1]);
258 $post['_CSRF_Check'] = $csrf;
259 $post['submitted'] = '1';
260 $post['user'] = $username;
261 $post['name'] = $channel;
262 $exp = explode('name="key" value="', $html);
263 $exp = explode('"', $exp[1]);
264 $default_key = $exp[0];
265 $post['key'] = ($key ? $key : $default_key);
266 $exp = explode('name="buffercount" value="', $html);
267 $exp = explode('"', $exp[1]);
268 $default_buffer = $exp[0];
269 $post['buffercount'] = ($buffer ? $buffer : $default_buffer);
270 $post['save'] = 'true';
271 $this->connector->post("http://".$this->host.":".$this->port."/mods/webadmin/addchan?user=".$username, $post);