Version 2.0 250406 Update auf neue API

This commit is contained in:
2025-04-06 12:57:55 +02:00
parent a8238c811c
commit 04bf5adb1e

View File

@@ -11,44 +11,39 @@
*08.03.2025 -> Anpassungen sendHeaders für Clouflare Erkennung
Inspiriert durch https://github.com/oscarminus/steinapi/blob/main/steinapi.py
Version 2.0 250406 Update auf neue API
*/
class steinAPPConnect{
private $VERSION = '1.4';
private $VERSION = '2.0';
private $MACHINENAME = 'default';
private $ServerURL = 'https://stein.app';
private $APIURL = '/api/api';
private $APIKey = '';
private $APIURL = '/api/api/ext';
private $APIToken = '';
private $businessunit = "";
private $user = "";
private $password = "";
private $userinfo = "";
private $userinfo;
private $cURL;
private $lasterror;
private $lastReturn = "";
//private $debug = true;
function __construct($user,$password) {
function __construct($APIToken) {
if(!isset($_SERVER['SERVER_NAME'])){ //wenn php ohne Apache aufgerufen wird ...
exec("hostname",$ret);
$_SERVER['SERVER_NAME'] = implode(" ",$ret);
}
$this->user = $user;
$this->password = $password;
$this->APIToken = $APIToken;
$this->cURL = curl_init();
$this->MACHINENAME = $_SERVER['SERVER_NAME'];
$this->_init();
if($this->APIKey != ''){
if(!$this->_check()){
$this->_login();
}
}
}
function __destruct() {
curl_close($this->cURL);
}
function request($_url,$_data,$_covertFromJson=true){
function request($_url,$_data,$_request='',$_covertFromJson=true){
$headers = array(
"Keep-Alive: timeout=5, max=100",
@@ -56,27 +51,35 @@ class steinAPPConnect{
);
$headers[] = "authority: stein.app";
$headers[] = "accept-language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7";
$headers[] = "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36";
$headers[] = "user-agent: N-Tools.de/THW-Alzenau PHP-Connector V".$this->VERSION;
if($this->APIKey != ''){
$headers[] = "accept: application/json, text/plain, */*";
$headers[] = "content-type: application/json";
$headers[] = "Pragma: no-cache";
$headers[] = "Cache-Control: no-cache, no-store";
$headers[] = "X-API-KEY: ".$this->APIKey;
}
$headers[] = "accept: application/json";
$headers[] = "content-type: application/json";
$headers[] = "Pragma: no-cache";
$headers[] = "Cache-Control: no-cache, no-store";
$headers[] = "Authorization: Bearer ".$this->APIToken;
curl_setopt($this->cURL, CURLOPT_URL, $_url);
curl_setopt($this->cURL, CURLOPT_VERBOSE, 0);
curl_setopt($this->cURL, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, 1);
//wenn Daten, dann als POST anhängen
if(count($_data) > 0){
curl_setopt($this->cURL, CURLOPT_POST, true);
curl_setopt($this->cURL, CURLOPT_POSTFIELDS,json_encode($_data));
}else{
curl_setopt($this->cURL, CURLOPT_POST, false);
}
//wenn nicht POST oder GET ...
if($_request != ''){
curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST, $_request);
}else{
if(count($_data) > 0){
curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST,'POST');
}else{
curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST,'GET');
}
}
curl_setopt($this->cURL, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->cURL, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->cURL, CURLOPT_COOKIESESSION, true );
@@ -84,13 +87,15 @@ class steinAPPConnect{
curl_setopt($this->cURL, CURLINFO_HEADER_OUT, true);
if(isset($this->debug))
echo "\n\nsenddata->:\n".print_r($_data,true);
echo "<pre>\n\nsenddata->:\n".print_r($_data,true)."</pre>";
$data = curl_exec($this->cURL);
if(!curl_errno($this->cURL)){
$this->lastReturn = print_r($data,true);
if(isset($this->debug)){
echo "\n\nSendheader->:\n".print_r(curl_getinfo($this->cURL, CURLINFO_HEADER_OUT),true);
echo "\n\nretdata->:\n".substr(print_r($data,true),0,8192);
echo "<pre>\n\nSendheader->:\n".print_r(curl_getinfo($this->cURL, CURLINFO_HEADER_OUT),true)."</pre>";
echo "<pre>\n\nretdata->:\n".substr(print_r($data,true),0,8192)."</pre>";
file_put_contents('./debug.log',print_r($data,true),FILE_APPEND);
}
if($_covertFromJson){
$data = json_decode($data);
@@ -106,33 +111,17 @@ class steinAPPConnect{
}
function _init(){
return true;
}
public function _get_UserName(){
if(property_exists($this->userinfo,'name'))
return $this->userinfo->name;
return false;
}
function _init(){
$data = [];
$response = $this->request($this->ServerURL."",$data,false);
if($response !== false){
preg_match('/<*script[^>]*src *= *["\']?([^"\']*)/i', $response, $matches, PREG_OFFSET_CAPTURE);
if(isset($matches[1][0])){
$response = $this->request($this->ServerURL.$matches[1][0],$data,false);
if($response !== false){
preg_match('/headers\.common\[\"X-API-KEY\"]=\"(\w+)\"/', $response, $matches, PREG_OFFSET_CAPTURE);
if(isset($matches[1][0])){
$this->APIKey = $matches[1][0];
return true;
}
}
}
}
return false;
}
function _check(){
public function _check(){
$response = $this->request($this->ServerURL.$this->APIURL."/userinfo",[]);
if($response !== false){
if(property_exists($response,'name')){
@@ -144,35 +133,28 @@ class steinAPPConnect{
return false;
}
function _login(){
$response = $this->request($this->ServerURL.$this->APIURL."/login_check",["username" => $this->user, "password" => $this->password]);
if($response !== false){
$this->_check();
}
return false;
public function _getLastResult(){
return $this->lastReturn;
}
public function get_basics(){
$response = $this->request($this->ServerURL.$this->APIURL."/app/data",[]);
if($response !== false){
return $response;
}
return false;
public function _getLastError(){
return $this->lasterror;
}
public function get_assets(){
$response = $this->request($this->ServerURL.$this->APIURL."/assets?buIds=".$this->businessunit,[]);
$response = $this->request($this->ServerURL.$this->APIURL."/assets/?buIds=".$this->businessunit,[]);
if($response !== false){
return $response;
}
return false;
}
/* funktioniert mit der neuen API (noch) nicht
public function get_reports(){
$response = $this->request($this->ServerURL.$this->APIURL."/reports?buIds=".$this->businessunit,[]);
$response = $this->request($this->ServerURL.$this->APIURL."/reports/?buIds=".$this->businessunit,[]);
if($response !== false){
return $response;
}
return false;
}
}*/
}
?>