update some stuff
[GitHub/Stricted/speedport-hybrid-php-api.git] / Speedportw724v.class.php
1 <?php
2 require_once('ISpeedport.class.php');
3 require_once('lib/exception/RouterException.class.php');
4 require_once('SpeedportHybrid.class.php');
5
6 /**
7 * @author Jan Altensen (Stricted)
8 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
9 * @copyright 2015 Jan Altensen (Stricted)
10 */
11 class Speedportw724v extends SpeedportHybrid implements ISpeedport {
12 /**
13 * login into the router with the given password
14 *
15 * @param string $password
16 * @return boolean
17 */
18 public function login ($password) {
19 /* this is experimental, i dont have a speedport w724v so i cant test this
20 * feel free to test it and report if it dosent work
21 */
22 $path = 'data/Login.json';
23 $this->hash = md5($password);
24 $fields = array('password' => $this->hash, 'password_shadowed' => $this->hash, 'showpw' => 0);
25 $data = $this->sentRequest($path, $fields);
26 $json = $this->getValues($data['body']);
27
28 if (isset($json['login']) && $json['login'] == 'success') {
29 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
30 $this->cookie = $data['header']['Set-Cookie'];
31 }
32 else {
33 throw new RouterException('unable to get the session cookie from the router');
34 }
35
36 return true;
37 }
38
39 return false;
40 }
41
42 /**
43 * get the csrf_token
44 *
45 * @return string
46 */
47 protected function getToken () {
48 // TODO: check if this is needed
49 }
50
51 /**
52 * sends the request to router
53 *
54 * @param string $path
55 * @param mixed $fields
56 * @param string $cookie
57 * @param integer $count
58 * @return array
59 */
60 protected function sentRequest ($path, $fields, $cookie = false, $count = 0) {
61 $data = parent::sentRequest($path, $fields, $cookie, $count);
62 $header = $data['header'];
63 $body = $data['body'];
64
65 // fix invalid json
66 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
67 $body = preg_replace('/\'/i', '"', $body);
68 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
69 $body = preg_replace("/},\s+]/", "}\n]", $body);
70
71 // decode json
72 if (strpos($path, '.json') !== false) {
73 $json = json_decode($body, true);
74
75 if (is_array($json)) {
76 $body = $json;
77 }
78 }
79
80 return array('header' => $header, 'body' => $body);
81 }
82 }