ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilECSConnector.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
34include_once('Services/WebServices/ECS/classes/class.ilECSSetting.php');
35include_once('Services/WebServices/ECS/classes/class.ilECSResult.php');
36include_once('Services/WebServices/Curl/classes/class.ilCurlConnection.php');
37
39{
40 const HTTP_CODE_CREATED = 201;
41 const HTTP_CODE_OK = 200;
43
44 const HEADER_MEMBERSHIPS = 'X-EcsReceiverMemberships';
45 const HEADER_COMMUNITIES = 'X-EcsReceiverCommunities';
46
47
48 protected $path_postfix = '';
49
50 protected $settings;
51
52 protected $header_strings = array();
53
61 public function __construct(ilECSSetting $settings = null)
62 {
63 if ($settings) {
64 $this->settings = $settings;
65 } else {
66 $GLOBALS['ilLog']->write(__METHOD__ . ': Using deprecated call');
67 $GLOBALS['ilLog']->logStack();
68 }
69 }
70
71 // Header methods
77 public function addHeader($a_name, $a_value)
78 {
79 $this->header_strings[] = ($a_name . ': ' . $a_value);
80 }
81
82 public function getHeader()
83 {
84 return (array) $this->header_strings;
85 }
86
87 public function setHeader($a_header_strings)
88 {
89 $this->header_strings = $a_header_strings;
90 }
91
96 public function getServer()
97 {
98 return $this->settings;
99 }
100
101
103 // auths methods
105
115 public function addAuth($a_post, $a_target_mid)
116 {
117 global $ilLog;
118
119 $ilLog->write(__METHOD__ . ': Add new Auth resource...');
120
121 $this->path_postfix = '/sys/auths';
122
123 try {
124 $this->prepareConnection();
125
126 $this->addHeader('Content-Type', 'application/json');
127 $this->addHeader('Accept', 'application/json');
128 $this->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, $a_target_mid);
129 #$this->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, 1);
130
131 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
132 $this->curl->setOpt(CURLOPT_POST, true);
133 $this->curl->setOpt(CURLOPT_POSTFIELDS, $a_post);
134 $ret = $this->call();
135
136 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
137
138 $ilLog->write(__METHOD__ . ': Checking HTTP status...');
139 if ($info != self::HTTP_CODE_CREATED) {
140 $ilLog->write(__METHOD__ . ': Cannot create auth resource, did not receive HTTP 201. ');
141 $ilLog->write(__METHOD__ . ': POST was: ' . $a_post);
142 $ilLog->write(__METHOD__ . ': HTTP code: ' . $info);
143 throw new ilECSConnectorException('Received HTTP status code: ' . $info);
144 }
145 $ilLog->write(__METHOD__ . ': ... got HTTP 201 (created)');
146 $ilLog->write(__METHOD__ . ': POST was: ' . $a_post);
147
148 $result = new ilECSResult($ret);
149 $auth = $result->getResult();
150
151 $ilLog->write(__METHOD__ . ': ... got hash: ' . $auth->hash);
152
153 return $auth->hash;
154 } catch (ilCurlConnectionException $exc) {
155 throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
156 }
157 }
158
166 public function getAuth($a_hash, $a_details_only = false)
167 {
168 global $ilLog;
169
170 if (!strlen($a_hash)) {
171 $ilLog->write(__METHOD__ . ': No auth hash given. Aborting.');
172 throw new ilECSConnectorException('No auth hash given.');
173 }
174
175 $this->path_postfix = '/sys/auths/' . $a_hash;
176
177 if ($a_details_only) {
178 $this->path_postfix .= ('/details');
179 }
180
181
182 try {
183 $this->prepareConnection();
184 $res = $this->call();
185 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
186
187 $ilLog->write(__METHOD__ . ': Checking HTTP status...');
188 if ($info != self::HTTP_CODE_OK) {
189 $ilLog->write(__METHOD__ . ': Cannot get auth resource, did not receive HTTP 200. ');
190 throw new ilECSConnectorException('Received HTTP status code: ' . $info);
191 }
192 $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
193
194 $ecs_result = new ilECSResult($res);
195 // Return ECSEContentDetails for details switch
196 if ($a_details_only) {
197 include_once './Services/WebServices/ECS/classes/class.ilECSEContentDetails.php';
198 $details = new ilECSEContentDetails();
199 $details->loadFromJson($ecs_result->getResult());
200 return $details;
201 }
202 return $ecs_result;
203 } catch (ilCurlConnectionException $exc) {
204 throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
205 }
206 }
207
209 // eventqueues methods
211
219 public function getEventQueues()
220 {
221 global $ilLog;
222
223 $this->path_postfix = '/eventqueues';
224
225 try {
226 $this->prepareConnection();
227
228 $res = $this->call();
229 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
230
231 $ilLog->write(__METHOD__ . ': Checking HTTP status...');
232 if ($info != self::HTTP_CODE_OK) {
233 $ilLog->write(__METHOD__ . ': Cannot get event queue, did not receive HTTP 200. ');
234 throw new ilECSConnectorException('Received HTTP status code: ' . $info);
235 }
236 $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
237 return new ilECSResult($res);
238 } catch (ilCurlConnectionException $exc) {
239 throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
240 }
241 }
242
243 #######################################################
244 # event fifo methods
245 #####################################################
252 public function readEventFifo($a_delete = false)
253 {
254 global $ilLog;
255
256 $this->path_postfix = '/sys/events/fifo';
257
258 try {
259 $this->prepareConnection();
260 $this->addHeader('Content-Type', 'application/json');
261 $this->addHeader('Accept', 'application/json');
262
263 if ($a_delete) {
264 $this->curl->setOpt(CURLOPT_POST, true);
265 $this->curl->setOpt(CURLOPT_POSTFIELDS, '');
266 }
267 $res = $this->call();
268
269 // Checking status code
270 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
271 #$ilLog->write(__METHOD__.': Checking HTTP status...');
272 if ($info != self::HTTP_CODE_OK) {
273 $ilLog->write(__METHOD__ . ': Cannot read event fifo, did not receive HTTP 200. ');
274 throw new ilECSConnectorException('Received HTTP status code: ' . $info);
275 }
276 #$ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
277
278 $result = new ilECSResult($res);
279
280 #$GLOBALS['ilLog']->write(__METHOD__.':------------------------------------- FIFO content'. print_r($result,true));
281
282 return $result;
283 } catch (ilCurlConnectionException $exc) {
284 throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
285 }
286 }
287
289 // econtents methods
291
292 public function getResourceList($a_path)
293 {
294 global $ilLog;
295
296 $this->path_postfix = $a_path;
297
298 try {
299 $this->prepareConnection();
300 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
301 $res = $this->call();
302
303 // Checking status code
304 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
305 $ilLog->write(__METHOD__ . ': Checking HTTP status...');
306 if ($info != self::HTTP_CODE_OK) {
307 $ilLog->write(__METHOD__ . ': Cannot get ressource list, did not receive HTTP 200. ');
308 throw new ilECSConnectorException('Received HTTP status code: ' . $info);
309 }
310 $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
311
313 } catch (ilCurlConnectionException $exc) {
314 throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
315 }
316 }
317
318
330 public function getResource($a_path, $a_econtent_id, $a_details_only = false)
331 {
332 global $ilLog;
333
334 if ($a_econtent_id) {
335 $ilLog->write(__METHOD__ . ': Get resource with ID: ' . $a_econtent_id);
336 } else {
337 $ilLog->write(__METHOD__ . ': Get all resources ...');
338 }
339
340 $this->path_postfix = $a_path;
341 if ($a_econtent_id) {
342 $this->path_postfix .= ('/' . (int) $a_econtent_id);
343 }
344 if ($a_details_only) {
345 $this->path_postfix .= ('/details');
346 }
347
348 try {
349 $this->prepareConnection();
350 $res = $this->call();
351
352 // Checking status code
353 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
354 $ilLog->write(__METHOD__ . ': Checking HTTP status...');
355 if ($info != self::HTTP_CODE_OK) {
356 $ilLog->write(__METHOD__ . ': Cannot get ressource, did not receive HTTP 200. ');
357 throw new ilECSConnectorException('Received HTTP status code: ' . $info);
358 }
359 $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
360
361 $result = new ilECSResult($res);
362 $result->setHeaders($this->curl->getResponseHeaderArray());
363 $result->setHTTPCode($info);
364
365 return $result;
366 } catch (ilCurlConnectionException $exc) {
367 throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
368 }
369 }
370
381 public function addResource($a_path, $a_post)
382 {
383 global $ilLog;
384
385 $ilLog->write(__METHOD__ . ': Add new EContent...');
386
387 $this->path_postfix = $a_path;
388
389 try {
390 $this->prepareConnection();
391
392 $this->addHeader('Content-Type', 'application/json');
393
394 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
395 $this->curl->setOpt(CURLOPT_HEADER, true);
396 $this->curl->setOpt(CURLOPT_POST, true);
397 $this->curl->setOpt(CURLOPT_POSTFIELDS, $a_post);
398 $res = $this->call();
399
400 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
401
402 $ilLog->write(__METHOD__ . ': Checking HTTP status...');
403 if ($info != self::HTTP_CODE_CREATED) {
404 $ilLog->write(__METHOD__ . ': Cannot create econtent, did not receive HTTP 201. ');
405 throw new ilECSConnectorException('Received HTTP status code: ' . $info);
406 }
407 $ilLog->write(__METHOD__ . ': ... got HTTP 201 (created)');
408
409 $eid = self::_fetchEContentIdFromHeader($this->curl->getResponseHeaderArray());
410 return $eid;
411 } catch (ilCurlConnectionException $exc) {
412 throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
413 }
414 }
415
425 public function updateResource($a_path, $a_econtent_id, $a_post_string)
426 {
427 global $ilLog;
428
429 $ilLog->write(__METHOD__ . ': Update resource with id ' . $a_econtent_id);
430
431 $this->path_postfix = $a_path;
432
433 if ($a_econtent_id) {
434 $this->path_postfix .= ('/' . (int) $a_econtent_id);
435 } else {
436 throw new ilECSConnectorException('Error calling updateResource: No content id given.');
437 }
438 try {
439 $this->prepareConnection();
440 $this->addHeader('Content-Type', 'application/json');
441 $this->addHeader('Accept', 'application/json');
442 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
443 $this->curl->setOpt(CURLOPT_HEADER, true);
444 $this->curl->setOpt(CURLOPT_PUT, true);
445
446 $tempfile = ilUtil::ilTempnam();
447 $ilLog->write(__METHOD__ . ': Created new tempfile: ' . $tempfile);
448
449 $fp = fopen($tempfile, 'w');
450 fwrite($fp, $a_post_string);
451 fclose($fp);
452
453 $this->curl->setOpt(CURLOPT_UPLOAD, true);
454 $this->curl->setOpt(CURLOPT_INFILESIZE, filesize($tempfile));
455 $fp = fopen($tempfile, 'r');
456 $this->curl->setOpt(CURLOPT_INFILE, $fp);
457
458 $res = $this->call();
459
460 fclose($fp);
461 unlink($tempfile);
462
463 return new ilECSResult($res);
464 } catch (ilCurlConnectionException $exc) {
465 throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
466 }
467 }
468
477 public function deleteResource($a_path, $a_econtent_id)
478 {
479 global $ilLog;
480
481 $ilLog->write(__METHOD__ . ': Delete resource with id ' . $a_econtent_id);
482
483 $this->path_postfix = $a_path;
484
485 if ($a_econtent_id) {
486 $this->path_postfix .= ('/' . (int) $a_econtent_id);
487 } else {
488 throw new ilECSConnectorException('Error calling deleteResource: No content id given.');
489 }
490
491 try {
492 $this->prepareConnection();
493 $this->curl->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE');
494 $res = $this->call();
495 return new ilECSResult($res);
496 } catch (ilCurlConnectionException $exc) {
497 throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
498 }
499 }
500
502 // membership methods
504
512 public function getMemberships($a_mid = 0)
513 {
514 global $ilLog;
515
516 $ilLog->write(__METHOD__ . ': Get existing memberships');
517
518 $this->path_postfix = '/sys/memberships';
519 if ($a_mid) {
520 $ilLog->write(__METHOD__ . ': Read membership with id: ' . $a_mid);
521 $this->path_postfix .= ('/' . (int) $a_mid);
522 }
523 try {
524 $this->prepareConnection();
525 $res = $this->call();
526
527 $this->curl->setOpt(CURLOPT_HTTPHEADER, array(0 => 'X-EcsQueryStrings: sender=true'));
528
529 // Checking status code
530 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
531 if ($info != self::HTTP_CODE_OK) {
532 $ilLog->write(__METHOD__ . ': Cannot get memberships, did not receive HTTP 200. ');
533 throw new ilECSConnectorException('Received HTTP status code: ' . $info);
534 }
535
536 return new ilECSResult($res);
537 } catch (ilCurlConnectionException $exc) {
538 throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
539 }
540 }
541
548 protected function prepareConnection()
549 {
550 try {
551 $this->curl = new ilCurlConnection($this->settings->getServerURI() . $this->path_postfix);
552 $this->curl->init();
553 $this->curl->setOpt(CURLOPT_HTTPHEADER, array(0 => 'Accept: application/json'));
554 $this->curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
555 $this->curl->setOpt(CURLOPT_VERBOSE, 1);
556 $this->curl->setOpt(CURLOPT_TIMEOUT_MS, 2000);
557
558 switch ($this->getServer()->getAuthType()) {
560 $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, 0);
561 #$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST,0);
562 $this->curl->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
563 $this->curl->setOpt(
564 CURLOPT_USERPWD,
565 $this->getServer()->getAuthUser() . ':' . $this->getServer()->getAuthPass()
566 );
567 break;
568
570 $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, 1);
571 // use default 2 for libcurl 7.28.1 support
572 $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2);
573 $this->curl->setOpt(CURLOPT_CAINFO, $this->settings->getCACertPath());
574 $this->curl->setOpt(CURLOPT_SSLCERT, $this->settings->getClientCertPath());
575 $this->curl->setOpt(CURLOPT_SSLKEY, $this->settings->getKeyPath());
576 $this->curl->setOpt(CURLOPT_SSLKEYPASSWD, $this->settings->getKeyPassword());
577 break;
578
579 }
580 } catch (ilCurlConnectionException $exc) {
581 throw($exc);
582 }
583 }
584
591 protected function call()
592 {
593 try {
594 $res = $this->curl->exec();
595 return $res;
596 } catch (ilCurlConnectionException $exc) {
597 throw($exc);
598 }
599 }
600
601
610 protected static function _fetchEContentIdFromHeader($a_header)
611 {
612 global $ilLog;
613
614 if (!isset($a_header['Location'])) {
615 return false;
616 }
617 $end_path = strrpos($a_header['Location'], "/");
618
619 if ($end_path === false) {
620 $ilLog->write(__METHOD__ . ': Cannot find path seperator.');
621 return false;
622 }
623 $econtent_id = substr($a_header['Location'], $end_path + 1);
624 $ilLog->write(__METHOD__ . ': Received EContentId ' . $econtent_id);
625 return (int) $econtent_id;
626 }
627}
$result
$auth
Definition: metadata.php:48
An exception for terminatinating execution or to throw for unit testing.
deleteResource($a_path, $a_econtent_id)
Delete resource.
getMemberships($a_mid=0)
@access public
static _fetchEContentIdFromHeader($a_header)
fetch new econtent id from location header
addHeader($a_name, $a_value)
Add Header.
readEventFifo($a_delete=false)
Read event fifo.
getResource($a_path, $a_econtent_id, $a_details_only=false)
Get resources from ECS server.
addResource($a_path, $a_post)
Add resource.
__construct(ilECSSetting $settings=null)
Constructor.
prepareConnection()
prepare connection
updateResource($a_path, $a_econtent_id, $a_post_string)
update resource
getServer()
Get current server setting.
getEventQueues()
get event queue
getAuth($a_hash, $a_details_only=false)
get auth resource
addAuth($a_post, $a_target_mid)
Add auth resource.
setHeader($a_header_strings)
Presentation of ecs content details (http://...campusconnect/courselinks/id/details)
const RESULT_TYPE_URL_LIST
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
$info
Definition: index.php:5
$ret
Definition: parser.php:6
foreach($_POST as $key=> $value) $res
settings()
Definition: settings.php:2