fix logout and add checkLogin method
authorStricted <info@stricted.de>
Fri, 17 Jul 2015 23:25:11 +0000 (01:25 +0200)
committerStricted <info@stricted.de>
Fri, 17 Jul 2015 23:25:11 +0000 (01:25 +0200)
SpeedportHybrid.class.php

index 81152c31498f3ed9a3e90e66a8e631254e7dab53..e7a2ab21c2991b9028eb9f96f90084c1c5ace2b4 100644 (file)
@@ -108,13 +108,40 @@ class SpeedportHybrid {
                                // get the csrf_token
                                $this->token = $this->getToken();
                                
-                               return true;
+                               if ($this->checkLogin() === true) {
+                                       return true;
+                               }
                        }
                }
                
                return false;
        }
        
+       /**
+        * check if we are logged in
+        *
+        * @return boolean
+        */
+       public function checkLogin () {
+               $path = 'data/'.$file.'.json';
+               $fields = array();
+               $cookie = 'challengev='.$this->challenge.'; '.$this->session;
+               $data = $this->sentRequest($path, $fields, $cookie);
+               
+               if (empty($data['body'])) {
+                       throw new Exception('unable to get '.$file.' data');
+               }
+               
+               $json = json_decode($data['body'], true);
+               $json = $this->getValues($json);
+               
+               if ($json['loginstate'] != 1) {
+                       return false;
+               }
+               
+               return true;
+       }
+       
        /**
         * logout
         * 
@@ -122,16 +149,22 @@ class SpeedportHybrid {
         */
        public function logout () {
                $path = 'data/Login.json';
-               $fields = array('logout' => 'byby');
-               $data = $this->sentRequest($path, $fields);
-               // reset challenge and session
-               $this->challenge = '';
-               $this->session = '';
-               $this->token = "";
-               
-               $json = json_decode($data['body'], true);
-               
-               return $json;
+               $fields = array('csrf_token' =>  $this->token, 'logout' => 'byby');
+               $cookie = 'challengev='.$this->challenge.'; '.$this->session;
+               $data = $this->sentRequest($path, $fields, $cookie);
+               if ($this->checkLogin() === false) {
+                       // reset challenge and session
+                       $this->challenge = '';
+                       $this->session = '';
+                       $this->token = "";
+                       
+                       $json = json_decode($data['body'], true);
+                       
+                       return $json;
+               }
+               else {
+                       throw new Exception('logout failed');
+               }
        }
        
        /**