| 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/Phone.class.php'); |
| 9 | require_once('lib/trait/System.class.php'); |
| 10 | |
| 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 | */ |
| 16 | class SpeedportHybrid { |
| 17 | use Connection; |
| 18 | use CryptLib; |
| 19 | use Login; |
| 20 | use Phone; |
| 21 | use System; |
| 22 | |
| 23 | /** |
| 24 | * class version |
| 25 | * @const string |
| 26 | */ |
| 27 | const VERSION = '1.0.4'; |
| 28 | |
| 29 | /** |
| 30 | * router url |
| 31 | * @var string |
| 32 | */ |
| 33 | private $url = ''; |
| 34 | |
| 35 | /** |
| 36 | * inititalize this class |
| 37 | * |
| 38 | * @param string $url |
| 39 | */ |
| 40 | public function __construct ($url = 'http://speedport.ip/') { |
| 41 | $this->url = $url; |
| 42 | } |
| 43 | |
| 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) { |
| 53 | // thank you telekom for this piece of shit |
| 54 | if ($item['vartype'] == 'template') { |
| 55 | if (is_array($item['varvalue'])) { |
| 56 | $data[$item['varid']][] = $this->getValues($item['varvalue']); |
| 57 | } |
| 58 | else { |
| 59 | // i dont know if we need this |
| 60 | $data[$item['varid']] = $item['varvalue']; |
| 61 | } |
| 62 | } |
| 63 | else { |
| 64 | if (is_array($item['varvalue'])) { |
| 65 | $data[$item['varid']] = $this->getValues($item['varvalue']); |
| 66 | } |
| 67 | else { |
| 68 | $data[$item['varid']] = $item['varvalue']; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return $data; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * sends the request to router |
| 78 | * |
| 79 | * @param string $path |
| 80 | * @param mixed $fields |
| 81 | * @param string $cookie |
| 82 | * @param integer $count |
| 83 | * @return array |
| 84 | */ |
| 85 | private function sentRequest ($path, $fields, $cookie = false, $count = 0) { |
| 86 | $url = $this->url.$path.'?lang=en'; |
| 87 | $ch = curl_init(); |
| 88 | curl_setopt($ch, CURLOPT_URL, $url); |
| 89 | |
| 90 | if (!empty($fields)) { |
| 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 | } |
| 99 | } |
| 100 | |
| 101 | if ($cookie === true) { |
| 102 | curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->cookie); |
| 103 | } |
| 104 | |
| 105 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 106 | curl_setopt($ch, CURLOPT_HEADER, true); |
| 107 | |
| 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 | |
| 115 | // check if response is empty |
| 116 | if (empty($body)) { |
| 117 | throw new RouterException('empty response'); |
| 118 | } |
| 119 | |
| 120 | // check if body is encrypted (hex instead of json) |
| 121 | if (ctype_xdigit($body)) { |
| 122 | $body = $this->decrypt($body); |
| 123 | } |
| 124 | |
| 125 | // fix invalid json |
| 126 | $body = preg_replace("/(\r\n)|(\r)/", "\n", $body); |
| 127 | $body = preg_replace('/\'/i', '"', $body); |
| 128 | $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body); |
| 129 | $body = preg_replace("/},\s+]/", "}\n]", $body); |
| 130 | |
| 131 | // decode json |
| 132 | if (strpos($url, '.json') !== false) { |
| 133 | $body = json_decode($body, true); |
| 134 | } |
| 135 | |
| 136 | return array('header' => $this->parse_headers($header), 'body' => $body); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * parse the curl return header into an array |
| 141 | * |
| 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 | |
| 149 | $header_text = explode("\r\n", $header_text); |
| 150 | foreach ($header_text as $i => $line) { |
| 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 | } |
| 162 | } |