fix last commit
[GitHub/Stricted/speedport-hybrid-php-api.git] / SpeedportHybrid.class.php
1 <?php
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');
12
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 */
18 class SpeedportHybrid {
19 use Connection;
20 use CryptLib;
21 use Firewall;
22 use Login;
23 use Network;
24 use Phone;
25 use System;
26
27 /**
28 * class version
29 * @const string
30 */
31 const VERSION = '1.0.4';
32
33 /**
34 * router url
35 * @var string
36 */
37 private $url = '';
38
39 /**
40 * inititalize this class
41 *
42 * @param string $url
43 */
44 public function __construct ($url = 'http://speedport.ip/') {
45 $this->url = $url;
46 }
47
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) {
57 if (!isset($item['vartype']) || !isset($item['varid']) || !isset($item['varvalue'])) continue;
58
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']);
63 }
64 else {
65 // i dont know if we need this
66 $data[$item['varid']] = $item['varvalue'];
67 }
68 }
69 else {
70 if (is_array($item['varvalue'])) {
71 $data[$item['varid']] = $this->getValues($item['varvalue']);
72 }
73 else {
74 $data[$item['varid']] = $item['varvalue'];
75 }
76 }
77 }
78
79 return $data;
80 }
81
82 /**
83 * sends the request to router
84 *
85 * @param string $path
86 * @param mixed $fields
87 * @param string $cookie
88 * @param integer $count
89 * @return array
90 */
91 private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
92 $url = $this->url.$path.'?lang=en';
93 $ch = curl_init();
94 curl_setopt($ch, CURLOPT_URL, $url);
95
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));
100 }
101 else {
102 curl_setopt($ch, CURLOPT_POST, $count);
103 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
104 }
105 }
106
107 if ($cookie === true) {
108 curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->cookie);
109 }
110
111 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
112 curl_setopt($ch, CURLOPT_HEADER, true);
113
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
121 // check if response is empty
122 if (empty($body)) {
123 throw new RouterException('empty response');
124 }
125
126 // check if body is encrypted (hex instead of json)
127 if (ctype_xdigit($body)) {
128 $body = $this->decrypt($body);
129 }
130
131 // fix invalid json
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);
136
137 // decode json
138 if (strpos($url, '.json') !== false) {
139 $json = json_decode($body, true);
140
141 if (is_array($json)) {
142 $body = $json;
143 }
144 }
145
146 return array('header' => $this->parse_headers($header), 'body' => $body);
147 }
148
149 /**
150 * parse the curl return header into an array
151 *
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
159 $header_text = explode("\r\n", $header_text);
160 foreach ($header_text as $i => $line) {
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 }
172 }