Commit | Line | Data |
---|---|---|
a91317a6 | 1 | <?php |
1d934afe S |
2 | /** |
3 | * @author Jan Altensen (Stricted) | |
4 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
5 | * @copyright 2015 Jan Altensen (Stricted) | |
6 | */ | |
a91317a6 S |
7 | class speedport { |
8 | /** | |
9 | * password-challenge | |
10 | * @var string | |
11 | */ | |
12 | private $challenge = ''; | |
13 | ||
14 | /** | |
15 | * hashed password | |
16 | * @var string | |
17 | */ | |
18 | private $hash = ''; | |
19 | ||
20 | /** | |
21 | * session cookie | |
22 | * @var string | |
23 | */ | |
24 | private $session = ''; | |
25 | ||
26 | /** | |
27 | * router url | |
28 | * @var string | |
29 | */ | |
b5a532a8 | 30 | private $url = ''; |
a91317a6 | 31 | |
b5a532a8 S |
32 | public function __construct ($password, $url = 'http://speedport.ip/') { |
33 | $this->url = $url; | |
a91317a6 S |
34 | $this->getChallenge(); |
35 | ||
36 | if (empty($this->challenge)) { | |
37 | throw new Exception('unable to get the challenge from the router'); | |
38 | } | |
39 | ||
40 | $login = $this->login($password); | |
41 | ||
42 | if ($login === false) { | |
43 | throw new Exception('unable to login'); | |
44 | } | |
45 | } | |
46 | ||
47 | /** | |
48 | * Requests the password-challenge from the router. | |
49 | */ | |
50 | public function getChallenge () { | |
b5a532a8 | 51 | $path = 'data/Login.json'; |
a91317a6 | 52 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null'); |
b5a532a8 | 53 | $data = $this->sentRequest($path, $fields); |
a91317a6 S |
54 | $data = json_decode($data['body'], true); |
55 | if ($data[1]['varid'] == 'challengev') { | |
56 | $this->challenge = $data[1]['varvalue']; | |
57 | } | |
58 | } | |
59 | ||
60 | /** | |
61 | * login into the router with the given password | |
5e44cffa | 62 | * |
a91317a6 S |
63 | * @param string $password |
64 | * @return boolean | |
65 | */ | |
66 | public function login ($password) { | |
b5a532a8 | 67 | $path = 'data/Login.json'; |
a91317a6 S |
68 | $this->hash = hash('sha256', $this->challenge.':'.$password); |
69 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash); | |
b5a532a8 | 70 | $data = $this->sentRequest($path, $fields); |
a91317a6 S |
71 | $json = json_decode($data['body'], true); |
72 | if ($json[15]['varid'] == 'login' && $json[15]['varvalue'] == 'success') { | |
73 | if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) { | |
74 | preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match); | |
75 | if (isset($match[1]) && !empty($match[1])) { | |
76 | $this->session = $match[1]; | |
77 | } | |
78 | else { | |
79 | throw new Exception('unable to get the session cookie from the router'); | |
80 | } | |
81 | ||
82 | return true; | |
83 | } | |
84 | } | |
85 | ||
86 | return false; | |
87 | } | |
88 | ||
89 | /** | |
90 | * logout | |
5e44cffa | 91 | * |
a91317a6 S |
92 | * @return array |
93 | */ | |
94 | public function logout () { | |
b5a532a8 | 95 | $path = 'data/Login.json'; |
a91317a6 | 96 | $fields = array('logout' => 'byby'); |
b5a532a8 | 97 | $data = $this->sentRequest($path, $fields); |
a91317a6 S |
98 | // reset challenge and session |
99 | $this->challenge = ''; | |
100 | $this->session = ''; | |
101 | ||
102 | $json = json_decode($data['body'], true); | |
103 | ||
104 | return $json; | |
105 | } | |
106 | ||
107 | /** | |
108 | * reboot the router | |
5e44cffa | 109 | * |
a91317a6 S |
110 | * @return array |
111 | */ | |
112 | public function reboot () { | |
b5a532a8 | 113 | $path = 'data/Reboot.json'; |
a91317a6 S |
114 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reboot_device' => 'true'); |
115 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
b5a532a8 | 116 | $data = $this->sentRequest($path, $fields, $cookie); |
a91317a6 S |
117 | $json = json_decode($data['body'], true); |
118 | ||
119 | return $json; | |
120 | } | |
121 | ||
87026df3 S |
122 | /** |
123 | * change dsl connection status | |
5e44cffa | 124 | * |
87026df3 S |
125 | * @param string $status |
126 | */ | |
127 | public function changeConnectionStatus ($status) { | |
b5a532a8 | 128 | $path = 'data/Connect.json'; |
87026df3 S |
129 | |
130 | if ($status == 'online' || $status == 'offline') { | |
131 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'req_connect' => $status); | |
132 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
b5a532a8 | 133 | $this->sentRequest($path, $fields, $cookie); |
87026df3 S |
134 | } |
135 | else { | |
136 | throw new Exception(); | |
137 | } | |
138 | } | |
139 | ||
a91317a6 S |
140 | /** |
141 | * return the given json as array | |
5e44cffa | 142 | * |
a91317a6 S |
143 | * the following paths are known to be valid: |
144 | * /data/dsl.json | |
145 | * /data/interfaces.json | |
146 | * /data/arp.json | |
147 | * /data/session.json | |
148 | * /data/dhcp_client.json | |
149 | * /data/dhcp_server.json | |
150 | * /data/ipv6.json | |
151 | * /data/dns.json | |
152 | * /data/routing.json | |
153 | * /data/igmp_proxy.json | |
154 | * /data/igmp_snooping.json | |
155 | * /data/wlan.json | |
156 | * /data/module.json | |
157 | * /data/memory.json | |
158 | * /data/speed.json | |
159 | * /data/webdav.json | |
160 | * /data/bonding_client.json | |
161 | * /data/bonding_tunnel.json | |
162 | * /data/filterlist.json | |
163 | * /data/bonding_tr181.json | |
164 | * /data/letinfo.json | |
5e44cffa | 165 | * |
a91317a6 | 166 | * /data/Status.json (No login needed) |
5e44cffa | 167 | * |
a91317a6 S |
168 | * @param string $file |
169 | * @return array | |
170 | */ | |
171 | public function getData ($file) { | |
b5a532a8 | 172 | $path = 'data/'.$file.'.json'; |
a91317a6 S |
173 | $fields = array(); |
174 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
b5a532a8 | 175 | $data = $this->sentRequest($path, $fields, $cookie); |
a91317a6 S |
176 | |
177 | if (empty($data['body'])) { | |
178 | throw new Exception('unable to get '.$file.' data'); | |
179 | } | |
180 | ||
181 | $json = json_decode($data['body'], true); | |
182 | ||
183 | return $json; | |
184 | } | |
185 | ||
5e44cffa S |
186 | /** |
187 | * get the router syslog | |
188 | * | |
189 | * @return array | |
190 | */ | |
191 | public function getSyslog() { | |
192 | $path = 'data/Syslog.json'; | |
193 | $fields = array('exporttype' => '0'); | |
194 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
195 | $data = $this->sentRequest($path, $fields, $cookie); | |
196 | ||
197 | if (empty($data['body'])) { | |
198 | throw new Exception('unable to get syslog data'); | |
199 | } | |
200 | ||
201 | return explode("\n", $data['body']); | |
202 | } | |
203 | ||
a35e3e67 S |
204 | /** |
205 | * get the Missed Calls from router | |
206 | * | |
207 | * @return array | |
208 | */ | |
209 | public function getMissedCalls() { | |
abf641bb | 210 | $path = 'data/ExportMissedCalls.json'; |
a35e3e67 S |
211 | $fields = array('exporttype' => '1'); |
212 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
213 | $data = $this->sentRequest($path, $fields, $cookie); | |
214 | ||
215 | if (empty($data['body'])) { | |
216 | throw new Exception('unable to get syslog data'); | |
217 | } | |
218 | ||
219 | return explode("\n", $data['body']); | |
220 | } | |
221 | ||
15260eeb S |
222 | /** |
223 | * get the Taken Calls from router | |
224 | * | |
225 | * @return array | |
226 | */ | |
227 | public function getTakenCalls() { | |
abf641bb | 228 | $path = 'data/ExportTakenCalls.json'; |
15260eeb S |
229 | $fields = array('exporttype' => '2'); |
230 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
231 | $data = $this->sentRequest($path, $fields, $cookie); | |
232 | ||
233 | if (empty($data['body'])) { | |
234 | throw new Exception('unable to get syslog data'); | |
235 | } | |
236 | ||
237 | return explode("\n", $data['body']); | |
238 | } | |
239 | ||
abf641bb S |
240 | /** |
241 | * get the Dialed Calls from router | |
242 | * | |
243 | * @return array | |
244 | */ | |
245 | public function getDialedCalls() { | |
246 | $path = 'data/ExportDialedCalls.json'; | |
247 | $fields = array('exporttype' => '3'); | |
248 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
249 | $data = $this->sentRequest($path, $fields, $cookie); | |
250 | ||
251 | if (empty($data['body'])) { | |
252 | throw new Exception('unable to get syslog data'); | |
253 | } | |
254 | ||
255 | return explode("\n", $data['body']); | |
256 | } | |
257 | ||
a91317a6 S |
258 | /** |
259 | * sends the request to router | |
5e44cffa | 260 | * |
b5a532a8 | 261 | * @param string $path |
a91317a6 S |
262 | * @param array $fields |
263 | * @param string $cookie | |
264 | * @return array | |
265 | */ | |
b5a532a8 S |
266 | private function sentRequest ($path, $fields = array(), $cookie = '') { |
267 | $url = $this->url.$path; | |
a91317a6 S |
268 | $ch = curl_init(); |
269 | curl_setopt($ch, CURLOPT_URL, $url); | |
270 | ||
271 | if (!empty($fields)) { | |
272 | curl_setopt($ch, CURLOPT_POST, count($fields)); | |
273 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); | |
274 | } | |
275 | ||
276 | if (!empty($cookie)) { | |
277 | curl_setopt($ch, CURLOPT_COOKIE, $cookie); | |
278 | } | |
279 | ||
280 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
281 | curl_setopt($ch, CURLOPT_HEADER, true); | |
282 | ||
283 | ||
284 | if ($cookie) { | |
285 | ||
286 | } | |
287 | ||
288 | $result = curl_exec($ch); | |
289 | ||
290 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
291 | $header = substr($result, 0, $header_size); | |
292 | $body = substr($result, $header_size); | |
293 | curl_close($ch); | |
294 | ||
295 | // fix invalid json | |
296 | $body = preg_replace("/(\r\n)|(\r)/", "\n", $body); | |
297 | $body = preg_replace('/\'/i', '"', $body); | |
5e44cffa | 298 | $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body); |
dae16c50 | 299 | $body = preg_replace("/},\s+]/", "}\n]", $body); |
a91317a6 S |
300 | |
301 | return array('header' => $this->parse_headers($header), 'body' => $body); | |
302 | } | |
303 | ||
304 | /** | |
305 | * parse the curl return header into an array | |
5e44cffa | 306 | * |
a91317a6 S |
307 | * @param string $response |
308 | * @return array | |
309 | */ | |
310 | private function parse_headers($response) { | |
311 | $headers = array(); | |
312 | $header_text = substr($response, 0, strpos($response, "\r\n\r\n")); | |
313 | ||
314 | foreach (explode("\r\n", $header_text) as $i => $line) { | |
315 | if ($i === 0) { | |
316 | $headers['http_code'] = $line; | |
317 | } | |
318 | else { | |
319 | list ($key, $value) = explode(': ', $line); | |
320 | $headers[$key] = $value; | |
321 | } | |
322 | } | |
323 | ||
324 | return $headers; | |
325 | } | |
326 | } |