1cf4c7e5eac40c4300061507295cae43e08f0689
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 = '';
24 private $session = '';
30 private $url = 'http://speedport.ip/';
32 public function __construct ($password) {
33 $this->getChallenge();
35 if (empty($this->challenge
)) {
36 throw new Exception('unable to get the challenge from the router');
39 $login = $this->login($password);
41 if ($login === false) {
42 throw new Exception('unable to login');
47 * Requests the password-challenge from the router.
49 public function getChallenge () {
50 $url = 'data/Login.json';
51 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
52 $data = $this->sentRequest($url, $fields);
53 $data = json_decode($data['body'], true);
54 if ($data[1]['varid'] == 'challengev') {
55 $this->challenge
= $data[1]['varvalue'];
60 * login into the router with the given password
62 * @param string $password
65 public function login ($password) {
66 $url = 'data/Login.json';
67 $this->hash
= hash('sha256', $this->challenge
.':'.$password);
68 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
69 $data = $this->sentRequest($url, $fields);
70 $json = json_decode($data['body'], true);
71 if ($json[15]['varid'] == 'login' && $json[15]['varvalue'] == 'success') {
72 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
73 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
74 if (isset($match[1]) && !empty($match[1])) {
75 $this->session
= $match[1];
78 throw new Exception('unable to get the session cookie from the router');
93 public function logout () {
94 $url = 'data/Login.json';
95 $fields = array('logout' => 'byby');
96 $data = $this->sentRequest($url, $fields);
97 // reset challenge and session
98 $this->challenge
= '';
101 $json = json_decode($data['body'], true);
111 public function reboot () {
112 $url = 'data/Reboot.json';
113 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reboot_device' => 'true');
114 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
115 $data = $this->sentRequest($url, $fields, $cookie);
116 $json = json_decode($data['body'], true);
122 * return the given json as array
124 * the following paths are known to be valid:
126 * /data/interfaces.json
129 * /data/dhcp_client.json
130 * /data/dhcp_server.json
134 * /data/igmp_proxy.json
135 * /data/igmp_snooping.json
141 * /data/bonding_client.json
142 * /data/bonding_tunnel.json
143 * /data/filterlist.json
144 * /data/bonding_tr181.json
147 * /data/Status.json (No login needed)
149 * @param string $file
152 public function getData ($file) {
153 $url = 'data/'.$file.'.json';
155 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
156 $data = $this->sentRequest($url, $fields, $cookie);
158 if (empty($data['body'])) {
159 throw new Exception('unable to get '.$file.' data');
162 $json = json_decode($data['body'], true);
168 * sends the request to router
171 * @param array $fields
172 * @param string $cookie
175 private function sentRequest ($url, $fields = array(), $cookie = '') {
176 $url = $this->url
.$url;
178 curl_setopt($ch, CURLOPT_URL
, $url);
180 if (!empty($fields)) {
181 curl_setopt($ch, CURLOPT_POST
, count($fields));
182 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
185 if (!empty($cookie)) {
186 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
189 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
190 curl_setopt($ch, CURLOPT_HEADER
, true);
197 $result = curl_exec($ch);
199 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
200 $header = substr($result, 0, $header_size);
201 $body = substr($result, $header_size);
205 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
206 $body = preg_replace('/\'/i', '"', $body);
207 $body = preg_replace("/},\n\n]/", "}\n]", $body);
208 $body = preg_replace('/\s+/', ' ', $body);
209 $body = preg_replace("/\[ \]/i", '[ {} ]', $body);
210 $body = preg_replace("/}, ]/", "} ]", $body);
211 $body = preg_replace("/\n/", " ", $body);
213 return array('header' => $this->parse_headers($header), 'body' => $body);
217 * parse the curl return header into an array
219 * @param string $response
222 private function parse_headers($response) {
224 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
226 foreach (explode("\r\n", $header_text) as $i => $line) {
228 $headers['http_code'] = $line;
231 list ($key, $value) = explode(': ', $line);
232 $headers[$key] = $value;