rename class file
[GitHub/Stricted/speedport-hybrid-php-api.git] / SpeedPort.class.php
CommitLineData
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 */
f878ee64 7class SpeedPort {
a91317a6
S
8 /**
9 * password-challenge
10 * @var string
11 */
12 private $challenge = '';
13
809e25bd
S
14 /**
15 * csrf_token
16 * @var string
17 */
18 private $token = '';
19
a91317a6
S
20 /**
21 * hashed password
22 * @var string
23 */
24 private $hash = '';
25
26 /**
27 * session cookie
28 * @var string
29 */
30 private $session = '';
31
32 /**
33 * router url
34 * @var string
35 */
b5a532a8 36 private $url = '';
a91317a6 37
c9e082da
S
38 /**
39 * derivedk cookie
40 * @var string
41 */
42 private $derivedk = '';
43
b5a532a8
S
44 public function __construct ($password, $url = 'http://speedport.ip/') {
45 $this->url = $url;
a91317a6
S
46 $this->getChallenge();
47
48 if (empty($this->challenge)) {
49 throw new Exception('unable to get the challenge from the router');
50 }
51
52 $login = $this->login($password);
53
54 if ($login === false) {
55 throw new Exception('unable to login');
56 }
57 }
58
59 /**
60 * Requests the password-challenge from the router.
61 */
62 public function getChallenge () {
b5a532a8 63 $path = 'data/Login.json';
a91317a6 64 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
b5a532a8 65 $data = $this->sentRequest($path, $fields);
a91317a6 66 $data = json_decode($data['body'], true);
c2678616
S
67 $data = $this->getValues($data);
68
69 if (isset($data['challengev']) && !empty($data['challengev'])) {
a91317a6
S
70 $this->challenge = $data[1]['varvalue'];
71 }
72 }
73
74 /**
75 * login into the router with the given password
5e44cffa 76 *
a91317a6
S
77 * @param string $password
78 * @return boolean
79 */
80 public function login ($password) {
b5a532a8 81 $path = 'data/Login.json';
a91317a6
S
82 $this->hash = hash('sha256', $this->challenge.':'.$password);
83 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash);
b5a532a8 84 $data = $this->sentRequest($path, $fields);
a91317a6 85 $json = json_decode($data['body'], true);
c2678616
S
86 $json = $this->getValues($json);
87 if (isset($json['login']) && $json['login'] == 'success') {
a91317a6
S
88 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
89 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
90 if (isset($match[1]) && !empty($match[1])) {
91 $this->session = $match[1];
92 }
93 else {
94 throw new Exception('unable to get the session cookie from the router');
95 }
96
c9e082da
S
97 // calculate derivedk
98 $this->derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32);
99
809e25bd
S
100 // get the csrf_token
101 $this->token = $this->getToken();
102
a91317a6
S
103 return true;
104 }
105 }
106
107 return false;
108 }
109
110 /**
111 * logout
5e44cffa 112 *
a91317a6
S
113 * @return array
114 */
115 public function logout () {
b5a532a8 116 $path = 'data/Login.json';
a91317a6 117 $fields = array('logout' => 'byby');
b5a532a8 118 $data = $this->sentRequest($path, $fields);
a91317a6
S
119 // reset challenge and session
120 $this->challenge = '';
121 $this->session = '';
c2678616 122 $this->token = "";
a91317a6
S
123
124 $json = json_decode($data['body'], true);
125
126 return $json;
127 }
128
129 /**
130 * reboot the router
5e44cffa 131 *
a91317a6
S
132 * @return array
133 */
134 public function reboot () {
b5a532a8 135 $path = 'data/Reboot.json';
a91317a6
S
136 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reboot_device' => 'true');
137 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
b5a532a8 138 $data = $this->sentRequest($path, $fields, $cookie);
14d4f286 139
a91317a6
S
140 $json = json_decode($data['body'], true);
141
142 return $json;
143 }
144
87026df3
S
145 /**
146 * change dsl connection status
5e44cffa 147 *
87026df3
S
148 * @param string $status
149 */
150 public function changeConnectionStatus ($status) {
b5a532a8 151 $path = 'data/Connect.json';
87026df3
S
152
153 if ($status == 'online' || $status == 'offline') {
154 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'req_connect' => $status);
155 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
c2678616
S
156 $data = $this->sentRequest($path, $fields, $cookie);
157
158 $json = json_decode($this->decrypt($data['body']), true);
159
160 return $json;
87026df3
S
161 }
162 else {
163 throw new Exception();
164 }
165 }
166
a91317a6
S
167 /**
168 * return the given json as array
5e44cffa 169 *
a91317a6
S
170 * @param string $file
171 * @return array
172 */
173 public function getData ($file) {
b5a532a8 174 $path = 'data/'.$file.'.json';
a91317a6
S
175 $fields = array();
176 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
b5a532a8 177 $data = $this->sentRequest($path, $fields, $cookie);
a91317a6
S
178
179 if (empty($data['body'])) {
180 throw new Exception('unable to get '.$file.' data');
181 }
182
183 $json = json_decode($data['body'], true);
184
185 return $json;
186 }
187
5e44cffa
S
188 /**
189 * get the router syslog
190 *
191 * @return array
192 */
193 public function getSyslog() {
194 $path = 'data/Syslog.json';
195 $fields = array('exporttype' => '0');
196 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
197 $data = $this->sentRequest($path, $fields, $cookie);
198
199 if (empty($data['body'])) {
200 throw new Exception('unable to get syslog data');
201 }
202
203 return explode("\n", $data['body']);
204 }
205
a35e3e67
S
206 /**
207 * get the Missed Calls from router
208 *
209 * @return array
210 */
211 public function getMissedCalls() {
abf641bb 212 $path = 'data/ExportMissedCalls.json';
a35e3e67
S
213 $fields = array('exporttype' => '1');
214 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
215 $data = $this->sentRequest($path, $fields, $cookie);
216
217 if (empty($data['body'])) {
218 throw new Exception('unable to get syslog data');
219 }
220
221 return explode("\n", $data['body']);
222 }
223
15260eeb
S
224 /**
225 * get the Taken Calls from router
226 *
227 * @return array
228 */
229 public function getTakenCalls() {
abf641bb 230 $path = 'data/ExportTakenCalls.json';
15260eeb
S
231 $fields = array('exporttype' => '2');
232 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
233 $data = $this->sentRequest($path, $fields, $cookie);
234
235 if (empty($data['body'])) {
236 throw new Exception('unable to get syslog data');
237 }
238
239 return explode("\n", $data['body']);
240 }
241
abf641bb
S
242 /**
243 * get the Dialed Calls from router
244 *
245 * @return array
246 */
247 public function getDialedCalls() {
248 $path = 'data/ExportDialedCalls.json';
249 $fields = array('exporttype' => '3');
250 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
251 $data = $this->sentRequest($path, $fields, $cookie);
9bd25bb4 252
abf641bb
S
253 if (empty($data['body'])) {
254 throw new Exception('unable to get syslog data');
255 }
256
257 return explode("\n", $data['body']);
258 }
259
809e25bd
S
260 /**
261 * reconnect LTE
262 *
263 * @return array
264 */
c9e082da
S
265 public function reconnectLte () {
266 $path = 'data/modules.json';
809e25bd
S
267 $fields = array('csrf_token' => $this->token, 'lte_reconn' => '1');
268 $fields = $this->encrypt($fields);
c9e082da 269 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
809e25bd 270 $data = $this->sentRequest($path, $fields, $cookie, 2);
c9e082da
S
271 $json = json_decode($data['body'], true);
272
273 return $json;
274 }
809e25bd 275
7f4a51d2
S
276 /**
277 * reset the router to Factory Default
278 * not tested
279 *
280 * @return array
281 */
9bd25bb4
S
282 public function resetToFactoryDefault () {
283 $path = 'data/resetAllSetting.json';
284 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reset_all' => 'true');
285 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
286 $data = $this->sentRequest($path, $fields, $cookie);
287 $json = json_decode($data['body'], true);
288
289 return $json;
290 }
7f4a51d2 291
9bd25bb4 292
4ae464d1
S
293 /**
294 * check if firmware is actual
295 *
296 * @return array
297 */
298 public function checkFirmware () {
299 $path = 'data/checkfirmware.json';
300 $fields = array('checkfirmware' => 'true');
301 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
302 $data = $this->sentRequest($path, $fields, $cookie);
303
304 if (empty($data['body'])) {
305 throw new Exception('unable to get checkfirmware data');
306 }
307
308 $json = json_decode($data['body'], true);
309
310 return $json;
311 }
312
14d4f286
S
313 /**
314 * decrypt data from router
315 *
316 * @param string $data
317 * @return array
318 */
c2678616 319 private function decrypt ($data) {
14d4f286
S
320 require_once 'CryptLib/CryptLib.php';
321 $factory = new CryptLib\Cipher\Factory();
322 $aes = $factory->getBlockCipher('rijndael-128');
323
324 $iv = hex2bin(substr($this->challenge, 16, 16));
325 $adata = hex2bin(substr($this->challenge, 32, 16));
326 $dkey = hex2bin($this->derivedk);
327 $enc = hex2bin($data);
328
329 $aes->setKey($dkey);
330 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
331
332 $mode->decrypt($enc);
333
334 return $mode->finish();
335 }
336
337 /**
338 * decrypt data for the router
339 *
340 * @param array $data
341 * @return string
342 */
c2678616 343 private function encrypt ($data) {
14d4f286
S
344 require_once 'CryptLib/CryptLib.php';
345 $factory = new CryptLib\Cipher\Factory();
346 $aes = $factory->getBlockCipher('rijndael-128');
347
348 $iv = hex2bin(substr($this->challenge, 16, 16));
349 $adata = hex2bin(substr($this->challenge, 32, 16));
350 $dkey = hex2bin($this->derivedk);
351
352 $aes->setKey($dkey);
353 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
354 $mode->encrypt(http_build_query($data));
355
809e25bd 356 return bin2hex($mode->finish());
14d4f286
S
357 }
358
c2678616
S
359 /**
360 * get the values from array
361 *
362 * @param array $array
363 * @return array
364 */
365 private function getValues($array) {
366 $data = array();
367 foreach ($array as $item) {
368 $data[$item['varid']] = $item['varvalue'];
369 }
370
371 return $data;
372 }
373
a91317a6
S
374 /**
375 * sends the request to router
5e44cffa 376 *
b5a532a8 377 * @param string $path
809e25bd 378 * @param mixed $fields
a91317a6 379 * @param string $cookie
809e25bd 380 * @param integer $count
a91317a6
S
381 * @return array
382 */
809e25bd 383 private function sentRequest ($path, $fields, $cookie = '', $count = 0) {
b5a532a8 384 $url = $this->url.$path;
a91317a6
S
385 $ch = curl_init();
386 curl_setopt($ch, CURLOPT_URL, $url);
387
388 if (!empty($fields)) {
809e25bd
S
389 if (is_array($fields)) {
390 curl_setopt($ch, CURLOPT_POST, count($fields));
391 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
392 }
393 else {
394 curl_setopt($ch, CURLOPT_POST, $count);
395 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
396 }
a91317a6
S
397 }
398
399 if (!empty($cookie)) {
400 curl_setopt($ch, CURLOPT_COOKIE, $cookie);
401 }
402
403 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
404 curl_setopt($ch, CURLOPT_HEADER, true);
405
a91317a6
S
406 if ($cookie) {
407
408 }
409
410 $result = curl_exec($ch);
411
412 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
413 $header = substr($result, 0, $header_size);
414 $body = substr($result, $header_size);
415 curl_close($ch);
416
417 // fix invalid json
14d4f286 418
a91317a6
S
419 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
420 $body = preg_replace('/\'/i', '"', $body);
5e44cffa 421 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
dae16c50 422 $body = preg_replace("/},\s+]/", "}\n]", $body);
a91317a6
S
423
424 return array('header' => $this->parse_headers($header), 'body' => $body);
425 }
426
809e25bd
S
427 /**
428 * get the csrf_token
429 *
430 * @return string
431 */
432 private function getToken () {
433 $path = 'html/content/overview/index.html?lang=de';
434 $fields = array();
435 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
436 $data = $this->sentRequest($path, $fields, $cookie);
437
438 if (empty($data['body'])) {
439 throw new Exception('unable to get csrf_token');
440 }
441
442 $a = explode('csrf_token = "', $data['body']);
443 $a = explode('";', $a[1]);
444
445 if (isset($a[0]) && !empty($a[0])) {
446 return $a[0];
447 }
448 else {
449 throw new Exception('unable to get csrf_token');
450 }
451 }
452
a91317a6
S
453 /**
454 * parse the curl return header into an array
5e44cffa 455 *
a91317a6
S
456 * @param string $response
457 * @return array
458 */
459 private function parse_headers($response) {
460 $headers = array();
461 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
462
7f4a51d2
S
463 $header_text = explode("\r\n", $header_text);
464 foreach ($header_text as $i => $line) {
a91317a6
S
465 if ($i === 0) {
466 $headers['http_code'] = $line;
467 }
468 else {
469 list ($key, $value) = explode(': ', $line);
470 $headers[$key] = $value;
471 }
472 }
473
474 return $headers;
475 }
7f4a51d2 476}