18 declare(strict_types=1);
47 $this->
logger = $DIC->logger()->wsrv();
51 $this->
logger->warning(
'Using deprecated call.');
62 public function addHeader(
string $a_name,
string $a_value): void
64 $this->header_strings[] = ($a_name .
': ' . $a_value);
72 public function setHeader(array $a_header_strings): void
74 $this->header_strings = $a_header_strings;
99 public function addAuth(
string $a_post,
int $a_target_mid): string
101 $this->
logger->info(__METHOD__ .
': Add new Auth resource...');
103 $this->path_postfix =
'/sys/auths';
108 $this->
addHeader(
'Content-Type',
'application/json');
109 $this->
addHeader(
'Accept',
'application/json');
110 $this->
addHeader(self::HEADER_MEMBERSHIPS, (
string) $a_target_mid);
112 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->
getHeader());
113 $this->curl->setOpt(CURLOPT_POST,
true);
114 $this->curl->setOpt(CURLOPT_POSTFIELDS, $a_post);
115 $ret = $this->
call();
117 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
119 $this->
logger->info(__METHOD__ .
': Checking HTTP status...');
120 if ($info !== self::HTTP_CODE_CREATED) {
121 $this->
logger->info(__METHOD__ .
': Cannot create auth resource, did not receive HTTP 201. ');
122 $this->
logger->info(__METHOD__ .
': POST was: ' . $a_post);
123 $this->
logger->info(__METHOD__ .
': HTTP code: ' . $info);
126 $this->
logger->info(__METHOD__ .
': ... got HTTP 201 (created)');
127 $this->
logger->info(__METHOD__ .
': POST was: ' . $a_post);
130 $auth = $result->getResult();
132 $this->
logger->info(__METHOD__ .
': ... got hash: ' .
$auth->hash);
146 public function getAuth(
string $a_hash,
bool $a_details_only =
false)
148 if ($a_hash ===
'') {
149 $this->
logger->error(__METHOD__ .
': No auth hash given. Aborting.');
153 $this->path_postfix =
'/sys/auths/' . $a_hash;
155 if ($a_details_only) {
156 $this->path_postfix .= (
'/details');
163 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
165 $this->
logger->info(__METHOD__ .
': Checking HTTP status...');
166 if ($info !== self::HTTP_CODE_OK) {
167 $this->
logger->info(__METHOD__ .
': Cannot get auth resource, did not receive HTTP 200. ');
170 $this->
logger->info(__METHOD__ .
': ... got HTTP 200 (ok)');
174 if ($a_details_only) {
176 $details->loadFromJson($ecs_result->getResult());
185 ####################################################### 187 ##################################################### 196 $this->path_postfix =
'/sys/events/fifo';
200 $this->
addHeader(
'Content-Type',
'application/json');
201 $this->
addHeader(
'Accept',
'application/json');
204 $this->curl->setOpt(CURLOPT_POST,
true);
205 $this->curl->setOpt(CURLOPT_POSTFIELDS,
'');
210 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
211 if ($info !== self::HTTP_CODE_OK) {
212 $this->
logger->info(__METHOD__ .
': Cannot read event fifo, did not receive HTTP 200. ');
220 $this->curl->close();
230 $this->path_postfix = $a_path;
234 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->
getHeader());
238 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
239 $this->
logger->debug(__METHOD__ .
': Checking HTTP status...');
240 if ($info !== self::HTTP_CODE_OK) {
241 $this->
logger->debug(__METHOD__ .
': Cannot get ressource list, did not receive HTTP 200. ');
244 $this->
logger->debug(__METHOD__ .
': ... got HTTP 200 (ok)');
261 if ($a_econtent_id) {
262 $this->
logger->debug(__METHOD__ .
': Get resource with ID: ' . $a_econtent_id);
264 $this->
logger->debug(__METHOD__ .
': Get all resources ...');
267 $this->path_postfix = $a_path;
268 if ($a_econtent_id) {
269 $this->path_postfix .= (
'/' . $a_econtent_id);
271 if ($a_details_only) {
272 $this->path_postfix .= (
'/details');
280 $info = (
int) $this->curl->getInfo(CURLINFO_HTTP_CODE);
281 $this->
logger->debug(__METHOD__ .
': Checking HTTP status...');
282 if ($info !== self::HTTP_CODE_OK) {
283 $this->
logger->debug(__METHOD__ .
': Cannot get ressource, did not receive HTTP 200. ');
286 $this->
logger->debug(__METHOD__ .
': ... got HTTP 200 (ok)');
289 $result->setHeaders($this->curl->getResponseHeaderArray());
290 $result->setHTTPCode($info);
310 $this->
logger->info(__METHOD__ .
': Add new EContent...');
312 $this->path_postfix = $a_path;
317 $this->
addHeader(
'Content-Type',
'application/json');
319 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->
getHeader());
320 $this->curl->setOpt(CURLOPT_HEADER,
true);
321 $this->curl->setOpt(CURLOPT_POST,
true);
322 $this->curl->setOpt(CURLOPT_POSTFIELDS, $a_post);
325 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
327 $this->
logger->debug(__METHOD__ .
': Checking HTTP status...');
328 if ($info !== self::HTTP_CODE_CREATED) {
329 $this->
logger->debug(__METHOD__ .
': Cannot create econtent, did not receive HTTP 201. ');
332 $this->
logger->debug(__METHOD__ .
': ... got HTTP 201 (created)');
348 public function updateResource(
string $a_path,
int $a_econtent_id,
string $a_post_string): void
350 $this->
logger->debug(__METHOD__ .
': Update resource with id ' . $a_econtent_id);
352 $this->path_postfix = $a_path;
354 if ($a_econtent_id) {
355 $this->path_postfix .= (
'/' . $a_econtent_id);
361 $this->
addHeader(
'Content-Type',
'application/json');
362 $this->
addHeader(
'Accept',
'application/json');
364 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->
getHeader());
365 $this->curl->setOpt(CURLOPT_HEADER,
true);
366 $this->curl->setOpt(CURLOPT_PUT,
true);
369 $this->
logger->info(__METHOD__ .
': Created new tempfile: ' . $tempfile);
371 $fp = fopen($tempfile,
'wb');
372 fwrite($fp, $a_post_string);
375 $this->curl->setOpt(CURLOPT_UPLOAD,
true);
376 $this->curl->setOpt(CURLOPT_INFILESIZE, filesize($tempfile));
377 $fp = fopen($tempfile,
'rb');
378 $this->curl->setOpt(CURLOPT_INFILE, $fp);
385 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
386 $this->
logger->debug(__METHOD__ .
': Checking HTTP status...');
387 if ($info !== self::HTTP_CODE_OK) {
388 $this->
logger->debug(__METHOD__ .
': Cannot update resource. ', $a_path, $a_econtent_id);
405 $this->
logger->debug(__METHOD__ .
': Delete resource with id ' . $a_econtent_id);
407 $this->path_postfix = $a_path;
409 if ($a_econtent_id) {
410 $this->path_postfix .= (
'/' . $a_econtent_id);
417 $this->curl->setOpt(CURLOPT_CUSTOMREQUEST,
'DELETE');
419 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
439 $this->
logger->debug(__METHOD__ .
': Get existing memberships');
441 $this->path_postfix =
'/sys/memberships';
443 $this->
logger->debug(__METHOD__ .
': Read membership with id: ' . $a_mid);
444 $this->path_postfix .= (
'/' . $a_mid);
450 $this->curl->setOpt(CURLOPT_HTTPHEADER, array(0 =>
'X-EcsQueryStrings: sender=true'));
453 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
454 if ($info !== self::HTTP_CODE_OK) {
455 $this->
logger->debug(__METHOD__ .
': Cannot get memberships, did not receive HTTP 200. ');
461 throw new ilECSConnectorException(
'Error calling ECS service: ' . $exc->getMessage() . $exc->getTraceAsString(), 0, $exc);
474 $this->curl->
init(
true);
475 $this->curl->setOpt(CURLOPT_HTTPHEADER, array(0 =>
'Accept: application/json'));
476 $this->curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
477 $this->curl->setOpt(CURLOPT_TIMEOUT_MS, 2000);
478 $this->curl->setOpt(CURLOPT_FORBID_REUSE,
true);
479 $this->curl->setOpt(CURLOPT_FRESH_CONNECT,
true);
482 $this->curl->setOpt(CURLOPT_VERBOSE, 1);
485 switch ($this->
getServer()->getAuthType()) {
487 $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, 1);
488 #$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST,0); 489 $this->curl->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
497 $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, 1);
499 $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2);
500 $this->curl->setOpt(CURLOPT_CAINFO, $this->
settings->getCACertPath());
501 $this->curl->setOpt(CURLOPT_SSLCERT, $this->
settings->getClientCertPath());
502 $this->curl->setOpt(CURLOPT_SSLKEY, $this->
settings->getKeyPath());
503 $this->curl->setOpt(CURLOPT_SSLKEYPASSWD, $this->
settings->getKeyPassword());
522 return $this->curl->exec();
524 $this->
logger->error($exc->getMessage());
536 $location_parts = [];
537 foreach ($a_header as $header => $value) {
538 if (strcasecmp(
'Location', $header) === 0) {
539 $location_parts = explode(
'/', $value);
543 if (!$location_parts) {
544 $this->
logger->error(__METHOD__ .
': Cannot find location headers.');
547 if (count($location_parts) === 1) {
548 $this->
logger->warning(__METHOD__ .
': Cannot find path seperator.');
551 $econtent_id = end($location_parts);
552 $this->
logger->info(__METHOD__ .
': Received EContentId ' . $econtent_id);
553 return (
int) $econtent_id;
updateResource(string $a_path, int $a_econtent_id, string $a_post_string)
update resource
const RESULT_TYPE_URL_LIST
getMemberships(int $a_mid=0)
__construct(ilECSSetting $settings=null)
readEventFifo(bool $a_delete=false)
Read event fifo.
addHeader(string $a_name, string $a_value)
Add Header.
Presentation of ecs content details (http://...campusconnect/courselinks/id/details) ...
init(bool $set_proxy=true)
addAuth(string $a_post, int $a_target_mid)
Add auth resource.
getResource(string $a_path, int $a_econtent_id, bool $a_details_only=false)
Get resources from ECS server.
addResource(string $a_path, $a_post)
Add resource.
static ilTempnam(?string $a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
_fetchEContentIdFromHeader(array $a_header)
fetch new econtent id from location header
getResourceList(string $a_path)
getServer()
Get current server setting.
setHeader(array $a_header_strings)
deleteResource(string $a_path, int $a_econtent_id)
Delete resource.
prepareConnection()
prepare connection
const HTTP_CODE_NOT_FOUND
getAuth(string $a_hash, bool $a_details_only=false)
get auth resource