Skip to content

Commit

Permalink
optimize existing code, update to version 1.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Stricted committed Jul 20, 2015
1 parent 58feafa commit 219ba66
Showing 1 changed file with 32 additions and 55 deletions.
87 changes: 32 additions & 55 deletions SpeedportHybrid.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SpeedportHybrid {
*
*
*/
const VERSION = '1.0.2';
const VERSION = '1.0.3';

/**
* password-challenge
Expand Down Expand Up @@ -61,14 +61,13 @@ private function getChallenge () {
$path = 'data/Login.json';
$fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
$data = $this->sentRequest($path, $fields);
$data = json_decode($data['body'], true);
$data = $this->getValues($data);
$data = $this->getValues($data['body']);

if (isset($data['challengev']) && !empty($data['challengev'])) {
return $data['challengev'];
}
else {
throw new RouterExeption('unable to get the challenge from the router');
throw new RouterException('unable to get the challenge from the router');
}
}

Expand All @@ -85,8 +84,7 @@ public function login ($password) {
$this->hash = hash('sha256', $this->challenge.':'.$password);
$fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash);
$data = $this->sentRequest($path, $fields);
$json = json_decode($data['body'], true);
$json = $this->getValues($json);
$json = $this->getValues($data['body']);

if (isset($json['login']) && $json['login'] == 'success') {
$this->cookie = $this->getCookie($data);
Expand Down Expand Up @@ -114,7 +112,7 @@ public function checkLogin ($exception = true) {
// check if challenge or session is empty
if (empty($this->challenge) || empty($this->cookie)) {
if ($exception === true) {
throw new RouterExeption('you musst be logged in to use this method');
throw new RouterException('you musst be logged in to use this method');
}

return false;
Expand All @@ -123,17 +121,11 @@ public function checkLogin ($exception = true) {
$path = 'data/SecureStatus.json';
$fields = array();
$data = $this->sentRequest($path, $fields, true);
$data = $this->getValues($data['body']);

if (empty($data['body'])) {
throw new RouterExeption('unable to get SecureStatus data');
}

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

if ($json['loginstate'] != 1) {
if ($data['loginstate'] != 1) {
if ($exception === true) {
throw new RouterExeption('you musst be logged in to use this method');
throw new RouterException('you musst be logged in to use this method');
}

return false;
Expand All @@ -152,8 +144,9 @@ public function logout () {

$path = 'data/Login.json';
$fields = array('csrf_token' => $this->token, 'logout' => 'byby');
$this->sentRequest($path, $fields, true);
if ($this->checkLogin(false) === false) {
$data = $this->sentRequest($path, $fields, true);
$data = $this->getValues($data['body']);
if ((isset($data['status']) && $data['status'] == 'ok') && $this->checkLogin(false) === false) {
// reset challenge and session
$this->challenge = '';
$this->cookie = '';
Expand All @@ -177,11 +170,9 @@ public function reboot () {
$path = 'data/Reboot.json';
$fields = array('csrf_token' => $this->token, 'reboot_device' => 'true');
$data = $this->sentEncryptedRequest($path, $fields, true);
$data = $this->getValues($data['body']);

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

if ($json['status'] == 'ok') {
if ($data['status'] == 'ok') {
// throw an exception because router is unavailable for other tasks
// like $this->logout() or $this->checkLogin
throw new RebootException('Router Reboot');
Expand All @@ -204,19 +195,17 @@ public function changeConnectionStatus ($status) {
if ($status == 'online' || $status == 'offline') {
$fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'req_connect' => $status);
$data = $this->sentRequest($path, $fields, true);
$data = $this->getValues($data['body']);

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

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

Expand Down Expand Up @@ -246,13 +235,7 @@ public function getData ($file) {
$fields = array();
$data = $this->sentRequest($path, $fields, true);

if (empty($data['body'])) {
throw new RouterExeption('unable to get '.$file.' data');
}

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

return $json;
return $data['body'];
}

/**
Expand Down Expand Up @@ -303,10 +286,6 @@ private function exportData ($type) {
$fields = array('exporttype' => $type);
$data = $this->sentRequest($path, $fields, true);

if (empty($data['body'])) {
throw new RouterExeption('unable to get export data');
}

return explode("\n", $data['body']);
}

Expand All @@ -321,9 +300,8 @@ public function reconnectLte () {
$path = 'data/modules.json';
$fields = array('csrf_token' => $this->token, 'lte_reconn' => '1');
$data = $this->sentEncryptedRequest($path, $fields, true);
$json = json_decode($data['body'], true);

return $json;
return $data['body'];
}

/**
Expand All @@ -338,9 +316,8 @@ public function resetToFactoryDefault () {
$path = 'data/resetAllSetting.json';
$fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reset_all' => 'true');
$data = $this->sentRequest($path, $fields, true);
$json = json_decode($data['body'], true);

return $json;
return $data['body'];
}


Expand All @@ -356,13 +333,7 @@ public function checkFirmware () {
$fields = array('checkfirmware' => 'true');
$data = $this->sentRequest($path, $fields, true);

if (empty($data['body'])) {
throw new RouterExeption('unable to get checkfirmware data');
}

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

return $json;
return $data['body'];
}

/**
Expand Down Expand Up @@ -488,6 +459,11 @@ private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
$body = substr($result, $header_size);
curl_close($ch);

// check if response is empty
if (empty($body)) {
throw new RouterException('empty response');
}

// check if body is encrypted (hex instead of json)
if (ctype_xdigit($body)) {
$body = $this->decrypt($body);
Expand All @@ -499,6 +475,11 @@ private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
$body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
$body = preg_replace("/},\s+]/", "}\n]", $body);

// decode json
if (strpos($url, '.json') !== false) {
$body = json_decode($body, true);
}

return array('header' => $this->parse_headers($header), 'body' => $body);
}

Expand All @@ -514,18 +495,14 @@ private function getToken () {
$fields = array();
$data = $this->sentRequest($path, $fields, true);

if (empty($data['body'])) {
throw new RouterExeption('unable to get csrf_token');
}

$a = explode('csrf_token = "', $data['body']);
$a = explode('";', $a[1]);

if (isset($a[0]) && !empty($a[0])) {
return $a[0];
}
else {
throw new RouterExeption('unable to get csrf_token');
throw new RouterException('unable to get csrf_token');
}
}

Expand Down Expand Up @@ -572,7 +549,7 @@ private function getCookie ($data) {
}

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

return $cookie;
Expand Down

0 comments on commit 219ba66

Please sign in to comment.