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