fix last commit
[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('lib/exception/NotImplementedException.class.php');
5 require_once('CryptLib/CryptLib.php');
6 require_once('lib/trait/Connection.class.php');
7 require_once('lib/trait/CryptLib.class.php');
8 require_once('lib/trait/Login.class.php');
9 require_once('lib/trait/Firewall.class.php');
10 require_once('lib/trait/Network.class.php');
11 require_once('lib/trait/Phone.class.php');
12 require_once('lib/trait/System.class.php');
13
14 /**
15 * @author Jan Altensen (Stricted)
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @copyright 2015-2016 Jan Altensen (Stricted)
18 */
19 class SpeedportHybrid {
20 use Connection;
21 use CryptLib;
22 use Firewall;
23 use Login;
24 use Network;
25 use Phone;
26 use System;
27
28 /**
29 * class version
30 * @const string
31 */
32 const VERSION = '1.0.5';
33
34 /**
35 * router url
36 * @var string
37 */
38 private $url = '';
39
40 /**
41 * inititalize this class
42 *
43 * @param string $url
44 */
45 public function __construct ($url = 'http://speedport.ip/') {
46 $this->url = $url;
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 }
72 }
73
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) {
83 if (!isset($item['vartype']) || !isset($item['varid']) || !isset($item['varvalue'])) continue;
84
85 // thank you telekom for this piece of shit
86 if ($item['vartype'] == 'template') {
87 if (is_array($item['varvalue'])) {
88 $data[$item['varid']][] = $this->getValues($item['varvalue']);
89 }
90 else {
91 // i dont know if we need this
92 $data[$item['varid']] = $item['varvalue'];
93 }
94 }
95 else {
96 if (is_array($item['varvalue'])) {
97 $data[$item['varid']] = $this->getValues($item['varvalue']);
98 }
99 else {
100 $data[$item['varid']] = $item['varvalue'];
101 }
102 }
103 }
104
105 return $data;
106 }
107
108 /**
109 * sends the request to router
110 *
111 * @param string $path
112 * @param mixed $fields
113 * @param string $cookie
114 * @param integer $count
115 * @return array
116 */
117 private function sendRequest ($path, $fields, $cookie = false, $count = 0) {
118 $url = $this->url.$path.'?lang=en';
119 $ch = curl_init();
120 curl_setopt($ch, CURLOPT_URL, $url);
121
122 if (!empty($fields)) {
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 }
131 }
132
133 if ($cookie === true) {
134 curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->cookie);
135 }
136
137 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
138 curl_setopt($ch, CURLOPT_HEADER, true);
139
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
147 // check if response is empty
148 if (empty($body)) {
149 throw new RouterException('empty response');
150 }
151
152 // check if body is encrypted (hex instead of json)
153 if (ctype_xdigit($body)) {
154 $body = $this->decrypt($body);
155 }
156
157 // fix invalid json
158 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
159 $body = preg_replace('/\'/i', '"', $body);
160 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
161 $body = preg_replace("/},\s+]/", "}\n]", $body);
162
163 // decode json
164 if (strpos($url, '.json') !== false) {
165 $json = json_decode($body, true);
166
167 if (is_array($json)) {
168 $body = $json;
169 }
170 }
171
172 return array('header' => $this->parse_headers($header), 'body' => $body);
173 }
174
175 /**
176 * parse the curl return header into an array
177 *
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
185 $header_text = explode("\r\n", $header_text);
186 foreach ($header_text as $i => $line) {
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 }
198 }