ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
17  const TYPE_ICAL = 1;
18 
19  // Fixed in the moment
21 
22  private $curl = null;
23 
24  private $url;
25  private $user;
26  private $pass;
27 
28  private $ical;
29 
30 
35  public function __construct($a_url)
36  {
37  $this->url = $a_url;
38  }
39 
40  public function setUser($a_user)
41  {
42  $this->user = $a_user;
43  }
44 
45  public function setPass($a_pass)
46  {
47  $this->pass = $a_pass;
48  }
49 
50  public function getType()
51  {
52  return $this->type;
53  }
54 
55  public function getUrl()
56  {
57  return $this->url;
58  }
59 
60 
66  public function read()
67  {
68  $this->initCurl();
69 
70  switch($this->getType())
71  {
72  case self::TYPE_ICAL:
73  return $this->readIcal();
74  }
75  }
76 
81  public function import(ilCalendarCategory $cat)
82  {
83  switch($this->getType())
84  {
85  case self::TYPE_ICAL:
86  return $this->importIcal($cat);
87  }
88  }
89 
95  protected function readIcal()
96  {
97  $this->ical = $this->call();
98  $GLOBALS['ilLog']->write(__METHOD__.': '.$this->ical);
99  return true;
100  }
101 
106  protected function importIcal(ilCalendarCategory $cat)
107  {
108  // Delete old appointments
109  include_once('./Services/Calendar/classes/class.ilCalendarCategoryAssignments.php');
111  {
112  include_once('./Services/Calendar/classes/class.ilCalendarEntry.php');
113  ilCalendarEntry::_delete($app_id);
114  }
116 
117  // Import new appointments
118  include_once './Services/Calendar/classes/iCal/class.ilICalParser.php';
119  $parser = new ilICalParser($this->ical, ilICalParser::INPUT_STRING);
120  $parser->setCategoryId($cat->getCategoryID());
121  $parser->parse();
122  }
123 
127  protected function initCurl()
128  {
129  try {
130 
131  $this->replaceWebCalProtocol();
132 
133  $this->curl = new ilCurlConnection($this->getUrl());
134  $this->curl->init();
135  $this->curl->setOpt(CURLOPT_SSL_VERIFYPEER, 0);
136  $this->curl->setOpt(CURLOPT_SSL_VERIFYHOST, 0);
137  $this->curl->setOpt(CURLOPT_RETURNTRANSFER, 1);
138 
139  $this->curl->setOpt(CURLOPT_FOLLOWLOCATION, 1);
140  $this->curl->setOpt(CURLOPT_MAXREDIRS, 3);
141 
142  if($this->user)
143  {
144  $this->curl->setOpt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
145  $this->curl->setOpt(CURLOPT_USERPWD,$this->user.':'.$this->pass);
146  }
147  }
148  catch(Exception $e)
149  {
150  throw $e;
151  }
152  }
153 
154  protected function replaceWebCalProtocol()
155  {
156  if(substr($this->getUrl(), 0, 6) == 'webcal')
157  {
158  $purged = preg_replace('/webcal/', 'http', $this->getUrl(), 1);
159  $this->url = $purged;
160  $GLOBALS['ilLog']->write(__METHOD__.': Using new url: '. $this->getUrl());
161  }
162  }
163 
170  private function call()
171  {
172  try
173  {
174  $res = $this->curl->exec();
175  return $res;
176  }
177  catch(ilCurlConnectionException $exc)
178  {
179  throw($exc);
180  }
181  }
182 
183 
184 
185 }
186 
187 ?>