add LICENSE and CopyRight informations
[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 = '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
121 /**
122 * return the given json as array
123 *
124 * the following paths are known to be valid:
125 * /data/dsl.json
126 * /data/interfaces.json
127 * /data/arp.json
128 * /data/session.json
129 * /data/dhcp_client.json
130 * /data/dhcp_server.json
131 * /data/ipv6.json
132 * /data/dns.json
133 * /data/routing.json
134 * /data/igmp_proxy.json
135 * /data/igmp_snooping.json
136 * /data/wlan.json
137 * /data/module.json
138 * /data/memory.json
139 * /data/speed.json
140 * /data/webdav.json
141 * /data/bonding_client.json
142 * /data/bonding_tunnel.json
143 * /data/filterlist.json
144 * /data/bonding_tr181.json
145 * /data/letinfo.json
146 *
147 * /data/Status.json (No login needed)
148 *
149 * @param string $file
150 * @return array
151 */
152 public function getData ($file) {
153 $url = 'data/'.$file.'.json';
154 $fields = array();
155 $cookie = 'challengev='.$this->challenge.'; '.$this->session;
156 $data = $this->sentRequest($url, $fields, $cookie);
157
158 if (empty($data['body'])) {
159 throw new Exception('unable to get '.$file.' data');
160 }
161
162 $json = json_decode($data['body'], true);
163
164 return $json;
165 }
166
167 /**
168 * sends the request to router
169 *
170 * @param string $url
171 * @param array $fields
172 * @param string $cookie
173 * @return array
174 */
175 private function sentRequest ($url, $fields = array(), $cookie = '') {
176 $url = $this->url.$url;
177 $ch = curl_init();
178 curl_setopt($ch, CURLOPT_URL, $url);
179
180 if (!empty($fields)) {
181 curl_setopt($ch, CURLOPT_POST, count($fields));
182 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
183 }
184
185 if (!empty($cookie)) {
186 curl_setopt($ch, CURLOPT_COOKIE, $cookie);
187 }
188
189 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
190 curl_setopt($ch, CURLOPT_HEADER, true);
191
192
193 if ($cookie) {
194
195 }
196
197 $result = curl_exec($ch);
198
199 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
200 $header = substr($result, 0, $header_size);
201 $body = substr($result, $header_size);
202 curl_close($ch);
203
204 // fix invalid json
205 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
206 $body = preg_replace('/\'/i', '"', $body);
207 $body = preg_replace("/},\n\n]/", "}\n]", $body);
208 $body = preg_replace('/\s+/', ' ', $body);
209 $body = preg_replace("/\[ \]/i", '[ {} ]', $body);
210 $body = preg_replace("/}, ]/", "} ]", $body);
211 $body = preg_replace("/\n/", " ", $body);
212
213 return array('header' => $this->parse_headers($header), 'body' => $body);
214 }
215
216 /**
217 * parse the curl return header into an array
218 *
219 * @param string $response
220 * @return array
221 */
222 private function parse_headers($response) {
223 $headers = array();
224 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
225
226 foreach (explode("\r\n", $header_text) as $i => $line) {
227 if ($i === 0) {
228 $headers['http_code'] = $line;
229 }
230 else {
231 list ($key, $value) = explode(': ', $line);
232 $headers[$key] = $value;
233 }
234 }
235
236 return $headers;
237 }
238 }