fix reconnectLte
[GitHub/Stricted/speedport-hybrid-php-api.git] / SpeedportHybrid.class.php
index 534d32309e3c27b3893f0b8e277873f7f311fef7..c135643cd5163d77b29c03fef5f52a4a9e0d8315 100644 (file)
@@ -1,22 +1,27 @@
 <?php
 require_once('lib/exception/RebootException.class.php');
 require_once('lib/exception/RouterException.class.php');
+require_once('lib/exception/NotImplementedException.class.php');
 require_once('CryptLib/CryptLib.php');
 require_once('lib/trait/Connection.class.php');
 require_once('lib/trait/CryptLib.class.php');
 require_once('lib/trait/Login.class.php');
+require_once('lib/trait/Firewall.class.php');
+require_once('lib/trait/Network.class.php');
 require_once('lib/trait/Phone.class.php');
 require_once('lib/trait/System.class.php');
 
 /**
  * @author      Jan Altensen (Stricted)
  * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @copyright   2015 Jan Altensen (Stricted)
+ * @copyright   2015-2016 Jan Altensen (Stricted)
  */
 class SpeedportHybrid {
        use Connection;
        use CryptLib;
+       use Firewall;
        use Login;
+       use Network;
        use Phone;
        use System;
        
@@ -24,7 +29,7 @@ class SpeedportHybrid {
         * class version
         * @const       string
         */
-       const VERSION = '1.0.4';
+       const VERSION = '1.0.5';
        
        /**
         * router url
@@ -39,6 +44,31 @@ class SpeedportHybrid {
         */
        public function __construct ($url = 'http://speedport.ip/') {
                $this->url = $url;
+               $this->checkRequirements();
+       }
+       
+       /**
+        * check php requirements
+        */
+       private function checkRequirements () {
+               if (!extension_loaded('curl')) {
+                       throw new Exception("The PHP Extension 'curl' is missing.");
+               }
+               else if (!extension_loaded('json')) {
+                       throw new Exception("The PHP Extension 'json' is missing.");
+               }
+               else if (!extension_loaded('pcre')) {
+                       throw new Exception("The PHP Extension 'pcre' is missing.");
+               }
+               else if (!extension_loaded('ctype')) {
+                       throw new Exception("The PHP Extension 'ctype' is missing.");
+               }
+               else if (!extension_loaded('hash')) {
+                       throw new Exception("The PHP Extension 'hash' is missing.");
+               }
+               else if (!in_array('sha256', hash_algos())) {
+                       throw new Exception('SHA-256 algorithm is not Supported.');
+               }
        }
        
        /**
@@ -50,6 +80,8 @@ class SpeedportHybrid {
        private function getValues($array) {
                $data = array();
                foreach ($array as $item) {
+                       if (!isset($item['vartype']) || !isset($item['varid']) || !isset($item['varvalue'])) continue;
+                       
                        // thank you telekom for this piece of shit
                        if ($item['vartype'] == 'template') {
                                if (is_array($item['varvalue'])) {
@@ -82,18 +114,18 @@ class SpeedportHybrid {
         * @param       integer $count
         * @return      array
         */
-       private function sentRequest ($path, $fields, $cookie = false, $count = 0) {
-               $url = $this->url.$path.'?lang=en';
+       private function sendRequest ($path, $fields, $cookie = false, $count = 0) {
+               $url = $this->url.$path;
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                
                if (!empty($fields)) {
+                       curl_setopt($ch, CURLOPT_POST, true);
+                       
                        if (is_array($fields)) {
-                               curl_setopt($ch, CURLOPT_POST, count($fields));
                                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
                        }
                        else {
-                               curl_setopt($ch, CURLOPT_POST, $count);
                                curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
                        }
                }
@@ -130,7 +162,11 @@ class SpeedportHybrid {
                
                // decode json
                if (strpos($url, '.json') !== false) {
-                       $body = json_decode($body, true);
+                       $json = json_decode($body, true);
+                       
+                       if (is_array($json)) {
+                               $body = $json;
+                       }
                }
                
                return array('header' => $this->parse_headers($header), 'body' => $body);