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']);
52 if (array_key_exists("challengev", $json)) {
53 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
54 $data = $this->sendRequest($path, $fields);
55 $json = $this->getValues($data['body']);
57 if (isset($json['login']) && $json['login'] == 'success') {
58 $this->cookie
= $this->getCookie($data);
60 $this->derivedk
= $this->getDerviedk($password);
63 $this->token
= $this->getToken();
65 if ($this->checkLogin(false) === true) {
74 * Requests the password-challenge from the router.
76 private function getChallenge_old () {
77 $path = 'data/Login.json';
78 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
79 $data = $this->sendRequest($path, $fields);
80 $data = $this->getValues($data['body']);
82 if (isset($data['challengev']) && !empty($data['challengev'])) {
83 return $data['challengev'];
86 throw new RouterException('unable to get the challenge from the router');
91 * Requests the password-challenge from the router.
93 private function getChallenge () {
94 $path = 'html/login/index.html';
96 $data = $this->sendRequest($path, $fields);
97 $a = explode('challenge = "', $data['body']);
100 return $this->getChallenge_old();
103 $a = explode('";', $a[1]);
105 if (isset($a[0]) && !empty($a[0])) {
109 throw new RouterException('unable to get the challenge from the router');
114 * check if we are logged in
116 * @param boolean $exception
119 public function checkLogin ($exception = true) {
120 // check if challenge or session is empty
121 if (empty($this->challenge
) ||
empty($this->cookie
)) {
122 if ($exception === true) {
123 throw new RouterException('you musst be logged in to use this method');
129 $path = 'data/SecureStatus.json';
131 $data = $this->sendRequest($path, $fields, true);
132 $data = $this->getValues($data['body']);
134 if ($data['loginstate'] != 1) {
135 if ($exception === true) {
136 throw new RouterException('you musst be logged in to use this method');
150 public function logout () {
153 $path = 'data/Login.json';
154 $fields = array('csrf_token' => $this->token
, 'logout' => 'byby');
155 $data = $this->sendRequest($path, $fields, true);
156 $data = $this->getValues($data['body']);
157 if ((isset($data['status']) && $data['status'] == 'ok') && $this->checkLogin(false) === false) {
158 // reset challenge and session
159 $this->challenge
= '';
162 $this->derivedk
= '';
175 private function getToken () {
178 $path = 'html/content/overview/index.html';
180 $data = $this->sendRequest($path, $fields, true);
182 $a = explode('csrf_token = "', $data['body']);
183 $a = explode('";', $a[1]);
185 if (isset($a[0]) && !empty($a[0])) {
189 throw new RouterException('unable to get csrf_token');
194 * calculate the derivedk
196 * @param string $password
199 private function getDerviedk ($password) {
202 // calculate derivedk
203 if (!function_exists('hash_pbkdf2')) {
204 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\
PBKDF2(array('hash' => 'sha1'));
205 $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 16));
208 $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
211 if (empty($derivedk)) {
212 throw new RouterException('unable to calculate derivedk');
219 * get cookie from header data
224 private function getCookie ($data) {
226 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
227 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
228 if (isset($match[1]) && !empty($match[1])) {
233 if (empty($cookie)) {
234 throw new RouterException('unable to get the session cookie from the router');