commit ba6959ce21dd38508dfc9c77a2b1b5f1bd0f251b Author: NTEN\Nenninger Date: Mon Aug 8 15:21:04 2022 +0200 erster Wurf -> funktioniert noch nicht! diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2bd8749 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +_login response.txt diff --git a/hermineConnect.php b/hermineConnect.php new file mode 100644 index 0000000..20da2b4 --- /dev/null +++ b/hermineConnect.php @@ -0,0 +1,134 @@ +connectorID = base64_encode('deadbeef'.sha1($_SERVER['SERVER_NAME'])); + $this->user = $user; + $this->password = $password; + $this->passphrase = $passphrase; + $this->cURL = curl_init(); + } + + function __destruct() { + curl_close($this->cURL); + } + + function request($_url,$_data){ + $_data['device_id'] = $this->connectorID; + if($this->client_key != '') + $_data['client_key'] = $this->client_key; + + print_r($_data); + curl_setopt($this->cURL, CURLOPT_URL, $_url); + //curl_setopt($this->cURL, CURLOPT_PORT , 443); + curl_setopt($this->cURL, CURLOPT_VERBOSE, 0); + //curl_setopt($this->cURL, CURLOPT_HEADER, 0); + //curl_setopt($this->cURL, CURLOPT_SSLVERSION, 3); + //curl_setopt($this->cURL, CURLOPT_SSLCERT, getcwd() . "/client.pem"); + //curl_setopt($this->cURL, CURLOPT_SSLKEY, getcwd() . "/keyout.pem"); + //curl_setopt($this->cURL, CURLOPT_CAINFO, getcwd() . "/ca.pem"); + + curl_setopt($this->cURL, CURLOPT_SSL_VERIFYPEER, 1); + curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($this->cURL, CURLOPT_POST, true); + //curl_setopt($this->cURL, CURLOPT_BINARYTRANSFER, true); + curl_setopt($this->cURL, CURLOPT_POSTFIELDS,$_data); + curl_setopt($this->cURL, CURLOPT_HTTPHEADER, array("Accept: application/json, text/plain, */*","Connection: keep-alive","Keep-Alive: timeout=5, max=100")); + + curl_setopt( $this->cURL, CURLOPT_COOKIESESSION, true ); + curl_setopt( $this->cURL, CURLOPT_COOKIEJAR, './hermineConnectCookie.txt' ); + curl_setopt( $this->cURL, CURLOPT_COOKIEFILE, './hermineConnectCookie.txt' ); + + $data = curl_exec($this->cURL); + if(!curl_errno($this->cURL)){ + $data = json_decode($data); + if($data->status->value != "OK") + print_r($data); + $info = curl_getinfo($this->cURL); + return $data->payload;// 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url']; + } else { + return false; //echo 'Curl error: ' . curl_error($cURL); + } + + + } + + function open_private_key(){ + $data = []; + $response = $this->request($this->hermineServer."/security/get_private_key",$data); + print_r($response->keys->private_key); + //$privkey_decoded = @openssl_pkey_get_private($private_key, $this->passphrase); + } + /*open_private_key(self, encryption_password): + data = self._post("security/get_private_key", data={}) + private_key_field = json.loads(data["keys"]["private_key"]) + # there might be an unescaping bug here.... + self.private_key = Crypto.PublicKey.RSA.import_key( + private_key_field["private"], passphrase=encryption_password + ) */ + + + public function getID(){ + return $this->connectorID; + } + public function login(){ + $data = [ + "email" => $this->user, + "password" => $this->password, + "app_name" => 'hermine@thw-php:1.1-browser-0', + "encrypted" => true, + "callable" => true + ]; + + $response = $this->request($this->hermineServer."/auth/login",$data); + if($response !== false){ + $this->client_key = $response->client_key; + $this->user_id = $response->userinfo->id; + $this->open_private_key(); + return $response; + } + } + + public function get_companies(){ + $data = [ + "no_cache" => true + ]; + + $response = $this->request($this->hermineServer."/company/member",$data); + if($response !== false){ + return $response; + }else{ + return false; + } + } + } + + +$test = new hermineConnect('xxx@n4h.de','xxx','xxxx'); +echo $test->getID(); +$test->login(); +print_r($test); +//print_r($test->get_companies()); +?> +