reenable changeLTEStatus support
[GitHub/Stricted/speedport-hybrid-php-api.git] / SpeedportHybrid.class.php
CommitLineData
a91317a6 1<?php
6e1250c3
S
2require_once('lib/exception/RebootException.class.php');
3require_once('lib/exception/RouterException.class.php');
1a5b0fe3 4require_once('lib/exception/NotImplementedException.class.php');
aacbbd28 5require_once('CryptLib/CryptLib.php');
6e1250c3
S
6require_once('lib/trait/Connection.class.php');
7require_once('lib/trait/CryptLib.class.php');
8require_once('lib/trait/Login.class.php');
324305fd 9require_once('lib/trait/Firewall.class.php');
e559c2bb 10require_once('lib/trait/Network.class.php');
6e1250c3
S
11require_once('lib/trait/Phone.class.php');
12require_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>
17 * @copyright 2015 Jan Altensen (Stricted)
18 */
21f877e4 19class 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) {
49abc701 118 $url = $this->url.$path.'?lang=en';
a91317a6
S
119 $ch = curl_init();
120 curl_setopt($ch, CURLOPT_URL, $url);
121
122 if (!empty($fields)) {
809e25bd
S
123 if (is_array($fields)) {
124 curl_setopt($ch, CURLOPT_POST, count($fields));
125 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
126 }
127 else {
128 curl_setopt($ch, CURLOPT_POST, $count);
129 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
130 }
a91317a6
S
131 }
132
0dca2d61 133 if ($cookie === true) {
559c5be7 134 curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->cookie);
a91317a6
S
135 }
136
137 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
138 curl_setopt($ch, CURLOPT_HEADER, true);
139
a91317a6
S
140 $result = curl_exec($ch);
141
142 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
143 $header = substr($result, 0, $header_size);
144 $body = substr($result, $header_size);
145 curl_close($ch);
146
219ba661
S
147 // check if response is empty
148 if (empty($body)) {
149 throw new RouterException('empty response');
150 }
151
8162139f
S
152 // check if body is encrypted (hex instead of json)
153 if (ctype_xdigit($body)) {
154 $body = $this->decrypt($body);
155 }
156
a91317a6
S
157 // fix invalid json
158 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
159 $body = preg_replace('/\'/i', '"', $body);
5e44cffa 160 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
dae16c50 161 $body = preg_replace("/},\s+]/", "}\n]", $body);
a91317a6 162
219ba661 163 // decode json
aacbbd28 164 if (strpos($url, '.json') !== false) {
87492136
S
165 $json = json_decode($body, true);
166
167 if (is_array($json)) {
168 $body = $json;
169 }
219ba661
S
170 }
171
a91317a6
S
172 return array('header' => $this->parse_headers($header), 'body' => $body);
173 }
174
175 /**
176 * parse the curl return header into an array
5e44cffa 177 *
a91317a6
S
178 * @param string $response
179 * @return array
180 */
181 private function parse_headers($response) {
182 $headers = array();
183 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
184
7f4a51d2
S
185 $header_text = explode("\r\n", $header_text);
186 foreach ($header_text as $i => $line) {
a91317a6
S
187 if ($i === 0) {
188 $headers['http_code'] = $line;
189 }
190 else {
191 list ($key, $value) = explode(': ', $line);
192 $headers[$key] = $value;
193 }
194 }
195
196 return $headers;
197 }
7f4a51d2 198}