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