do some changes to support other speedport router
[GitHub/Stricted/speedport-hybrid-php-api.git] / Speedportw724v.class.php
CommitLineData
7367e951 1<?php
35a39175
S
2require_once('ISpeedport.class.php');
3require_once('lib/exception/RouterException.class.php');
7367e951
S
4require_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 */
35a39175 11class Speedportw724v extends SpeedportHybrid implements ISpeedport {
ca3cb447
S
12 public function login ($password) {
13 /* this is experimental, i dont have a speedport w724v so i cant test this
14 * feel free to test it and report if it dosent work
15 */
16 $path = 'data/Login.json';
17 $this->hash = md5($password);
18 $fields = array('password' => $this->hash, 'password_shadowed' => $this->hash, 'showpw' => 0);
19 $data = $this->sentRequest($path, $fields);
20 $json = $this->getValues($data['body']);
21
22 if (isset($json['login']) && $json['login'] == 'success') {
35a39175
S
23 if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
24 $this->cookie = $data['header']['Set-Cookie'];
25 }
26 else {
27 throw new RouterException('unable to get the session cookie from the router');
28 }
ca3cb447
S
29
30 return true;
31 }
32
33 return false;
34 }
35a39175
S
35
36 /**
37 * sends the request to router
38 *
39 * @param string $path
40 * @param mixed $fields
41 * @param string $cookie
42 * @param integer $count
43 * @return array
44 */
45 protected function sentRequest ($path, $fields, $cookie = false, $count = 0) {
46 $data = parent::sentRequest($path, $fields, $cookie, $count);
47 $header = $data['header'];
48 $body = $data['body'];
49
50 // fix invalid json
51 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
52 $body = preg_replace('/\'/i', '"', $body);
53 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
54 $body = preg_replace("/},\s+]/", "}\n]", $body);
55
56 // decode json
57 if (strpos($path, '.json') !== false) {
58 $json = json_decode($body, true);
59
60 if (is_array($json)) {
61 $body = $json;
62 }
63 }
64
65 return array('header' => $header, 'body' => $body);
66 }
7367e951 67}