ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilCalendarAuthenticationToken.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 
13 {
14  const SELECTION_NONE = 0;
15  const SELECTION_PD = 1;
16  const SELECTION_CATEGORY = 2;
17  const SELECTION_CALENDAR = 3;
18 
19  private $user = null;
20 
21  private $token = '';
22  private $selection_type = 0;
23  private $calendar = 0;
24 
25  private $ical = null;
26  private $ical_ctime = null;
27 
34  public function __construct($a_user_id, $a_token = '')
35  {
36  $this->user = $a_user_id;
37  $this->token = $a_token;
38 
39  $this->read();
40  }
41 
42  public static function lookupAuthToken($a_user_id, $a_selection, $a_calendar = 0)
43  {
44  global $ilDB;
45 
46  $query = "SELECT * FROM cal_auth_token " .
47  "WHERE user_id = " . $ilDB->quote($a_user_id, 'integer') . ' ' .
48  "AND selection = " . $ilDB->quote($a_selection, 'integer') . ' ' .
49  "AND calendar = " . $ilDB->quote($a_calendar, 'integer');
50  $res = $ilDB->query($query);
51  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
52  return $row->hash;
53  }
54  return false;
55  }
56 
62  public static function lookupUser($a_token)
63  {
64  global $ilDB;
65 
66  $query = "SELECT * FROM cal_auth_token " .
67  "WHERE hash = " . $ilDB->quote($a_token, 'text');
68  $res = $ilDB->query($query);
69  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
70  return $row->user_id;
71  }
72  return 0;
73  }
74 
79  public function getSelectionType()
80  {
81  return $this->selection_type;
82  }
83 
84 
89  public function getUserId()
90  {
91  return $this->user;
92  }
93 
99  public function setSelectionType($a_type)
100  {
101  $this->selection_type = $a_type;
102  }
103 
109  public function setCalendar($a_cal)
110  {
111  $this->calendar = $a_cal;
112  }
113 
114  public function getCalendar()
115  {
116  return $this->calendar;
117  }
118 
119  public function setIcal($ical)
120  {
121  $this->ical = $ical;
122  }
123 
128  public function getIcal()
129  {
130  return $this->ical;
131  }
132 
133 
138  public function getToken()
139  {
140  return $this->token;
141  }
142 
147  public function storeIcal()
148  {
149  global $ilDB;
150 
151  $ilDB->update(
152  'cal_auth_token',
153  array(
154  'ical' => array('clob',$this->getIcal()),
155  'c_time' => array('integer',time())
156  ),
157  array(
158  'user_id' => array('integer',$this->getUserId()),
159  'hash' => array('text',$this->getToken())
160  )
161  );
162  }
163 
168  public function isIcalExpired()
169  {
170  return true;
171 
172  include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
173 
174  if (!ilCalendarSettings::_getInstance()->isSynchronisationCacheEnabled()) {
175  return true;
176  }
177  if (!ilCalendarSettings::_getInstance()->getSynchronisationCacheMinutes()) {
178  return true;
179  }
180  return time() > ($this->ical_ctime + 60 * ilCalendarSettings::_getInstance()->getSynchronisationCacheMinutes());
181  }
182 
187  public function add()
188  {
189  global $ilDB;
190 
191  $this->createToken();
192 
193  $query = "INSERT INTO cal_auth_token (user_id,hash,selection,calendar) " .
194  "VALUES ( " .
195  $ilDB->quote($this->getUserId(), 'integer') . ', ' .
196  $ilDB->quote($this->getToken(), 'text') . ', ' .
197  $ilDB->quote($this->getSelectionType(), 'integer') . ', ' .
198  $ilDB->quote($this->getCalendar(), 'integer') . ' ' .
199  ')';
200  $ilDB->manipulate($query);
201 
202  return $this->getToken();
203  }
204 
209  protected function createToken()
210  {
211  $random = new \ilRandom();
212  $this->token = md5($this->getUserId() . $this->getSelectionType() . $random->int());
213  }
214 
219  protected function read()
220  {
221  global $ilDB;
222 
223  if (!$this->getToken()) {
224  $query = "SELECT * FROM cal_auth_token " .
225  "WHERE user_id = " . $ilDB->quote($this->getUserId(), 'integer');
226  } else {
227  $query = 'SELECT * FROM cal_auth_token ' .
228  'WHERE user_id = ' . $ilDB->quote($this->getUserId(), 'integer') . ' ' .
229  'AND hash = ' . $ilDB->quote($this->getToken(), 'text');
230  }
231 
232  $res = $ilDB->query($query);
233  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
234  $this->token = $row->hash;
235  $this->selection_type = $row->selection;
236  $this->calendar = $row->calendar;
237  $this->ical = $row->ical;
238  $this->ical_ctime = $row->c_time;
239  }
240  return true;
241  }
242 }
static _getInstance()
get singleton instance
isIcalExpired()
Check if cache is disabled or expired.
user()
Definition: user.php:4
$a_type
Definition: workflow.php:92
foreach($_POST as $key=> $value) $res
$query
Create styles array
The data for the language used.
static lookupUser($a_token)
Lookup user by hash.
static lookupAuthToken($a_user_id, $a_selection, $a_calendar=0)
global $ilDB
Handles calendar authentication tokens for external calendar subscriptions.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
__construct($a_user_id, $a_token='')
Constructor.