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 = '';
32 public function __construct ($password, $url = 'http://speedport.ip/') {
34 $this->getChallenge();
36 if (empty($this->challenge
)) {
37 throw new Exception('unable to get the challenge from the router');
40 $login = $this->login($password);
42 if ($login === false) {
43 throw new Exception('unable to login');
48 * Requests the password-challenge from the router.
50 public function getChallenge () {
51 $path = 'data/Login.json';
52 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'challengev' => 'null');
53 $data = $this->sentRequest($path, $fields);
54 $data = json_decode($data['body'], true);
55 if ($data[1]['varid'] == 'challengev') {
56 $this->challenge
= $data[1]['varvalue'];
61 * login into the router with the given password
63 * @param string $password
66 public function login ($password) {
67 $path = 'data/Login.json';
68 $this->hash
= hash('sha256', $this->challenge
.':'.$password);
69 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
);
70 $data = $this->sentRequest($path, $fields);
71 $json = json_decode($data['body'], true);
72 if ($json[15]['varid'] == 'login' && $json[15]['varvalue'] == 'success') {
73 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
74 preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
75 if (isset($match[1]) && !empty($match[1])) {
76 $this->session
= $match[1];
79 throw new Exception('unable to get the session cookie from the router');
94 public function logout () {
95 $path = 'data/Login.json';
96 $fields = array('logout' => 'byby');
97 $data = $this->sentRequest($path, $fields);
98 // reset challenge and session
99 $this->challenge
= '';
102 $json = json_decode($data['body'], true);
112 public function reboot () {
113 $path = 'data/Reboot.json';
114 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'reboot_device' => 'true');
115 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
116 $data = $this->sentRequest($path, $fields, $cookie);
117 $json = json_decode($data['body'], true);
123 * change dsl connection status
125 * @param string $status
127 public function changeConnectionStatus ($status) {
128 $path = 'data/Connect.json';
130 if ($status == 'online' ||
$status == 'offline') {
131 $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash
, 'req_connect' => $status);
132 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
133 $this->sentRequest($path, $fields, $cookie);
136 throw new Exception();
141 * return the given json as array
143 * the following paths are known to be valid:
145 * /data/interfaces.json
148 * /data/dhcp_client.json
149 * /data/dhcp_server.json
153 * /data/igmp_proxy.json
154 * /data/igmp_snooping.json
160 * /data/bonding_client.json
161 * /data/bonding_tunnel.json
162 * /data/filterlist.json
163 * /data/bonding_tr181.json
166 * /data/Status.json (No login needed)
168 * @param string $file
171 public function getData ($file) {
172 $path = 'data/'.$file.'.json';
174 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
175 $data = $this->sentRequest($path, $fields, $cookie);
177 if (empty($data['body'])) {
178 throw new Exception('unable to get '.$file.' data');
181 $json = json_decode($data['body'], true);
187 * get the router syslog
191 public function getSyslog() {
192 $path = 'data/Syslog.json';
193 $fields = array('exporttype' => '0');
194 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
195 $data = $this->sentRequest($path, $fields, $cookie);
197 if (empty($data['body'])) {
198 throw new Exception('unable to get syslog data');
201 return explode("\n", $data['body']);
205 * sends the request to router
207 * @param string $path
208 * @param array $fields
209 * @param string $cookie
212 private function sentRequest ($path, $fields = array(), $cookie = '') {
213 $url = $this->url
.$path;
215 curl_setopt($ch, CURLOPT_URL
, $url);
217 if (!empty($fields)) {
218 curl_setopt($ch, CURLOPT_POST
, count($fields));
219 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
222 if (!empty($cookie)) {
223 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
226 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
227 curl_setopt($ch, CURLOPT_HEADER
, true);
234 $result = curl_exec($ch);
236 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
237 $header = substr($result, 0, $header_size);
238 $body = substr($result, $header_size);
242 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
243 $body = preg_replace('/\'/i', '"', $body);
244 $body = preg_replace("/},\n\n]/", "}\n]", $body);
245 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
246 $body = preg_replace("/},\n\s+]/", "} ]", $body);
248 return array('header' => $this->parse_headers($header), 'body' => $body);
252 * parse the curl return header into an array
254 * @param string $response
257 private function parse_headers($response) {
259 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
261 foreach (explode("\r\n", $header_text) as $i => $line) {
263 $headers['http_code'] = $line;
266 list ($key, $value) = explode(': ', $line);
267 $headers[$key] = $value;