3 * @author Jan Altensen (Stricted)
4 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
5 * @copyright 2015-2016 Jan Altensen (Stricted)
12 private $challenge = '';
36 private $derivedk = '';
39 * login into the router with the given password
41 * @param string $password
44 public function login ($password) {
45 $this->challenge
= $this->getChallenge();
47 $path = 'data/Login.json';
48 $this->hash
= hash('sha256', $this->challenge
.':'.$password);
49 $fields = array('csrf_token' => 'nulltoken', "challengev" => $this->challenge
, 'showpw' => 0, 'password' => $this->hash
);
50 $data = $this->sendRequest($path, $fields);
51 $json = $this->getValues($data['body']);
53 if (in_array("challenge", $json)) {
54 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
55 $data = $this->sendRequest($path, $fields);
56 $json = $this->getValues($data['body']);
59 if (isset($json['login']) && $json['login'] == 'success') {
60 $this->cookie
= $this->getCookie($data);
62 $this->derivedk
= $this->getDerviedk($password);
65 $this->token
= $this->getToken();
67 if ($this->checkLogin(false) === true) {
76 * Requests the password-challenge from the router.
78 private function getChallenge_old () {
79 $path = 'data/Login.json';
80 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
81 $data = $this->sendRequest($path, $fields);
82 $data = $this->getValues($data['body']);
84 if (isset($data['challengev']) && !empty($data['challengev'])) {
85 return $data['challengev'];
88 throw new RouterException('unable to get the challenge from the router');
93 * Requests the password-challenge from the router.
95 private function getChallenge () {
96 $path = 'html/content/overview/index.html';
98 $data = $this->sendRequest($path, $fields, true);
100 $a = explode('challenge = "', $data['body']);
103 return getChallenge_old();
106 $a = explode('";', $a[1]);
108 if (isset($a[0]) && !empty($a[0])) {
112 throw new RouterException('unable to get the challenge from the router');
117 * check if we are logged in
119 * @param boolean $exception
122 public function checkLogin ($exception = true) {
123 // check if challenge or session is empty
124 if (empty($this->challenge
) ||
empty($this->cookie
)) {
125 if ($exception === true) {
126 throw new RouterException('you musst be logged in to use this method');
132 $path = 'data/SecureStatus.json';
134 $data = $this->sendRequest($path, $fields, true);
135 $data = $this->getValues($data['body']);
137 if ($data['loginstate'] != 1) {
138 if ($exception === true) {
139 throw new RouterException('you musst be logged in to use this method');
153 public function logout () {
156 $path = 'data/Login.json';
157 $fields = array('csrf_token' => $this->token
, 'logout' => 'byby');
158 $data = $this->sendRequest($path, $fields, true);
159 $data = $this->getValues($data['body']);
160 if ((isset($data['status']) && $data['status'] == 'ok') && $this->checkLogin(false) === false) {
161 // reset challenge and session
162 $this->challenge
= '';
165 $this->derivedk
= '';
178 private function getToken () {
181 $path = 'html/content/overview/index.html';
183 $data = $this->sendRequest($path, $fields, true);
185 $a = explode('csrf_token = "', $data['body']);
186 $a = explode('";', $a[1]);
188 if (isset($a[0]) && !empty($a[0])) {
192 throw new RouterException('unable to get csrf_token');
197 * calculate the derivedk
199 * @param string $password
202 private function getDerviedk ($password) {
205 // calculate derivedk
206 if (!function_exists('hash_pbkdf2')) {
207 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\
PBKDF2(array('hash' => 'sha1'));
208 $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 16));
211 $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
214 if (empty($derivedk)) {
215 throw new RouterException('unable to calculate derivedk');
222 * get cookie from header data
227 private function getCookie ($data) {
229 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
230 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
231 if (isset($match[1]) && !empty($match[1])) {
236 if (empty($cookie)) {
237 throw new RouterException('unable to get the session cookie from the router');