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/Network.class.php');
9 require_once('lib/trait/Phone.class.php');
10 require_once('lib/trait/System.class.php');
13 * @author Jan Altensen (Stricted)
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @copyright 2015 Jan Altensen (Stricted)
17 class SpeedportHybrid
{
29 const VERSION
= '1.0.4';
38 * inititalize this class
42 public function __construct ($url = 'http://speedport.ip/') {
47 * get the values from array
52 private function getValues($array) {
54 foreach ($array as $item) {
55 // thank you telekom for this piece of shit
56 if ($item['vartype'] == 'template') {
57 if (is_array($item['varvalue'])) {
58 $data[$item['varid']][] = $this->getValues($item['varvalue']);
61 // i dont know if we need this
62 $data[$item['varid']] = $item['varvalue'];
66 if (is_array($item['varvalue'])) {
67 $data[$item['varid']] = $this->getValues($item['varvalue']);
70 $data[$item['varid']] = $item['varvalue'];
79 * sends the request to router
82 * @param mixed $fields
83 * @param string $cookie
84 * @param integer $count
87 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
88 $url = $this->url
.$path.'?lang=en';
90 curl_setopt($ch, CURLOPT_URL
, $url);
92 if (!empty($fields)) {
93 if (is_array($fields)) {
94 curl_setopt($ch, CURLOPT_POST
, count($fields));
95 curl_setopt($ch, CURLOPT_POSTFIELDS
, http_build_query($fields));
98 curl_setopt($ch, CURLOPT_POST
, $count);
99 curl_setopt($ch, CURLOPT_POSTFIELDS
, $fields);
103 if ($cookie === true) {
104 curl_setopt($ch, CURLOPT_COOKIE
, 'challengev='.$this->challenge
.'; '.$this->cookie
);
107 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, true);
108 curl_setopt($ch, CURLOPT_HEADER
, true);
110 $result = curl_exec($ch);
112 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE
);
113 $header = substr($result, 0, $header_size);
114 $body = substr($result, $header_size);
117 // check if response is empty
119 throw new RouterException('empty response');
122 // check if body is encrypted (hex instead of json)
123 if (ctype_xdigit($body)) {
124 $body = $this->decrypt($body);
128 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
129 $body = preg_replace('/\'/i', '"', $body);
130 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
131 $body = preg_replace("/},\s+]/", "}\n]", $body);
134 if (strpos($url, '.json') !== false) {
135 $body = json_decode($body, true);
138 return array('header' => $this->parse_headers($header), 'body' => $body);
142 * parse the curl return header into an array
144 * @param string $response
147 private function parse_headers($response) {
149 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
151 $header_text = explode("\r\n", $header_text);
152 foreach ($header_text as $i => $line) {
154 $headers['http_code'] = $line;
157 list ($key, $value) = explode(': ', $line);
158 $headers[$key] = $value;