update some stuff
[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('Speedport.class.php');
6 require_once('ISpeedport.class.php');
7 require_once('lib/trait/Connection.class.php');
8 require_once('lib/trait/CryptLib.class.php');
9 require_once('lib/trait/Login.class.php');
10 require_once('lib/trait/Firewall.class.php');
11 require_once('lib/trait/Network.class.php');
12 require_once('lib/trait/Phone.class.php');
13 require_once('lib/trait/System.class.php');
14
15 /**
16 * @author Jan Altensen (Stricted)
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @copyright 2015 Jan Altensen (Stricted)
19 */
20 class SpeedportHybrid extends Speedport implements ISpeedport {
21 use Connection;
22 use CryptLib;
23 use Firewall;
24 use Login;
25 use Network;
26 use Phone;
27 use System;
28
29 /**
30 * class version
31 * @const string
32 */
33 const VERSION = '1.0.4';
34
35 /**
36 * check php requirements
37 */
38 protected function checkRequirements () {
39 if (!extension_loaded('curl')) {
40 throw new Exception("The PHP Extension 'curl' is missing.");
41 }
42 else if (!extension_loaded('json')) {
43 throw new Exception("The PHP Extension 'json' is missing.");
44 }
45 else if (!extension_loaded('pcre')) {
46 throw new Exception("The PHP Extension 'pcre' is missing.");
47 }
48 else if (!extension_loaded('ctype')) {
49 throw new Exception("The PHP Extension 'ctype' is missing.");
50 }
51 else if (!extension_loaded('hash')) {
52 throw new Exception("The PHP Extension 'hash' is missing.");
53 }
54 else if (!in_array('sha256', hash_algos())) {
55 throw new Exception('SHA-256 algorithm is not Supported.');
56 }
57 }
58
59 /**
60 * sends the encrypted request to router
61 *
62 * @param string $path
63 * @param mixed $fields
64 * @param string $cookie
65 * @return array
66 */
67 protected function sentEncryptedRequest ($path, $fields, $cookie = false) {
68 $count = count($fields);
69 $fields = $this->encrypt(http_build_query($fields));
70 return $this->sentRequest($path, $fields, $cookie, $count);
71 }
72
73 /**
74 * sends the request to router
75 *
76 * @param string $path
77 * @param mixed $fields
78 * @param string $cookie
79 * @param integer $count
80 * @return array
81 */
82 protected function sentRequest ($path, $fields, $cookie = false, $count = 0) {
83 $data = parent::sentRequest($path, $fields, $cookie, $count);
84 $header = $data['header'];
85 $body = $data['body'];
86 // check if body is encrypted (hex instead of json)
87 if (ctype_xdigit($body)) {
88 $body = $this->decrypt($body);
89 }
90
91 // fix invalid json
92 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
93 $body = preg_replace('/\'/i', '"', $body);
94 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
95 $body = preg_replace("/},\s+]/", "}\n]", $body);
96
97 // decode json
98 if (strpos($path, '.json') !== false) {
99 $json = json_decode($body, true);
100
101 if (is_array($json)) {
102 $body = $json;
103 }
104 }
105
106 return array('header' => $header, 'body' => $body);
107 }
108 }