Commit | Line | Data |
---|---|---|
a91317a6 | 1 | <?php |
6e1250c3 S |
2 | require_once('lib/exception/RebootException.class.php'); |
3 | require_once('lib/exception/RouterException.class.php'); | |
1a5b0fe3 | 4 | require_once('lib/exception/NotImplementedException.class.php'); |
aacbbd28 | 5 | require_once('CryptLib/CryptLib.php'); |
6e1250c3 S |
6 | require_once('lib/trait/Connection.class.php'); |
7 | require_once('lib/trait/CryptLib.class.php'); | |
8 | require_once('lib/trait/Login.class.php'); | |
324305fd | 9 | require_once('lib/trait/Firewall.class.php'); |
e559c2bb | 10 | require_once('lib/trait/Network.class.php'); |
6e1250c3 S |
11 | require_once('lib/trait/Phone.class.php'); |
12 | require_once('lib/trait/System.class.php'); | |
8162139f | 13 | |
1d934afe S |
14 | /** |
15 | * @author Jan Altensen (Stricted) | |
16 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
dd76f288 | 17 | * @copyright 2015-2016 Jan Altensen (Stricted) |
1d934afe | 18 | */ |
21f877e4 | 19 | class SpeedportHybrid { |
9b926efe | 20 | use Connection; |
e9631169 | 21 | use CryptLib; |
324305fd | 22 | use Firewall; |
e9631169 | 23 | use Login; |
e559c2bb | 24 | use Network; |
9b926efe S |
25 | use Phone; |
26 | use System; | |
27 | ||
0dca2d61 | 28 | /** |
23cc0061 S |
29 | * class version |
30 | * @const string | |
0dca2d61 | 31 | */ |
1a5b0fe3 | 32 | const VERSION = '1.0.5'; |
a91317a6 S |
33 | |
34 | /** | |
35 | * router url | |
36 | * @var string | |
37 | */ | |
b5a532a8 | 38 | private $url = ''; |
a91317a6 | 39 | |
c9e082da | 40 | /** |
e9631169 S |
41 | * inititalize this class |
42 | * | |
43 | * @param string $url | |
c9e082da | 44 | */ |
e58a96d1 | 45 | public function __construct ($url = 'http://speedport.ip/') { |
b5a532a8 | 46 | $this->url = $url; |
0d814efc S |
47 | $this->checkRequirements(); |
48 | } | |
49 | ||
50 | /** | |
51 | * check php requirements | |
52 | */ | |
53 | private function checkRequirements () { | |
54 | if (!extension_loaded('curl')) { | |
55 | throw new Exception("The PHP Extension 'curl' is missing."); | |
56 | } | |
57 | else if (!extension_loaded('json')) { | |
58 | throw new Exception("The PHP Extension 'json' is missing."); | |
59 | } | |
60 | else if (!extension_loaded('pcre')) { | |
61 | throw new Exception("The PHP Extension 'pcre' is missing."); | |
62 | } | |
63 | else if (!extension_loaded('ctype')) { | |
64 | throw new Exception("The PHP Extension 'ctype' is missing."); | |
65 | } | |
66 | else if (!extension_loaded('hash')) { | |
67 | throw new Exception("The PHP Extension 'hash' is missing."); | |
68 | } | |
69 | else if (!in_array('sha256', hash_algos())) { | |
70 | throw new Exception('SHA-256 algorithm is not Supported.'); | |
71 | } | |
a91317a6 S |
72 | } |
73 | ||
c2678616 S |
74 | /** |
75 | * get the values from array | |
76 | * | |
77 | * @param array $array | |
78 | * @return array | |
79 | */ | |
80 | private function getValues($array) { | |
81 | $data = array(); | |
82 | foreach ($array as $item) { | |
324305fd S |
83 | if (!isset($item['vartype']) || !isset($item['varid']) || !isset($item['varvalue'])) continue; |
84 | ||
aacbbd28 | 85 | // thank you telekom for this piece of shit |
8b7e3972 | 86 | if ($item['vartype'] == 'template') { |
ec351a7d S |
87 | if (is_array($item['varvalue'])) { |
88 | $data[$item['varid']][] = $this->getValues($item['varvalue']); | |
89 | } | |
90 | else { | |
23cc0061 | 91 | // i dont know if we need this |
ec351a7d S |
92 | $data[$item['varid']] = $item['varvalue']; |
93 | } | |
58feafa2 S |
94 | } |
95 | else { | |
8b7e3972 S |
96 | if (is_array($item['varvalue'])) { |
97 | $data[$item['varid']] = $this->getValues($item['varvalue']); | |
98 | } | |
99 | else { | |
100 | $data[$item['varid']] = $item['varvalue']; | |
101 | } | |
58feafa2 | 102 | } |
c2678616 S |
103 | } |
104 | ||
105 | return $data; | |
106 | } | |
107 | ||
a91317a6 S |
108 | /** |
109 | * sends the request to router | |
5e44cffa | 110 | * |
b5a532a8 | 111 | * @param string $path |
809e25bd | 112 | * @param mixed $fields |
a91317a6 | 113 | * @param string $cookie |
809e25bd | 114 | * @param integer $count |
a91317a6 S |
115 | * @return array |
116 | */ | |
e94573dd | 117 | private function sendRequest ($path, $fields, $cookie = false, $count = 0) { |
061f7b31 | 118 | $url = $this->url.$path; |
a91317a6 S |
119 | $ch = curl_init(); |
120 | curl_setopt($ch, CURLOPT_URL, $url); | |
121 | ||
122 | if (!empty($fields)) { | |
061f7b31 | 123 | curl_setopt($ch, CURLOPT_POST, true); |
809e25bd | 124 | if (is_array($fields)) { |
809e25bd S |
125 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); |
126 | } | |
127 | else { | |
809e25bd S |
128 | curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); |
129 | } | |
a91317a6 S |
130 | } |
131 | ||
0dca2d61 | 132 | if ($cookie === true) { |
559c5be7 | 133 | curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->cookie); |
a91317a6 S |
134 | } |
135 | ||
136 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
137 | curl_setopt($ch, CURLOPT_HEADER, true); | |
138 | ||
a91317a6 S |
139 | $result = curl_exec($ch); |
140 | ||
141 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
142 | $header = substr($result, 0, $header_size); | |
143 | $body = substr($result, $header_size); | |
144 | curl_close($ch); | |
145 | ||
219ba661 S |
146 | // check if response is empty |
147 | if (empty($body)) { | |
148 | throw new RouterException('empty response'); | |
149 | } | |
150 | ||
8162139f S |
151 | // check if body is encrypted (hex instead of json) |
152 | if (ctype_xdigit($body)) { | |
153 | $body = $this->decrypt($body); | |
154 | } | |
155 | ||
a91317a6 S |
156 | // fix invalid json |
157 | $body = preg_replace("/(\r\n)|(\r)/", "\n", $body); | |
158 | $body = preg_replace('/\'/i', '"', $body); | |
5e44cffa | 159 | $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body); |
dae16c50 | 160 | $body = preg_replace("/},\s+]/", "}\n]", $body); |
a91317a6 | 161 | |
219ba661 | 162 | // decode json |
aacbbd28 | 163 | if (strpos($url, '.json') !== false) { |
87492136 S |
164 | $json = json_decode($body, true); |
165 | ||
166 | if (is_array($json)) { | |
167 | $body = $json; | |
168 | } | |
219ba661 S |
169 | } |
170 | ||
a91317a6 S |
171 | return array('header' => $this->parse_headers($header), 'body' => $body); |
172 | } | |
173 | ||
174 | /** | |
175 | * parse the curl return header into an array | |
5e44cffa | 176 | * |
a91317a6 S |
177 | * @param string $response |
178 | * @return array | |
179 | */ | |
180 | private function parse_headers($response) { | |
181 | $headers = array(); | |
182 | $header_text = substr($response, 0, strpos($response, "\r\n\r\n")); | |
183 | ||
7f4a51d2 S |
184 | $header_text = explode("\r\n", $header_text); |
185 | foreach ($header_text as $i => $line) { | |
a91317a6 S |
186 | if ($i === 0) { |
187 | $headers['http_code'] = $line; | |
188 | } | |
189 | else { | |
190 | list ($key, $value) = explode(': ', $line); | |
191 | $headers[$key] = $value; | |
192 | } | |
193 | } | |
194 | ||
195 | return $headers; | |
196 | } | |
7f4a51d2 | 197 | } |