update getDerviedk
[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');
aacbbd28 4require_once('CryptLib/CryptLib.php');
6e1250c3
S
5require_once('lib/trait/Connection.class.php');
6require_once('lib/trait/CryptLib.class.php');
7require_once('lib/trait/Login.class.php');
324305fd 8require_once('lib/trait/Firewall.class.php');
e559c2bb 9require_once('lib/trait/Network.class.php');
6e1250c3
S
10require_once('lib/trait/Phone.class.php');
11require_once('lib/trait/System.class.php');
8162139f 12
1d934afe
S
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 */
21f877e4 18class SpeedportHybrid {
9b926efe 19 use Connection;
e9631169 20 use CryptLib;
324305fd 21 use Firewall;
e9631169 22 use Login;
e559c2bb 23 use Network;
9b926efe
S
24 use Phone;
25 use System;
26
0dca2d61 27 /**
23cc0061
S
28 * class version
29 * @const string
0dca2d61 30 */
e9631169 31 const VERSION = '1.0.4';
a91317a6
S
32
33 /**
34 * router url
35 * @var string
36 */
b5a532a8 37 private $url = '';
a91317a6 38
c9e082da 39 /**
e9631169
S
40 * inititalize this class
41 *
42 * @param string $url
c9e082da 43 */
e58a96d1 44 public function __construct ($url = 'http://speedport.ip/') {
b5a532a8 45 $this->url = $url;
0d814efc
S
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 }
a91317a6
S
71 }
72
c2678616
S
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) {
324305fd
S
82 if (!isset($item['vartype']) || !isset($item['varid']) || !isset($item['varvalue'])) continue;
83
aacbbd28 84 // thank you telekom for this piece of shit
8b7e3972 85 if ($item['vartype'] == 'template') {
ec351a7d
S
86 if (is_array($item['varvalue'])) {
87 $data[$item['varid']][] = $this->getValues($item['varvalue']);
88 }
89 else {
23cc0061 90 // i dont know if we need this
ec351a7d
S
91 $data[$item['varid']] = $item['varvalue'];
92 }
58feafa2
S
93 }
94 else {
8b7e3972
S
95 if (is_array($item['varvalue'])) {
96 $data[$item['varid']] = $this->getValues($item['varvalue']);
97 }
98 else {
99 $data[$item['varid']] = $item['varvalue'];
100 }
58feafa2 101 }
c2678616
S
102 }
103
104 return $data;
105 }
106
a91317a6
S
107 /**
108 * sends the request to router
5e44cffa 109 *
b5a532a8 110 * @param string $path
809e25bd 111 * @param mixed $fields
a91317a6 112 * @param string $cookie
809e25bd 113 * @param integer $count
a91317a6
S
114 * @return array
115 */
0dca2d61 116 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
49abc701 117 $url = $this->url.$path.'?lang=en';
a91317a6
S
118 $ch = curl_init();
119 curl_setopt($ch, CURLOPT_URL, $url);
120
121 if (!empty($fields)) {
809e25bd
S
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 }
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}