diff --git a/hermineConnect.php b/hermineConnect.php index 7bd4d95..efc9e5d 100644 --- a/hermineConnect.php +++ b/hermineConnect.php @@ -1,13 +1,34 @@ 8.1 hermine@THW PHPConnector inspiriert von https://gitlab.com/aeberhardt/stashcat-api-client - Beispiel: + curl und php >= 7.4 + + Funktionen: + + get_last_error(); + + + login(soll der Login gespeichert werden); + + get_companies_list(); -> List der Firmen (wird beim Login erstellt) + get_conversations_list(); -> List der Konversationen (wird beim Login erstellt) + get_channels_list(); -> List der Kanäle (1. Firma) (wird beim Login erstellt) + + send_message_to_conversation(id,Nachricht,optional URLs als array, optional Standort als Array); + send_message_to_channel(id,Nachricht,optional URLs als array, optional Standort als Array); + + send_message_with_file_to_conversation(id,Nachricht,Dateiinhalt (max 5MB),Dateiname in Hermine,Type Bsp:'image/jpeg',Breite Standard 0,Höhe Standard 0); + send_message_with_file_to_channel(id,Nachricht,Dateiinhalt (max 5MB),Dateiname in Hermine,Type Bsp:'image/jpeg',Breite Standard 0,Höhe Standard 0); + + get_channel_messages(id, $_limit=50, $_offset=0); -< array + get_conversation_messages(id,limit=50,offset=0); -< array + + Beispiele: $hermine = new hermineConnect('mailadresse','Accountpassword','Verschlüsselungskennwort'); if($hermine->login(true) !== false){ @@ -18,15 +39,19 @@ $array = $hermine->get_channels_list(); print_r($array); - $file = file_get_contents('./ich.jpg'); + $file = file_get_contents('./test.jpg'); $hermine->send_message_with_file_to_channel(165562,'jpg per php',$file,'test.jpg','image/jpeg',1080,2068); - send_message_to_conversation(id,message); - send_message_to_channel(id,message); + $array = $hermine->get_channel_messages(165562); + foreach($array as $msg){ + echo $msg->text."\n"; + } + + $array = $hermine->search_user('Nennin'); }else{ //login fehlgeschlagen - print_r($hermine->lasterror); + print_r($hermine->get_last_error); } */ @@ -144,7 +169,6 @@ class UUID { class hermineConnect{ private $VERSION = '1.3'; private $hermineServer = 'https://api.thw-messenger.de'; - //private $hermineServer = 'https://thw.n-tools.de'; private $connectorID = ""; private $user = ""; private $password = ""; @@ -159,31 +183,27 @@ class hermineConnect{ private $companies; private $channels; private $conversations; - //private $debug = true; + /* Hilfsfunktion für multipart/form-data; */ function build_data_files($boundary, $fields, $files){ $data = ''; $eol = "\r\n"; - $delimiter = '-------------' . $boundary; - foreach ($fields as $name => $content) { - $data .= "--" . $delimiter . $eol - . 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol - . $content . $eol; + $data .= "--" . $delimiter . $eol + . 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol + . $content . $eol + ; } - - foreach ($files as $name => $content) { - $data .= "--" . $delimiter . $eol - . 'Content-Disposition: form-data; name="file"; filename="[object Object]"' . $eol - . 'Content-Type: application/octet-stream'.$eol - //. 'Content-Transfer-Encoding: binary'.$eol - ; + $data .= "--" . $delimiter . $eol + . 'Content-Disposition: form-data; name="file"; filename="[object Object]"' . $eol + . 'Content-Type: application/octet-stream'.$eol + ; - $data .= $eol; - $data .= $content . $eol; + $data .= $eol; + $data .= $content . $eol; } $data .= "--" . $delimiter . "--".$eol; @@ -191,7 +211,6 @@ class hermineConnect{ return $data; } - function __construct($user,$password,$passphrase) { function intArrayToString($ia){ $ret = ''; @@ -221,8 +240,7 @@ class hermineConnect{ $boundary = uniqid(); $delimiter = '-------------' . $boundary; $_data = $this->build_data_files($boundary, $_data, $_files); - - + curl_setopt($this->cURL, CURLOPT_URL, $_url); curl_setopt($this->cURL, CURLOPT_VERBOSE, 0); curl_setopt($this->cURL, CURLOPT_SSL_VERIFYPEER, 1); @@ -248,8 +266,8 @@ class hermineConnect{ $data = curl_exec($this->cURL); if(!curl_errno($this->cURL)){ if(isset($this->debug)){ - echo "\n\nretdata->:\n".print_r($data,true); echo "\n\nSendheader->:\n".print_r(curl_getinfo($this->cURL, CURLINFO_HEADER_OUT),true); + echo "\n\nretdata->:\n".print_r($data,true); } $data = json_decode($data); if($data->status->value === "OK"){ @@ -266,7 +284,7 @@ class hermineConnect{ } - + function _open_private_key(){ $data = []; $response = $this->request($this->hermineServer."/security/get_private_key",$data); @@ -279,7 +297,7 @@ class hermineConnect{ return false; } } - + function _get_conversation_key($_target){ if($_target[0]=='conversation'){ if(!isset($this->key_cache_conversations[$_target[1]])){ @@ -307,64 +325,21 @@ class hermineConnect{ return false; } } - + function _encrypt_aes($_plain, $_key, $_iv){ $ret = openssl_encrypt($_plain, "AES-256-CBC", $_key, 0, $_iv); - print_r($ret); return $ret; } - public function getLastError(){ - return $this->lasterror; + function _decrypt_aes($_crypt, $_key, $_iv){ + if(strlen($_crypt) == 0) + return '[Dieser Inhalt wurde gelöscht.]'; + $ret = openssl_decrypt($_crypt, "AES-256-CBC", $_key, 0, $_iv); + if($ret === false) + return '[!!ERROR DeCrypt] -> '.openssl_error_string(); + return $ret; } - public function getID(){ - return $this->connectorID; - } - - public function login($_saveLogin=false){ - if($_saveLogin){ - if(file_exists('./login.dat')){ - $savelogin = json_decode(file_get_contents('./login.dat')); - $this->client_key = $savelogin->ClientKey; - $this->connectorID = $savelogin->DeviceId; - $this->user_id = $savelogin->UserID; - if($this->_open_private_key()){ - $this->companies = $this->get_companies(); - $this->channels = $this->get_channels($this->companies->companies[0]->id); - $this->conversations = $this->get_conversations(); - return true; - } - } - } - $data = [ - "email" => $this->user, - "password" => $this->password, - "app_name" => 'hermine@thw-PHP:'.$this->VERSION, - "encrypted" => true, - "callable" => false - ]; - - $response = $this->request($this->hermineServer."/auth/login",$data); - if($response !== false){ - $this->client_key = $response->client_key; - $this->user_id = $response->userinfo->id; - if($this->_open_private_key()){ - $this->companies = $this->get_companies(); - $this->channels = $this->get_channels($this->companies->companies[0]->id); - $this->conversations = $this->get_conversations(); - if($_saveLogin){ - $savelogin['ClientKey'] = $this->client_key; - $savelogin['DeviceId'] = $this->connectorID; - $savelogin['UserID'] = $this->user_id; - file_put_contents('./login.dat',json_encode($savelogin)); - } - return true; - } - } - return false; - } - function get_companies(){ $data = [ "no_cache" => true @@ -407,31 +382,7 @@ class hermineConnect{ } } - public function get_companies_list(){ - $ret = []; - foreach($this->companies->companies as $company){ - $ret[$company->id] = $company->name; - } - return $ret; - } - - public function get_conversations_list(){ - $ret = []; - foreach($this->conversations->conversations as $conversation){ - $ret[$conversation->id] = $conversation->members[0]->last_name; - } - return $ret; - } - - public function get_channels_list(){ - $ret = []; - foreach($this->channels->channels as $channels){ - $ret[$channels->id] = $channels->name; - } - return $ret; - } - - function sendmsg($_target,$_message,$_files=[],$_location=false,$_encrypted=true){ + function sendmsg($_target,$_message,$_files=[],$_url=[],$_location=NULL){ $iv = openssl_random_pseudo_bytes(16); $conversation_key = $this->_get_conversation_key($_target); $data = [ @@ -440,24 +391,16 @@ class hermineConnect{ "text" => bin2hex(base64_decode($this->_encrypt_aes($_message, $conversation_key, $iv))), "iv" => bin2hex($iv), "files" => json_encode($_files), //Nummern vom Upload! - "url" => "[]", + "url" => json_encode($_url), "type" => "text", "verification" => "", "encrypted" => true ]; - if(!$_encrypted){ - $data['encrypted'] = false; - $data['text'] = $_message; - unset($data[$iv]); - } - - if($_location !== false){ - if(!$_encrypted){ - - } - } - print_r($data); + if(!is_null($_location)){ + $data["latitude"] = bin2hex(base64_decode($this->_encrypt_aes(strval($_location[0]), $conversation_key, $iv))); + $data["longitude"] = bin2hex(base64_decode($this->_encrypt_aes(strval($_location[1]), $conversation_key, $iv))); + } $response = $this->request($this->hermineServer."/message/send",$data); if($response !== false){ return $response; @@ -466,24 +409,14 @@ class hermineConnect{ } } - //function uploadfile($_target,$_filerawdatat,$_filename,$_content_type="application/octet-stream",$_media_size=NULL,$_encrypted=false){ function uploadfile($_target,$_fileraw,$_filename,$_filetype,$_mediawidth=0,$_mediaheight=0){ $iv = openssl_random_pseudo_bytes(16); $file_key = openssl_random_pseudo_bytes(32); - - //$file = file_get_contents($_filewithpath); - $file_uuid = UUID::v4(); - $chunk_size = 5 * 1024 * 1024; $filesize = strlen($_fileraw); - - //$file = new \CURLStringFile($_fileraw, $_filename, $_filetype); - $file_encryptet = base64_decode($this->_encrypt_aes($_fileraw, $file_key, $iv)); - $file = ['dummy' => $file_encryptet]; - $data = [ "resumableChunkNumber" => 0, "resumableChunkSize" => $chunk_size, @@ -522,8 +455,138 @@ class hermineConnect{ } } - public function send_message_to_conversation($_conversation_id,$_message){ - return $this->sendmsg(array('conversation',$_conversation_id),$_message); + function get_messages($_source, $_limit=30, $_offset=0){ + $conversation_key = $this->_get_conversation_key($_source); + $data = [ + $_source[0]."_id" => $_source[1], + "source" => $_source[0], + "limit" => $_limit, + "offset" => $_offset, + ]; + + $response = $this->request($this->hermineServer."/message/content",$data); + $ret = []; + if($response !== false){ + foreach($response->messages as $message){ + if($message->kind == "message"){ + if($message->encrypted){ + $encryptet = $this->_decrypt_aes(base64_encode(hex2bin($message->text)), $conversation_key, hex2bin($message->iv)); + if($encryptet !== false) + $message->text = $encryptet; + else + $message->text = '[!!decrypterror!!]'; + } + if($message->location->encrypted){ + $encryptet = $this->_decrypt_aes(base64_encode(hex2bin($message->location->latitude)), $conversation_key, hex2bin($message->location->iv)); + if($encryptet !== false) + $message->location->latitude = $encryptet; + else + $message->location->latitude = '[!!decrypterror!!]'; + $encryptet = $this->_decrypt_aes(base64_encode(hex2bin($message->location->longitude)), $conversation_key, hex2bin($message->location->iv)); + if($encryptet !== false) + $message->location->longitude = $encryptet; + else + $message->location->longitude = '[!!decrypterror!!]'; + } + $ret[] = $message; + } + } + return $ret; + }else{ + return false; + } + } + +/* + Öffentliche Funktionen +*/ + + /* + Ausgabe Fehlermeldung von Hermine + */ + public function get_last_error(){ + return $this->lasterror; + } + + /* + Ausgabe der ID (wird aus eigenem Servername ermittelt) + */ + public function get_connector_id(){ + return $this->connectorID; + } + + /* + Login, wenn $_saveLogin=true, dann wird device_id und client_key in login.dat gespeichert und es erfolgt keine Mitteilung in Hermine + */ + public function login($_saveLogin=false){ + if($_saveLogin){ + if(file_exists('./login.dat')){ + $savelogin = json_decode(file_get_contents('./login.dat')); + $this->client_key = $savelogin->ClientKey; + $this->connectorID = $savelogin->DeviceId; + $this->user_id = $savelogin->UserID; + if($this->_open_private_key()){ + $this->companies = $this->get_companies(); + $this->channels = $this->get_channels($this->companies->companies[0]->id); + $this->conversations = $this->get_conversations(); + return true; + } + } + } + $data = [ + "email" => $this->user, + "password" => $this->password, + "app_name" => 'hermine@thw-PHP:'.$this->VERSION, + "encrypted" => true, + "callable" => false + ]; + + $response = $this->request($this->hermineServer."/auth/login",$data); + if($response !== false){ + $this->client_key = $response->client_key; + $this->user_id = $response->userinfo->id; + if($this->_open_private_key()){ + $this->companies = $this->get_companies(); + $this->channels = $this->get_channels($this->companies->companies[0]->id); + $this->conversations = $this->get_conversations(); + if($_saveLogin){ + $savelogin['ClientKey'] = $this->client_key; + $savelogin['DeviceId'] = $this->connectorID; + $savelogin['UserID'] = $this->user_id; + file_put_contents('./login.dat',json_encode($savelogin)); + } + return true; + } + } + return false; + } + + public function get_companies_list(){ + $ret = []; + foreach($this->companies->companies as $company){ + $ret[$company->id] = $company->name; + } + return $ret; + } + + public function get_conversations_list(){ + $ret = []; + foreach($this->conversations->conversations as $conversation){ + $ret[$conversation->id] = $conversation->members[0]->first_name.' '.$conversation->members[0]->last_name; + } + return $ret; + } + + public function get_channels_list(){ + $ret = []; + foreach($this->channels->channels as $channels){ + $ret[$channels->id] = $channels->name; + } + return $ret; + } + + public function send_message_to_conversation($_conversation_id,$_message,$_url=[],$_location=NULL){ + return $this->sendmsg(array('conversation',$_conversation_id),$_message,[],$_url,$_location); } public function send_message_with_file_to_conversation($_conversation_id,$_message,$_fileraw,$_filename,$_filetype,$_mediawidth=0,$_mediaheight=0){ @@ -531,14 +594,44 @@ class hermineConnect{ return $this->sendmsg(array('conversation',$_conversation_id),$_message,[$ret]); } - public function send_message_to_channel($_channel_id,$_message){ - return $this->sendmsg(array('channel',$_channel_id),$_message); + public function send_message_to_channel($_channel_id,$_message,$_url=[],$_location=NULL){ + return $this->sendmsg(array('channel',$_channel_id),$_message,[],$_url,$_location); } public function send_message_with_file_to_channel($_channel_id,$_message,$_fileraw,$_filename,$_filetype,$_mediawidth=0,$_mediaheight=0){ $ret = $this->uploadfile(array('channel',$_channel_id),$_fileraw,$_filename,$_filetype,$_mediawidth,$_mediaheight); return $this->sendmsg(array('channel',$_channel_id),$_message,[$ret]); } + + public function get_conversation_messages($_conversation_id, $_limit=30, $_offset=0){ + return $this->get_messages(array('conversation',$_conversation_id),$_limit,$_offset); + } + + public function get_channel_messages($_channel_id){ + return $this->get_messages(array('channel',$_channel_id),$_limit,$_offset); + } + + public function search_user($_searchby, $_limit=50, $_offset=0){ + $data = [ + "limit" => $_limit, + "offset" => $_offset, + "key_hashes" => false, + "search" => $_searchby, + "sorting" => ["first_name_asc", "last_name_asc"], + "exclude_user_ids" => [], + "group_ids" => [], + ]; + + $response = $this->request($this->hermineServer."/users/listing",$data); + if($response !== false){ + return $response->users; + }else{ + return false; + } + } + + + } ?> diff --git a/hermineConnect.zip b/hermineConnect.zip new file mode 100644 index 0000000..dce72b7 Binary files /dev/null and b/hermineConnect.zip differ diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..5354f7b --- /dev/null +++ b/readme.txt @@ -0,0 +1,42 @@ +Nenninger N-Tools.de + Version 1.5 + + hermine@THW PHPConnector + inspiriert von https://gitlab.com/aeberhardt/stashcat-api-client + + curl und php >= 7.4 + + Funktionen: + + get_last_error(); + + + login(soll der Login gespeichert werden); + + get_companies_list(); -> List De Firmen (wird beim Login erstellt) + + send_message_to_conversation(id,Nachricht,optional URLs als array, optional Standort als Array); + send_message_to_channel(id,Nachricht,optional URLs als array, optional Standort als Array); + + send_message_with_file_to_channel(id,Nachricht,Dateiinhalt (max 5MB),Dateiname in Hermine,Type Bsp:'image/jpeg',Breite Standard 0,Höhe Standard 0); + + Beispiele: + + $hermine = new hermineConnect('mailadresse','Accountpassword','Verschlüsselungskennwort'); + if($hermine->login(true) !== false){ + $array = $hermine->get_companies_list(); + print_r($array); + $array = $hermine->get_conversations_list(); + print_r($array); + $array = $hermine->get_channels_list(); + print_r($array); + + $file = file_get_contents('./ich.jpg'); + $hermine->send_message_with_file_to_channel(165562,'jpg per php',$file,'test.jpg','image/jpeg',1080,2068); + + + + }else{ + //login fehlgeschlagen + print_r($hermine->get_last_error); + } \ No newline at end of file