add checkLogin, logout and getToken to Speedportw724v class
authorStricted <info@stricted.de>
Mon, 10 Aug 2015 16:23:50 +0000 (18:23 +0200)
committerStricted <info@stricted.de>
Mon, 10 Aug 2015 16:23:50 +0000 (18:23 +0200)
Speedportw724v.class.php

index 8668fc21edccc7897aed9f87556b10c4327ac927..db971720e2cac4ee7c1e36ca5369cdbb77e5266b 100644 (file)
@@ -39,13 +39,82 @@ class Speedportw724v extends SpeedportHybrid implements ISpeedport {
                return false;
        }
        
+       /**
+        * check if we are logged in
+        *
+        * @param       boolean $exception
+        * @return      boolean
+        */
+       public function checkLogin ($exception = true) {
+               // check if session is empty
+               if (empty($this->cookie)) {
+                       if ($exception === true) {
+                               throw new RouterException('you musst be logged in to use this method');
+                       }
+                       
+                       return false;
+               }
+               
+               $path = 'data/SecureStatus.json';
+               $fields = array();
+               $data = $this->sentRequest($path, $fields, true);
+               $data = $this->getValues($data['body']);
+               
+               if ($data['loginstate'] != 1) {
+                       if ($exception === true) {
+                               throw new RouterException('you musst be logged in to use this method');
+                       }
+                       
+                       return false;
+               }
+               
+               return true;
+       }
+       
+       /**
+        * logout
+        * 
+        * @return      boolean
+        */
+       public function logout () {
+               $this->checkLogin();
+               
+               $path = 'data/Login.json';
+               $fields = array('csrf_token' => $this->token, 'logout' => 'byby');
+               $data = $this->sentRequest($path, $fields, true);
+               $data = $this->getValues($data['body']);
+               if ((isset($data['status']) && $data['status'] == 'ok') && $this->checkLogin(false) === false) {
+                       // reset challenge and session
+                       $this->cookie = '';
+                       $this->token = '';
+                       
+                       return true;
+               }
+               
+               return false;
+       }
+       
        /**
         * get the csrf_token
         * 
         * @return      string
         */
        protected function getToken () {
-               // TODO: check if this is needed
+               $this->checkLogin();
+               
+               $path = 'html/content/overview/index.html';
+               $fields = array();
+               $data = $this->sentRequest($path, $fields, true);
+               
+               $a = explode('csrf_token = "', $data['body']);
+               $a = explode('";', $a[1]);
+               
+               if (isset($a[0]) && !empty($a[0])) {
+                       return $a[0];
+               }
+               else {
+                       throw new RouterException('unable to get csrf_token');
+               }
        }
        
        /**