add test file for reboot
[GitHub/Stricted/speedport-hybrid-php-api.git] / speedport.class.php
... / ...
CommitLineData
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 */
7class 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 * sends the request to router
188 *
189 * @param string $path
190 * @param array $fields
191 * @param string $cookie
192 * @return array
193 */
194 private function sentRequest ($path, $fields = array(), $cookie = '') {
195 $url = $this->url.$path;
196 $ch = curl_init();
197 curl_setopt($ch, CURLOPT_URL, $url);
198
199 if (!empty($fields)) {
200 curl_setopt($ch, CURLOPT_POST, count($fields));
201 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
202 }
203
204 if (!empty($cookie)) {
205 curl_setopt($ch, CURLOPT_COOKIE, $cookie);
206 }
207
208 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
209 curl_setopt($ch, CURLOPT_HEADER, true);
210
211
212 if ($cookie) {
213
214 }
215
216 $result = curl_exec($ch);
217
218 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
219 $header = substr($result, 0, $header_size);
220 $body = substr($result, $header_size);
221 curl_close($ch);
222
223 // fix invalid json
224 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
225 $body = preg_replace('/\'/i', '"', $body);
226 $body = preg_replace("/},\n\n]/", "}\n]", $body);
227 $body = preg_replace('/\s+/', ' ', $body);
228 $body = preg_replace("/\[ \]/i", '[ {} ]', $body);
229 $body = preg_replace("/}, ]/", "} ]", $body);
230 $body = preg_replace("/\n/", " ", $body);
231
232 return array('header' => $this->parse_headers($header), 'body' => $body);
233 }
234
235 /**
236 * parse the curl return header into an array
237 *
238 * @param string $response
239 * @return array
240 */
241 private function parse_headers($response) {
242 $headers = array();
243 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
244
245 foreach (explode("\r\n", $header_text) as $i => $line) {
246 if ($i === 0) {
247 $headers['http_code'] = $line;
248 }
249 else {
250 list ($key, $value) = explode(': ', $line);
251 $headers[$key] = $value;
252 }
253 }
254
255 return $headers;
256 }
257}