Skip to content

Commit

Permalink
remove and add some exceptions, add getUptime and fix derivedk alcula…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Stricted committed Jul 20, 2015
1 parent ac344b9 commit 58feafa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ known valid paths for getData() :
* data/filterlist.json
* data/bonding_tr181.json
* data/letinfo.json
* data/Connect.json
* data/Status.json

PHP requirements
Expand Down
64 changes: 47 additions & 17 deletions SpeedportHybrid.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,34 +145,31 @@ public function checkLogin ($exception = true) {
/**
* logout
*
* @return array
* @return boolean
*/
public function logout () {
$this->checkLogin();

$path = 'data/Login.json';
$fields = array('csrf_token' => $this->token, 'logout' => 'byby');
$data = $this->sentRequest($path, $fields, true);
$this->sentRequest($path, $fields, true);
if ($this->checkLogin(false) === false) {
// reset challenge and session
$this->challenge = '';
$this->cookie = '';
$this->token = '';
$this->derivedk = '';

$json = json_decode($data['body'], true);

return $json;
}
else {
throw new RouterExeption('logout failed');
return true;
}

return false;
}

/**
* reboot the router
*
* @return array
* @return boolean
*/
public function reboot () {
$this->checkLogin();
Expand All @@ -189,15 +186,15 @@ public function reboot () {
// like $this->logout() or $this->checkLogin
throw new RebootException('Router Reboot');
}
else {
throw new RouterException('unable to reboot');
}

return false;
}

/**
* change dsl connection status
*
* @param string $status
* @return boolean
*/
public function changeConnectionStatus ($status) {
$this->checkLogin();
Expand All @@ -209,14 +206,33 @@ public function changeConnectionStatus ($status) {
$data = $this->sentRequest($path, $fields, true);

$json = json_decode($data['body'], true);
$json = $this->getValues($json);

return $json;
if ($json['status'] == 'ok') {
return true;
}
else {
return false;
}
}
else {
throw new RouterExeption();
}
}

/**
* get uptime based on online (connection) time
*
* @return string
*/
public function getUptime () {
// TODO: search for a better solution, calling Connect.json need some time
$data = $this->getData('Connect');
$data = $this->getValues($data);

return $data['days_online'];
}

/**
* return the given json as array
*
Expand Down Expand Up @@ -404,7 +420,12 @@ private function encrypt ($data) {
private function getValues($array) {
$data = array();
foreach ($array as $item) {
$data[$item['varid']] = $item['varvalue'];
if (is_array($item['varvalue'])) {
$data[$item['varid']] = $this->getValues($item['varvalue']);
}
else {
$data[$item['varid']] = $item['varvalue'];
}
}

return $data;
Expand Down Expand Up @@ -522,12 +543,16 @@ private function getDerviedk ($password) {
require_once 'CryptLib/CryptLib.php';
$pbkdf2 = new CryptLib\Key\Derivation\PBKDF\PBKDF2(array('hash' => 'sha1'));
$derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32));
$derivedk = substr($this->derivedk, 0, 32);
$derivedk = substr($derivedk, 0, 32);
}
else {
$derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32);
}

if (empty($derivedk)) {
throw new RouterException('unable to calculate derivedk');
}

return $derivedk;
}

Expand All @@ -538,14 +563,19 @@ private function getDerviedk ($password) {
* @return string
*/
private function getCookie ($data) {
$cookie = '';
if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
if (isset($match[1]) && !empty($match[1])) {
return $match[1];
$cookie = $match[1];
}
}

throw new RouterExeption('unable to get the session cookie from the router');
if (empty($cookie)) {
throw new RouterExeption('unable to get the session cookie from the router');
}

return $cookie;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/uptime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
require_once('config.php');
print_r($sp->getUptime());
$sp->logout();

0 comments on commit 58feafa

Please sign in to comment.