remove and add some exceptions, add getUptime and fix derivedk alculation
[GitHub/Stricted/speedport-hybrid-php-api.git] / SpeedportHybrid.class.php
index 43d60686009998d6b1eff53a8e81f928dc3c3660..1bcc4fbf11cc1f3c04b368b4994549939a64ef82 100644 (file)
@@ -36,7 +36,7 @@ class SpeedportHybrid {
         * session cookie
         * @var string
         */
-       private $session = '';
+       private $cookie = '';
        
        /**
         * router url
@@ -65,7 +65,10 @@ class SpeedportHybrid {
                $data = $this->getValues($data);
                
                if (isset($data['challengev']) && !empty($data['challengev'])) {
-                       $this->challenge = $data['challengev'];
+                       return $data['challengev'];
+               }
+               else {
+                       throw new RouterExeption('unable to get the challenge from the router');
                }
        }
        
@@ -76,11 +79,7 @@ class SpeedportHybrid {
         * @return      boolean
         */
        public function login ($password) {
-               $this->getChallenge();
-               
-               if (empty($this->challenge)) {
-                       throw new RouterExeption('unable to get the challenge from the router');
-               }
+               $this->challenge = $this->getChallenge();
                
                $path = 'data/Login.json';
                $this->hash = hash('sha256', $this->challenge.':'.$password);
@@ -88,33 +87,17 @@ class SpeedportHybrid {
                $data = $this->sentRequest($path, $fields);
                $json = json_decode($data['body'], true);
                $json = $this->getValues($json);
+               
                if (isset($json['login']) && $json['login'] == 'success') {
-                       if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
-                               preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
-                               if (isset($match[1]) && !empty($match[1])) {
-                                       $this->session = $match[1];
-                               }
-                               else {
-                                       throw new RouterExeption('unable to get the session cookie from the router');
-                               }
-                               
-                               // calculate derivedk
-                               if (!function_exists("hash_pbkdf2")) {
-                                       require_once 'CryptLib/CryptLib.php';
-                                       $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\PBKDF2(array('hash' => 'sha1'));
-                                       $this->derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32));
-                                       $this->derivedk = substr($this->derivedk, 0, 32);
-                               }
-                               else {
-                                       $this->derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32);
-                               }
-                               
-                               // get the csrf_token
-                               $this->token = $this->getToken();
-                               
-                               if ($this->checkLogin() === true) {
-                                       return true;
-                               }
+                       $this->cookie = $this->getCookie($data);
+                       
+                       $this->derivedk = $this->getDerviedk($password);
+                       
+                       // get the csrf_token
+                       $this->token = $this->getToken();
+                       
+                       if ($this->checkLogin(false) === true) {
+                               return true;
                        }
                }
                
@@ -124,10 +107,16 @@ class SpeedportHybrid {
        /**
         * check if we are logged in
         *
-        * @return boolean
+        * @param       boolean $exception
+        * @return      boolean
         */
-       public function checkLogin () {
-               if (empty($this->challenge) && empty($this->session)) {
+       public function checkLogin ($exception = true) {
+               // check if challenge or session is empty
+               if (empty($this->challenge) || empty($this->cookie)) {
+                       if ($exception === true) {
+                               throw new RouterExeption('you musst be logged in to use this method');
+                       }
+                       
                        return false;
                }
                
@@ -143,6 +132,10 @@ class SpeedportHybrid {
                $json = $this->getValues($json);
                
                if ($json['loginstate'] != 1) {
+                       if ($exception === true) {
+                               throw new RouterExeption('you musst be logged in to use this method');
+                       }
+                       
                        return false;
                }
                
@@ -152,36 +145,34 @@ class SpeedportHybrid {
        /**
         * logout
         * 
-        * @return      array
+        * @return      boolean
         */
        public function logout () {
-               if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+               $this->checkLogin();
                
                $path = 'data/Login.json';
                $fields = array('csrf_token' =>  $this->token, 'logout' => 'byby');
-               $data = $this->sentRequest($path, $fields, true);
-               if ($this->checkLogin() === false) {
+               $this->sentRequest($path, $fields, true);
+               if ($this->checkLogin(false) === false) {
                        // reset challenge and session
                        $this->challenge = '';
-                       $this->session = '';
-                       $this->token = "";
+                       $this->cookie = '';
+                       $this->token = '';
+                       $this->derivedk = '';
                        
-                       $json = json_decode($data['body'], true);
-                       
-                       return $json;
-               }
-               else {
-                       throw new RouterExeption('logout failed');
+                       return true;
                }
+               
+               return false;
        }
        
        /**
         * reboot the router
         * 
-        * @return      array
+        * @return      boolean
         */
        public function reboot () {
-               if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+               $this->checkLogin();
                
                $path = 'data/Reboot.json';
                $fields = array('csrf_token' => $this->token, 'reboot_device' => 'true');
@@ -195,18 +186,18 @@ class SpeedportHybrid {
                        // like $this->logout() or $this->checkLogin
                        throw new RebootException('Router Reboot');
                }
-               else {
-                       throw new RouterException('unable to reboot');
-               }
+               
+               return false;
        }
        
        /**
         * change dsl connection status
         * 
         * @param       string  $status
+        * @return      boolean
         */
        public function changeConnectionStatus ($status) {
-               if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+               $this->checkLogin();
                
                $path = 'data/Connect.json';
                
@@ -215,14 +206,33 @@ class SpeedportHybrid {
                        $data = $this->sentRequest($path, $fields, true);
                        
                        $json = json_decode($data['body'], true);
+                       $json = $this->getValues($json);
                        
-                       return $json;
+                       if ($json['status'] == 'ok') {
+                               return true;
+                       }
+                       else {
+                               return false;
+                       }
                }
                else {
                        throw new RouterExeption();
                }
        }
        
+       /**
+        * get uptime based on online (connection) time
+        *
+        * @return      string
+        */
+       public function getUptime () {
+               // TODO: search for a better solution, calling Connect.json need some time
+               $data = $this->getData('Connect');
+               $data = $this->getValues($data);
+               
+               return $data['days_online'];
+       }
+       
        /**
         * return the given json as array
         * 
@@ -230,7 +240,7 @@ class SpeedportHybrid {
         * @return      array
         */
        public function getData ($file) {
-               if ($this->checkLogin() !== true && $file != "Status") throw new RouterExeption('you musst be logged in to use this method');
+               if ($file != 'Status') $this->checkLogin();
                
                $path = 'data/'.$file.'.json';
                $fields = array();
@@ -287,7 +297,7 @@ class SpeedportHybrid {
         * @return      array
         */
        private function exportData ($type) {
-               if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+               $this->checkLogin();
                
                $path = 'data/Syslog.json';
                $fields = array('exporttype' => $type);
@@ -306,7 +316,7 @@ class SpeedportHybrid {
         * @return      array
         */
        public function reconnectLte () {
-               if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+               $this->checkLogin();
                
                $path = 'data/modules.json';
                $fields = array('csrf_token' => $this->token, 'lte_reconn' => '1');
@@ -323,7 +333,7 @@ class SpeedportHybrid {
         * @return      array
         */
        public function resetToFactoryDefault () {
-               if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+               $this->checkLogin();
                
                $path = 'data/resetAllSetting.json';
                $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reset_all' => 'true');
@@ -340,7 +350,7 @@ class SpeedportHybrid {
         * @return      array
         */
        public function checkFirmware () {
-               if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+               $this->checkLogin();
                
                $path = 'data/checkfirmware.json';
                $fields = array('checkfirmware' => 'true');
@@ -410,7 +420,12 @@ class SpeedportHybrid {
        private function getValues($array) {
                $data = array();
                foreach ($array as $item) {
-                       $data[$item['varid']] = $item['varvalue'];
+                       if (is_array($item['varvalue'])) {
+                               $data[$item['varid']] = $this->getValues($item['varvalue']);
+                       }
+                       else {
+                               $data[$item['varid']] = $item['varvalue'];
+                       }
                }
                
                return $data;
@@ -456,7 +471,7 @@ class SpeedportHybrid {
                }
                
                if ($cookie === true) {
-                       curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->session);
+                       curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->cookie);
                }
                
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -493,7 +508,7 @@ class SpeedportHybrid {
         * @return      string
         */
        private function getToken () {
-               if ($this->checkLogin() !== true) throw new RouterExeption('you musst be logged in to use this method');
+               $this->checkLogin();
                
                $path = 'html/content/overview/index.html';
                $fields = array();
@@ -514,6 +529,55 @@ class SpeedportHybrid {
                }
        }
        
+       /**
+        * calculate the derivedk
+        *
+        * @param       string  $password
+        * @return      string
+        */
+       private function getDerviedk ($password) {
+               $derivedk = '';
+               
+               // calculate derivedk
+               if (!function_exists('hash_pbkdf2')) {
+                       require_once 'CryptLib/CryptLib.php';
+                       $pbkdf2 = new CryptLib\Key\Derivation\PBKDF\PBKDF2(array('hash' => 'sha1'));
+                       $derivedk = bin2hex($pbkdf2->derive(hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32));
+                       $derivedk = substr($derivedk, 0, 32);
+               }
+               else {
+                       $derivedk = hash_pbkdf2('sha1', hash('sha256', $password), substr($this->challenge, 0, 16), 1000, 32);
+               }
+               
+               if (empty($derivedk)) {
+                       throw new RouterException('unable to calculate derivedk');
+               }
+               
+               return $derivedk;
+       }
+       
+       /**
+        * get cookie from header data
+        *
+        * @param       array   $data
+        * @return      string
+        */
+       private function getCookie ($data) {
+               $cookie = '';
+               if (isset($data['header']['Set-Cookie']) && !empty($data['header']['Set-Cookie'])) {
+                       preg_match('/^.*(SessionID_R3=[a-z0-9]*).*/i', $data['header']['Set-Cookie'], $match);
+                       if (isset($match[1]) && !empty($match[1])) {
+                               $cookie = $match[1];
+                       }
+               }
+               
+               if (empty($cookie)) {
+                       throw new RouterExeption('unable to get the session cookie from the router');
+               }
+               
+               return $cookie;
+       }
+       
        /**
         * parse the curl return header into an array
         *