From 01880929984a033356b1f122cadddd6c2908ce3d Mon Sep 17 00:00:00 2001 From: idfake Date: Tue, 30 Jan 2024 12:53:08 +0100 Subject: [PATCH 1/2] - allow passing options - enable setting SSL_VERIFYPEER and SSL_CAINFO for curl --- src/Jaspersoft/Client/Client.php | 13 ++++++++----- src/Jaspersoft/Tool/RESTRequest.php | 28 +++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/Jaspersoft/Client/Client.php b/src/Jaspersoft/Client/Client.php index 94759eb..7aab914 100644 --- a/src/Jaspersoft/Client/Client.php +++ b/src/Jaspersoft/Client/Client.php @@ -17,10 +17,10 @@ class Client { private $restReq; private $restUrl2; - protected $hostname; - protected $username; - protected $password; - protected $orgId; + protected $hostname; + protected $username; + protected $password; + protected $orgId; protected $repositoryService; protected $userService; protected $organizationService; @@ -35,13 +35,15 @@ class Client protected $thumbnailService; protected $logCollectorService; protected $serverService; + protected $options; - public function __construct($serverUrl, $username, $password, $orgId = null) + public function __construct($serverUrl, $username, $password, $orgId = null, $options = array()) { $this->serverUrl = $serverUrl; $this->username = $username; $this->password = $password; $this->orgId = $orgId; + $this->options = $options; $this->restReq = new RESTRequest(); if (!empty($this->orgId)) { @@ -49,6 +51,7 @@ public function __construct($serverUrl, $username, $password, $orgId = null) } else { $this->restReq->setUsername($this->username); } + $this->restReq->setOptions($this->options); $this->restReq->setPassword($this->password); $this->restUrl2 = $this->serverUrl . BASE_REST2_URL; } diff --git a/src/Jaspersoft/Tool/RESTRequest.php b/src/Jaspersoft/Tool/RESTRequest.php index 2ad536a..33feca3 100644 --- a/src/Jaspersoft/Tool/RESTRequest.php +++ b/src/Jaspersoft/Tool/RESTRequest.php @@ -20,8 +20,9 @@ class RESTRequest protected $headers; protected $curl_timeout; protected $curl_handle; + protected $options; - public function __construct ($url = null, $verb = 'GET', $request_body = null) + public function __construct ($url = null, $verb = 'GET', $request_body = null, $options = array()) { $this->url = $url; $this->verb = $verb; @@ -37,6 +38,7 @@ public function __construct ($url = null, $verb = 'GET', $request_body = null) $this->curl_timeout = 30; $this->curl_handle = curl_init(); $this->curl_cookiejar = null; + $this->options = $options; if ($this->request_body !== null) { @@ -276,6 +278,9 @@ protected function doExecute (&$curlHandle) protected function setCurlOpts (&$curlHandle) { + $ssl_verifypeer = $this->getOptionValue('ssl_verifypeer',true); + $ssl_cainfo = $this->getOptionValue('ssl_cainfo',''); + curl_setopt($curlHandle, CURLOPT_URL, $this->url); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlHandle, CURLOPT_HEADER, true); @@ -286,6 +291,10 @@ protected function setCurlOpts (&$curlHandle) $this->headers[] = "Accept: " . $this->accept_type; if (!empty($this->headers)) curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $this->headers); + if (!empty($ssl_cainfo)) + curl_setopt($curlHandle, CURLOPT_CAINFO, $ssl_cainfo); + if (!$ssl_verifypeer) + curl_setopt($curlHandle, CURLOPT_SSL_VERIFYPEER, 0); } protected function setAuth (&$curlHandle) @@ -336,6 +345,23 @@ public function setContentType ($content_type) { $this->content_type = $content_type; } + public function getOptions () + { + return $this->options; + } + public function setOptions ($options = array()) + { + $this->options = $options; + } + protected function getOptionValue($option_name, $default_value = null) + { + $option_value = $default_value; + if (is_array($this->options) && array_key_exists($option_name, $this->options)) + { + $option_value = $this->options[$option_name]; + } + return $option_value; + } public function getPassword () { return $this->password; From f9e51ce9afed2ac3338a19126a4a2833a63364b4 Mon Sep 17 00:00:00 2001 From: idfake Date: Tue, 30 Jan 2024 13:18:53 +0100 Subject: [PATCH 2/2] fix some PHP 8+ deprecation messages --- src/Jaspersoft/Client/Client.php | 1 + src/Jaspersoft/Service/RepositoryService.php | 5 ++-- src/Jaspersoft/Tool/RESTRequest.php | 26 +++++++++++--------- src/Jaspersoft/Tool/Util.php | 2 +- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Jaspersoft/Client/Client.php b/src/Jaspersoft/Client/Client.php index 7aab914..1fee189 100644 --- a/src/Jaspersoft/Client/Client.php +++ b/src/Jaspersoft/Client/Client.php @@ -17,6 +17,7 @@ class Client { private $restReq; private $restUrl2; + private $serverUrl; protected $hostname; protected $username; protected $password; diff --git a/src/Jaspersoft/Service/RepositoryService.php b/src/Jaspersoft/Service/RepositoryService.php index 964f6f6..17760d5 100644 --- a/src/Jaspersoft/Service/RepositoryService.php +++ b/src/Jaspersoft/Service/RepositoryService.php @@ -175,7 +175,8 @@ public function updateFileResource(File $resource, $binaryData) $url = self::makeUrl(null, $resource->uri); $body = $binaryData; - $response = $this->service->sendBinary($url, array(201, 200), $body, MimeMapper::mapType($resource->type), 'attachment; filename=' . $resource->label, $resource->description, 'PUT'); + $response = $this->service->sendBinary($url, $body, MimeMapper::mapType($resource->type), 'attachment; filename=' . $resource->label, $resource->description, 'PUT', array(201, 200)); + return File::createFromJSON(json_decode($response, true), get_class($resource)); } @@ -198,7 +199,7 @@ public function createFileResource(File $resource, $binaryData, $parentFolder, $ $url .= '?' . Util::query_suffix(array("createFolders" => $createFolders)); $body = $binaryData; - $response = $this->service->sendBinary($url, array(201, 200), $body, MimeMapper::mapType($resource->type), 'attachment; filename=' . $resource->label, $resource->description, 'POST'); + $response = $this->service->sendBinary($url, $body, MimeMapper::mapType($resource->type), 'attachment; filename=' . $resource->label, $resource->description, 'POST', array(201, 200)); return File::createFromJSON(json_decode($response, true), get_class($resource)); } diff --git a/src/Jaspersoft/Tool/RESTRequest.php b/src/Jaspersoft/Tool/RESTRequest.php index 33feca3..180e7f6 100644 --- a/src/Jaspersoft/Tool/RESTRequest.php +++ b/src/Jaspersoft/Tool/RESTRequest.php @@ -6,20 +6,22 @@ class RESTRequest { - protected $url; - protected $verb; - protected $request_body; - protected $request_length; - protected $username; - protected $password; - protected $accept_type; - protected $content_type; - protected $response_body; - protected $response_info; - protected $file_to_upload = array(); + protected $url; + protected $verb; + protected $request_body; + protected $request_length; + protected $username; + protected $password; + protected $accept_type; + protected $content_type; + protected $response_body; + protected $response_headers; + protected $response_info; + protected $file_to_upload = array(); protected $headers; protected $curl_timeout; protected $curl_handle; + protected $curl_cookiejar; protected $options; public function __construct ($url = null, $verb = 'GET', $request_body = null, $options = array()) @@ -545,7 +547,7 @@ public function multipartRequestSend($url, $expectedCode = 200, $verb = 'PUT_MP' } - public function sendBinary($url, $expectedCodes = array(200), $body, $contentType, $contentDisposition, $contentDescription, $verb = "POST") + public function sendBinary($url, $body, $contentType, $contentDisposition, $contentDescription, $verb = "POST", $expectedCodes = array(200)) { $this->flush(); $this->setUrl($url); diff --git a/src/Jaspersoft/Tool/Util.php b/src/Jaspersoft/Tool/Util.php index 672cb49..28ff02d 100644 --- a/src/Jaspersoft/Tool/Util.php +++ b/src/Jaspersoft/Tool/Util.php @@ -15,7 +15,7 @@ public static function query_suffix($params) $params[$k] = ($v) ? 'true' : 'false'; } } - $url = http_build_query($params, null, '&'); + $url = http_build_query($params, '', '&'); return preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $url); }