7 private $challenge = '';
19 private $session = '';
25 private $url = 'http://speedport.ip/';
27 public function __construct ($password) {
28 $this->getChallenge();
30 if (empty($this->challenge
)) {
31 throw new Exception('unable to get the challenge from the router');
34 $login = $this->login($password);
36 if ($login === false) {
37 throw new Exception('unable to login');
42 * Requests the password-challenge from the router.
44 public function getChallenge () {
45 $url = 'data/Login.json';
46 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
47 $data = $this->sentRequest($url, $fields);
48 $data = json_decode($data['body'], true);
49 if ($data[1]['varid'] == 'challengev') {
50 $this->challenge
= $data[1]['varvalue'];
55 * login into the router with the given password
57 * @param string $password
60 public function login ($password) {
61 $url = 'data/Login.json';
62 $this->hash
= hash('sha256', $this->challenge
.':'.$password);
63 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
64 $data = $this->sentRequest($url, $fields);
65 $json = json_decode($data['body'], true);
66 if ($json[15]['varid'] == 'login' && $json[15]['varvalue'] == 'success') {
67 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
68 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
69 if (isset($match[1]) && !empty($match[1])) {
70 $this->session
= $match[1];
73 throw new Exception('unable to get the session cookie from the router');
88 public function logout () {
89 $url = 'data/Login.json';
90 $fields = array('logout' => 'byby');
91 $data = $this->sentRequest($url, $fields);
92 // reset challenge and session
93 $this->challenge
= '';
96 $json = json_decode($data['body'], true);
106 public function reboot () {
107 $url = 'data/Reboot.json';
108 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reboot_device' => 'true');
109 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
110 $data = $this->sentRequest($url, $fields, $cookie);
111 $json = json_decode($data['body'], true);
117 * return the given json as array
119 * the following paths are known to be valid:
121 * /data/interfaces.json
124 * /data/dhcp_client.json
125 * /data/dhcp_server.json
129 * /data/igmp_proxy.json
130 * /data/igmp_snooping.json
136 * /data/bonding_client.json
137 * /data/bonding_tunnel.json
138 * /data/filterlist.json
139 * /data/bonding_tr181.json
142 * /data/Status.json (No login needed)
144 * @param string $file
147 public function getData ($file) {
148 $url = 'data/'.$file.'.json';
150 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
151 $data = $this->sentRequest($url, $fields, $cookie);
153 if (empty($data['body'])) {
154 throw new Exception('unable to get '.$file.' data');
157 $json = json_decode($data['body'], true);
163 * sends the request to router
166 * @param array $fields
167 * @param string $cookie
170 private function sentRequest ($url, $fields = array(), $cookie = '') {
171 $url = $this->url
.$url;
173 curl_setopt($ch, CURLOPT_URL
, $url);
175 if (!empty($fields)) {
176 curl_setopt($ch, CURLOPT_POST
, count($fields));
177 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
180 if (!empty($cookie)) {
181 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
184 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
185 curl_setopt($ch, CURLOPT_HEADER
, true);
192 $result = curl_exec($ch);
194 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
195 $header = substr($result, 0, $header_size);
196 $body = substr($result, $header_size);
200 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
201 $body = preg_replace('/\'/i', '"', $body);
202 $body = preg_replace("/},\n\n]/", "}\n]", $body);
203 $body = preg_replace('/\s+/', ' ', $body);
204 $body = preg_replace("/\[ \]/i", '[ {} ]', $body);
205 $body = preg_replace("/}, ]/", "} ]", $body);
206 $body = preg_replace("/\n/", " ", $body);
208 return array('header' => $this->parse_headers($header), 'body' => $body);
212 * parse the curl return header into an array
214 * @param string $response
217 private function parse_headers($response) {
219 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
221 foreach (explode("\r\n", $header_text) as $i => $line) {
223 $headers['http_code'] = $line;
226 list ($key, $value) = explode(': ', $line);
227 $headers[$key] = $value;