Commit | Line | Data |
---|---|---|
a91317a6 | 1 | <?php |
1d934afe S |
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 | */ | |
a91317a6 S |
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 = 'http://speedport.ip/'; | |
31 | ||
32 | public function __construct ($password) { | |
33 | $this->getChallenge(); | |
34 | ||
35 | if (empty($this->challenge)) { | |
36 | throw new Exception('unable to get the challenge from the router'); | |
37 | } | |
38 | ||
39 | $login = $this->login($password); | |
40 | ||
41 | if ($login === false) { | |
42 | throw new Exception('unable to login'); | |
43 | } | |
44 | } | |
45 | ||
46 | /** | |
47 | * Requests the password-challenge from the router. | |
48 | */ | |
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']; | |
56 | } | |
57 | } | |
58 | ||
59 | /** | |
60 | * login into the router with the given password | |
61 | * | |
62 | * @param string $password | |
63 | * @return boolean | |
64 | */ | |
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]; | |
76 | } | |
77 | else { | |
78 | throw new Exception('unable to get the session cookie from the router'); | |
79 | } | |
80 | ||
81 | return true; | |
82 | } | |
83 | } | |
84 | ||
85 | return false; | |
86 | } | |
87 | ||
88 | /** | |
89 | * logout | |
90 | * | |
91 | * @return array | |
92 | */ | |
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 = ''; | |
99 | $this->session = ''; | |
100 | ||
101 | $json = json_decode($data['body'], true); | |
102 | ||
103 | return $json; | |
104 | } | |
105 | ||
106 | /** | |
107 | * reboot the router | |
108 | * | |
109 | * @return array | |
110 | */ | |
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); | |
117 | ||
118 | return $json; | |
119 | } | |
120 | ||
87026df3 S |
121 | /** |
122 | * change dsl connection status | |
123 | * | |
124 | * @param string $status | |
125 | */ | |
126 | public function changeConnectionStatus ($status) { | |
127 | $url = 'data/Connect.json'; | |
128 | ||
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); | |
133 | } | |
134 | else { | |
135 | throw new Exception(); | |
136 | } | |
137 | } | |
138 | ||
a91317a6 S |
139 | /** |
140 | * return the given json as array | |
141 | * | |
142 | * the following paths are known to be valid: | |
143 | * /data/dsl.json | |
144 | * /data/interfaces.json | |
145 | * /data/arp.json | |
146 | * /data/session.json | |
147 | * /data/dhcp_client.json | |
148 | * /data/dhcp_server.json | |
149 | * /data/ipv6.json | |
150 | * /data/dns.json | |
151 | * /data/routing.json | |
152 | * /data/igmp_proxy.json | |
153 | * /data/igmp_snooping.json | |
154 | * /data/wlan.json | |
155 | * /data/module.json | |
156 | * /data/memory.json | |
157 | * /data/speed.json | |
158 | * /data/webdav.json | |
159 | * /data/bonding_client.json | |
160 | * /data/bonding_tunnel.json | |
161 | * /data/filterlist.json | |
162 | * /data/bonding_tr181.json | |
163 | * /data/letinfo.json | |
164 | * | |
165 | * /data/Status.json (No login needed) | |
166 | * | |
167 | * @param string $file | |
168 | * @return array | |
169 | */ | |
170 | public function getData ($file) { | |
171 | $url = 'data/'.$file.'.json'; | |
172 | $fields = array(); | |
173 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; | |
174 | $data = $this->sentRequest($url, $fields, $cookie); | |
175 | ||
176 | if (empty($data['body'])) { | |
177 | throw new Exception('unable to get '.$file.' data'); | |
178 | } | |
179 | ||
180 | $json = json_decode($data['body'], true); | |
181 | ||
182 | return $json; | |
183 | } | |
184 | ||
185 | /** | |
186 | * sends the request to router | |
187 | * | |
188 | * @param string $url | |
189 | * @param array $fields | |
190 | * @param string $cookie | |
191 | * @return array | |
192 | */ | |
193 | private function sentRequest ($url, $fields = array(), $cookie = '') { | |
194 | $url = $this->url.$url; | |
195 | $ch = curl_init(); | |
196 | curl_setopt($ch, CURLOPT_URL, $url); | |
197 | ||
198 | if (!empty($fields)) { | |
199 | curl_setopt($ch, CURLOPT_POST, count($fields)); | |
200 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); | |
201 | } | |
202 | ||
203 | if (!empty($cookie)) { | |
204 | curl_setopt($ch, CURLOPT_COOKIE, $cookie); | |
205 | } | |
206 | ||
207 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
208 | curl_setopt($ch, CURLOPT_HEADER, true); | |
209 | ||
210 | ||
211 | if ($cookie) { | |
212 | ||
213 | } | |
214 | ||
215 | $result = curl_exec($ch); | |
216 | ||
217 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
218 | $header = substr($result, 0, $header_size); | |
219 | $body = substr($result, $header_size); | |
220 | curl_close($ch); | |
221 | ||
222 | // fix invalid json | |
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); | |
230 | ||
231 | return array('header' => $this->parse_headers($header), 'body' => $body); | |
232 | } | |
233 | ||
234 | /** | |
235 | * parse the curl return header into an array | |
236 | * | |
237 | * @param string $response | |
238 | * @return array | |
239 | */ | |
240 | private function parse_headers($response) { | |
241 | $headers = array(); | |
242 | $header_text = substr($response, 0, strpos($response, "\r\n\r\n")); | |
243 | ||
244 | foreach (explode("\r\n", $header_text) as $i => $line) { | |
245 | if ($i === 0) { | |
246 | $headers['http_code'] = $line; | |
247 | } | |
248 | else { | |
249 | list ($key, $value) = explode(': ', $line); | |
250 | $headers[$key] = $value; | |
251 | } | |
252 | } | |
253 | ||
254 | return $headers; | |
255 | } | |
256 | } |