Commit | Line | Data |
---|---|---|
a91317a6 | 1 | <?php |
6e1250c3 S |
2 | require_once('lib/exception/RebootException.class.php'); |
3 | require_once('lib/exception/RouterException.class.php'); | |
aacbbd28 | 4 | require_once('CryptLib/CryptLib.php'); |
6e1250c3 S |
5 | require_once('lib/trait/Connection.class.php'); |
6 | require_once('lib/trait/CryptLib.class.php'); | |
7 | require_once('lib/trait/Login.class.php'); | |
e559c2bb | 8 | require_once('lib/trait/Network.class.php'); |
6e1250c3 S |
9 | require_once('lib/trait/Phone.class.php'); |
10 | require_once('lib/trait/System.class.php'); | |
8162139f | 11 | |
1d934afe S |
12 | /** |
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) | |
16 | */ | |
21f877e4 | 17 | class SpeedportHybrid { |
9b926efe | 18 | use Connection; |
e9631169 S |
19 | use CryptLib; |
20 | use Login; | |
e559c2bb | 21 | use Network; |
9b926efe S |
22 | use Phone; |
23 | use System; | |
24 | ||
0dca2d61 | 25 | /** |
23cc0061 S |
26 | * class version |
27 | * @const string | |
0dca2d61 | 28 | */ |
e9631169 | 29 | const VERSION = '1.0.4'; |
a91317a6 S |
30 | |
31 | /** | |
32 | * router url | |
33 | * @var string | |
34 | */ | |
b5a532a8 | 35 | private $url = ''; |
a91317a6 | 36 | |
c9e082da | 37 | /** |
e9631169 S |
38 | * inititalize this class |
39 | * | |
40 | * @param string $url | |
c9e082da | 41 | */ |
e58a96d1 | 42 | public function __construct ($url = 'http://speedport.ip/') { |
b5a532a8 | 43 | $this->url = $url; |
a91317a6 S |
44 | } |
45 | ||
c2678616 S |
46 | /** |
47 | * get the values from array | |
48 | * | |
49 | * @param array $array | |
50 | * @return array | |
51 | */ | |
52 | private function getValues($array) { | |
53 | $data = array(); | |
54 | foreach ($array as $item) { | |
aacbbd28 | 55 | // thank you telekom for this piece of shit |
8b7e3972 | 56 | if ($item['vartype'] == 'template') { |
ec351a7d S |
57 | if (is_array($item['varvalue'])) { |
58 | $data[$item['varid']][] = $this->getValues($item['varvalue']); | |
59 | } | |
60 | else { | |
23cc0061 | 61 | // i dont know if we need this |
ec351a7d S |
62 | $data[$item['varid']] = $item['varvalue']; |
63 | } | |
58feafa2 S |
64 | } |
65 | else { | |
8b7e3972 S |
66 | if (is_array($item['varvalue'])) { |
67 | $data[$item['varid']] = $this->getValues($item['varvalue']); | |
68 | } | |
69 | else { | |
70 | $data[$item['varid']] = $item['varvalue']; | |
71 | } | |
58feafa2 | 72 | } |
c2678616 S |
73 | } |
74 | ||
75 | return $data; | |
76 | } | |
77 | ||
a91317a6 S |
78 | /** |
79 | * sends the request to router | |
5e44cffa | 80 | * |
b5a532a8 | 81 | * @param string $path |
809e25bd | 82 | * @param mixed $fields |
a91317a6 | 83 | * @param string $cookie |
809e25bd | 84 | * @param integer $count |
a91317a6 S |
85 | * @return array |
86 | */ | |
0dca2d61 | 87 | private function sentRequest ($path, $fields, $cookie = false, $count = 0) { |
49abc701 | 88 | $url = $this->url.$path.'?lang=en'; |
a91317a6 S |
89 | $ch = curl_init(); |
90 | curl_setopt($ch, CURLOPT_URL, $url); | |
91 | ||
92 | if (!empty($fields)) { | |
809e25bd S |
93 | if (is_array($fields)) { |
94 | curl_setopt($ch, CURLOPT_POST, count($fields)); | |
95 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields)); | |
96 | } | |
97 | else { | |
98 | curl_setopt($ch, CURLOPT_POST, $count); | |
99 | curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); | |
100 | } | |
a91317a6 S |
101 | } |
102 | ||
0dca2d61 | 103 | if ($cookie === true) { |
559c5be7 | 104 | curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->cookie); |
a91317a6 S |
105 | } |
106 | ||
107 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
108 | curl_setopt($ch, CURLOPT_HEADER, true); | |
109 | ||
a91317a6 S |
110 | $result = curl_exec($ch); |
111 | ||
112 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); | |
113 | $header = substr($result, 0, $header_size); | |
114 | $body = substr($result, $header_size); | |
115 | curl_close($ch); | |
116 | ||
219ba661 S |
117 | // check if response is empty |
118 | if (empty($body)) { | |
119 | throw new RouterException('empty response'); | |
120 | } | |
121 | ||
8162139f S |
122 | // check if body is encrypted (hex instead of json) |
123 | if (ctype_xdigit($body)) { | |
124 | $body = $this->decrypt($body); | |
125 | } | |
126 | ||
a91317a6 S |
127 | // fix invalid json |
128 | $body = preg_replace("/(\r\n)|(\r)/", "\n", $body); | |
129 | $body = preg_replace('/\'/i', '"', $body); | |
5e44cffa | 130 | $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body); |
dae16c50 | 131 | $body = preg_replace("/},\s+]/", "}\n]", $body); |
a91317a6 | 132 | |
219ba661 | 133 | // decode json |
aacbbd28 | 134 | if (strpos($url, '.json') !== false) { |
219ba661 S |
135 | $body = json_decode($body, true); |
136 | } | |
137 | ||
a91317a6 S |
138 | return array('header' => $this->parse_headers($header), 'body' => $body); |
139 | } | |
140 | ||
141 | /** | |
142 | * parse the curl return header into an array | |
5e44cffa | 143 | * |
a91317a6 S |
144 | * @param string $response |
145 | * @return array | |
146 | */ | |
147 | private function parse_headers($response) { | |
148 | $headers = array(); | |
149 | $header_text = substr($response, 0, strpos($response, "\r\n\r\n")); | |
150 | ||
7f4a51d2 S |
151 | $header_text = explode("\r\n", $header_text); |
152 | foreach ($header_text as $i => $line) { | |
a91317a6 S |
153 | if ($i === 0) { |
154 | $headers['http_code'] = $line; | |
155 | } | |
156 | else { | |
157 | list ($key, $value) = explode(': ', $line); | |
158 | $headers[$key] = $value; | |
159 | } | |
160 | } | |
161 | ||
162 | return $headers; | |
163 | } | |
7f4a51d2 | 164 | } |