692b7c31647a148f0489ddf6ea6ea3cb9fdfa17e
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 * get the Missed Calls from router
209 public function getMissedCalls() {
210 $path = 'data/ExportMissedCalls.json';
211 $fields = array('exporttype' => '1');
212 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
213 $data = $this->sentRequest($path, $fields, $cookie);
215 if (empty($data['body'])) {
216 throw new Exception('unable to get syslog data');
219 return explode("\n", $data['body']);
223 * get the Taken Calls from router
227 public function getTakenCalls() {
228 $path = 'data/ExportTakenCalls.json';
229 $fields = array('exporttype' => '2');
230 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
231 $data = $this->sentRequest($path, $fields, $cookie);
233 if (empty($data['body'])) {
234 throw new Exception('unable to get syslog data');
237 return explode("\n", $data['body']);
241 * get the Dialed Calls from router
245 public function getDialedCalls() {
246 $path = 'data/ExportDialedCalls.json';
247 $fields = array('exporttype' => '3');
248 $cookie = 'challengev='.$this->challenge
.'; '.$this->session
;
249 $data = $this->sentRequest($path, $fields, $cookie);
251 if (empty($data['body'])) {
252 throw new Exception('unable to get syslog data');
255 return explode("\n", $data['body']);
259 * sends the request to router
261 * @param string $path
262 * @param array $fields
263 * @param string $cookie
266 private function sentRequest ($path, $fields = array(), $cookie = '') {
267 $url = $this->url
.$path;
269 curl_setopt($ch, CURLOPT_URL
, $url);
271 if (!empty($fields)) {
272 curl_setopt($ch, CURLOPT_POST
, count($fields));
273 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
276 if (!empty($cookie)) {
277 curl_setopt($ch, CURLOPT_COOKIE
, $cookie);
280 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
281 curl_setopt($ch, CURLOPT_HEADER
, true);
288 $result = curl_exec($ch);
290 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
291 $header = substr($result, 0, $header_size);
292 $body = substr($result, $header_size);
296 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
297 $body = preg_replace('/\'/i', '"', $body);
298 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
299 $body = preg_replace("/},\s+]/", "}\n]", $body);
301 return array('header' => $this->parse_headers($header), 'body' => $body);
305 * parse the curl return header into an array
307 * @param string $response
310 private function parse_headers($response) {
312 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
314 foreach (explode("\r\n", $header_text) as $i => $line) {
316 $headers['http_code'] = $line;
319 list ($key, $value) = explode(': ', $line);
320 $headers[$key] = $value;