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');
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)
18 class SpeedportHybrid
{
31 const VERSION
= '1.0.4';
40 * inititalize this class
44 public function __construct ($url = 'http://speedport.ip/') {
46 $this->checkRequirements();
50 * check php requirements
52 private function checkRequirements () {
53 if (!extension_loaded('curl')) {
54 throw new Exception("The PHP Extension 'curl' is missing.");
56 else if (!extension_loaded('json')) {
57 throw new Exception("The PHP Extension 'json' is missing.");
59 else if (!extension_loaded('pcre')) {
60 throw new Exception("The PHP Extension 'pcre' is missing.");
62 else if (!extension_loaded('ctype')) {
63 throw new Exception("The PHP Extension 'ctype' is missing.");
65 else if (!extension_loaded('hash')) {
66 throw new Exception("The PHP Extension 'hash' is missing.");
68 else if (!in_array('sha256', hash_algos())) {
69 throw new Exception('SHA-256 algorithm is not Supported.');
74 * get the values from array
79 private function getValues($array) {
81 foreach ($array as $item) {
82 if (!isset($item['vartype']) ||
!isset($item['varid']) ||
!isset($item['varvalue'])) continue;
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']);
90 // i dont know if we need this
91 $data[$item['varid']] = $item['varvalue'];
95 if (is_array($item['varvalue'])) {
96 $data[$item['varid']] = $this->getValues($item['varvalue']);
99 $data[$item['varid']] = $item['varvalue'];
108 * sends the request to router
110 * @param string $path
111 * @param mixed $fields
112 * @param string $cookie
113 * @param integer $count
116 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
117 $url = $this->url
.$path.'?lang=en';
119 curl_setopt($ch, CURLOPT_URL
, $url);
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));
127 curl_setopt($ch, CURLOPT_POST
, $count);
128 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
132 if ($cookie === true) {
133 curl_setopt($ch, CURLOPT_COOKIE
, 'challengev='.$this->challenge
.'; '.$this->cookie
);
136 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
137 curl_setopt($ch, CURLOPT_HEADER
, true);
139 $result = curl_exec($ch);
141 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
142 $header = substr($result, 0, $header_size);
143 $body = substr($result, $header_size);
146 // check if response is empty
148 throw new RouterException('empty response');
151 // check if body is encrypted (hex instead of json)
152 if (ctype_xdigit($body)) {
153 $body = $this->decrypt($body);
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);
163 if (strpos($url, '.json') !== false) {
164 $json = json_decode($body, true);
166 if (is_array($json)) {
171 return array('header' => $this->parse_headers($header), 'body' => $body);
175 * parse the curl return header into an array
177 * @param string $response
180 private function parse_headers($response) {
182 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
184 $header_text = explode("\r\n", $header_text);
185 foreach ($header_text as $i => $line) {
187 $headers['http_code'] = $line;
190 list ($key, $value) = explode(': ', $line);
191 $headers[$key] = $value;