Compare commits

...

10 Commits

2 changed files with 133 additions and 53 deletions

View File

@@ -1,7 +1,7 @@
<?php
/*
Nenninger N-Tools.de
Version 1.13
Version 1.18
*/
@@ -116,7 +116,7 @@ class UUID {
class hermineConnect{
private $VERSION = '1.13';
private $VERSION = '1.18';
private $MACHINENAME = 'default';
private $hermineServer = 'https://api.thw-messenger.de';
private $connectorID = "";
@@ -188,7 +188,7 @@ class hermineConnect{
curl_close($this->cURL);
}
function request($_url,$_data,$_files=[]){
function request($_url,$_data,$_files=[],$_rowData=false){
$_data['device_id'] = $this->connectorID;
if($this->client_key != '')
$_data['client_key'] = $this->client_key;
@@ -211,8 +211,8 @@ class hermineConnect{
'Expect:'
));
curl_setopt( $this->cURL, CURLOPT_COOKIESESSION, true );
curl_setopt( $this->cURL, CURLOPT_COOKIEJAR, './hermineConnectCookie.txt' );
curl_setopt( $this->cURL, CURLOPT_COOKIEFILE, './hermineConnectCookie.txt' );
curl_setopt( $this->cURL, CURLOPT_COOKIEJAR, __DIR__.'/data/hermineConnectCookie.txt' );
curl_setopt( $this->cURL, CURLOPT_COOKIEFILE, __DIR__.'/data/hermineConnectCookie.txt' );
curl_setopt( $this->cURL, CURLINFO_HEADER_OUT, true);
if(isset($this->debug))
@@ -224,13 +224,17 @@ class hermineConnect{
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"){
$this->lasterror = '';
return $data->payload;
if(!$_rowData){
$data = json_decode($data);
if($data->status->value === "OK"){
$this->lasterror = '';
return $data->payload;
}else{
$this->lasterror = $data;
return false;
}
}else{
$this->lasterror = $data;
return false;
return $data;
}
} else {
$this->lasterror = 'Curl error: ' . curl_error($this->cURL);
@@ -278,31 +282,38 @@ class hermineConnect{
}
}
function _get_conversation_key($_target){
if($_target[0]=='conversation'){
if(!isset($this->key_cache_conversations[$_target[1]])){
$data = [ "conversation_id" => $_target[1]
];
$response = $this->request($this->hermineServer."/message/conversation",$data);
$this->key_cache_conversations[$_target[1]] = $response->conversation->key;
}
if(openssl_private_decrypt(base64_decode($this->key_cache_conversations[$_target[1]]),$ret,$this->private_key,OPENSSL_PKCS1_OAEP_PADDING))
return $ret;
else
return false;
}else if($_target[0]=='channel'){
if(!isset($this->key_cache_channels[$_target[1]])){
$data = [ "channel_id" => $_target[1],
"without_members" => true
];
$response = $this->request($this->hermineServer."/channels/info",$data);
$this->key_cache_channels[$_target[1]] = $response->channels->key;
function _get_conversation_key($_target,$_key=''){
if($_key == ''){
if($_target[0]=='conversation'){
if(!isset($this->key_cache_conversations[$_target[1]])){
$data = [ "conversation_id" => $_target[1]
];
$response = $this->request($this->hermineServer."/message/conversation",$data);
$this->key_cache_conversations[$_target[1]] = $response->conversation->key;
}
if(openssl_private_decrypt(base64_decode($this->key_cache_conversations[$_target[1]]),$ret,$this->private_key,OPENSSL_PKCS1_OAEP_PADDING))
return $ret;
else
return false;
}else if($_target[0]=='channel'){
if(!isset($this->key_cache_channels[$_target[1]])){
$data = [ "channel_id" => $_target[1],
"without_members" => true
];
$response = $this->request($this->hermineServer."/channels/info",$data);
$this->key_cache_channels[$_target[1]] = $response->channels->key;
}
if(openssl_private_decrypt(base64_decode($this->key_cache_channels[$_target[1]]),$ret,$this->private_key,OPENSSL_PKCS1_OAEP_PADDING))
return $ret;
else
return false;
}
if(openssl_private_decrypt(base64_decode($this->key_cache_channels[$_target[1]]),$ret,$this->private_key,OPENSSL_PKCS1_OAEP_PADDING))
return $ret;
else
return false;
}else{
if(openssl_private_decrypt(base64_decode($_key),$ret,$this->private_key,OPENSSL_PKCS1_OAEP_PADDING))
return $ret;
else
return false;
}
}
@@ -374,7 +385,8 @@ class hermineConnect{
"url" => json_encode($_url),
"type" => "text",
"verification" => "",
"encrypted" => true
"encrypted" => true,
"metainfo" => '{"v":1,"style":"md"}'
];
if(!is_null($_location)){
@@ -460,17 +472,19 @@ class hermineConnect{
$message->text = '[Dieser Inhalt wurde gelöscht.]';
}
}
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!!]';
if(isset($message->location->encrypted)){
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;
}
@@ -483,13 +497,16 @@ class hermineConnect{
function _get_channel_infos($_id){
$data = [
"channel_id" => $_id,
"without_members" => false
"channel_id" => $_id
];
$response = $this->request($this->hermineServer."/channels/info",$data);
/*30.10.2024 seit 6.8 sind die Mitglieder nicht mehr bestand der Abfrage und müssen gesondert geholt werden*/
$data['limit'] = 1000;
$members = $this->request($this->hermineServer."/channels/members",$data);
if($response !== false){
$response->channels->members = $members->members;
return $response->channels;
}else{
return false;
@@ -516,6 +533,48 @@ class hermineConnect{
}
}
function _file_info($_id){
$data = [
"file_id" => $_id
];
$response = $this->request($this->hermineServer."/file/info",$data);
if($response !== false){
return $response;
}else{
return false;
}
}
function _download_file($_id){
$fileInfo = $this->_file_info($_id);
//print_r($fileInfo);
$response = $this->request($this->hermineServer."/file/download?id=".$_id,[],[],true);
$ret = [];
if($response !== false){
$ret[0]['name'] = $fileInfo->file->name;
$ret[0]['mime'] = $fileInfo->file->mime;
if($fileInfo->file->encrypted){
$key = $fileInfo->file->keys[0];
$conversation_key = $this->_get_conversation_key([$key->type,$key->chat_id],$key->chat_key);
$decryptedkey = $this->_decrypt_aes(base64_encode(hex2bin($key->key)), $conversation_key, hex2bin($key->iv));
$decrypted = $this->_decrypt_aes(base64_encode($response), $decryptedkey, hex2bin($fileInfo->file->e2e_iv));
if($decrypted !== false)
$ret[1] = $decrypted;
else
return '[!!decrypterror!!]';
}else{
$ret[1] = $response;
}
}else{
$ret = false;
}
return $ret;
}
function _delete_files($_ids=[]){
$data = [
"file_ids" => json_encode($_ids)
@@ -588,8 +647,8 @@ class hermineConnect{
public function login($_saveLogin=false){
$savelogin = new stdClass();
if($_saveLogin){
if(file_exists('./hermineConnectLogin.dat')){
$savelogin = json_decode(file_get_contents('./hermineConnectLogin.dat'));
if(file_exists(__DIR__.'/data/hermineConnectLogin.dat')){
$savelogin = json_decode(file_get_contents(__DIR__.'/data/hermineConnectLogin.dat'));
$this->client_key = $savelogin->ClientKey;
$this->connectorID = $savelogin->DeviceId;
$this->user_id = $savelogin->UserID;
@@ -624,7 +683,7 @@ class hermineConnect{
$savelogin->ClientKey = $this->client_key;
$savelogin->DeviceId = $this->connectorID;
$savelogin->UserID = $this->user_id;
file_put_contents('./hermineConnectLogin.dat',json_encode($savelogin));
file_put_contents(__DIR__.'/data/hermineConnectLogin.dat',json_encode($savelogin));
}
return true;
}
@@ -713,6 +772,10 @@ class hermineConnect{
return $this->_delete_files($_ids);
}
public function download_file($_id){
return $this->_download_file($_id);
}
public function open_conversation($_members){
return $this->_open_conversation($_members);
}

View File

@@ -1,6 +1,7 @@
!!! DAS SCRIPT IST NUR IN EINER GESICHERTEN UMGEBUNG ZU NUTZEN !!!
Das Speichern von Zugangsdaten auf öffentlichen Servern stellt ein hohes Risiko da!
Nenninger N-Tools.de
Version 1.12
* 20.06.2023 -> neue Parameter und devid darf keine Zeichen sondern nur Buchstaben und Zahlen enthalten
* 13.09.2023 -> cookie Datei und Login.dat umbenannt
@@ -8,7 +9,21 @@
* 20.09.2023 -> get_channel_messages korrigiert (limit, offset)
* 26.09.2023 -> löschen von Dateien eingebaut;
* 31.10.2023 -> Fehler beim speichern der Logins gefixt
* 26.11.2023 -> open_conversation eingebaut
* V1.13 26.11.2023 -> open_conversation eingebaut
* V1.14 -> 231220 sendmsg: Text wird jetzt als Markdown formatiert markiert (Parameter "metainfo")
* V1.15 -> 240208 Die Logindaten und der Keks werden jetzt von der php Datei aus im Unterverzeichnis /data gespeichert (muss von Hand erstellt werden)
* V1.16 -> 240820 neue Funktion "download_file($_id)"
* V1.17 -> 241021 nach Update auf 6.8.0 gibt es bei der Message Location kein "encryptet" mehr (if(isset($message->location->encrypted)) eingebaut)
* V1.18 -> 241030 nach Update auf 6.8.0 sind bei der ChannelInfo keine Mitglieder mehr dabei. Abfrage geändert;
* 12.12.2025 Umzug auf gitea
Formatierungen Markdown:
*fett*
~durchgestrichen~
`monospace`
geschützte Leerzeichen (0x00A0) alt+0160
hermine@THW PHPConnector
inspiriert von https://gitlab.com/aeberhardt/stashcat-api-client
@@ -40,6 +55,8 @@
list_uploaded_chat_files($_search); -< array
download_file($_id) -< array
delete_files($_ids=[]) -< boolean
open_conversation($_members) -< array