remove debug code
[GitHub/Stricted/speedport-hybrid-php-api.git] / SpeedportHybrid.class.php
index 6793763124d74cd752e6c5732f89ec6337e92383..8b986ba5a2431e0a465c3bc3a91194e2b329a913 100644 (file)
@@ -10,8 +10,8 @@ require_once('CryptLib/CryptLib.php');
  */
 class SpeedportHybrid {
        /**
-        *
-        *
+        * class version
+        * @const       string
         */
        const VERSION = '1.0.3';
        
@@ -194,7 +194,7 @@ class SpeedportHybrid {
         * @param       string  $status
         * @return      boolean
         */
-       public function changeConnectionStatus ($status) {
+       public function changeDSLStatus ($status) {
                $this->checkLogin();
                
                $path = 'data/Connect.json';
@@ -212,10 +212,135 @@ 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();
+               }
+       }
+       
+       /**
+        * add Phone Book Entry
+        *
+        * @param       string  $name
+        * @param       string  $firstname
+        * @param       string  $private
+        * @param       string  $work
+        * @param       string  $mobile
+        * @param       integer $id
+        *
+        * @return      array
+        */
+       public function addPhoneBookEntry ($name, $firstname, $private, $work, $mobile, $id = -1) {
+               $this->checkLogin();
+               
+               $path = 'data/PhoneBook.json';
+               $fields = array(
+                                               'csrf_token' => $this->getToken(),
+                                               'id' => $id,
+                                               'search' => '',
+                                               'phonebook_name' => $name,
+                                               'phonebook_vorname' => $firstname,
+                                               'phonebook_number_p' => $private,
+                                               'phonebook_number_a' => $work,
+                                               'phonebook_number_m' => $mobile
+                                               );
+               
+               $data = $this->sentRequest($path, $fields, true);
+               $data = $this->getValues($data['body']);
+               
+               if ($data['status'] == 'ok') {
+                       return $data;
+               }
+               else {
+                       throw new RouterException('can not add/edit Phone Book Entry');
+               }
+       }
+       
+       /**
+        * edit Phone Book Entry
+        *
+        * @param       integer $id
+        * @param       string  $name
+        * @param       string  $firstname
+        * @param       string  $private
+        * @param       string  $work
+        * @param       string  $mobile
+        *
+        * @return      array
+        */
+       public function changePhoneBookEntry ($id, $name, $firstname, $private, $work, $mobile) {
+               return $this->addPhoneBookEntry($name, $firstname, $private, $work, $private, $id);
+       }
+       
+       /**
+        * delete Phone Book Entry
+        *
+        * @param       integer $id
+        *
+        * @return      array
+        */
+       public function deletePhoneBookEntry ($id) {
+               $this->checkLogin();
+               
+               $path = 'data/PhoneBook.json';
+               $fields = array(
+                                               'csrf_token' => $this->getToken(),
+                                               'id' => $id,
+                                               'deleteEntry' => 'delete'
+                                               );
+               
+               $data = $this->sentRequest($path, $fields, true);
+               $data = $this->getValues($data['body']);
+               
+               if ($data['status'] == 'ok') {
+                       return $data;
+               }
+               else {
+                       throw new RouterException('can not delete Phone Book Entry');
+               }
+               
+       }
+       
        /**
         * get uptime based on online (connection) time
         *
@@ -261,27 +386,6 @@ class SpeedportHybrid {
                }
        }
        
-       /**
-        * get the router syslog
-        * 
-        * @return      array
-        */
-       public function test() {
-               $data = $this->getData('NASLight');
-               $data = $this->getValues($data);
-               print_r($data);
-               /*
-               $data = $this->getValues($data);
-               
-               if (isset($data['addmessage'])) {
-                       return $data['addmessage'];
-               }
-               else {
-                       return array();
-               }
-               */
-       }
-       
        /**
         * get the Missed Calls from router
         * 
@@ -387,15 +491,14 @@ class SpeedportHybrid {
         * @return      array
         */
        private function decrypt ($data) {
-               $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);
@@ -406,20 +509,19 @@ class SpeedportHybrid {
        /**
         * decrypt data for the router
         * 
-        * @param       array   $data
+        * @param       string  $data
         * @return      string
         */
        private function encrypt ($data) {
-               $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());
        }
@@ -439,6 +541,7 @@ class SpeedportHybrid {
                                        $data[$item['varid']][] = $this->getValues($item['varvalue']);
                                }
                                else {
+                                       // i dont know if we need this
                                        $data[$item['varid']] = $item['varvalue'];
                                }
                        }
@@ -465,7 +568,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);
        }
        
@@ -495,7 +598,7 @@ class SpeedportHybrid {
                }
                
                if ($cookie === true) {
-                       curl_setopt($ch, CURLOPT_COOKIE, 'lang=en; challengev='.$this->challenge.'; '.$this->cookie);
+                       curl_setopt($ch, CURLOPT_COOKIE, 'challengev='.$this->challenge.'; '.$this->cookie);
                }
                
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);