ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups 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 
61  public function __construct(ilECSSetting $settings = null)
62  {
63  if($settings)
64  {
65  $this->settings = $settings;
66  }
67  else
68  {
69  $GLOBALS['ilLog']->write(__METHOD__.': Using deprecated call');
70  $GLOBALS['ilLog']->logStack();
71  }
72  }
73 
74  // Header methods
80  public function addHeader($a_name,$a_value)
81  {
82  $this->header_strings[] = ($a_name.': '.$a_value);
83  }
84 
85  public function getHeader()
86  {
87  return (array) $this->header_strings;
88  }
89 
90  public function setHeader($a_header_strings)
91  {
92  $this->header_strings = $a_header_strings;
93  }
94 
99  public function getServer()
100  {
101  return $this->settings;
102  }
103 
104 
106  // auths methods
108 
118  public function addAuth($a_post,$a_target_mid)
119  {
120  global $ilLog;
121 
122  $ilLog->write(__METHOD__.': Add new Auth resource...');
123 
124  $this->path_postfix = '/sys/auths';
125 
126  try
127  {
128  $this->prepareConnection();
129 
130  $this->addHeader('Content-Type', 'application/json');
131  $this->addHeader('Accept', 'application/json');
132  $this->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, $a_target_mid);
133  #$this->addHeader(ilECSConnector::HEADER_MEMBERSHIPS, 1);
134 
135  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
136  $this->curl->setOpt(CURLOPT_POST,true);
137  $this->curl->setOpt(CURLOPT_POSTFIELDS,$a_post);
138  $ret = $this->call();
139 
140  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
141 
142  $ilLog->write(__METHOD__.': Checking HTTP status...');
143  if($info != self::HTTP_CODE_CREATED)
144  {
145  $ilLog->write(__METHOD__.': Cannot create auth resource, did not receive HTTP 201. ');
146  $ilLog->write(__METHOD__.': POST was: '.$a_post);
147  $ilLog->write(__METHOD__.': HTTP code: '.$info);
148  throw new ilECSConnectorException('Received HTTP status code: '.$info);
149  }
150  $ilLog->write(__METHOD__.': ... got HTTP 201 (created)');
151 
152  $result = new ilECSResult($ret);
153  $auth = $result->getResult();
154 
155  $ilLog->write(__METHOD__.': ... got hash: '.$auth->hash);
156 
157  return $auth->hash;
158  }
159  catch(ilCurlConnectionException $exc)
160  {
161  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
162  }
163  }
164 
172  public function getAuth($a_hash)
173  {
174  global $ilLog;
175 
176  if(!strlen($a_hash))
177  {
178  $ilLog->write(__METHOD__.': No auth hash given. Aborting.');
179  throw new ilECSConnectorException('No auth hash given.');
180  }
181 
182  $this->path_postfix = '/sys/auths/'.$a_hash;
183 
184  try
185  {
186  $this->prepareConnection();
187  $res = $this->call();
188  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
189 
190  $ilLog->write(__METHOD__.': Checking HTTP status...');
191  if($info != self::HTTP_CODE_OK)
192  {
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  return new ilECSResult($res);
198  }
199  catch(ilCurlConnectionException $exc)
200  {
201  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
202  }
203  }
204 
206  // eventqueues methods
208 
216  public function getEventQueues()
217  {
218  global $ilLog;
219 
220  $this->path_postfix = '/eventqueues';
221 
222  try
223  {
224  $this->prepareConnection();
225 
226  $res = $this->call();
227  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
228 
229  $ilLog->write(__METHOD__.': Checking HTTP status...');
230  if($info != self::HTTP_CODE_OK)
231  {
232  $ilLog->write(__METHOD__.': Cannot get event queue, did not receive HTTP 200. ');
233  throw new ilECSConnectorException('Received HTTP status code: '.$info);
234  }
235  $ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
236  return new ilECSResult($res);
237  }
238  catch(ilCurlConnectionException $exc)
239  {
240  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
241  }
242  }
243 
244  #######################################################
245  # event fifo methods
246  #####################################################
247 
253  public function readEventFifo($a_delete = false)
254  {
255  global $ilLog;
256 
257  $this->path_postfix = '/sys/events/fifo';
258 
259  try {
260  $this->prepareConnection();
261  $this->addHeader('Content-Type', 'application/json');
262  $this->addHeader('Accept', 'application/json');
263 
264  if($a_delete)
265  {
266  $this->curl->setOpt(CURLOPT_POST,true);
267  $this->curl->setOpt(CURLOPT_POSTFIELDS, '');
268  }
269  $res = $this->call();
270 
271  // Checking status code
272  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
273  #$ilLog->write(__METHOD__.': Checking HTTP status...');
274  if($info != self::HTTP_CODE_OK)
275  {
276  $ilLog->write(__METHOD__.': Cannot read event fifo, did not receive HTTP 200. ');
277  throw new ilECSConnectorException('Received HTTP status code: '.$info);
278  }
279  #$ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
280 
281  $result = new ilECSResult($res);
282 
283  #$GLOBALS['ilLog']->write(__METHOD__.':------------------------------------- FIFO content'. print_r($result,true));
284 
285  return $result;
286  }
287  catch(ilCurlConnectionException $exc)
288  {
289  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
290  }
291  }
292 
294  // econtents methods
296 
297  public function getResourceList($a_path)
298  {
299  global $ilLog;
300 
301  $this->path_postfix = $a_path;
302 
303  try {
304  $this->prepareConnection();
305  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
306  $res = $this->call();
307 
308  // Checking status code
309  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
310  $ilLog->write(__METHOD__.': Checking HTTP status...');
311  if($info != self::HTTP_CODE_OK)
312  {
313  $ilLog->write(__METHOD__.': Cannot get ressource list, did not receive HTTP 200. ');
314  throw new ilECSConnectorException('Received HTTP status code: '.$info);
315  }
316  $ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
317 
319 
320  }
321  catch(ilCurlConnectionException $exc) {
322  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
323  }
324  }
325 
326 
338  public function getResource($a_path, $a_econtent_id, $a_details_only = false)
339  {
340  global $ilLog;
341 
342  if($a_econtent_id)
343  {
344  $ilLog->write(__METHOD__.': Get resource with ID: '.$a_econtent_id);
345  }
346  else
347  {
348  $ilLog->write(__METHOD__.': Get all resources ...');
349  }
350 
351  $this->path_postfix = $a_path;
352  if($a_econtent_id)
353  {
354  $this->path_postfix .= ('/'.(int) $a_econtent_id);
355  }
356  if($a_details_only)
357  {
358  $this->path_postfix .= ('/details');
359  }
360 
361  try
362  {
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  {
371  $ilLog->write(__METHOD__.': Cannot get ressource, did not receive HTTP 200. ');
372  throw new ilECSConnectorException('Received HTTP status code: '.$info);
373  }
374  $ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
375 
376  $result = new ilECSResult($res);
377  $result->setHeaders($this->curl->getResponseHeaderArray());
378  $result->setHTTPCode($info);
379 
380  return $result;
381  }
382  catch(ilCurlConnectionException $exc)
383  {
384  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
385  }
386  }
387 
398  public function addResource($a_path, $a_post)
399  {
400  global $ilLog;
401 
402  $ilLog->write(__METHOD__.': Add new EContent...');
403 
404  $this->path_postfix = $a_path;
405 
406  try
407  {
408  $this->prepareConnection();
409 
410  $this->addHeader('Content-Type', 'application/json');
411 
412  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
413  $this->curl->setOpt(CURLOPT_HEADER,true);
414  $this->curl->setOpt(CURLOPT_POST,true);
415  $this->curl->setOpt(CURLOPT_POSTFIELDS,$a_post);
416  $res = $this->call();
417 
418  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
419 
420  $ilLog->write(__METHOD__.': Checking HTTP status...');
421  if($info != self::HTTP_CODE_CREATED)
422  {
423  $ilLog->write(__METHOD__.': Cannot create econtent, did not receive HTTP 201. ');
424  throw new ilECSConnectorException('Received HTTP status code: '.$info);
425  }
426  $ilLog->write(__METHOD__.': ... got HTTP 201 (created)');
427 
428  $eid = self::_fetchEContentIdFromHeader($this->curl->getResponseHeaderArray());
429  return $eid;
430  }
431  catch(ilCurlConnectionException $exc)
432  {
433  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
434  }
435  }
436 
446  public function updateResource($a_path, $a_econtent_id,$a_post_string)
447  {
448  global $ilLog;
449 
450  $ilLog->write(__METHOD__.': Update resource with id '.$a_econtent_id);
451 
452  $this->path_postfix = $a_path;
453 
454  if($a_econtent_id)
455  {
456  $this->path_postfix .= ('/'.(int) $a_econtent_id);
457  }
458  else
459  {
460  throw new ilECSConnectorException('Error calling updateResource: No content id given.');
461  }
462  try
463  {
464  $this->prepareConnection();
465  $this->addHeader('Content-Type', 'application/json');
466  $this->addHeader('Accept', 'application/json');
467  $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
468  $this->curl->setOpt(CURLOPT_HEADER,true);
469  $this->curl->setOpt(CURLOPT_PUT,true);
470 
471  $tempfile = ilUtil::ilTempnam();
472  $ilLog->write(__METHOD__.': Created new tempfile: '.$tempfile);
473 
474  $fp = fopen($tempfile,'w');
475  fwrite($fp,$a_post_string);
476  fclose($fp);
477 
478  $this->curl->setOpt(CURLOPT_UPLOAD,true);
479  $this->curl->setOpt(CURLOPT_INFILESIZE,filesize($tempfile));
480  $fp = fopen($tempfile,'r');
481  $this->curl->setOpt(CURLOPT_INFILE,$fp);
482 
483  $res = $this->call();
484 
485  fclose($fp);
486  unlink($tempfile);
487 
488  return new ilECSResult($res);
489  }
490  catch(ilCurlConnectionException $exc)
491  {
492  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
493  }
494  }
495 
504  public function deleteResource($a_path, $a_econtent_id)
505  {
506  global $ilLog;
507 
508  $ilLog->write(__METHOD__.': Delete resource with id '.$a_econtent_id);
509 
510  $this->path_postfix = $a_path;
511 
512  if($a_econtent_id)
513  {
514  $this->path_postfix .= ('/'.(int) $a_econtent_id);
515  }
516  else
517  {
518  throw new ilECSConnectorException('Error calling deleteResource: No content id given.');
519  }
520 
521  try
522  {
523  $this->prepareConnection();
524  $this->curl->setOpt(CURLOPT_CUSTOMREQUEST,'DELETE');
525  $res = $this->call();
526  return new ilECSResult($res);
527  }
528  catch(ilCurlConnectionException $exc)
529  {
530  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
531  }
532 
533  }
534 
536  // membership methods
538 
546  public function getMemberships($a_mid = 0)
547  {
548  global $ilLog;
549 
550  $ilLog->write(__METHOD__.': Get existing memberships');
551 
552  $this->path_postfix = '/sys/memberships';
553  if($a_mid)
554  {
555  $ilLog->write(__METHOD__.': Read membership with id: '.$a_mid);
556  $this->path_postfix .= ('/'.(int) $a_mid);
557  }
558  try
559  {
560  $this->prepareConnection();
561  $res = $this->call();
562 
563  // Checking status code
564  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
565  if($info != self::HTTP_CODE_OK)
566  {
567  $ilLog->write(__METHOD__.': Cannot get memberships, did not receive HTTP 200. ');
568  throw new ilECSConnectorException('Received HTTP status code: '.$info);
569  }
570 
571  return new ilECSResult($res);
572  }
573  catch(ilCurlConnectionException $exc)
574  {
575  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
576  }
577  }
578 
585  protected function prepareConnection()
586  {
587  #$GLOBALS['ilLog']->write(__METHOD__.': '.$this->settings->getServerURI());
588 
589  try
590  {
591  $this->curl = new ilCurlConnection($this->settings->getServerURI().$this->path_postfix);
592  $this->curl->init();
593  $this->curl->setOpt(CURLOPT_HTTPHEADER,array(0 => 'Accept: application/json'));
594  $this->curl->setOpt(CURLOPT_RETURNTRANSFER,1);
595  $this->curl->setOpt(CURLOPT_VERBOSE,1);
596 
597  switch($this->getServer()->getAuthType())
598  {
600  $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER,0);
601  #$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST,0);
602  $this->curl->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
603  $this->curl->setOpt(CURLOPT_USERPWD,
604  $this->getServer()->getAuthUser().':'.$this->getServer()->getAuthPass()
605  );
606  break;
607 
609  $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER,1);
610  // use default 2 for libcurl 7.28.1 support
611  $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST,2);
612  $this->curl->setOpt(CURLOPT_CAINFO,$this->settings->getCACertPath());
613  $this->curl->setOpt(CURLOPT_SSLCERT,$this->settings->getClientCertPath());
614  $this->curl->setOpt(CURLOPT_SSLKEY,$this->settings->getKeyPath());
615  $this->curl->setOpt(CURLOPT_SSLKEYPASSWD,$this->settings->getKeyPassword());
616  break;
617 
618  }
619  }
620  catch(ilCurlConnectionException $exc)
621  {
622  throw($exc);
623  }
624  }
625 
632  protected function call()
633  {
634  try
635  {
636  $res = $this->curl->exec();
637  return $res;
638  }
639  catch(ilCurlConnectionException $exc)
640  {
641  throw($exc);
642  }
643  }
644 
645 
654  protected static function _fetchEContentIdFromHeader($a_header)
655  {
656  global $ilLog;
657 
658  if(!isset($a_header['Location']))
659  {
660  return false;
661  }
662  $end_path = strrpos($a_header['Location'],"/");
663 
664  if($end_path === false)
665  {
666  $ilLog->write(__METHOD__.': Cannot find path seperator.');
667  return false;
668  }
669  $econtent_id = substr($a_header['Location'],$end_path + 1);
670  $ilLog->write(__METHOD__.': Received EContentId '.$econtent_id);
671  return (int) $econtent_id;
672  }
673 }
674 ?>