add getPhoneBookEntrys
[GitHub/Stricted/speedport-hybrid-php-api.git] / SpeedportHybrid.class.php
CommitLineData
a91317a6 1<?php
8162139f
S
2require_once('RebootException.class.php');
3require_once('RouterException.class.php');
aacbbd28 4require_once('CryptLib/CryptLib.php');
8162139f 5
1d934afe
S
6/**
7 * @author Jan Altensen (Stricted)
8 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
9 * @copyright 2015 Jan Altensen (Stricted)
10 */
21f877e4 11class SpeedportHybrid {
0dca2d61 12 /**
23cc0061
S
13 * class version
14 * @const string
0dca2d61 15 */
219ba661 16 const VERSION = '1.0.3';
0dca2d61 17
a91317a6
S
18 /**
19 * password-challenge
20 * @var string
21 */
22 private $challenge = '';
23
809e25bd
S
24 /**
25 * csrf_token
26 * @var string
27 */
28 private $token = '';
29
a91317a6
S
30 /**
31 * hashed password
32 * @var string
33 */
34 private $hash = '';
35
36 /**
37 * session cookie
38 * @var string
39 */
ac344b92 40 private $cookie = '';
a91317a6
S
41
42 /**
43 * router url
44 * @var string
45 */
b5a532a8 46 private $url = '';
a91317a6 47
c9e082da
S
48 /**
49 * derivedk cookie
50 * @var string
51 */
52 private $derivedk = '';
53
e58a96d1 54 public function __construct ($url = 'http://speedport.ip/') {
b5a532a8 55 $this->url = $url;
a91317a6
S
56 }
57
58 /**
59 * Requests the password-challenge from the router.
60 */
e58a96d1 61 private function getChallenge () {
b5a532a8 62 $path = 'data/Login.json';
a91317a6 63 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
b5a532a8 64 $data = $this->sentRequest($path, $fields);
219ba661 65 $data = $this->getValues($data['body']);
c2678616
S
66
67 if (isset($data['challengev']) && !empty($data['challengev'])) {
ac344b92
S
68 return $data['challengev'];
69 }
70 else {
219ba661 71 throw new RouterException('unable to get the challenge from the router');
a91317a6
S
72 }
73 }
74
75 /**
76 * login into the router with the given password
5e44cffa 77 *
a91317a6
S
78 * @param string $password
79 * @return boolean
80 */
81 public function login ($password) {
ac344b92 82 $this->challenge = $this->getChallenge();
e58a96d1 83
b5a532a8 84 $path = 'data/Login.json';
a91317a6
S
85 $this->hash = hash('sha256', $this->challenge.':'.$password);
86 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash);
b5a532a8 87 $data = $this->sentRequest($path, $fields);
219ba661 88 $json = $this->getValues($data['body']);
ac344b92 89
c2678616 90 if (isset($json['login']) && $json['login'] == 'success') {
ac344b92
S
91 $this->cookie = $this->getCookie($data);
92
93 $this->derivedk = $this->getDerviedk($password);
94
95 // get the csrf_token
96 $this->token = $this->getToken();
97
98 if ($this->checkLogin(false) === true) {
99 return true;
a91317a6
S
100 }
101 }
102
103 return false;
104 }
105
df1e1394
S
106 /**
107 * check if we are logged in
108 *
adcedd8b
S
109 * @param boolean $exception
110 * @return boolean
df1e1394 111 */
adcedd8b 112 public function checkLogin ($exception = true) {
ac344b92
S
113 // check if challenge or session is empty
114 if (empty($this->challenge) || empty($this->cookie)) {
adcedd8b 115 if ($exception === true) {
219ba661 116 throw new RouterException('you musst be logged in to use this method');
adcedd8b
S
117 }
118
0dca2d61
S
119 return false;
120 }
121
34701575 122 $path = 'data/SecureStatus.json';
df1e1394 123 $fields = array();
0dca2d61 124 $data = $this->sentRequest($path, $fields, true);
219ba661 125 $data = $this->getValues($data['body']);
df1e1394 126
219ba661 127 if ($data['loginstate'] != 1) {
adcedd8b 128 if ($exception === true) {
219ba661 129 throw new RouterException('you musst be logged in to use this method');
adcedd8b
S
130 }
131
df1e1394
S
132 return false;
133 }
134
135 return true;
136 }
137
a91317a6
S
138 /**
139 * logout
5e44cffa 140 *
58feafa2 141 * @return boolean
a91317a6
S
142 */
143 public function logout () {
adcedd8b 144 $this->checkLogin();
e58a96d1 145
b5a532a8 146 $path = 'data/Login.json';
df1e1394 147 $fields = array('csrf_token' => $this->token, 'logout' => 'byby');
219ba661
S
148 $data = $this->sentRequest($path, $fields, true);
149 $data = $this->getValues($data['body']);
150 if ((isset($data['status']) && $data['status'] == 'ok') && $this->checkLogin(false) === false) {
df1e1394
S
151 // reset challenge and session
152 $this->challenge = '';
ac344b92
S
153 $this->cookie = '';
154 $this->token = '';
155 $this->derivedk = '';
df1e1394 156
58feafa2 157 return true;
df1e1394 158 }
58feafa2
S
159
160 return false;
a91317a6
S
161 }
162
163 /**
164 * reboot the router
5e44cffa 165 *
58feafa2 166 * @return boolean
a91317a6
S
167 */
168 public function reboot () {
adcedd8b 169 $this->checkLogin();
e58a96d1 170
b5a532a8 171 $path = 'data/Reboot.json';
8162139f
S
172 $fields = array('csrf_token' => $this->token, 'reboot_device' => 'true');
173 $data = $this->sentEncryptedRequest($path, $fields, true);
219ba661 174 $data = $this->getValues($data['body']);
14d4f286 175
219ba661 176 if ($data['status'] == 'ok') {
aacbbd28
S
177 // reset challenge and session
178 $this->challenge = '';
179 $this->cookie = '';
180 $this->token = '';
181 $this->derivedk = '';
182
8162139f
S
183 // throw an exception because router is unavailable for other tasks
184 // like $this->logout() or $this->checkLogin
185 throw new RebootException('Router Reboot');
186 }
58feafa2
S
187
188 return false;
a91317a6
S
189 }
190
87026df3
S
191 /**
192 * change dsl connection status
5e44cffa 193 *
87026df3 194 * @param string $status
58feafa2 195 * @return boolean
87026df3 196 */
655c9abd 197 public function changeDSLStatus ($status) {
adcedd8b 198 $this->checkLogin();
e58a96d1 199
b5a532a8 200 $path = 'data/Connect.json';
87026df3
S
201
202 if ($status == 'online' || $status == 'offline') {
203 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'req_connect' => $status);
0dca2d61 204 $data = $this->sentRequest($path, $fields, true);
219ba661 205 $data = $this->getValues($data['body']);
c2678616 206
219ba661 207 if ($data['status'] == 'ok') {
58feafa2
S
208 return true;
209 }
210 else {
211 return false;
212 }
87026df3
S
213 }
214 else {
23cc0061 215 throw new RouterException('unknown status');
87026df3
S
216 }
217 }
218
655c9abd
S
219 /**
220 * change lte connection status
221 *
222 * @param string $status
223 * @return boolean
224 */
225 public function changeLTEStatus ($status) {
226 throw new Exception('unstable funtion');
227 $path = 'data/Modules.json';
228
229 if ($status == '0' || $status == '1' || $status == 'yes' || $status == 'no') {
230 if ($status == 'yes') $status = '1';
231 else if ($status == 'no') $status = '0';
232
233 $fields = array('csrf_token' => $this->token, 'use_lte' => $status);
234 $data = $this->sentEncryptedRequest($path, $fields, true);
235
236 // debug only
237 return $data;
238 }
239 else {
240 throw new RouterException('unknown status');
241 }
242 }
243
f94e171e
S
244 /**
245 * get phone book entrys
246 *
247 * @return array
248 */
249 public function getPhoneBookEntrys () {
250 $data = $this->getData('PhoneBook');
251 $data = $this->getValues($data);
252
253 if (isset($data['addbookentry'])) {
254 return $data['addbookentry'];
255 }
256 else {
257 return array();
258 }
259 }
260
58feafa2
S
261 /**
262 * get uptime based on online (connection) time
263 *
264 * @return string
265 */
266 public function getUptime () {
aacbbd28 267 $data = $this->getData('LAN');
58feafa2
S
268 $data = $this->getValues($data);
269
270 return $data['days_online'];
271 }
272
a91317a6
S
273 /**
274 * return the given json as array
5e44cffa 275 *
a91317a6
S
276 * @param string $file
277 * @return array
278 */
279 public function getData ($file) {
ac344b92 280 if ($file != 'Status') $this->checkLogin();
e58a96d1 281
b5a532a8 282 $path = 'data/'.$file.'.json';
a91317a6 283 $fields = array();
0dca2d61 284 $data = $this->sentRequest($path, $fields, true);
a91317a6 285
219ba661 286 return $data['body'];
a91317a6
S
287 }
288
5e44cffa
S
289 /**
290 * get the router syslog
291 *
292 * @return array
293 */
294 public function getSyslog() {
aacbbd28
S
295 $data = $this->getData('SystemMessages');
296 $data = $this->getValues($data);
297
298 if (isset($data['addmessage'])) {
299 return $data['addmessage'];
300 }
301 else {
302 return array();
303 }
5e44cffa
S
304 }
305
15260eeb 306 /**
aacbbd28 307 * get the Missed Calls from router
15260eeb
S
308 *
309 * @return array
310 */
aacbbd28
S
311 public function getMissedCalls() {
312 $data = $this->getData('PhoneCalls');
313 $data = $this->getValues($data);
314
315 if (isset($data['addmissedcalls'])) {
316 return $data['addmissedcalls'];
317 }
318 else {
319 return array();
320 }
15260eeb
S
321 }
322
abf641bb 323 /**
aacbbd28 324 * get the Taken Calls from router
abf641bb
S
325 *
326 * @return array
327 */
aacbbd28
S
328 public function getTakenCalls() {
329 $data = $this->getData('PhoneCalls');
330 $data = $this->getValues($data);
331
332 if (isset($data['addtakencalls'])) {
333 return $data['addtakencalls'];
334 }
335 else {
336 return array();
337 }
0dca2d61
S
338 }
339
340 /**
aacbbd28 341 * get the Dialed Calls from router
0dca2d61
S
342 *
343 * @return array
344 */
aacbbd28
S
345 public function getDialedCalls() {
346 $data = $this->getData('PhoneCalls');
347 $data = $this->getValues($data);
9bd25bb4 348
aacbbd28
S
349 if (isset($data['adddialedcalls'])) {
350 return $data['adddialedcalls'];
351 }
352 else {
353 return array();
354 }
abf641bb
S
355 }
356
809e25bd
S
357 /**
358 * reconnect LTE
359 *
360 * @return array
361 */
c9e082da 362 public function reconnectLte () {
adcedd8b 363 $this->checkLogin();
e58a96d1 364
c9e082da 365 $path = 'data/modules.json';
809e25bd 366 $fields = array('csrf_token' => $this->token, 'lte_reconn' => '1');
8162139f 367 $data = $this->sentEncryptedRequest($path, $fields, true);
c9e082da 368
219ba661 369 return $data['body'];
c9e082da 370 }
809e25bd 371
7f4a51d2
S
372 /**
373 * reset the router to Factory Default
374 * not tested
375 *
376 * @return array
377 */
9bd25bb4 378 public function resetToFactoryDefault () {
adcedd8b 379 $this->checkLogin();
e58a96d1 380
9bd25bb4
S
381 $path = 'data/resetAllSetting.json';
382 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reset_all' => 'true');
0dca2d61 383 $data = $this->sentRequest($path, $fields, true);
9bd25bb4 384
219ba661 385 return $data['body'];
9bd25bb4 386 }
7f4a51d2 387
9bd25bb4 388
4ae464d1
S
389 /**
390 * check if firmware is actual
391 *
392 * @return array
393 */
394 public function checkFirmware () {
adcedd8b 395 $this->checkLogin();
e58a96d1 396
4ae464d1
S
397 $path = 'data/checkfirmware.json';
398 $fields = array('checkfirmware' => 'true');
0dca2d61 399 $data = $this->sentRequest($path, $fields, true);
4ae464d1 400
219ba661 401 return $data['body'];
4ae464d1
S
402 }
403
14d4f286
S
404 /**
405 * decrypt data from router
406 *
407 * @param string $data
408 * @return array
409 */
c2678616 410 private function decrypt ($data) {
14d4f286
S
411 $iv = hex2bin(substr($this->challenge, 16, 16));
412 $adata = hex2bin(substr($this->challenge, 32, 16));
23cc0061 413 $key = hex2bin($this->derivedk);
14d4f286
S
414 $enc = hex2bin($data);
415
23cc0061
S
416 $factory = new CryptLib\Cipher\Factory();
417 $aes = $factory->getBlockCipher('rijndael-128');
418 $aes->setKey($key);
14d4f286
S
419 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
420
421 $mode->decrypt($enc);
422
423 return $mode->finish();
424 }
425
426 /**
427 * decrypt data for the router
428 *
23cc0061 429 * @param string $data
14d4f286
S
430 * @return string
431 */
c2678616 432 private function encrypt ($data) {
14d4f286
S
433 $iv = hex2bin(substr($this->challenge, 16, 16));
434 $adata = hex2bin(substr($this->challenge, 32, 16));
23cc0061 435 $key = hex2bin($this->derivedk);
14d4f286 436
23cc0061
S
437 $factory = new CryptLib\Cipher\Factory();
438 $aes = $factory->getBlockCipher('rijndael-128');
439 $aes->setKey($key);
14d4f286 440 $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
23cc0061 441 $mode->encrypt($data);
14d4f286 442
809e25bd 443 return bin2hex($mode->finish());
14d4f286
S
444 }
445
c2678616
S
446 /**
447 * get the values from array
448 *
449 * @param array $array
450 * @return array
451 */
452 private function getValues($array) {
453 $data = array();
454 foreach ($array as $item) {
aacbbd28 455 // thank you telekom for this piece of shit
8b7e3972 456 if ($item['vartype'] == 'template') {
ec351a7d
S
457 if (is_array($item['varvalue'])) {
458 $data[$item['varid']][] = $this->getValues($item['varvalue']);
459 }
460 else {
23cc0061 461 // i dont know if we need this
ec351a7d
S
462 $data[$item['varid']] = $item['varvalue'];
463 }
58feafa2
S
464 }
465 else {
8b7e3972
S
466 if (is_array($item['varvalue'])) {
467 $data[$item['varid']] = $this->getValues($item['varvalue']);
468 }
469 else {
470 $data[$item['varid']] = $item['varvalue'];
471 }
58feafa2 472 }
c2678616
S
473 }
474
475 return $data;
476 }
477
8162139f
S
478 /**
479 * sends the encrypted request to router
480 *
481 * @param string $path
482 * @param mixed $fields
483 * @param string $cookie
484 * @return array
485 */
486 private function sentEncryptedRequest ($path, $fields, $cookie = false) {
487 $count = count($fields);
23cc0061 488 $fields = $this->encrypt(http_build_query($fields));
8162139f
S
489 return $this->sentRequest($path, $fields, $cookie, $count);
490 }
491
a91317a6
S
492 /**
493 * sends the request to router
5e44cffa 494 *
b5a532a8 495 * @param string $path
809e25bd 496 * @param mixed $fields
a91317a6 497 * @param string $cookie
809e25bd 498 * @param integer $count
a91317a6
S
499 * @return array
500 */
0dca2d61 501 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
49abc701 502 $url = $this->url.$path.'?lang=en';
a91317a6
S
503 $ch = curl_init();
504 curl_setopt($ch, CURLOPT_URL, $url);
505
506 if (!empty($fields)) {
809e25bd
S
507 if (is_array($fields)) {
508 curl_setopt($ch, CURLOPT_POST, count($fields));
509 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
510 }
511 else {
512 curl_setopt($ch, CURLOPT_POST, $count);
513 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
514 }
a91317a6
S
515 }
516
0dca2d61 517 if ($cookie === true) {
8b7e3972 518 curl_setopt($ch, CURLOPT_COOKIE, 'lang=en; challengev='.$this->challenge.'; '.$this->cookie);
a91317a6
S
519 }
520
521 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
522 curl_setopt($ch, CURLOPT_HEADER, true);
523
a91317a6
S
524 $result = curl_exec($ch);
525
526 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
527 $header = substr($result, 0, $header_size);
528 $body = substr($result, $header_size);
529 curl_close($ch);
530
219ba661
S
531 // check if response is empty
532 if (empty($body)) {
533 throw new RouterException('empty response');
534 }
535
8162139f
S
536 // check if body is encrypted (hex instead of json)
537 if (ctype_xdigit($body)) {
538 $body = $this->decrypt($body);
539 }
540
a91317a6
S
541 // fix invalid json
542 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
543 $body = preg_replace('/\'/i', '"', $body);
5e44cffa 544 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
dae16c50 545 $body = preg_replace("/},\s+]/", "}\n]", $body);
a91317a6 546
219ba661 547 // decode json
aacbbd28 548 if (strpos($url, '.json') !== false) {
219ba661
S
549 $body = json_decode($body, true);
550 }
551
a91317a6
S
552 return array('header' => $this->parse_headers($header), 'body' => $body);
553 }
554
809e25bd
S
555 /**
556 * get the csrf_token
557 *
558 * @return string
559 */
560 private function getToken () {
adcedd8b 561 $this->checkLogin();
e58a96d1 562
49abc701 563 $path = 'html/content/overview/index.html';
809e25bd 564 $fields = array();
0dca2d61 565 $data = $this->sentRequest($path, $fields, true);
809e25bd 566
809e25bd
S
567 $a = explode('csrf_token = "', $data['body']);
568 $a = explode('";', $a[1]);
569
570 if (isset($a[0]) && !empty($a[0])) {
571 return $a[0];
572 }
573 else {
219ba661 574 throw new RouterException('unable to get csrf_token');
809e25bd
S
575 }
576 }
577
ac344b92
S
578 /**
579 * calculate the derivedk
580 *
581 * @param string $password
582 * @return string
583 */
584 private function getDerviedk ($password) {
585 $derivedk = '';
586
587 // calculate derivedk
588 if (!function_exists('hash_pbkdf2')) {
ac344b92
S
589 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\PBKDF2(array('hash' => 'sha1'));
590 $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32));
58feafa2 591 $derivedk = substr($derivedk, 0, 32);
ac344b92
S
592 }
593 else {
594 $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32);
595 }
596
58feafa2
S
597 if (empty($derivedk)) {
598 throw new RouterException('unable to calculate derivedk');
599 }
600
ac344b92
S
601 return $derivedk;
602 }
603
604 /**
605 * get cookie from header data
606 *
607 * @param array $data
608 * @return string
609 */
610 private function getCookie ($data) {
58feafa2 611 $cookie = '';
ac344b92
S
612 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
613 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
614 if (isset($match[1]) && !empty($match[1])) {
58feafa2 615 $cookie = $match[1];
ac344b92
S
616 }
617 }
618
58feafa2 619 if (empty($cookie)) {
219ba661 620 throw new RouterException('unable to get the session cookie from the router');
58feafa2
S
621 }
622
623 return $cookie;
ac344b92
S
624 }
625
a91317a6
S
626 /**
627 * parse the curl return header into an array
5e44cffa 628 *
a91317a6
S
629 * @param string $response
630 * @return array
631 */
632 private function parse_headers($response) {
633 $headers = array();
634 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
635
7f4a51d2
S
636 $header_text = explode("\r\n", $header_text);
637 foreach ($header_text as $i => $line) {
a91317a6
S
638 if ($i === 0) {
639 $headers['http_code'] = $line;
640 }
641 else {
642 list ($key, $value) = explode(': ', $line);
643 $headers[$key] = $value;
644 }
645 }
646
647 return $headers;
648 }
7f4a51d2 649}