add lanDeviceOverview
[GitHub/Stricted/speedport-hybrid-php-api.git] / SpeedportHybrid.class.php
1 <?php
2 require_once('lib/exception/RebootException.class.php');
3 require_once('lib/exception/RouterException.class.php');
4 require_once('CryptLib/CryptLib.php');
5 require_once('lib/trait/Connection.class.php');
6 require_once('lib/trait/CryptLib.class.php');
7 require_once('lib/trait/Login.class.php');
8 require_once('lib/trait/Network.class.php');
9 require_once('lib/trait/Phone.class.php');
10 require_once('lib/trait/System.class.php');
11
12 /**
13 * @author Jan Altensen (Stricted)
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @copyright 2015 Jan Altensen (Stricted)
16 */
17 class SpeedportHybrid {
18 use Connection;
19 use CryptLib;
20 use Login;
21 use Network;
22 use Phone;
23 use System;
24
25 /**
26 * class version
27 * @const string
28 */
29 const VERSION = '1.0.4';
30
31 /**
32 * router url
33 * @var string
34 */
35 private $url = '';
36
37 /**
38 * inititalize this class
39 *
40 * @param string $url
41 */
42 public function __construct ($url = 'http://speedport.ip/') {
43 $this->url = $url;
44 }
45
46 /**
47 * get the values from array
48 *
49 * @param array $array
50 * @return array
51 */
52 private function getValues($array) {
53 $data = array();
54 foreach ($array as $item) {
55 // thank you telekom for this piece of shit
56 if ($item['vartype'] == 'template') {
57 if (is_array($item['varvalue'])) {
58 $data[$item['varid']][] = $this->getValues($item['varvalue']);
59 }
60 else {
61 // i dont know if we need this
62 $data[$item['varid']] = $item['varvalue'];
63 }
64 }
65 else {
66 if (is_array($item['varvalue'])) {
67 $data[$item['varid']] = $this->getValues($item['varvalue']);
68 }
69 else {
70 $data[$item['varid']] = $item['varvalue'];
71 }
72 }
73 }
74
75 return $data;
76 }
77
78 /**
79 * sends the request to router
80 *
81 * @param string $path
82 * @param mixed $fields
83 * @param string $cookie
84 * @param integer $count
85 * @return array
86 */
87 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
88 $url = $this->url.$path.'?lang=en';
89 $ch = curl_init();
90 curl_setopt($ch, CURLOPT_URL, $url);
91
92 if (!empty($fields)) {
93 if (is_array($fields)) {
94 curl_setopt($ch, CURLOPT_POST, count($fields));
95 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
96 }
97 else {
98 curl_setopt($ch, CURLOPT_POST, $count);
99 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
100 }
101 }
102
103 if ($cookie === true) {
104 curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->cookie);
105 }
106
107 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
108 curl_setopt($ch, CURLOPT_HEADER, true);
109
110 $result = curl_exec($ch);
111
112 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
113 $header = substr($result, 0, $header_size);
114 $body = substr($result, $header_size);
115 curl_close($ch);
116
117 // check if response is empty
118 if (empty($body)) {
119 throw new RouterException('empty response');
120 }
121
122 // check if body is encrypted (hex instead of json)
123 if (ctype_xdigit($body)) {
124 $body = $this->decrypt($body);
125 }
126
127 // fix invalid json
128 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
129 $body = preg_replace('/\'/i', '"', $body);
130 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
131 $body = preg_replace("/},\s+]/", "}\n]", $body);
132
133 // decode json
134 if (strpos($url, '.json') !== false) {
135 $body = json_decode($body, true);
136 }
137
138 return array('header' => $this->parse_headers($header), 'body' => $body);
139 }
140
141 /**
142 * parse the curl return header into an array
143 *
144 * @param string $response
145 * @return array
146 */
147 private function parse_headers($response) {
148 $headers = array();
149 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
150
151 $header_text = explode("\r\n", $header_text);
152 foreach ($header_text as $i => $line) {
153 if ($i === 0) {
154 $headers['http_code'] = $line;
155 }
156 else {
157 list ($key, $value) = explode(': ', $line);
158 $headers[$key] = $value;
159 }
160 }
161
162 return $headers;
163 }
164 }