ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
61  public function __construct(ilECSSetting $settings = null)
62  {
63  if ($settings) {
64  $this->settings = $settings;
65  } else {
66  $GLOBALS['DIC']['ilLog']->write(__METHOD__ . ': Using deprecated call');
67  $GLOBALS['DIC']['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 $DIC;
118 
119  $ilLog = $DIC['ilLog'];
120 
121  $ilLog->write(__METHOD__ . ': Add new Auth resource...');
122 
123  $this->path_postfix = '/sys/auths';
124 
125  try {
126  $this->prepareConnection();
127 
128  $this->addHeader('Content-Type', 'application/json');
129  $this->addHeader('Accept', 'application/json');
130  $this->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, $a_target_mid);
131  #$this->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, 1);
132 
133  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
134  $this->curl->setOpt(CURLOPT_POST, true);
135  $this->curl->setOpt(CURLOPT_POSTFIELDS, $a_post);
136  $ret = $this->call();
137 
138  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
139 
140  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
141  if ($info != self::HTTP_CODE_CREATED) {
142  $ilLog->write(__METHOD__ . ': Cannot create auth resource, did not receive HTTP 201. ');
143  $ilLog->write(__METHOD__ . ': POST was: ' . $a_post);
144  $ilLog->write(__METHOD__ . ': HTTP code: ' . $info);
145  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
146  }
147  $ilLog->write(__METHOD__ . ': ... got HTTP 201 (created)');
148  $ilLog->write(__METHOD__ . ': POST was: ' . $a_post);
149 
150  $result = new ilECSResult($ret);
151  $auth = $result->getResult();
152 
153  $ilLog->write(__METHOD__ . ': ... got hash: ' . $auth->hash);
154 
155  return $auth->hash;
156  } catch (ilCurlConnectionException $exc) {
157  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
158  }
159  }
160 
168  public function getAuth($a_hash, $a_details_only = false)
169  {
170  global $DIC;
171 
172  $ilLog = $DIC['ilLog'];
173 
174  if (!strlen($a_hash)) {
175  $ilLog->write(__METHOD__ . ': No auth hash given. Aborting.');
176  throw new ilECSConnectorException('No auth hash given.');
177  }
178 
179  $this->path_postfix = '/sys/auths/' . $a_hash;
180 
181  if ($a_details_only) {
182  $this->path_postfix .= ('/details');
183  }
184 
185 
186  try {
187  $this->prepareConnection();
188  $res = $this->call();
189  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
190 
191  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
192  if ($info != self::HTTP_CODE_OK) {
193  $ilLog->write(__METHOD__ . ': Cannot get auth resource, did not receive HTTP 200. ');
194  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
195  }
196  $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
197 
198  $ecs_result = new ilECSResult($res);
199  // Return ECSEContentDetails for details switch
200  if ($a_details_only) {
201  include_once './Services/WebServices/ECS/classes/class.ilECSEContentDetails.php';
202  $details = new ilECSEContentDetails();
203  $details->loadFromJson($ecs_result->getResult());
204  return $details;
205  }
206  return $ecs_result;
207  } catch (ilCurlConnectionException $exc) {
208  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
209  }
210  }
211 
213  // eventqueues methods
215 
223  public function getEventQueues()
224  {
225  global $DIC;
226 
227  $ilLog = $DIC['ilLog'];
228 
229  $this->path_postfix = '/eventqueues';
230 
231  try {
232  $this->prepareConnection();
233 
234  $res = $this->call();
235  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
236 
237  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
238  if ($info != self::HTTP_CODE_OK) {
239  $ilLog->write(__METHOD__ . ': Cannot get event queue, did not receive HTTP 200. ');
240  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
241  }
242  $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
243  return new ilECSResult($res);
244  } catch (ilCurlConnectionException $exc) {
245  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
246  }
247  }
248 
249  #######################################################
250  # event fifo methods
251  #####################################################
252 
258  public function readEventFifo($a_delete = false)
259  {
260  global $DIC;
261 
262  $ilLog = $DIC['ilLog'];
263 
264  $this->path_postfix = '/sys/events/fifo';
265 
266  try {
267  $this->prepareConnection();
268  $this->addHeader('Content-Type', 'application/json');
269  $this->addHeader('Accept', 'application/json');
270 
271  if ($a_delete) {
272  $this->curl->setOpt(CURLOPT_POST, true);
273  $this->curl->setOpt(CURLOPT_POSTFIELDS, '');
274  }
275  $res = $this->call();
276 
277  // Checking status code
278  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
279  #$ilLog->write(__METHOD__.': Checking HTTP status...');
280  if ($info != self::HTTP_CODE_OK) {
281  $ilLog->write(__METHOD__ . ': Cannot read event fifo, did not receive HTTP 200. ');
282  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
283  }
284  #$ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
285 
286  $result = new ilECSResult($res);
287 
288  #$GLOBALS['DIC']['ilLog']->write(__METHOD__.':------------------------------------- FIFO content'. print_r($result,true));
289 
290  return $result;
291  } catch (ilCurlConnectionException $exc) {
292  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
293  } finally {
294  $this->curl->close();
295  }
296  }
297 
299  // econtents methods
301 
302  public function getResourceList($a_path)
303  {
304  global $DIC;
305 
306  $ilLog = $DIC['ilLog'];
307 
308  $this->path_postfix = $a_path;
309 
310  try {
311  $this->prepareConnection();
312  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
313  $res = $this->call();
314 
315  // Checking status code
316  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
317  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
318  if ($info != self::HTTP_CODE_OK) {
319  $ilLog->write(__METHOD__ . ': Cannot get ressource list, did not receive HTTP 200. ');
320  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
321  }
322  $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
323 
325  } catch (ilCurlConnectionException $exc) {
326  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
327  }
328  }
329 
330 
342  public function getResource($a_path, $a_econtent_id, $a_details_only = false)
343  {
344  global $DIC;
345 
346  $ilLog = $DIC['ilLog'];
347 
348  if ($a_econtent_id) {
349  $ilLog->write(__METHOD__ . ': Get resource with ID: ' . $a_econtent_id);
350  } else {
351  $ilLog->write(__METHOD__ . ': Get all resources ...');
352  }
353 
354  $this->path_postfix = $a_path;
355  if ($a_econtent_id) {
356  $this->path_postfix .= ('/' . (int) $a_econtent_id);
357  }
358  if ($a_details_only) {
359  $this->path_postfix .= ('/details');
360  }
361 
362  try {
363  $this->prepareConnection();
364  $res = $this->call();
365 
366  // Checking status code
367  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
368  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
369  if ($info != self::HTTP_CODE_OK) {
370  $ilLog->write(__METHOD__ . ': Cannot get ressource, did not receive HTTP 200. ');
371  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
372  }
373  $ilLog->write(__METHOD__ . ': ... got HTTP 200 (ok)');
374 
375  $result = new ilECSResult($res);
376  $result->setHeaders($this->curl->getResponseHeaderArray());
377  $result->setHTTPCode($info);
378 
379  return $result;
380  } catch (ilCurlConnectionException $exc) {
381  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
382  }
383  }
384 
395  public function addResource($a_path, $a_post)
396  {
397  global $DIC;
398 
399  $ilLog = $DIC['ilLog'];
400 
401  $ilLog->write(__METHOD__ . ': Add new EContent...');
402 
403  $this->path_postfix = $a_path;
404 
405  try {
406  $this->prepareConnection();
407 
408  $this->addHeader('Content-Type', 'application/json');
409 
410  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
411  $this->curl->setOpt(CURLOPT_HEADER, true);
412  $this->curl->setOpt(CURLOPT_POST, true);
413  $this->curl->setOpt(CURLOPT_POSTFIELDS, $a_post);
414  $res = $this->call();
415 
416  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
417 
418  $ilLog->write(__METHOD__ . ': Checking HTTP status...');
419  if ($info != self::HTTP_CODE_CREATED) {
420  $ilLog->write(__METHOD__ . ': Cannot create econtent, did not receive HTTP 201. ');
421  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
422  }
423  $ilLog->write(__METHOD__ . ': ... got HTTP 201 (created)');
424 
425  $eid = self::_fetchEContentIdFromHeader($this->curl->getResponseHeaderArray());
426  return $eid;
427  } catch (ilCurlConnectionException $exc) {
428  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
429  }
430  }
431 
441  public function updateResource($a_path, $a_econtent_id, $a_post_string)
442  {
443  global $DIC;
444 
445  $ilLog = $DIC['ilLog'];
446 
447  $ilLog->write(__METHOD__ . ': Update resource with id ' . $a_econtent_id);
448 
449  $this->path_postfix = $a_path;
450 
451  if ($a_econtent_id) {
452  $this->path_postfix .= ('/' . (int) $a_econtent_id);
453  } else {
454  throw new ilECSConnectorException('Error calling updateResource: No content id given.');
455  }
456  try {
457  $this->prepareConnection();
458  $this->addHeader('Content-Type', 'application/json');
459  $this->addHeader('Accept', 'application/json');
460  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
461  $this->curl->setOpt(CURLOPT_HEADER, true);
462  $this->curl->setOpt(CURLOPT_PUT, true);
463 
464  $tempfile = ilUtil::ilTempnam();
465  $ilLog->write(__METHOD__ . ': Created new tempfile: ' . $tempfile);
466 
467  $fp = fopen($tempfile, 'w');
468  fwrite($fp, $a_post_string);
469  fclose($fp);
470 
471  $this->curl->setOpt(CURLOPT_UPLOAD, true);
472  $this->curl->setOpt(CURLOPT_INFILESIZE, filesize($tempfile));
473  $fp = fopen($tempfile, 'r');
474  $this->curl->setOpt(CURLOPT_INFILE, $fp);
475 
476  $res = $this->call();
477 
478  fclose($fp);
479  unlink($tempfile);
480 
481  return new ilECSResult($res);
482  } catch (ilCurlConnectionException $exc) {
483  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
484  }
485  }
486 
495  public function deleteResource($a_path, $a_econtent_id)
496  {
497  global $DIC;
498 
499  $ilLog = $DIC['ilLog'];
500 
501  $ilLog->write(__METHOD__ . ': Delete resource with id ' . $a_econtent_id);
502 
503  $this->path_postfix = $a_path;
504 
505  if ($a_econtent_id) {
506  $this->path_postfix .= ('/' . (int) $a_econtent_id);
507  } else {
508  throw new ilECSConnectorException('Error calling deleteResource: No content id given.');
509  }
510 
511  try {
512  $this->prepareConnection();
513  $this->curl->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE');
514  $res = $this->call();
515  return new ilECSResult($res);
516  } catch (ilCurlConnectionException $exc) {
517  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
518  }
519  }
520 
522  // membership methods
524 
532  public function getMemberships($a_mid = 0)
533  {
534  global $DIC;
535 
536  $ilLog = $DIC['ilLog'];
537 
538  $ilLog->write(__METHOD__ . ': Get existing memberships');
539 
540  $this->path_postfix = '/sys/memberships';
541  if ($a_mid) {
542  $ilLog->write(__METHOD__ . ': Read membership with id: ' . $a_mid);
543  $this->path_postfix .= ('/' . (int) $a_mid);
544  }
545  try {
546  $this->prepareConnection();
547  $res = $this->call();
548 
549  $this->curl->setOpt(CURLOPT_HTTPHEADER, array(0 => 'X-EcsQueryStrings: sender=true'));
550 
551  // Checking status code
552  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
553  if ($info != self::HTTP_CODE_OK) {
554  $ilLog->write(__METHOD__ . ': Cannot get memberships, did not receive HTTP 200. ');
555  throw new ilECSConnectorException('Received HTTP status code: ' . $info);
556  }
557 
558  return new ilECSResult($res);
559  } catch (ilCurlConnectionException $exc) {
560  throw new ilECSConnectorException('Error calling ECS service: ' . $exc->getMessage());
561  }
562  }
563 
570  protected function prepareConnection()
571  {
572  try {
573  $this->curl = new ilCurlConnection($this->settings->getServerURI() . $this->path_postfix);
574  $this->curl->init();
575  $this->curl->setOpt(CURLOPT_HTTPHEADER, array(0 => 'Accept: application/json'));
576  $this->curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
577  $this->curl->setOpt(CURLOPT_VERBOSE, 1);
578  $this->curl->setOpt(CURLOPT_TIMEOUT_MS, 2000);
579 
580  switch ($this->getServer()->getAuthType()) {
582  $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, 0);
583  #$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST,0);
584  $this->curl->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
585  $this->curl->setOpt(
586  CURLOPT_USERPWD,
587  $this->getServer()->getAuthUser() . ':' . $this->getServer()->getAuthPass()
588  );
589  break;
590 
592  $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, 1);
593  // use default 2 for libcurl 7.28.1 support
594  $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2);
595  $this->curl->setOpt(CURLOPT_CAINFO, $this->settings->getCACertPath());
596  $this->curl->setOpt(CURLOPT_SSLCERT, $this->settings->getClientCertPath());
597  $this->curl->setOpt(CURLOPT_SSLKEY, $this->settings->getKeyPath());
598  $this->curl->setOpt(CURLOPT_SSLKEYPASSWD, $this->settings->getKeyPassword());
599  break;
600 
601  }
602  } catch (ilCurlConnectionException $exc) {
603  throw($exc);
604  }
605  }
606 
613  protected function call()
614  {
615  try {
616  $res = $this->curl->exec();
617  return $res;
618  } catch (ilCurlConnectionException $exc) {
619  throw($exc);
620  }
621  }
622 
623 
632  protected static function _fetchEContentIdFromHeader($a_header)
633  {
634  global $DIC;
635 
636  $ilLog = $DIC['ilLog'];
637 
638  if (!isset($a_header['Location'])) {
639  return false;
640  }
641  $end_path = strrpos($a_header['Location'], "/");
642 
643  if ($end_path === false) {
644  $ilLog->write(__METHOD__ . ': Cannot find path seperator.');
645  return false;
646  }
647  $econtent_id = substr($a_header['Location'], $end_path + 1);
648  $ilLog->write(__METHOD__ . ': Received EContentId ' . $econtent_id);
649  return (int) $econtent_id;
650  }
651 }
const RESULT_TYPE_URL_LIST
settings()
Definition: settings.php:2
$result
global $DIC
Definition: saml.php:7
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) ...
$auth
Definition: fileserver.php:48
init()
Init curl connection.
foreach($_POST as $key=> $value) $res
addAuth($a_post, $a_target_mid)
Add auth resource.
getMemberships($a_mid=0)
public
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
$info
Definition: index.php:5
getEventQueues()
get event queue
$GLOBALS['JPEG_Segment_Names']
Global Variable: XMP_tag_captions.