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 = '';
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', 'showpw' => 0, 'password' => $this->hash
);
50 $data = $this->sentRequest($path, $fields);
51 $json = $this->getValues($data['body']);
53 if (isset($json['login']) && $json['login'] == 'success') {
54 $this->cookie
= $this->getCookie($data);
56 $this->derivedk
= $this->getDerviedk($password);
59 $this->token
= $this->getToken();
61 if ($this->checkLogin(false) === true) {
70 * Requests the password-challenge from the router.
72 private function getChallenge () {
73 $path = 'data/Login.json';
74 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
75 $data = $this->sentRequest($path, $fields);
76 $data = $this->getValues($data['body']);
78 if (isset($data['challengev']) && !empty($data['challengev'])) {
79 return $data['challengev'];
82 throw new RouterException('unable to get the challenge from the router');
87 * check if we are logged in
89 * @param boolean $exception
92 public function checkLogin ($exception = true) {
93 // check if challenge or session is empty
94 if (empty($this->challenge
) ||
empty($this->cookie
)) {
95 if ($exception === true) {
96 throw new RouterException('you musst be logged in to use this method');
102 $path = 'data/SecureStatus.json';
104 $data = $this->sentRequest($path, $fields, true);
105 $data = $this->getValues($data['body']);
107 if ($data['loginstate'] != 1) {
108 if ($exception === true) {
109 throw new RouterException('you musst be logged in to use this method');
123 public function logout () {
126 $path = 'data/Login.json';
127 $fields = array('csrf_token' => $this->token
, 'logout' => 'byby');
128 $data = $this->sentRequest($path, $fields, true);
129 $data = $this->getValues($data['body']);
130 if ((isset($data['status']) && $data['status'] == 'ok') && $this->checkLogin(false) === false) {
131 // reset challenge and session
132 $this->challenge
= '';
135 $this->derivedk
= '';
148 private function getToken () {
151 $path = 'html/content/overview/index.html';
153 $data = $this->sentRequest($path, $fields, true);
155 $a = explode('csrf_token = "', $data['body']);
156 $a = explode('";', $a[1]);
158 if (isset($a[0]) && !empty($a[0])) {
162 throw new RouterException('unable to get csrf_token');
167 * calculate the derivedk
169 * @param string $password
172 private function getDerviedk ($password) {
175 // calculate derivedk
176 if (!function_exists('hash_pbkdf2')) {
177 $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\
PBKDF2(array('hash' => 'sha1'));
178 $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32));
179 $derivedk = substr($derivedk, 0, 32);
182 $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge
, 0, 16), 1000, 32);
185 if (empty($derivedk)) {
186 throw new RouterException('unable to calculate derivedk');
193 * get cookie from header data
198 private function getCookie ($data) {
200 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
201 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
202 if (isset($match[1]) && !empty($match[1])) {
207 if (empty($cookie)) {
208 throw new RouterException('unable to get the session cookie from the router');