ILIAS  eassessment Revision 61809
 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.ilECSSettings.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  protected $path_postfix = '';
45 
46  protected $settings;
47 
55  public function __construct()
56  {
57  $this->settings = ilECSSettings::_getInstance();
58  }
59 
61  // auths methods
63 
73  public function addAuth($a_post)
74  {
75  global $ilLog;
76 
77  $ilLog->write(__METHOD__.': Add new Auth resource...');
78 
79  $this->path_postfix = '/auths';
80 
81  try
82  {
83  $this->prepareConnection();
84  $this->curl->setOpt(CURLOPT_POST,true);
85  $this->curl->setOpt(CURLOPT_POSTFIELDS,$a_post);
86  $res = $this->call();
87 
88  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
89 
90  $ilLog->write(__METHOD__.': Checking HTTP status...');
91  if($info != self::HTTP_CODE_CREATED)
92  {
93  $ilLog->write(__METHOD__.': Cannot create auth resource, did not receive HTTP 201. ');
94  $ilLog->write(__METHOD__.': POST was: '.$a_post);
95  $ilLog->write(__METHOD__.': HTTP code: '.$info);
96  throw new ilECSConnectorException('Received HTTP status code: '.$info);
97  }
98  $ilLog->write(__METHOD__.': ... got HTTP 201 (created)');
99  return true;
100  }
101  catch(ilCurlConnectionException $exc)
102  {
103  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
104  }
105  }
106 
114  public function getAuth($a_hash)
115  {
116  global $ilLog;
117 
118  if(!strlen($a_hash))
119  {
120  $ilLog->write(__METHOD__.': No auth hash given. Aborting.');
121  throw new ilECSConnectorException('No auth hash given.');
122  }
123 
124  $this->path_postfix = '/auths/'.$a_hash;
125 
126  try
127  {
128  $this->prepareConnection();
129  $res = $this->call();
130 
131  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
132 
133  $ilLog->write(__METHOD__.': Checking HTTP status...');
134  if($info != self::HTTP_CODE_OK)
135  {
136  $ilLog->write(__METHOD__.': Cannot get auth resource, did not receive HTTP 200. ');
137  throw new ilECSConnectorException('Received HTTP status code: '.$info);
138  }
139  $ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
140  return new ilECSResult($res);
141  }
142  catch(ilCurlConnectionException $exc)
143  {
144  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
145  }
146  }
147 
149  // eventqueues methods
151 
158  public function getEventQueues()
159  {
160  global $ilLog;
161 
162  $this->path_postfix = '/eventqueues';
163 
164  try
165  {
166  $this->prepareConnection();
167 
168  $res = $this->call();
169 
170  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
171 
172  $ilLog->write(__METHOD__.': Checking HTTP status...');
173  if($info != self::HTTP_CODE_OK)
174  {
175  $ilLog->write(__METHOD__.': Cannot get event queue, did not receive HTTP 200. ');
176  throw new ilECSConnectorException('Received HTTP status code: '.$info);
177  }
178  $ilLog->write(__METHOD__.': ... got HTTP 200 (ok)');
179  return new ilECSResult($res);
180  }
181  catch(ilCurlConnectionException $exc)
182  {
183  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
184  }
185  }
186 
187 
189  // econtents methods
191 
202  public function getResources($a_econtent_id = 0)
203  {
204  global $ilLog;
205 
206  if($a_econtent_id)
207  {
208  $ilLog->write(__METHOD__.': Get resource with ID: '.$a_econtent_id);
209  }
210  else
211  {
212  $ilLog->write(__METHOD__.': Get all resources ...');
213  }
214 
215 
216  $this->path_postfix = '/econtents';
217  if($a_econtent_id)
218  {
219  $this->path_postfix .= ('/'.(int) $a_econtent_id);
220  }
221 
222  try
223  {
224  $this->prepareConnection();
225  $this->curl->setOpt(CURLOPT_HEADER,false);
226  $res = $this->call();
227 
228  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
229 
230  $result = new ilECSResult($res);
231  $result->setHTTPCode($info);
232 
233  return $result;
234  }
235  catch(ilCurlConnectionException $exc)
236  {
237  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
238  }
239  }
240 
250  public function addResource($a_post)
251  {
252  global $ilLog;
253 
254  $ilLog->write(__METHOD__.': Add new EContent...');
255 
256  $this->path_postfix = '/econtents';
257 
258  try
259  {
260  $this->prepareConnection();
261  $this->curl->setOpt(CURLOPT_HEADER,true);
262  $this->curl->setOpt(CURLOPT_POST,true);
263  $this->curl->setOpt(CURLOPT_POSTFIELDS,$a_post);
264  $res = $this->call();
265 
266  $info = $this->curl->getInfo(CURLINFO_HTTP_CODE);
267 
268  $ilLog->write(__METHOD__.': Checking HTTP status...');
269  if($info != self::HTTP_CODE_CREATED)
270  {
271  $ilLog->write(__METHOD__.': Cannot create econtent, did not receive HTTP 201. ');
272  throw new ilECSConnectorException('Received HTTP status code: '.$info);
273  }
274  $ilLog->write(__METHOD__.': ... got HTTP 201 (created)');
275  $result = new ilECSResult($res,true);
276  $headers = $result->getHeaders();
277 
278  include_once('./Services/WebServices/ECS/classes/class.ilECSUtils.php');
280  }
281  catch(ilCurlConnectionException $exc)
282  {
283  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
284  }
285  }
286 
295  public function updateResource($a_econtent_id,$a_post_string)
296  {
297  global $ilLog;
298 
299  $ilLog->write(__METHOD__.': Update resource with id '.$a_econtent_id);
300 
301  $this->path_postfix = '/econtents';
302 
303  if($a_econtent_id)
304  {
305  $this->path_postfix .= ('/'.(int) $a_econtent_id);
306  }
307  else
308  {
309  throw new ilECSConnectorException('Error calling updateResource: No content id given.');
310  }
311  try
312  {
313  $this->prepareConnection();
314  $this->curl->setOpt(CURLOPT_PUT,true);
315 
316  $tempfile = ilUtil::ilTempnam();
317  $ilLog->write(__METHOD__.': Created new tempfile: '.$tempfile);
318 
319  $fp = fopen($tempfile,'w');
320  fwrite($fp,$a_post_string);
321  fclose($fp);
322 
323  #$this->curl->setOpt(CURLOPT_POSTFIELDS,$a_post_string);
324 
325  $this->curl->setOpt(CURLOPT_UPLOAD,true);
326  $this->curl->setOpt(CURLOPT_INFILESIZE,filesize($tempfile));
327  $fp = fopen($tempfile,'r');
328  $this->curl->setOpt(CURLOPT_INFILE,$fp);
329 
330  $res = $this->call();
331  unlink($tempfile);
332  return new ilECSResult($res);
333  }
334  catch(ilCurlConnectionException $exc)
335  {
336  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
337  }
338  }
339 
347  public function deleteResource($a_econtent_id)
348  {
349  global $ilLog;
350 
351  $ilLog->write(__METHOD__.': Delete resource with id '.$a_econtent_id);
352 
353  $this->path_postfix = '/econtents';
354 
355  if($a_econtent_id)
356  {
357  $this->path_postfix .= ('/'.(int) $a_econtent_id);
358  }
359  else
360  {
361  throw new ilECSConnectorException('Error calling deleteResource: No content id given.');
362  }
363 
364  try
365  {
366  $this->prepareConnection();
367  $this->curl->setOpt(CURLOPT_CUSTOMREQUEST,'DELETE');
368  $res = $this->call();
369  return new ilECSResult($res);
370  }
371  catch(ilCurlConnectionException $exc)
372  {
373  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
374  }
375 
376  }
377 
379  // membership methods
381 
389  public function getMemberships($a_mid = 0)
390  {
391  global $ilLog;
392 
393  $ilLog->write(__METHOD__.': Get existing memberships');
394 
395  $this->path_postfix = '/memberships';
396  if($a_mid)
397  {
398  $ilLog->write(__METHOD__.': Read membership with id: '.$a_mid);
399  $this->path_postfix .= ('/'.(int) $a_mid);
400  }
401  try
402  {
403  $this->prepareConnection();
404  $res = $this->call();
405 
406  return new ilECSResult($res);
407  }
408  catch(ilCurlConnectionException $exc)
409  {
410  throw new ilECSConnectorException('Error calling ECS service: '.$exc->getMessage());
411  }
412  }
413 
420  private function prepareConnection()
421  {
422  try
423  {
424  $this->curl = new ilCurlConnection($this->settings->getServerURI().$this->path_postfix);
425  $this->curl->init();
426  if($this->settings->getProtocol() == ilECSSettings::PROTOCOL_HTTPS)
427  {
428  $this->curl->setOpt(CURLOPT_HTTPHEADER,array(0 => 'Accept: application/json'));
429  $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER,1);
430  $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST,1);
431  $this->curl->setOpt(CURLOPT_RETURNTRANSFER,1);
432  $this->curl->setOpt(CURLOPT_VERBOSE,1);
433  $this->curl->setOpt(CURLOPT_CAINFO,$this->settings->getCACertPath());
434  $this->curl->setOpt(CURLOPT_SSLCERT,$this->settings->getClientCertPath());
435  $this->curl->setOpt(CURLOPT_SSLKEY,$this->settings->getKeyPath());
436  $this->curl->setOpt(CURLOPT_SSLKEYPASSWD,$this->settings->getKeyPassword());
437 
438  }
439  }
440  catch(ilCurlConnectionException $exc)
441  {
442  throw($exc);
443  }
444  }
445 
452  private function call()
453  {
454  try
455  {
456  $res = $this->curl->exec();
457  return $res;
458  }
459  catch(ilCurlConnectionException $exc)
460  {
461  throw($exc);
462  }
463  }
464 }
465 ?>