Files
steinAPPconnect/steinAPPConnect.php

191 lines
5.3 KiB
PHP

<?php
/*
Nenninger N-Tools.de
Version 1.2
*16.03.2024 -> Anpassung an Update 21
Version 1.3
*20.08.2024 -> UTF-8 Zeichen decordiert
Version 1.4
*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
Version 2.1 250406 get_asset, set_asset, set_assetStatus eingebaut
Version 2.2 260725 PHP 8.5 -> curl_close is deprecated
*/
class steinAPPConnect{
private $VERSION = '2.2';
private $MACHINENAME = 'default';
private $ServerURL = 'https://stein.app';
private $APIURL = '/api/api/ext';
private $APIToken = '';
private $businessunit = "";
private $userinfo;
private $cURL;
private $lasterror;
private $lastReturn = "";
//private $debug = true;
function __construct($APIToken) {
if(!isset($_SERVER['SERVER_NAME'])){ //wenn php ohne Apache aufgerufen wird ...
exec("hostname",$ret);
$_SERVER['SERVER_NAME'] = implode(" ",$ret);
}
$this->APIToken = $APIToken;
$this->cURL = curl_init();
$this->MACHINENAME = $_SERVER['SERVER_NAME'];
$this->_init();
}
function __destruct() {
//curl_close($this->cURL);
}
function request($_url,$_data,$_request='',$_covertFromJson=true){
$headers = array(
"Keep-Alive: timeout=5, max=100",
"Connection: keep-alive"
);
$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: N-Tools.de/THW-Alzenau PHP-Connector V".$this->VERSION;
$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 );
curl_setopt($this->cURL, CURLOPT_COOKIEFILE, NULL );
curl_setopt($this->cURL, CURLINFO_HEADER_OUT, true);
if(isset($this->debug))
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 "<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);
return $data;
}else{
return $data;
}
} else {
$this->lasterror = 'Curl error: ' . curl_error($this->cURL);
return false;
}
}
function _init(){
return true;
}
public function _get_UserName(){
if(property_exists($this->userinfo,'name'))
return $this->userinfo->name;
return false;
}
public function _check(){
$response = $this->request($this->ServerURL.$this->APIURL."/userinfo",[]);
if($response !== false){
if(property_exists($response,'name')){
$this->userinfo = $response;
$this->businessunit = $this->userinfo->scopeRole->entityId;
return true;
}
}
return false;
}
public function _getLastResult(){
return $this->lastReturn;
}
public function _getLastError(){
return $this->lasterror;
}
public function get_assets(){
$response = $this->request($this->ServerURL.$this->APIURL."/assets/?buIds=".$this->businessunit,[]);
if($response !== false){
return $response;
}
return false;
}
public function get_asset($_id){
$response = $this->request($this->ServerURL.$this->APIURL."/assets/".$_id,[]);
if($response !== false){
return $response;
}
return false;
}
public function set_asset($_id,$_asset,$_withNotifyRadio=false){
$notifyRadio = '';
if($_withNotifyRadio)
$notifyRadio = '?notifyRadio=1';
$response = $this->request($this->ServerURL.$this->APIURL."/assets/".$_id.$notifyRadio,$_asset,'PATCH');
if($response !== false){
return $response;
}
return false;
}
public function set_assetStatus($_id,$_status,$_withNotifyRadio=true){
$asset = $this->get_asset($_id);
if($asset !== false){
$_withNotifyRadio = $_withNotifyRadio && !empty($asset->issi) && (strlen($asset->issi) == 7);
$asset->status = $_status;
return $this->set_asset($_id, (array)$asset, $_withNotifyRadio);
}
return false;
}
/* funktioniert mit der neuen API (noch) nicht
public function get_reports(){
$response = $this->request($this->ServerURL.$this->APIURL."/reports/?buIds=".$this->businessunit,[]);
if($response !== false){
return $response;
}
return false;
}*/
}
?>