ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
34 include_once('Services/WebServices/ECS/classes/class.ilECSSetting.php');
35 include_once('Services/WebServices/ECS/classes/class.ilECSResult.php');
36 include_once('Services/WebServices/Curl/classes/class.ilCurlConnection.php');
37 
39 {
40  const HTTP_CODE_CREATED = 201;
41  const HTTP_CODE_OK = 200;
42  const HTTP_CODE_NOT_FOUND = 404;
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 
57  protected $logger;
58 
66  public function __construct(ilECSSetting $settings = null)
67  {
68  global $DIC;
69 
70  $this->logger = $DIC->logger()->wsrv();
71  if ($settings) {
72  $this->settings = $settings;
73  } else {
74  $this->logger->warning('Using deprecated call.');
75  $this->logger->logStack(ilLogLevel::WARNING);
76  }
77  }
78 
79  // Header methods
85  public function addHeader($a_name, $a_value)
86  {
87  $this->header_strings[] = ($a_name . ': ' . $a_value);
88  }
89 
90  public function getHeader()
91  {
92  return (array) $this->header_strings;
93  }
94 
95  public function setHeader($a_header_strings)
96  {
97  $this->header_strings = $a_header_strings;
98  }
99 
104  public function getServer()
105  {
106  return $this->settings;
107  }
108 
109 
111  // auths methods
113 
123  public function addAuth($a_post, $a_target_mid)
124  {
125  global $DIC;
126 
127  $ilLog = $DIC['ilLog'];
128 
129  $ilLog->write(__METHOD__ . ': Add new Auth resource...');
130 
131  $this->path_postfix = '/sys/auths';
132 
133  try {
134  $this->prepareConnection();
135 
136  $this->addHeader('Content-Type', 'application/json');
137  $this->addHeader('Accept', 'application/json');
138  $this->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, $a_target_mid);
139  #$this->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, 1);
140 
141  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
142  $this->curl->setOpt(CURLOPT_POST, true);
143  $this->curl->setOpt(CURLOPT_POSTFIELDS, $a_post);
144  $ret = $this->call();
145 
146  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
147 
148  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
149  if ($info != self::HTTP_CODE_CREATED) {
150  $ilLog->write(__METHOD__ . ': Cannot create auth resource, did not receive HTTP 201. ');
151  $ilLog->write(__METHOD__ . ': POST was: ' . $a_post);
152  $ilLog->write(__METHOD__ . ': HTTP code: ' . $info);
153  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
154  }
155  $ilLog->write(__METHOD__ . ': ... got HTTP 201 (created)');
156  $ilLog->write(__METHOD__ . ': POST was: ' . $a_post);
157 
158  $result = new ilECSResult($ret);
159  $auth = $result->getResult();
160 
161  $ilLog->write(__METHOD__ . ': ... got hash: ' . $auth->hash);
162 
163  return $auth->hash;
164  } catch (ilCurlConnectionException $exc) {
165  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
166  }
167  }
168 
176  public function getAuth($a_hash, $a_details_only = false)
177  {
178  global $DIC;
179 
180  $ilLog = $DIC['ilLog'];
181 
182  if (!strlen($a_hash)) {
183  $ilLog->write(__METHOD__ . ': No auth hash given. Aborting.');
184  throw new ilECSConnectorException('No auth hash given.');
185  }
186 
187  $this->path_postfix = '/sys/auths/' . $a_hash;
188 
189  if ($a_details_only) {
190  $this->path_postfix .= ('/details');
191  }
192 
193 
194  try {
195  $this->prepareConnection();
196  $res = $this->call();
197  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
198 
199  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
200  if ($info != self::HTTP_CODE_OK) {
201  $ilLog->write(__METHOD__ . ': Cannot get auth resource, did not receive HTTP 200. ');
202  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
203  }
204  $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
205 
206  $ecs_result = new ilECSResult($res);
207  // Return ECSEContentDetails for details switch
208  if ($a_details_only) {
209  include_once './Services/WebServices/ECS/classes/class.ilECSEContentDetails.php';
210  $details = new ilECSEContentDetails();
211  $details->loadFromJson($ecs_result->getResult());
212  return $details;
213  }
214  return $ecs_result;
215  } catch (ilCurlConnectionException $exc) {
216  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
217  }
218  }
219 
221  // eventqueues methods
223 
231  public function getEventQueues()
232  {
233  global $DIC;
234 
235  $ilLog = $DIC['ilLog'];
236 
237  $this->path_postfix = '/eventqueues';
238 
239  try {
240  $this->prepareConnection();
241 
242  $res = $this->call();
243  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
244 
245  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
246  if ($info != self::HTTP_CODE_OK) {
247  $ilLog->write(__METHOD__ . ': Cannot get event queue, did not receive HTTP 200. ');
248  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
249  }
250  $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
251  return new ilECSResult($res);
252  } catch (ilCurlConnectionException $exc) {
253  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
254  }
255  }
256 
257  #######################################################
258  # event fifo methods
259  #####################################################
260 
266  public function readEventFifo($a_delete = false)
267  {
268  global $DIC;
269 
270  $ilLog = $DIC['ilLog'];
271 
272  $this->path_postfix = '/sys/events/fifo';
273 
274  try {
275  $this->prepareConnection();
276  $this->addHeader('Content-Type', 'application/json');
277  $this->addHeader('Accept', 'application/json');
278 
279  if ($a_delete) {
280  $this->curl->setOpt(CURLOPT_POST, true);
281  $this->curl->setOpt(CURLOPT_POSTFIELDS, '');
282  }
283  $res = $this->call();
284 
285  // Checking status code
286  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
287  #$ilLog->write(__METHOD__.': Checking HTTP status...');
288  if ($info != self::HTTP_CODE_OK) {
289  $ilLog->write(__METHOD__ . ': Cannot read event fifo, did not receive HTTP 200. ');
290  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
291  }
292  #$ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
293 
294  $result = new ilECSResult($res);
295 
296  #$GLOBALS['DIC']['ilLog']->write(__METHOD__.':------------------------------------- FIFO content'. print_r($result,true));
297 
298  return $result;
299  } catch (ilCurlConnectionException $exc) {
300  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
301  } finally {
302  $this->curl->close();
303  }
304  }
305 
307  // econtents methods
309 
310  public function getResourceList($a_path)
311  {
312  global $DIC;
313 
314  $ilLog = $DIC['ilLog'];
315 
316  $this->path_postfix = $a_path;
317 
318  try {
319  $this->prepareConnection();
320  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
321  $res = $this->call();
322 
323  // Checking status code
324  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
325  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
326  if ($info != self::HTTP_CODE_OK) {
327  $ilLog->write(__METHOD__ . ': Cannot get ressource list, did not receive HTTP 200. ');
328  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
329  }
330  $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
331 
333  } catch (ilCurlConnectionException $exc) {
334  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
335  }
336  }
337 
338 
350  public function getResource($a_path, $a_econtent_id, $a_details_only = false)
351  {
352  global $DIC;
353 
354  $ilLog = $DIC['ilLog'];
355 
356  if ($a_econtent_id) {
357  $ilLog->write(__METHOD__ . ': Get resource with ID: ' . $a_econtent_id);
358  } else {
359  $ilLog->write(__METHOD__ . ': Get all resources ...');
360  }
361 
362  $this->path_postfix = $a_path;
363  if ($a_econtent_id) {
364  $this->path_postfix .= ('/' . (int) $a_econtent_id);
365  }
366  if ($a_details_only) {
367  $this->path_postfix .= ('/details');
368  }
369 
370  try {
371  $this->prepareConnection();
372  $res = $this->call();
373 
374  // Checking status code
375  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
376  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
377  if ($info != self::HTTP_CODE_OK) {
378  $ilLog->write(__METHOD__ . ': Cannot get ressource, did not receive HTTP 200. ');
379  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
380  }
381  $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
382 
383  $result = new ilECSResult($res);
384  $result->setHeaders($this->curl->getResponseHeaderArray());
385  $result->setHTTPCode($info);
386 
387  return $result;
388  } catch (ilCurlConnectionException $exc) {
389  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
390  }
391  }
392 
403  public function addResource($a_path, $a_post)
404  {
405  global $DIC;
406 
407  $ilLog = $DIC['ilLog'];
408 
409  $ilLog->write(__METHOD__ . ': Add new EContent...');
410 
411  $this->path_postfix = $a_path;
412 
413  try {
414  $this->prepareConnection();
415 
416  $this->addHeader('Content-Type', 'application/json');
417 
418  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
419  $this->curl->setOpt(CURLOPT_HEADER, true);
420  $this->curl->setOpt(CURLOPT_POST, true);
421  $this->curl->setOpt(CURLOPT_POSTFIELDS, $a_post);
422  $res = $this->call();
423 
424  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
425 
426  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
427  if ($info != self::HTTP_CODE_CREATED) {
428  $ilLog->write(__METHOD__ . ': Cannot create econtent, did not receive HTTP 201. ');
429  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
430  }
431  $ilLog->write(__METHOD__ . ': ... got HTTP 201 (created)');
432 
433  $eid = self::_fetchEContentIdFromHeader($this->curl->getResponseHeaderArray());
434  return $eid;
435  } catch (ilCurlConnectionException $exc) {
436  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
437  }
438  }
439 
449  public function updateResource($a_path, $a_econtent_id, $a_post_string)
450  {
451  global $DIC;
452 
453  $ilLog = $DIC['ilLog'];
454 
455  $ilLog->write(__METHOD__ . ': Update resource with id ' . $a_econtent_id);
456 
457  $this->path_postfix = $a_path;
458 
459  if ($a_econtent_id) {
460  $this->path_postfix .= ('/' . (int) $a_econtent_id);
461  } else {
462  throw new ilECSConnectorException('Error calling updateResource: No content id given.');
463  }
464  try {
465  $this->prepareConnection();
466  $this->addHeader('Content-Type', 'application/json');
467  $this->addHeader('Accept', 'application/json');
468  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
469  $this->curl->setOpt(CURLOPT_HEADER, true);
470  $this->curl->setOpt(CURLOPT_PUT, true);
471 
472  $tempfile = ilUtil::ilTempnam();
473  $ilLog->write(__METHOD__ . ': Created new tempfile: ' . $tempfile);
474 
475  $fp = fopen($tempfile, 'w');
476  fwrite($fp, $a_post_string);
477  fclose($fp);
478 
479  $this->curl->setOpt(CURLOPT_UPLOAD, true);
480  $this->curl->setOpt(CURLOPT_INFILESIZE, filesize($tempfile));
481  $fp = fopen($tempfile, 'r');
482  $this->curl->setOpt(CURLOPT_INFILE, $fp);
483 
484  $res = $this->call();
485 
486  fclose($fp);
487  unlink($tempfile);
488 
489  return new ilECSResult($res);
490  } catch (ilCurlConnectionException $exc) {
491  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
492  }
493  }
494 
503  public function deleteResource($a_path, $a_econtent_id)
504  {
505  global $DIC;
506 
507  $ilLog = $DIC['ilLog'];
508 
509  $ilLog->write(__METHOD__ . ': Delete resource with id ' . $a_econtent_id);
510 
511  $this->path_postfix = $a_path;
512 
513  if ($a_econtent_id) {
514  $this->path_postfix .= ('/' . (int) $a_econtent_id);
515  } else {
516  throw new ilECSConnectorException('Error calling deleteResource: No content id given.');
517  }
518 
519  try {
520  $this->prepareConnection();
521  $this->curl->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE');
522  $res = $this->call();
523  return new ilECSResult($res);
524  } catch (ilCurlConnectionException $exc) {
525  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
526  }
527  }
528 
530  // membership methods
532 
540  public function getMemberships($a_mid = 0)
541  {
542  global $DIC;
543 
544  $ilLog = $DIC['ilLog'];
545 
546  $ilLog->write(__METHOD__ . ': Get existing memberships');
547 
548  $this->path_postfix = '/sys/memberships';
549  if ($a_mid) {
550  $ilLog->write(__METHOD__ . ': Read membership with id: ' . $a_mid);
551  $this->path_postfix .= ('/' . (int) $a_mid);
552  }
553  try {
554  $this->prepareConnection();
555  $res = $this->call();
556 
557  $this->curl->setOpt(CURLOPT_HTTPHEADER, array(0 => 'X-EcsQueryStrings: sender=true'));
558 
559  // Checking status code
560  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
561  if ($info != self::HTTP_CODE_OK) {
562  $ilLog->write(__METHOD__ . ': Cannot get memberships, did not receive HTTP 200. ');
563  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
564  }
565 
566  return new ilECSResult($res);
567  } catch (ilCurlConnectionException $exc) {
568  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
569  }
570  }
571 
578  protected function prepareConnection()
579  {
580  try {
581  $this->curl = new ilCurlConnection($this->settings->getServerURI() . $this->path_postfix);
582  $this->curl->init(true);
583  $this->curl->setOpt(CURLOPT_HTTPHEADER, array(0 => 'Accept: application/json'));
584  $this->curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
585  $this->curl->setOpt(CURLOPT_TIMEOUT_MS, 2000);
586 
587  if ($this->logger->isHandling(ilLogLevel::DEBUG)) {
588  $this->curl->setOpt(CURLOPT_VERBOSE, 1);
589  }
590 
591  switch ($this->getServer()->getAuthType()) {
593  $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, 0);
594  #$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST,0);
595  $this->curl->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
596  $this->curl->setOpt(
597  CURLOPT_USERPWD,
598  $this->getServer()->getAuthUser() . ':' . $this->getServer()->getAuthPass()
599  );
600  break;
601 
603  $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, 1);
604  // use default 2 for libcurl 7.28.1 support
605  $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2);
606  $this->curl->setOpt(CURLOPT_CAINFO, $this->settings->getCACertPath());
607  $this->curl->setOpt(CURLOPT_SSLCERT, $this->settings->getClientCertPath());
608  $this->curl->setOpt(CURLOPT_SSLKEY, $this->settings->getKeyPath());
609  $this->curl->setOpt(CURLOPT_SSLKEYPASSWD, $this->settings->getKeyPassword());
610  break;
611 
612  }
613  } catch (ilCurlConnectionException $exc) {
614  throw($exc);
615  }
616  }
617 
624  protected function call()
625  {
626  try {
627  $res = $this->curl->exec();
628  return $res;
629  } catch (ilCurlConnectionException $exc) {
630  throw($exc);
631  }
632  }
633 
634 
643  protected static function _fetchEContentIdFromHeader($a_header)
644  {
645  global $DIC;
646 
647  $ilLog = $DIC['ilLog'];
648  $location_parts = [];
649  foreach ($a_header as $header => $value) {
650  if (strcasecmp('Location', $header) == 0) {
651  $location_parts = explode('/', $value);
652  break;
653  }
654  }
655  if (!$location_parts) {
656  $ilLog->write(__METHOD__ . ': Cannot find location headers.');
657  return false;
658  }
659  if (count($location_parts) == 1) {
660  $ilLog->write(__METHOD__ . ': Cannot find path seperator.');
661  return false;
662  }
663  $econtent_id = end($location_parts);
664  $ilLog->write(__METHOD__ . ': Received EContentId ' . $econtent_id);
665  return (int) $econtent_id;
666  }
667 }
const RESULT_TYPE_URL_LIST
settings()
Definition: settings.php:2
$result
addResource($a_path, $a_post)
Add resource.
__construct(ilECSSetting $settings=null)
Constructor.
getAuth($a_hash, $a_details_only=false)
get auth resource
deleteResource($a_path, $a_econtent_id)
Delete resource.
Presentation of ecs content details (http://...campusconnect/courselinks/id/details) ...
init(bool $set_proxy=true)
Init curl connection.
$auth
Definition: metadata.php:59
foreach($_POST as $key=> $value) $res
addAuth($a_post, $a_target_mid)
Add auth resource.
getMemberships($a_mid=0)
public
global $DIC
Definition: goto.php:24
setHeader($a_header_strings)
static _fetchEContentIdFromHeader($a_header)
fetch new econtent id from location header
static ilTempnam($a_temp_path=null)
Returns a unique and non existing Path for e temporary file or directory.
$ret
Definition: parser.php:6
updateResource($a_path, $a_econtent_id, $a_post_string)
update resource
getResource($a_path, $a_econtent_id, $a_details_only=false)
Get resources from ECS server.
getServer()
Get current server setting.
addHeader($a_name, $a_value)
Add Header.
readEventFifo($a_delete=false)
Read event fifo.
prepareConnection()
prepare connection
getEventQueues()
get event queue