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/Phone.class.php');
9 require_once('lib/trait/System.class.php');
12 * @author Jan Altensen (Stricted)
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @copyright 2015 Jan Altensen (Stricted)
16 class SpeedportHybrid
{
27 const VERSION
= '1.0.4';
36 * inititalize this class
40 public function __construct ($url = 'http://speedport.ip/') {
45 * get the values from array
50 private function getValues($array) {
52 foreach ($array as $item) {
53 // thank you telekom for this piece of shit
54 if ($item['vartype'] == 'template') {
55 if (is_array($item['varvalue'])) {
56 $data[$item['varid']][] = $this->getValues($item['varvalue']);
59 // i dont know if we need this
60 $data[$item['varid']] = $item['varvalue'];
64 if (is_array($item['varvalue'])) {
65 $data[$item['varid']] = $this->getValues($item['varvalue']);
68 $data[$item['varid']] = $item['varvalue'];
77 * sends the request to router
80 * @param mixed $fields
81 * @param string $cookie
82 * @param integer $count
85 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
86 $url = $this->url
.$path.'?lang=en';
88 curl_setopt($ch, CURLOPT_URL
, $url);
90 if (!empty($fields)) {
91 if (is_array($fields)) {
92 curl_setopt($ch, CURLOPT_POST
, count($fields));
93 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
96 curl_setopt($ch, CURLOPT_POST
, $count);
97 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
101 if ($cookie === true) {
102 curl_setopt($ch, CURLOPT_COOKIE
, 'challengev='.$this->challenge
.'; '.$this->cookie
);
105 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
106 curl_setopt($ch, CURLOPT_HEADER
, true);
108 $result = curl_exec($ch);
110 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
111 $header = substr($result, 0, $header_size);
112 $body = substr($result, $header_size);
115 // check if response is empty
117 throw new RouterException('empty response');
120 // check if body is encrypted (hex instead of json)
121 if (ctype_xdigit($body)) {
122 $body = $this->decrypt($body);
126 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
127 $body = preg_replace('/\'/i', '"', $body);
128 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
129 $body = preg_replace("/},\s+]/", "}\n]", $body);
132 if (strpos($url, '.json') !== false) {
133 $body = json_decode($body, true);
136 return array('header' => $this->parse_headers($header), 'body' => $body);
140 * parse the curl return header into an array
142 * @param string $response
145 private function parse_headers($response) {
147 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
149 $header_text = explode("\r\n", $header_text);
150 foreach ($header_text as $i => $line) {
152 $headers['http_code'] = $line;
155 list ($key, $value) = explode(': ', $line);
156 $headers[$key] = $value;