add getPhoneBookEntrys
[GitHub/Stricted/speedport-hybrid-php-api.git] / SpeedportHybrid.class.php
index adc668ba6b2424d6b7454313f986ccfc9ce757b3..ad3c17168a930b279d3734e06c3774da5691f780 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 require_once('RebootException.class.php');
 require_once('RouterException.class.php');
+require_once('CryptLib/CryptLib.php');
 
 /**
  * @author      Jan Altensen (Stricted)
@@ -9,8 +10,8 @@ require_once('RouterException.class.php');
  */
 class SpeedportHybrid {
        /**
-        *
-        *
+        * class version
+        * @const       string
         */
        const VERSION = '1.0.3';
        
@@ -173,6 +174,12 @@ class SpeedportHybrid {
                $data = $this->getValues($data['body']);
                
                if ($data['status'] == 'ok') {
+                       // reset challenge and session
+                       $this->challenge = '';
+                       $this->cookie = '';
+                       $this->token = '';
+                       $this->derivedk = ''; 
+                       
                        // throw an exception because router is unavailable for other tasks
                        // like $this->logout() or $this->checkLogin
                        throw new RebootException('Router Reboot');
@@ -187,7 +194,7 @@ class SpeedportHybrid {
         * @param       string  $status
         * @return      boolean
         */
-       public function changeConnectionStatus ($status) {
+       public function changeDSLStatus ($status) {
                $this->checkLogin();
                
                $path = 'data/Connect.json';
@@ -205,7 +212,49 @@ class SpeedportHybrid {
                        }
                }
                else {
-                       throw new RouterException();
+                       throw new RouterException('unknown status');
+               }
+       }
+       
+       /**
+        * change lte connection status
+        * 
+        * @param       string  $status
+        * @return      boolean
+        */
+       public function changeLTEStatus ($status) {
+               throw new Exception('unstable funtion');
+               $path = 'data/Modules.json';
+               
+               if ($status == '0' || $status == '1' || $status == 'yes' || $status == 'no') {
+                       if ($status == 'yes') $status = '1';
+                       else if ($status == 'no') $status = '0';
+                       
+                       $fields = array('csrf_token' => $this->token, 'use_lte' => $status);
+                       $data = $this->sentEncryptedRequest($path, $fields, true);
+                       
+                       // debug only
+                       return $data;
+               }
+               else {
+                       throw new RouterException('unknown status');
+               }
+       }
+       
+       /**
+        * get phone book entrys
+        *
+        * @return      array
+        */
+       public function getPhoneBookEntrys () {
+               $data = $this->getData('PhoneBook');
+               $data = $this->getValues($data);
+               
+               if (isset($data['addbookentry'])) {
+                       return $data['addbookentry'];
+               }
+               else {
+                       return array();
                }
        }
        
@@ -215,8 +264,7 @@ class SpeedportHybrid {
         * @return      string
         */
        public function getUptime () {
-               // TODO: search for a better solution, calling Connect.json need some time
-               $data = $this->getData('Connect');
+               $data = $this->getData('LAN');
                $data = $this->getValues($data);
                
                return $data['days_online'];
@@ -244,7 +292,15 @@ class SpeedportHybrid {
         * @return      array
         */
        public function getSyslog() {
-               return $this->exportData('0');
+               $data = $this->getData('SystemMessages');
+               $data = $this->getValues($data);
+               
+               if (isset($data['addmessage'])) {
+                       return $data['addmessage'];
+               }
+               else {
+                       return array();
+               }
        }
        
        /**
@@ -253,7 +309,15 @@ class SpeedportHybrid {
         * @return      array
         */
        public function getMissedCalls() {
-               return $this->exportData('1');
+               $data = $this->getData('PhoneCalls');
+               $data = $this->getValues($data);
+               
+               if (isset($data['addmissedcalls'])) {
+                       return $data['addmissedcalls'];
+               }
+               else {
+                       return array();
+               }
        }
        
        /**
@@ -262,7 +326,15 @@ class SpeedportHybrid {
         * @return      array
         */
        public function getTakenCalls() {
-               return $this->exportData('2');
+               $data = $this->getData('PhoneCalls');
+               $data = $this->getValues($data);
+               
+               if (isset($data['addtakencalls'])) {
+                       return $data['addtakencalls'];
+               }
+               else {
+                       return array();
+               }
        }
        
        /**
@@ -271,22 +343,15 @@ class SpeedportHybrid {
         * @return      array
         */
        public function getDialedCalls() {
-               return $this->exportData('3');
-       }
-       
-       /**
-        * export data from router
-        * 
-        * @return      array
-        */
-       private function exportData ($type) {
-               $this->checkLogin();
-               
-               $path = 'data/Syslog.json';
-               $fields = array('exporttype' => $type);
-               $data = $this->sentRequest($path, $fields, true);
+               $data = $this->getData('PhoneCalls');
+               $data = $this->getValues($data);
                
-               return explode("\n", $data['body']);
+               if (isset($data['adddialedcalls'])) {
+                       return $data['adddialedcalls'];
+               }
+               else {
+                       return array();
+               }
        }
        
        /**
@@ -343,16 +408,14 @@ class SpeedportHybrid {
         * @return      array
         */
        private function decrypt ($data) {
-               require_once 'CryptLib/CryptLib.php';
-               $factory = new CryptLib\Cipher\Factory();
-               $aes = $factory->getBlockCipher('rijndael-128');
-               
                $iv = hex2bin(substr($this->challenge, 16, 16));
                $adata = hex2bin(substr($this->challenge, 32, 16));
-               $dkey = hex2bin($this->derivedk);
+               $key = hex2bin($this->derivedk);
                $enc = hex2bin($data);
                
-               $aes->setKey($dkey);
+               $factory = new CryptLib\Cipher\Factory();
+               $aes = $factory->getBlockCipher('rijndael-128');
+               $aes->setKey($key);
                $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
                
                $mode->decrypt($enc);
@@ -363,21 +426,19 @@ class SpeedportHybrid {
        /**
         * decrypt data for the router
         * 
-        * @param       array   $data
+        * @param       string  $data
         * @return      string
         */
        private function encrypt ($data) {
-               require_once 'CryptLib/CryptLib.php';
-               $factory = new CryptLib\Cipher\Factory();
-               $aes = $factory->getBlockCipher('rijndael-128');
-               
                $iv = hex2bin(substr($this->challenge, 16, 16));
                $adata = hex2bin(substr($this->challenge, 32, 16));
-               $dkey = hex2bin($this->derivedk);
+               $key = hex2bin($this->derivedk);
                
-               $aes->setKey($dkey);
+               $factory = new CryptLib\Cipher\Factory();
+               $aes = $factory->getBlockCipher('rijndael-128');
+               $aes->setKey($key);
                $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
-               $mode->encrypt(http_build_query($data));
+               $mode->encrypt($data);
                
                return bin2hex($mode->finish());
        }
@@ -391,9 +452,15 @@ class SpeedportHybrid {
        private function getValues($array) {
                $data = array();
                foreach ($array as $item) {
-                       // thank you telekom for this piece of bullshit
+                       // thank you telekom for this piece of shit
                        if ($item['vartype'] == 'template') {
-                               $data[$item['varid']] = $this->getValues($item['varvalue']);
+                               if (is_array($item['varvalue'])) {
+                                       $data[$item['varid']][] = $this->getValues($item['varvalue']);
+                               }
+                               else {
+                                       // i dont know if we need this
+                                       $data[$item['varid']] = $item['varvalue'];
+                               }
                        }
                        else {
                                if (is_array($item['varvalue'])) {
@@ -418,7 +485,7 @@ class SpeedportHybrid {
         */
        private function sentEncryptedRequest ($path, $fields, $cookie = false) {
                $count = count($fields);
-               $fields = $this->encrypt($fields);
+               $fields = $this->encrypt(http_build_query($fields));
                return $this->sentRequest($path, $fields, $cookie, $count);
        }
        
@@ -454,10 +521,6 @@ class SpeedportHybrid {
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HEADER, true);
                
-               if ($cookie) {
-                       
-               }
-               
                $result = curl_exec($ch);
                
                $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
@@ -482,7 +545,7 @@ class SpeedportHybrid {
                $body = preg_replace("/},\s+]/", "}\n]", $body);
                
                // decode json
-               if (strpos($url, '.json') !== false && strpos($url, 'Syslog.json') === false) {
+               if (strpos($url, '.json') !== false) {
                        $body = json_decode($body, true);
                }
                
@@ -523,7 +586,6 @@ class SpeedportHybrid {
                
                // 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);