update some stuff
[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');
35a39175
S
5require_once('Speedport.class.php');
6require_once('ISpeedport.class.php');
6e1250c3
S
7require_once('lib/trait/Connection.class.php');
8require_once('lib/trait/CryptLib.class.php');
9require_once('lib/trait/Login.class.php');
324305fd 10require_once('lib/trait/Firewall.class.php');
e559c2bb 11require_once('lib/trait/Network.class.php');
6e1250c3
S
12require_once('lib/trait/Phone.class.php');
13require_once('lib/trait/System.class.php');
8162139f 14
1d934afe
S
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 */
35a39175 20class SpeedportHybrid extends Speedport implements ISpeedport {
9b926efe 21 use Connection;
e9631169 22 use CryptLib;
324305fd 23 use Firewall;
e9631169 24 use Login;
e559c2bb 25 use Network;
9b926efe
S
26 use Phone;
27 use System;
28
0dca2d61 29 /**
23cc0061
S
30 * class version
31 * @const string
0dca2d61 32 */
e9631169 33 const VERSION = '1.0.4';
a91317a6 34
0d814efc
S
35 /**
36 * check php requirements
37 */
35a39175 38 protected function checkRequirements () {
0d814efc
S
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 }
a91317a6
S
57 }
58
c2678616 59 /**
35a39175 60 * sends the encrypted request to router
c2678616 61 *
35a39175
S
62 * @param string $path
63 * @param mixed $fields
64 * @param string $cookie
c2678616
S
65 * @return array
66 */
35a39175
S
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);
c2678616
S
71 }
72
a91317a6
S
73 /**
74 * sends the request to router
5e44cffa 75 *
b5a532a8 76 * @param string $path
809e25bd 77 * @param mixed $fields
a91317a6 78 * @param string $cookie
809e25bd 79 * @param integer $count
a91317a6
S
80 * @return array
81 */
35a39175
S
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'];
8162139f
S
86 // check if body is encrypted (hex instead of json)
87 if (ctype_xdigit($body)) {
88 $body = $this->decrypt($body);
89 }
90
a91317a6
S
91 // fix invalid json
92 $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
93 $body = preg_replace('/\'/i', '"', $body);
5e44cffa 94 $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);
dae16c50 95 $body = preg_replace("/},\s+]/", "}\n]", $body);
a91317a6 96
219ba661 97 // decode json
35a39175 98 if (strpos($path, '.json') !== false) {
87492136
S
99 $json = json_decode($body, true);
100
101 if (is_array($json)) {
102 $body = $json;
103 }
219ba661
S
104 }
105
35a39175 106 return array('header' => $header, 'body' => $body);
a91317a6 107 }
7f4a51d2 108}