ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilCalendarRemoteReader.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
5 include_once './Services/WebServices/Curl/classes/class.ilCurlConnection.php';
6 include_once './Services/WebServices/Curl/classes/class.ilCurlConnectionException.php';
7 
15 {
16  const TYPE_ICAL = 1;
17 
18  // Fixed in the moment
19  private $type = self::TYPE_ICAL;
20 
21  private $curl = null;
22 
23  private $url;
24  private $user;
25  private $pass;
26 
27  private $ical;
28 
32  private $logger;
33 
34 
39  public function __construct($a_url)
40  {
41  global $DIC;
42 
43  $this->logger = $DIC->logger();
44  $this->url = $a_url;
45  }
46 
47  public function setUser($a_user)
48  {
49  $this->user = $a_user;
50  }
51 
52  public function setPass($a_pass)
53  {
54  $this->pass = $a_pass;
55  }
56 
57  public function getType()
58  {
59  return $this->type;
60  }
61 
62  public function getUrl()
63  {
64  return $this->url;
65  }
66 
67 
73  public function read()
74  {
75  $this->initCurl();
76 
77  switch ($this->getType()) {
78  case self::TYPE_ICAL:
79  return $this->readIcal();
80  }
81  }
82 
87  public function import(ilCalendarCategory $cat)
88  {
89  switch ($this->getType()) {
90  case self::TYPE_ICAL:
91  return $this->importIcal($cat);
92  }
93  }
94 
100  protected function readIcal()
101  {
102  $this->ical = $this->call();
103  $this->logger->debug($this->ical);
104  return true;
105  }
106 
111  protected function importIcal(ilCalendarCategory $cat)
112  {
113  // Delete old appointments
114  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
115  foreach (ilCalendarCategoryAssignments::_getAssignedAppointments(array($cat->getCategoryID())) as $app_id) {
116  include_once('./Services/Calendar/classes/class.ilCalendarEntry.php');
117  ilCalendarEntry::_delete($app_id);
118  }
120 
121  // Import new appointments
122  include_once './Services/Calendar/classes/iCal/class.ilICalParser.php';
123  $parser = new ilICalParser($this->ical, ilICalParser::INPUT_STRING);
124  $parser->setCategoryId($cat->getCategoryID());
125  $parser->parse();
126  }
127 
131  protected function initCurl()
132  {
133  try {
134  $this->replaceWebCalProtocol();
135 
136  $this->curl = new ilCurlConnection($this->getUrl());
137  $this->curl->init();
138 
139  if (ilProxySettings::_getInstance()->isActive()) {
140  $this->curl->setOpt(CURLOPT_HTTPPROXYTUNNEL, true);
141  $this->curl->setOpt(CURLOPT_PROXY, ilProxySettings::_getInstance()->getHost());
142  $this->curl->setOpt(CURLOPT_PROXYPORT, ilProxySettings::_getInstance()->getPort());
143  }
144 
145  $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, 0);
146  $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, 0);
147  $this->curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
148 
149  $this->curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
150  $this->curl->setOpt(CURLOPT_MAXREDIRS, 3);
151 
152  if ($this->user) {
153  $this->curl->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
154  $this->curl->setOpt(CURLOPT_USERPWD, $this->user . ':' . $this->pass);
155  }
156  } catch (Exception $e) {
157  throw $e;
158  }
159  }
160 
161  protected function replaceWebCalProtocol()
162  {
163  if (substr($this->getUrl(), 0, 6) == 'webcal') {
164  $purged = preg_replace('/webcal/', 'http', $this->getUrl(), 1);
165  $this->url = $purged;
166  }
167  }
168 
175  private function call()
176  {
177  try {
178  $res = $this->curl->exec();
179  return $res;
180  } catch (ilCurlConnectionException $exc) {
181  throw($exc);
182  }
183  }
184 }
global $DIC
Definition: saml.php:7
Stores calendar categories.
Reader for remote ical calendars.
user()
Definition: user.php:4
static _delete($a_entry_id)
delete entry
foreach($_POST as $key=> $value) $res
importIcal(ilCalendarCategory $cat)
Import ical in calendar.
__construct($a_url)
Constructor init curl.
static _deleteByCategoryId($a_cat_id)
Delete assignments by category id.
static _getAssignedAppointments($a_cat_id)
Get assigned apointments.
$parser
Definition: BPMN2Parser.php:23
getCategoryID()
get category id
static _getInstance()
Getter for unique instance.