| 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 | |
| 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 | public function resetToFactoryDefault () { |
| 260 | $path = 'data/resetAllSetting.json'; |
| 261 | $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reset_all' => 'true'); |
| 262 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; |
| 263 | $data = $this->sentRequest($path, $fields, $cookie); |
| 264 | $json = json_decode($data['body'], true); |
| 265 | |
| 266 | return $json; |
| 267 | } |
| 268 | */ |
| 269 | |
| 270 | /** |
| 271 | * check if firmware is actual |
| 272 | * |
| 273 | * @return array |
| 274 | */ |
| 275 | public function checkFirmware () { |
| 276 | $path = 'data/checkfirmware.json'; |
| 277 | $fields = array('checkfirmware' => 'true'); |
| 278 | $cookie = 'challengev='.$this->challenge.'; '.$this->session; |
| 279 | $data = $this->sentRequest($path, $fields, $cookie); |
| 280 | |
| 281 | if (empty($data['body'])) { |
| 282 | throw new Exception('unable to get checkfirmware data'); |
| 283 | } |
| 284 | |
| 285 | $json = json_decode($data['body'], true); |
| 286 | |
| 287 | return $json; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * sends the request to router |
| 292 | * |
| 293 | * @param string $path |
| 294 | * @param array $fields |
| 295 | * @param string $cookie |
| 296 | * @return array |
| 297 | */ |
| 298 | private function sentRequest ($path, $fields = array(), $cookie = '') { |
| 299 | $url = $this->url.$path; |
| 300 | $ch = curl_init(); |
| 301 | curl_setopt($ch, CURLOPT_URL, $url); |
| 302 | |
| 303 | if (!empty($fields)) { |
| 304 | curl_setopt($ch, CURLOPT_POST, count($fields)); |
| 305 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); |
| 306 | } |
| 307 | |
| 308 | if (!empty($cookie)) { |
| 309 | curl_setopt($ch, CURLOPT_COOKIE, $cookie); |
| 310 | } |
| 311 | |
| 312 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 313 | curl_setopt($ch, CURLOPT_HEADER, true); |
| 314 | |
| 315 | |
| 316 | if ($cookie) { |
| 317 | |
| 318 | } |
| 319 | |
| 320 | $result = curl_exec($ch); |
| 321 | |
| 322 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
| 323 | $header = substr($result, 0, $header_size); |
| 324 | $body = substr($result, $header_size); |
| 325 | curl_close($ch); |
| 326 | |
| 327 | // fix invalid json |
| 328 | $body = preg_replace("/(\r\n)|(\r)/", "\n", $body); |
| 329 | $body = preg_replace('/\'/i', '"', $body); |
| 330 | $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body); |
| 331 | $body = preg_replace("/},\s+]/", "}\n]", $body); |
| 332 | |
| 333 | return array('header' => $this->parse_headers($header), 'body' => $body); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * parse the curl return header into an array |
| 338 | * |
| 339 | * @param string $response |
| 340 | * @return array |
| 341 | */ |
| 342 | private function parse_headers($response) { |
| 343 | $headers = array(); |
| 344 | $header_text = substr($response, 0, strpos($response, "\r\n\r\n")); |
| 345 | |
| 346 | foreach (explode("\r\n", $header_text) as $i => $line) { |
| 347 | if ($i === 0) { |
| 348 | $headers['http_code'] = $line; |
| 349 | } |
| 350 | else { |
| 351 | list ($key, $value) = explode(': ', $line); |
| 352 | $headers[$key] = $value; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return $headers; |
| 357 | } |
| 358 | } |