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)
12 private $challenge = '';
18 private $derivedk = '';
21 * login into the router with the given password
23 * @param string $password
26 public function login ($password) {
27 $this->challenge
= $this->getChallenge();
29 $path = 'data/Login.json';
30 $this->hash
= hash('sha256', $this->challenge
.':'.$password);
31 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
32 $data = $this->sentRequest($path, $fields);
33 $json = $this->getValues($data['body']);
35 if (isset($json['login']) && $json['login'] == 'success') {
36 $this->cookie
= 'challengev='.$this->challenge
.'; ';
37 $this->cookie
.= $this->getCookie($data);
39 $this->derivedk
= $this->getDerviedk($password);
42 $this->token
= $this->getToken();
44 if ($this->checkLogin(false) === true) {
53 * Requests the password-challenge from the router.
55 private function getChallenge () {
56 $path = 'data/Login.json';
57 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
58 $data = $this->sentRequest($path, $fields);
59 $data = $this->getValues($data['body']);
61 if (isset($data['challengev']) && !empty($data['challengev'])) {
62 return $data['challengev'];
65 throw new RouterException('unable to get the challenge from the router');
70 * check if we are logged in
72 * @param boolean $exception
75 public function checkLogin ($exception = true) {
76 // check if challenge or session is empty
77 if (empty($this->challenge
) ||
empty($this->cookie
)) {
78 if ($exception === true) {
79 throw new RouterException('you musst be logged in to use this method');
85 $path = 'data/SecureStatus.json';
87 $data = $this->sentRequest($path, $fields, true);
88 $data = $this->getValues($data['body']);
90 if ($data['loginstate'] != 1) {
91 if ($exception === true) {
92 throw new RouterException('you musst be logged in to use this method');
106 public function logout () {
109 $path = 'data/Login.json';
110 $fields = array('csrf_token' => $this->token
, 'logout' => 'byby');
111 $data = $this->sentRequest($path, $fields, true);
112 $data = $this->getValues($data['body']);
113 if ((isset($data['status']) && $data['status'] == 'ok') && $this->checkLogin(false) === false) {
114 // reset challenge and session
115 $this->challenge
= '';
118 $this->derivedk
= '';
131 protected function getToken () {
134 $path = 'html/content/overview/index.html';
136 $data = $this->sentRequest($path, $fields, true);
138 $a = explode('csrf_token = "', $data['body']);
139 $a = explode('";', $a[1]);
141 if (isset($a[0]) && !empty($a[0])) {
145 throw new RouterException('unable to get csrf_token');
150 * calculate the derivedk
152 * @param string $password
155 private function getDerviedk ($password) {
158 // calculate derivedk
159 if (!function_exists('hash_pbkdf2')) {
160 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\
PBKDF2(array('hash' => 'sha1'));
161 $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32));
162 $derivedk = substr($derivedk, 0, 32);
165 $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
168 if (empty($derivedk)) {
169 throw new RouterException('unable to calculate derivedk');
176 * get cookie from header data
181 private function getCookie ($data) {
183 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
184 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
185 if (isset($match[1]) && !empty($match[1])) {
190 if (empty($cookie)) {
191 throw new RouterException('unable to get the session cookie from the router');