From: Stricted Date: Fri, 17 Jul 2015 23:25:11 +0000 (+0200) Subject: fix logout and add checkLogin method X-Git-Tag: 1.0.2~5 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=df1e13944cb773390e06525a198b8feeb9390794;p=GitHub%2FStricted%2Fspeedport-hybrid-php-api.git fix logout and add checkLogin method --- diff --git a/SpeedportHybrid.class.php b/SpeedportHybrid.class.php index 81152c3..e7a2ab2 100644 --- a/SpeedportHybrid.class.php +++ b/SpeedportHybrid.class.php @@ -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'); + } } /**