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