add method getSyslog
[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 * sends the request to router
206 *
207 * @param string $path
208 * @param array $fields
209 * @param string $cookie
210 * @return array
211 */
212 private function sentRequest ($path, $fields = array(), $cookie = '') {
213 $url = $this->url.$path;
214 $ch = curl_init();
215 curl_setopt($ch, CURLOPT_URL, $url);
216
217 if (!empty($fields)) {
218 curl_setopt($ch, CURLOPT_POST, count($fields));
219 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
220 }
221
222 if (!empty($cookie)) {
223 curl_setopt($ch, CURLOPT_COOKIE, $cookie);
224 }
225
226 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
227 curl_setopt($ch, CURLOPT_HEADER, true);
228
229
230 if ($cookie) {
231
232 }
233
234 $result = curl_exec($ch);
235
236 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
237 $header = substr($result, 0, $header_size);
238 $body = substr($result, $header_size);
239 curl_close($ch);
240
241 // fix invalid json
242 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
243 $body = preg_replace('/\'/i', '"', $body);
244 $body = preg_replace("/},\n\n]/", "}\n]", $body);
245 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
246 $body = preg_replace("/},\n\s+]/", "} ]", $body);
247
248 return array('header' => $this->parse_headers($header), 'body' => $body);
249 }
250
251 /**
252 * parse the curl return header into an array
253 *
254 * @param string $response
255 * @return array
256 */
257 private function parse_headers($response) {
258 $headers = array();
259 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
260
261 foreach (explode("\r\n", $header_text) as $i => $line) {
262 if ($i === 0) {
263 $headers['http_code'] = $line;
264 }
265 else {
266 list ($key, $value) = explode(': ', $line);
267 $headers[$key] = $value;
268 }
269 }
270
271 return $headers;
272 }
273 }