add checkFirmware
[GitHub/Stricted/speedport-hybrid-php-api.git] / speedport.class.php
1 <?php
2 /**
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)
6 */
7 class speedport {
8 /**
9 * password-challenge
10 * @var string
11 */
12 private $challenge = '';
13
14 /**
15 * hashed password
16 * @var string
17 */
18 private $hash = '';
19
20 /**
21 * session cookie
22 * @var string
23 */
24 private $session = '';
25
26 /**
27 * router url
28 * @var string
29 */
30 private $url = '';
31
32 public function __construct ($password, $url = 'http://speedport.ip/') {
33 $this->url = $url;
34 $this->getChallenge();
35
36 if (empty($this->challenge)) {
37 throw new Exception('unable to get the challenge from the router');
38 }
39
40 $login = $this->login($password);
41
42 if ($login === false) {
43 throw new Exception('unable to login');
44 }
45 }
46
47 /**
48 * Requests the password-challenge from the router.
49 */
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'];
57 }
58 }
59
60 /**
61 * login into the router with the given password
62 *
63 * @param string $password
64 * @return boolean
65 */
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];
77 }
78 else {
79 throw new Exception('unable to get the session cookie from the router');
80 }
81
82 return true;
83 }
84 }
85
86 return false;
87 }
88
89 /**
90 * logout
91 *
92 * @return array
93 */
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 = '';
100 $this->session = '';
101
102 $json = json_decode($data['body'], true);
103
104 return $json;
105 }
106
107 /**
108 * reboot the router
109 *
110 * @return array
111 */
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);
118
119 return $json;
120 }
121
122 /**
123 * change dsl connection status
124 *
125 * @param string $status
126 */
127 public function changeConnectionStatus ($status) {
128 $path = 'data/Connect.json';
129
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);
134 }
135 else {
136 throw new Exception();
137 }
138 }
139
140 /**
141 * return the given json as array
142 *
143 * the following paths are known to be valid:
144 * /data/dsl.json
145 * /data/interfaces.json
146 * /data/arp.json
147 * /data/session.json
148 * /data/dhcp_client.json
149 * /data/dhcp_server.json
150 * /data/ipv6.json
151 * /data/dns.json
152 * /data/routing.json
153 * /data/igmp_proxy.json
154 * /data/igmp_snooping.json
155 * /data/wlan.json
156 * /data/module.json
157 * /data/memory.json
158 * /data/speed.json
159 * /data/webdav.json
160 * /data/bonding_client.json
161 * /data/bonding_tunnel.json
162 * /data/filterlist.json
163 * /data/bonding_tr181.json
164 * /data/letinfo.json
165 *
166 * /data/Status.json (No login needed)
167 *
168 * @param string $file
169 * @return array
170 */
171 public function getData ($file) {
172 $path = 'data/'.$file.'.json';
173 $fields = array();
174 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
175 $data = $this->sentRequest($path, $fields, $cookie);
176
177 if (empty($data['body'])) {
178 throw new Exception('unable to get '.$file.' data');
179 }
180
181 $json = json_decode($data['body'], true);
182
183 return $json;
184 }
185
186 /**
187 * get the router syslog
188 *
189 * @return array
190 */
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);
196
197 if (empty($data['body'])) {
198 throw new Exception('unable to get syslog data');
199 }
200
201 return explode("\n", $data['body']);
202 }
203
204 /**
205 * get the Missed Calls from router
206 *
207 * @return array
208 */
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);
214
215 if (empty($data['body'])) {
216 throw new Exception('unable to get syslog data');
217 }
218
219 return explode("\n", $data['body']);
220 }
221
222 /**
223 * get the Taken Calls from router
224 *
225 * @return array
226 */
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);
232
233 if (empty($data['body'])) {
234 throw new Exception('unable to get syslog data');
235 }
236
237 return explode("\n", $data['body']);
238 }
239
240 /**
241 * get the Dialed Calls from router
242 *
243 * @return array
244 */
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);
250 print_r($data);
251 if (empty($data['body'])) {
252 throw new Exception('unable to get syslog data');
253 }
254
255 return explode("\n", $data['body']);
256 }
257
258 /**
259 * check if firmware is actual
260 *
261 * @return array
262 */
263 public function checkFirmware () {
264 $path = 'data/checkfirmware.json';
265 $fields = array('checkfirmware' => 'true');
266 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
267 $data = $this->sentRequest($path, $fields, $cookie);
268
269 if (empty($data['body'])) {
270 throw new Exception('unable to get checkfirmware data');
271 }
272
273 $json = json_decode($data['body'], true);
274
275 return $json;
276 }
277
278 /**
279 * sends the request to router
280 *
281 * @param string $path
282 * @param array $fields
283 * @param string $cookie
284 * @return array
285 */
286 private function sentRequest ($path, $fields = array(), $cookie = '') {
287 $url = $this->url.$path;
288 $ch = curl_init();
289 curl_setopt($ch, CURLOPT_URL, $url);
290
291 if (!empty($fields)) {
292 curl_setopt($ch, CURLOPT_POST, count($fields));
293 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
294 }
295
296 if (!empty($cookie)) {
297 curl_setopt($ch, CURLOPT_COOKIE, $cookie);
298 }
299
300 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
301 curl_setopt($ch, CURLOPT_HEADER, true);
302
303
304 if ($cookie) {
305
306 }
307
308 $result = curl_exec($ch);
309
310 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
311 $header = substr($result, 0, $header_size);
312 $body = substr($result, $header_size);
313 curl_close($ch);
314
315 // fix invalid json
316 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
317 $body = preg_replace('/\'/i', '"', $body);
318 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
319 $body = preg_replace("/},\s+]/", "}\n]", $body);
320
321 return array('header' => $this->parse_headers($header), 'body' => $body);
322 }
323
324 /**
325 * parse the curl return header into an array
326 *
327 * @param string $response
328 * @return array
329 */
330 private function parse_headers($response) {
331 $headers = array();
332 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
333
334 foreach (explode("\r\n", $header_text) as $i => $line) {
335 if ($i === 0) {
336 $headers['http_code'] = $line;
337 }
338 else {
339 list ($key, $value) = explode(': ', $line);
340 $headers[$key] = $value;
341 }
342 }
343
344 return $headers;
345 }
346 }