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');
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)
19 class SpeedportHybrid
{
32 const VERSION
= '1.0.5';
41 * inititalize this class
45 public function __construct ($url = 'http://speedport.ip/') {
47 $this->checkRequirements();
51 * check php requirements
53 private function checkRequirements () {
54 if (!extension_loaded('curl')) {
55 throw new Exception("The PHP Extension 'curl' is missing.");
57 else if (!extension_loaded('json')) {
58 throw new Exception("The PHP Extension 'json' is missing.");
60 else if (!extension_loaded('pcre')) {
61 throw new Exception("The PHP Extension 'pcre' is missing.");
63 else if (!extension_loaded('ctype')) {
64 throw new Exception("The PHP Extension 'ctype' is missing.");
66 else if (!extension_loaded('hash')) {
67 throw new Exception("The PHP Extension 'hash' is missing.");
69 else if (!in_array('sha256', hash_algos())) {
70 throw new Exception('SHA-256 algorithm is not Supported.');
75 * get the values from array
80 private function getValues($array) {
82 foreach ($array as $item) {
83 if (!isset($item['vartype']) ||
!isset($item['varid']) ||
!isset($item['varvalue'])) continue;
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']);
91 // i dont know if we need this
92 $data[$item['varid']] = $item['varvalue'];
96 if (is_array($item['varvalue'])) {
97 $data[$item['varid']] = $this->getValues($item['varvalue']);
100 $data[$item['varid']] = $item['varvalue'];
109 * sends the request to router
111 * @param string $path
112 * @param mixed $fields
113 * @param string $cookie
114 * @param integer $count
117 private function sendRequest ($path, $fields, $cookie = false, $count = 0) {
118 $url = $this->url
.$path.'?lang=en';
120 curl_setopt($ch, CURLOPT_URL
, $url);
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));
128 curl_setopt($ch, CURLOPT_POST
, $count);
129 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
133 if ($cookie === true) {
134 curl_setopt($ch, CURLOPT_COOKIE
, 'challengev='.$this->challenge
.'; '.$this->cookie
);
137 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
138 curl_setopt($ch, CURLOPT_HEADER
, true);
140 $result = curl_exec($ch);
142 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
143 $header = substr($result, 0, $header_size);
144 $body = substr($result, $header_size);
147 // check if response is empty
149 throw new RouterException('empty response');
152 // check if body is encrypted (hex instead of json)
153 if (ctype_xdigit($body)) {
154 $body = $this->decrypt($body);
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);
164 if (strpos($url, '.json') !== false) {
165 $json = json_decode($body, true);
167 if (is_array($json)) {
172 return array('header' => $this->parse_headers($header), 'body' => $body);
176 * parse the curl return header into an array
178 * @param string $response
181 private function parse_headers($response) {
183 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
185 $header_text = explode("\r\n", $header_text);
186 foreach ($header_text as $i => $line) {
188 $headers['http_code'] = $line;
191 list ($key, $value) = explode(': ', $line);
192 $headers[$key] = $value;