* session cookie
* @var string
*/
- private $session = '';
+ private $cookie = '';
/**
* router url
$data = $this->getValues($data);
if (isset($data['challengev']) && !empty($data['challengev'])) {
- $this->challenge = $data['challengev'];
+ return $data['challengev'];
+ }
+ else {
+ throw new RouterExeption('unable to get the challenge from the router');
}
}
* @return boolean
*/
public function login ($password) {
- $this->getChallenge();
-
- if (empty($this->challenge)) {
- throw new RouterExeption('unable to get the challenge from the router');
- }
+ $this->challenge = $this->getChallenge();
$path = 'data/Login.json';
$this->hash = hash('sha256', $this->challenge.':'.$password);
$data = $this->sentRequest($path, $fields);
$json = json_decode($data['body'], true);
$json = $this->getValues($json);
+
if (isset($json['login']) && $json['login'] == 'success') {
- 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])) {
- $this->session = $match[1];
- }
- else {
- throw new RouterExeption('unable to get the session cookie from the router');
- }
-
- // calculate derivedk
- if (!function_exists("hash_pbkdf2")) {
- require_once 'CryptLib/CryptLib.php';
- $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\PBKDF2(array('hash' => 'sha1'));
- $this->derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32));
- $this->derivedk = substr($this->derivedk, 0, 32);
- }
- else {
- $this->derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32);
- }
-
- // get the csrf_token
- $this->token = $this->getToken();
-
- if ($this->checkLogin() === true) {
- return true;
- }
+ $this->cookie = $this->getCookie($data);
+
+ $this->derivedk = $this->getDerviedk($password);
+
+ // get the csrf_token
+ $this->token = $this->getToken();
+
+ if ($this->checkLogin(false) === true) {
+ return true;
}
}
/**
* check if we are logged in
*
- * @return boolean
+ * @param boolean $exception
+ * @return boolean
*/
- public function checkLogin () {
- if (empty($this->challenge) && empty($this->session)) {
+ 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');
+ }
+
return false;
}
$json = $this->getValues($json);
if ($json['loginstate'] != 1) {
+ if ($exception === true) {
+ throw new RouterExeption('you musst be logged in to use this method');
+ }
+
return false;
}
/**
* logout
*
- * @return array
+ * @return boolean
*/
public function logout () {
- if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+ $this->checkLogin();
$path = 'data/Login.json';
$fields = array('csrf_token' => $this->token, 'logout' => 'byby');
- $data = $this->sentRequest($path, $fields, true);
- if ($this->checkLogin() === false) {
+ $this->sentRequest($path, $fields, true);
+ if ($this->checkLogin(false) === false) {
// reset challenge and session
$this->challenge = '';
- $this->session = '';
- $this->token = "";
+ $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 () {
- if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+ $this->checkLogin();
$path = 'data/Reboot.json';
$fields = array('csrf_token' => $this->token, 'reboot_device' => 'true');
// 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) {
- if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+ $this->checkLogin();
$path = 'data/Connect.json';
$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
*
* @return array
*/
public function getData ($file) {
- if ($this->checkLogin() !== true && $file != "Status") throw new RouterExeption('you musst be logged in to use this method');
+ if ($file != 'Status') $this->checkLogin();
$path = 'data/'.$file.'.json';
$fields = array();
* @return array
*/
private function exportData ($type) {
- if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+ $this->checkLogin();
$path = 'data/Syslog.json';
$fields = array('exporttype' => $type);
* @return array
*/
public function reconnectLte () {
- if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+ $this->checkLogin();
$path = 'data/modules.json';
$fields = array('csrf_token' => $this->token, 'lte_reconn' => '1');
* @return array
*/
public function resetToFactoryDefault () {
- if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+ $this->checkLogin();
$path = 'data/resetAllSetting.json';
$fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reset_all' => 'true');
* @return array
*/
public function checkFirmware () {
- if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+ $this->checkLogin();
$path = 'data/checkfirmware.json';
$fields = array('checkfirmware' => 'true');
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;
}
if ($cookie === true) {
- curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->session);
+ curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->cookie);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
* @return string
*/
private function getToken () {
- if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+ $this->checkLogin();
$path = 'html/content/overview/index.html';
$fields = array();
}
}
+ /**
+ * calculate the derivedk
+ *
+ * @param string $password
+ * @return string
+ */
+ private function getDerviedk ($password) {
+ $derivedk = '';
+
+ // calculate derivedk
+ if (!function_exists('hash_pbkdf2')) {
+ 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($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;
+ }
+
+ /**
+ * get cookie from header data
+ *
+ * @param array $data
+ * @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])) {
+ $cookie = $match[1];
+ }
+ }
+
+ if (empty($cookie)) {
+ throw new RouterExeption('unable to get the session cookie from the router');
+ }
+
+ return $cookie;
+ }
+
/**
* parse the curl return header into an array
*