do some changes to support other speedport router
[GitHub/Stricted/speedport-hybrid-php-api.git] / Speedport.class.php
1 <?php
2
3 /**
4 * @author Jan Altensen (Stricted)
5 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
6 * @copyright 2015 Jan Altensen (Stricted)
7 */
8 class Speedport {
9 /**
10 * router url
11 * @var string
12 */
13 protected $url = '';
14
15 /**
16 * hashed password
17 * @var string
18 */
19 protected $hash = '';
20
21 /**
22 * session cookie
23 * @var string
24 */
25 protected $cookie = '';
26
27 /**
28 * csrf_token
29 * @var string
30 */
31 protected $token = '';
32
33 /**
34 * inititalize this class
35 *
36 * @param string $url
37 */
38 public function __construct ($url = 'http://speedport.ip/') {
39 $this->url = $url;
40 $this->checkRequirements();
41 }
42
43 /**
44 * check php requirements
45 */
46 protected function checkRequirements () {
47 }
48
49 /**
50 * get the values from array
51 *
52 * @param array $array
53 * @return array
54 */
55 protected function getValues($array) {
56 $data = array();
57 foreach ($array as $item) {
58 if (!isset($item['vartype']) || !isset($item['varid']) || !isset($item['varvalue'])) continue;
59
60 // thank you telekom for this piece of shit
61 if ($item['vartype'] == 'template') {
62 if (is_array($item['varvalue'])) {
63 $data[$item['varid']][] = $this->getValues($item['varvalue']);
64 }
65 else {
66 // i dont know if we need this
67 $data[$item['varid']] = $item['varvalue'];
68 }
69 }
70 else {
71 if (is_array($item['varvalue'])) {
72 $data[$item['varid']] = $this->getValues($item['varvalue']);
73 }
74 else {
75 $data[$item['varid']] = $item['varvalue'];
76 }
77 }
78 }
79
80 return $data;
81 }
82
83 /**
84 * sends the request to router
85 *
86 * @param string $path
87 * @param mixed $fields
88 * @param string $cookie
89 * @param integer $count
90 * @return array
91 */
92 protected function sentRequest ($path, $fields, $cookie = false, $count = 0) {
93 $url = $this->url.$path.'?lang=en';
94 $ch = curl_init();
95 curl_setopt($ch, CURLOPT_URL, $url);
96
97 if (!empty($fields)) {
98 if (is_array($fields)) {
99 curl_setopt($ch, CURLOPT_POST, count($fields));
100 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
101 }
102 else {
103 curl_setopt($ch, CURLOPT_POST, $count);
104 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
105 }
106 }
107
108 if ($cookie === true) {
109 curl_setopt($ch, CURLOPT_COOKIE, $this->cookie);
110 }
111
112 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
113 curl_setopt($ch, CURLOPT_HEADER, true);
114
115 $result = curl_exec($ch);
116
117 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
118 $header = substr($result, 0, $header_size);
119 $body = substr($result, $header_size);
120 curl_close($ch);
121
122 // check if response is empty
123 if (empty($body)) {
124 throw new RouterException('empty response');
125 }
126
127 return array('header' => $this->parse_headers($header), 'body' => $body);
128 }
129
130 /**
131 * parse the curl return header into an array
132 *
133 * @param string $response
134 * @return array
135 */
136 private function parse_headers($response) {
137 $headers = array();
138 $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));
139
140 $header_text = explode("\r\n", $header_text);
141 foreach ($header_text as $i => $line) {
142 if ($i === 0) {
143 $headers['http_code'] = $line;
144 }
145 else {
146 list ($key, $value) = explode(': ', $line);
147 $headers[$key] = $value;
148 }
149 }
150
151 return $headers;
152 }
153 }