ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 {
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 $ilLog->write(__METHOD__.': POST was: '.$a_post);
152
153 $result = new ilECSResult($ret);
154 $auth = $result->getResult();
155
156 $ilLog->write(__METHOD__.': ... got hash: '.$auth->hash);
157
158 return $auth->hash;
159 }
160 catch(ilCurlConnectionException $exc)
161 {
162 throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
163 }
164 }
165
173 public function getAuth($a_hash, $a_details_only = FALSE)
174 {
175 global $ilLog;
176
177 if(!strlen($a_hash))
178 {
179 $ilLog->write(__METHOD__.': No auth hash given. Aborting.');
180 throw new ilECSConnectorException('No auth hash given.');
181 }
182
183 $this->path_postfix = '/sys/auths/'.$a_hash;
184
185 if($a_details_only)
186 {
187 $this->path_postfix .= ('/details');
188 }
189
190
191 try
192 {
193 $this->prepareConnection();
194 $res = $this->call();
195 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
196
197 $ilLog->write(__METHOD__.': Checking HTTP status...');
198 if($info != self::HTTP_CODE_OK)
199 {
200 $ilLog->write(__METHOD__.': Cannot get auth resource, did not receive HTTP 200. ');
201 throw new ilECSConnectorException('Received HTTP status code: '.$info);
202 }
203 $ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
204
205 $ecs_result = new ilECSResult($res);
206 // Return ECSEContentDetails for details switch
207 if($a_details_only)
208 {
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 }
216 catch(ilCurlConnectionException $exc)
217 {
218 throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
219 }
220 }
221
223 // eventqueues methods
225
233 public function getEventQueues()
234 {
235 global $ilLog;
236
237 $this->path_postfix = '/eventqueues';
238
239 try
240 {
241 $this->prepareConnection();
242
243 $res = $this->call();
244 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
245
246 $ilLog->write(__METHOD__.': Checking HTTP status...');
247 if($info != self::HTTP_CODE_OK)
248 {
249 $ilLog->write(__METHOD__.': Cannot get event queue, did not receive HTTP 200. ');
250 throw new ilECSConnectorException('Received HTTP status code: '.$info);
251 }
252 $ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
253 return new ilECSResult($res);
254 }
255 catch(ilCurlConnectionException $exc)
256 {
257 throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
258 }
259 }
260
261 #######################################################
262 # event fifo methods
263 #####################################################
270 public function readEventFifo($a_delete = false)
271 {
272 global $ilLog;
273
274 $this->path_postfix = '/sys/events/fifo';
275
276 try {
277 $this->prepareConnection();
278 $this->addHeader('Content-Type', 'application/json');
279 $this->addHeader('Accept', 'application/json');
280
281 if($a_delete)
282 {
283 $this->curl->setOpt(CURLOPT_POST,true);
284 $this->curl->setOpt(CURLOPT_POSTFIELDS, '');
285 }
286 $res = $this->call();
287
288 // Checking status code
289 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
290 #$ilLog->write(__METHOD__.': Checking HTTP status...');
291 if($info != self::HTTP_CODE_OK)
292 {
293 $ilLog->write(__METHOD__.': Cannot read event fifo, did not receive HTTP 200. ');
294 throw new ilECSConnectorException('Received HTTP status code: '.$info);
295 }
296 #$ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
297
298 $result = new ilECSResult($res);
299
300 #$GLOBALS['ilLog']->write(__METHOD__.':------------------------------------- FIFO content'. print_r($result,true));
301
302 return $result;
303 }
304 catch(ilCurlConnectionException $exc)
305 {
306 throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
307 }
308 }
309
311 // econtents methods
313
314 public function getResourceList($a_path)
315 {
316 global $ilLog;
317
318 $this->path_postfix = $a_path;
319
320 try {
321 $this->prepareConnection();
322 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
323 $res = $this->call();
324
325 // Checking status code
326 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
327 $ilLog->write(__METHOD__.': Checking HTTP status...');
328 if($info != self::HTTP_CODE_OK)
329 {
330 $ilLog->write(__METHOD__.': Cannot get ressource list, did not receive HTTP 200. ');
331 throw new ilECSConnectorException('Received HTTP status code: '.$info);
332 }
333 $ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
334
336
337 }
338 catch(ilCurlConnectionException $exc) {
339 throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
340 }
341 }
342
343
355 public function getResource($a_path, $a_econtent_id, $a_details_only = false)
356 {
357 global $ilLog;
358
359 if($a_econtent_id)
360 {
361 $ilLog->write(__METHOD__.': Get resource with ID: '.$a_econtent_id);
362 }
363 else
364 {
365 $ilLog->write(__METHOD__.': Get all resources ...');
366 }
367
368 $this->path_postfix = $a_path;
369 if($a_econtent_id)
370 {
371 $this->path_postfix .= ('/'.(int) $a_econtent_id);
372 }
373 if($a_details_only)
374 {
375 $this->path_postfix .= ('/details');
376 }
377
378 try
379 {
380 $this->prepareConnection();
381 $res = $this->call();
382
383 // Checking status code
384 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
385 $ilLog->write(__METHOD__.': Checking HTTP status...');
386 if($info != self::HTTP_CODE_OK)
387 {
388 $ilLog->write(__METHOD__.': Cannot get ressource, did not receive HTTP 200. ');
389 throw new ilECSConnectorException('Received HTTP status code: '.$info);
390 }
391 $ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
392
393 $result = new ilECSResult($res);
394 $result->setHeaders($this->curl->getResponseHeaderArray());
395 $result->setHTTPCode($info);
396
397 return $result;
398 }
399 catch(ilCurlConnectionException $exc)
400 {
401 throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
402 }
403 }
404
415 public function addResource($a_path, $a_post)
416 {
417 global $ilLog;
418
419 $ilLog->write(__METHOD__.': Add new EContent...');
420
421 $this->path_postfix = $a_path;
422
423 try
424 {
425 $this->prepareConnection();
426
427 $this->addHeader('Content-Type', 'application/json');
428
429 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
430 $this->curl->setOpt(CURLOPT_HEADER,true);
431 $this->curl->setOpt(CURLOPT_POST,true);
432 $this->curl->setOpt(CURLOPT_POSTFIELDS,$a_post);
433 $res = $this->call();
434
435 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
436
437 $ilLog->write(__METHOD__.': Checking HTTP status...');
438 if($info != self::HTTP_CODE_CREATED)
439 {
440 $ilLog->write(__METHOD__.': Cannot create econtent, did not receive HTTP 201. ');
441 throw new ilECSConnectorException('Received HTTP status code: '.$info);
442 }
443 $ilLog->write(__METHOD__.': ... got HTTP 201 (created)');
444
445 $eid = self::_fetchEContentIdFromHeader($this->curl->getResponseHeaderArray());
446 return $eid;
447 }
448 catch(ilCurlConnectionException $exc)
449 {
450 throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
451 }
452 }
453
463 public function updateResource($a_path, $a_econtent_id,$a_post_string)
464 {
465 global $ilLog;
466
467 $ilLog->write(__METHOD__.': Update resource with id '.$a_econtent_id);
468
469 $this->path_postfix = $a_path;
470
471 if($a_econtent_id)
472 {
473 $this->path_postfix .= ('/'.(int) $a_econtent_id);
474 }
475 else
476 {
477 throw new ilECSConnectorException('Error calling updateResource: No content id given.');
478 }
479 try
480 {
481 $this->prepareConnection();
482 $this->addHeader('Content-Type', 'application/json');
483 $this->addHeader('Accept', 'application/json');
484 $this->curl->setOpt(CURLOPT_HTTPHEADER, $this->getHeader());
485 $this->curl->setOpt(CURLOPT_HEADER,true);
486 $this->curl->setOpt(CURLOPT_PUT,true);
487
488 $tempfile = ilUtil::ilTempnam();
489 $ilLog->write(__METHOD__.': Created new tempfile: '.$tempfile);
490
491 $fp = fopen($tempfile,'w');
492 fwrite($fp,$a_post_string);
493 fclose($fp);
494
495 $this->curl->setOpt(CURLOPT_UPLOAD,true);
496 $this->curl->setOpt(CURLOPT_INFILESIZE,filesize($tempfile));
497 $fp = fopen($tempfile,'r');
498 $this->curl->setOpt(CURLOPT_INFILE,$fp);
499
500 $res = $this->call();
501
502 fclose($fp);
503 unlink($tempfile);
504
505 return new ilECSResult($res);
506 }
507 catch(ilCurlConnectionException $exc)
508 {
509 throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
510 }
511 }
512
521 public function deleteResource($a_path, $a_econtent_id)
522 {
523 global $ilLog;
524
525 $ilLog->write(__METHOD__.': Delete resource with id '.$a_econtent_id);
526
527 $this->path_postfix = $a_path;
528
529 if($a_econtent_id)
530 {
531 $this->path_postfix .= ('/'.(int) $a_econtent_id);
532 }
533 else
534 {
535 throw new ilECSConnectorException('Error calling deleteResource: No content id given.');
536 }
537
538 try
539 {
540 $this->prepareConnection();
541 $this->curl->setOpt(CURLOPT_CUSTOMREQUEST,'DELETE');
542 $res = $this->call();
543 return new ilECSResult($res);
544 }
545 catch(ilCurlConnectionException $exc)
546 {
547 throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
548 }
549
550 }
551
553 // membership methods
555
563 public function getMemberships($a_mid = 0)
564 {
565 global $ilLog;
566
567 $ilLog->write(__METHOD__.': Get existing memberships');
568
569 $this->path_postfix = '/sys/memberships';
570 if($a_mid)
571 {
572 $ilLog->write(__METHOD__.': Read membership with id: '.$a_mid);
573 $this->path_postfix .= ('/'.(int) $a_mid);
574 }
575 try
576 {
577 $this->prepareConnection();
578 $res = $this->call();
579
580 $this->curl->setOpt(CURLOPT_HTTPHEADER,array(0 => 'X-EcsQueryStrings: sender=true'));
581
582 // Checking status code
583 $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
584 if($info != self::HTTP_CODE_OK)
585 {
586 $ilLog->write(__METHOD__.': Cannot get memberships, did not receive HTTP 200. ');
587 throw new ilECSConnectorException('Received HTTP status code: '.$info);
588 }
589
590 return new ilECSResult($res);
591 }
592 catch(ilCurlConnectionException $exc)
593 {
594 throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
595 }
596 }
597
604 protected function prepareConnection()
605 {
606
607 try
608 {
609 $this->curl = new ilCurlConnection($this->settings->getServerURI().$this->path_postfix);
610 $this->curl->init();
611 $this->curl->setOpt(CURLOPT_HTTPHEADER,array(0 => 'Accept: application/json'));
612 $this->curl->setOpt(CURLOPT_RETURNTRANSFER,1);
613 $this->curl->setOpt(CURLOPT_VERBOSE,1);
614 $this->curl->setOpt(CURLOPT_TIMEOUT_MS,2000);
615
616 switch($this->getServer()->getAuthType())
617 {
619 $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER,0);
620 #$this->curl->setOpt(CURLOPT_SSL_VERIFYHOST,0);
621 $this->curl->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
622 $this->curl->setOpt(CURLOPT_USERPWD,
623 $this->getServer()->getAuthUser().':'.$this->getServer()->getAuthPass()
624 );
625 break;
626
628 $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER,1);
629 // use default 2 for libcurl 7.28.1 support
630 $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST,2);
631 $this->curl->setOpt(CURLOPT_CAINFO,$this->settings->getCACertPath());
632 $this->curl->setOpt(CURLOPT_SSLCERT,$this->settings->getClientCertPath());
633 $this->curl->setOpt(CURLOPT_SSLKEY,$this->settings->getKeyPath());
634 $this->curl->setOpt(CURLOPT_SSLKEYPASSWD,$this->settings->getKeyPassword());
635 break;
636
637 }
638 }
639 catch(ilCurlConnectionException $exc)
640 {
641 throw($exc);
642 }
643 }
644
651 protected function call()
652 {
653 try
654 {
655 $res = $this->curl->exec();
656 return $res;
657 }
658 catch(ilCurlConnectionException $exc)
659 {
660 throw($exc);
661 }
662 }
663
664
673 protected static function _fetchEContentIdFromHeader($a_header)
674 {
675 global $ilLog;
676
677 if(!isset($a_header['Location']))
678 {
679 return false;
680 }
681 $end_path = strrpos($a_header['Location'],"/");
682
683 if($end_path === false)
684 {
685 $ilLog->write(__METHOD__.': Cannot find path seperator.');
686 return false;
687 }
688 $econtent_id = substr($a_header['Location'],$end_path + 1);
689 $ilLog->write(__METHOD__.': Received EContentId '.$econtent_id);
690 return (int) $econtent_id;
691 }
692}
693?>
$result
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
getAuth($a_hash, $a_details_only=FALSE)
get auth resource
getServer()
Get current server setting.
getEventQueues()
get event queue
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.
$info
Definition: example_052.php:80
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276