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