36837cef33c7c4b5de695fe0671da36a9d5bb6fd
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 * change dsl connection status
124 * @param string $status
126 public function changeConnectionStatus ($status) {
127 $url = 'data/Connect.json';
129 if ($status == 'online' ||
$status == 'offline') {
130 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'req_connect' => $status);
131 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
132 $this->sentRequest($url, $fields, $cookie);
135 throw new Exception();
140 * return the given json as array
142 * the following paths are known to be valid:
144 * /data/interfaces.json
147 * /data/dhcp_client.json
148 * /data/dhcp_server.json
152 * /data/igmp_proxy.json
153 * /data/igmp_snooping.json
159 * /data/bonding_client.json
160 * /data/bonding_tunnel.json
161 * /data/filterlist.json
162 * /data/bonding_tr181.json
165 * /data/Status.json (No login needed)
167 * @param string $file
170 public function getData ($file) {
171 $url = 'data/'.$file.'.json';
173 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
174 $data = $this->sentRequest($url, $fields, $cookie);
176 if (empty($data['body'])) {
177 throw new Exception('unable to get '.$file.' data');
180 $json = json_decode($data['body'], true);
186 * sends the request to router
189 * @param array $fields
190 * @param string $cookie
193 private function sentRequest ($url, $fields = array(), $cookie = '') {
194 $url = $this->url
.$url;
196 curl_setopt($ch, CURLOPT_URL
, $url);
198 if (!empty($fields)) {
199 curl_setopt($ch, CURLOPT_POST
, count($fields));
200 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
203 if (!empty($cookie)) {
204 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
207 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
208 curl_setopt($ch, CURLOPT_HEADER
, true);
215 $result = curl_exec($ch);
217 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
218 $header = substr($result, 0, $header_size);
219 $body = substr($result, $header_size);
223 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
224 $body = preg_replace('/\'/i', '"', $body);
225 $body = preg_replace("/},\n\n]/", "}\n]", $body);
226 $body = preg_replace('/\s+/', ' ', $body);
227 $body = preg_replace("/\[ \]/i", '[ {} ]', $body);
228 $body = preg_replace("/}, ]/", "} ]", $body);
229 $body = preg_replace("/\n/", " ", $body);
231 return array('header' => $this->parse_headers($header), 'body' => $body);
235 * parse the curl return header into an array
237 * @param string $response
240 private function parse_headers($response) {
242 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
244 foreach (explode("\r\n", $header_text) as $i => $line) {
246 $headers['http_code'] = $line;
249 list ($key, $value) = explode(': ', $line);
250 $headers[$key] = $value;