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/') {
49 * get the values from array
54 private function getValues($array) {
56 foreach ($array as $item) {
57 if (!isset($item['vartype']) ||
!isset($item['varid']) ||
!isset($item['varvalue'])) continue;
59 // thank you telekom for this piece of shit
60 if ($item['vartype'] == 'template') {
61 if (is_array($item['varvalue'])) {
62 $data[$item['varid']][] = $this->getValues($item['varvalue']);
65 // i dont know if we need this
66 $data[$item['varid']] = $item['varvalue'];
70 if (is_array($item['varvalue'])) {
71 $data[$item['varid']] = $this->getValues($item['varvalue']);
74 $data[$item['varid']] = $item['varvalue'];
83 * sends the request to router
86 * @param mixed $fields
87 * @param string $cookie
88 * @param integer $count
91 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
92 $url = $this->url
.$path.'?lang=en';
94 curl_setopt($ch, CURLOPT_URL
, $url);
96 if (!empty($fields)) {
97 if (is_array($fields)) {
98 curl_setopt($ch, CURLOPT_POST
, count($fields));
99 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
102 curl_setopt($ch, CURLOPT_POST
, $count);
103 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
107 if ($cookie === true) {
108 curl_setopt($ch, CURLOPT_COOKIE
, 'challengev='.$this->challenge
.'; '.$this->cookie
);
111 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
112 curl_setopt($ch, CURLOPT_HEADER
, true);
114 $result = curl_exec($ch);
116 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
117 $header = substr($result, 0, $header_size);
118 $body = substr($result, $header_size);
121 // check if response is empty
123 throw new RouterException('empty response');
126 // check if body is encrypted (hex instead of json)
127 if (ctype_xdigit($body)) {
128 $body = $this->decrypt($body);
132 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
133 $body = preg_replace('/\'/i', '"', $body);
134 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
135 $body = preg_replace("/},\s+]/", "}\n]", $body);
138 if (strpos($url, '.json') !== false) {
139 $body = json_decode($body, true);
142 return array('header' => $this->parse_headers($header), 'body' => $body);
146 * parse the curl return header into an array
148 * @param string $response
151 private function parse_headers($response) {
153 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
155 $header_text = explode("\r\n", $header_text);
156 foreach ($header_text as $i => $line) {
158 $headers['http_code'] = $line;
161 list ($key, $value) = explode(': ', $line);
162 $headers[$key] = $value;