<?php
// index.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Api\Client;use Docuform\Api\HttpRequestHandler;use Docuform\Api\RestApi;use Docuform\Api\Exception\ApiHttpBadRequestException;use Docuform\Api\Exception\ApiHttpNotFoundException;use Docuform\Lib\XmlrpcClient;use Psr\Http\Message\ServerRequestInterface;use Slim\Routing\RouteContext;use Docuform\Api\ClassFactory;global $g_loader;require_once '..\..\api_global_v2.php';$g_loader->addPsr4('Docuform\\Api\\Client\\','../V'.(string)(int)RESTAPI_VERSION);class ClientApi extends RestApi{private $m_bulk;private $m_device;private $m_customer;public function checkApiEnabled():bool{global $apiSettings;@include '../apiconfig.php';return!empty($apiSettings['enabled']);}public function checkLicense():void{}public function checkRouteArguments(ServerRequestInterface $request):bool{$result=true;$route=$request->getAttribute(RouteContext::ROUTE);$customerId=$route->getArgument('customerId');if($customerId!==null){$this->m_customer=new Customer($this->getContainer());$this->m_customer->read($customerId);if($this->m_customer->id===0){throw new ApiHttpNotFoundException($request);}}$deviceId=$route->getArgument('deviceId');if($deviceId!==null){if(empty($customerId)){throw new ApiHttpBadRequestException($request);}$this->m_device=new Device($this->getContainer());$this->m_device->read($deviceId);if($this->m_device->id===0){throw new ApiHttpNotFoundException($request);}}$bulkId=$route->getArgument('bulkId');if($bulkId!==null){if(empty($deviceId)){throw new ApiHttpBadRequestException($request);}$this->m_bulk=new Bulk($this->getContainer());$this->m_bulk->read($bulkId);if($this->m_bulk->id===0){throw new ApiHttpNotFoundException($request);}}return $result;}public function getApiName():string{return 'dfmclient';}public function getNamespace():string{return 'Docuform\\Api\\Client\\';}public function getBulk():Bulk{return $this->m_bulk;}public function getDevice():Device{return $this->m_device;}public function getCustomer():Customer{return $this->m_customer;}public function customSetup():void{}public function setupContainer():void{$this->m_container->set('XmlrpcClient',function($container){$cfg=$container->get('cfg');return new XmlrpcClient($cfg['fsm_client']['address'],$cfg['fsm_client']['port']);});$container=$this->m_container;$container->set('classfactory',function($container){return new ClassFactory($container);});}public function setupRoutes():void{$this->routeCustomers();$this->routeDevices();$this->routeDeviceBulks();$this->routeDeviceBulkOids();$this->routeDeviceModel();}private function buildRoute($endpoint):string{return 'v'.(string)(int)RESTAPI_VERSION.'/'.$endpoint;}private function routeCustomers():void{$this->m_app->group($this->buildRoute('customers'),function($app){$app->map(['GET','POST'],'',function($request,$response,$args){return HttpRequestHandler::onRequest($this,'Customers',$request,$response,$args);});});$this->m_app->group($this->buildRoute('customers/{customerId:[0-9]+}'),function($app){$app->map(['GET','DELETE'],'',function($request,$response,$args){return HttpRequestHandler::onRequest($this,'Customers',$request,$response,$args);});});}private function routeDevices():void{$this->m_app->group($this->buildRoute('customers/{customerId}/devices'),function($app){$app->map(['GET','POST'],'',function($request,$response,$args){return HttpRequestHandler::onRequest($this,'Devices',$request,$response,$args);});});$this->m_app->group($this->buildRoute('customers/{customerId}/devices/{deviceId:[0-9]+}'),function($app){$app->map(['GET','DELETE'],'',function($request,$response,$args){return HttpRequestHandler::onRequest($this,'Devices',$request,$response,$args);});});}private function routeDeviceBulks():void{$this->m_app->group($this->buildRoute('customers/{customerId}/devices/{deviceId:[0-9]+}/bulks'),function($app){$app->map(['GET','POST'],'',function($request,$response,$args){return HttpRequestHandler::onRequest($this,'DeviceBulks',$request,$response,$args);});});$this->m_app->group($this->buildRoute('customers/{customerId}/devices/{deviceId:[0-9]+}/bulks/{bulkId:[0-9]+}'),function($app){$app->map(['GET','DELETE'],'',function($request,$response,$args){return HttpRequestHandler::onRequest($this,'DeviceBulks',$request,$response,$args);});});}private function routeDeviceBulkOids():void{$app=$this->m_app;$app->group($this->buildRoute('customers/{customerId}/devices/{deviceId:[0-9]+}/bulks/{bulkId:[0-9]+}/oids'),function($app){$app->map(['GET','PATCH'],'',function($request,$response,$args){return HttpRequestHandler::onRequest($this,'DeviceBulkOids',$request,$response,$args);});});}private function routeDeviceModel():void{$app=$this->m_app;$app->group($this->buildRoute('device-model'),function($app){$app->map(['GET'],'',function($request,$response,$args){return HttpRequestHandler::onRequest($this,'DeviceModel',$request,$response,$args);});});}}RestApi::run(new ClientApi());