ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 $DIC;
45 
46  $ilDB = $DIC['ilDB'];
47 
48  $query = "SELECT * FROM cal_auth_token " .
49  "WHERE user_id = " . $ilDB->quote($a_user_id, 'integer') . ' ' .
50  "AND selection = " . $ilDB->quote($a_selection, 'integer') . ' ' .
51  "AND calendar = " . $ilDB->quote($a_calendar, 'integer');
52  $res = $ilDB->query($query);
53  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
54  return $row->hash;
55  }
56  return false;
57  }
58 
64  public static function lookupUser($a_token)
65  {
66  global $DIC;
67 
68  $ilDB = $DIC['ilDB'];
69 
70  $query = "SELECT * FROM cal_auth_token " .
71  "WHERE hash = " . $ilDB->quote($a_token, 'text');
72  $res = $ilDB->query($query);
73  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
74  return $row->user_id;
75  }
76  return 0;
77  }
78 
83  public function getSelectionType()
84  {
85  return $this->selection_type;
86  }
87 
88 
93  public function getUserId()
94  {
95  return $this->user;
96  }
97 
103  public function setSelectionType($a_type)
104  {
105  $this->selection_type = $a_type;
106  }
107 
113  public function setCalendar($a_cal)
114  {
115  $this->calendar = $a_cal;
116  }
117 
118  public function getCalendar()
119  {
120  return $this->calendar;
121  }
122 
123  public function setIcal($ical)
124  {
125  $this->ical = $ical;
126  }
127 
132  public function getIcal()
133  {
134  return $this->ical;
135  }
136 
137 
142  public function getToken()
143  {
144  return $this->token;
145  }
146 
151  public function storeIcal()
152  {
153  global $DIC;
154 
155  $ilDB = $DIC['ilDB'];
156 
157  $ilDB->update(
158  'cal_auth_token',
159  array(
160  'ical' => array('clob',$this->getIcal()),
161  'c_time' => array('integer',time())
162  ),
163  array(
164  'user_id' => array('integer',$this->getUserId()),
165  'hash' => array('text',$this->getToken())
166  )
167  );
168  }
169 
174  public function isIcalExpired()
175  {
176  return true;
177 
178  include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
179 
180  if (!ilCalendarSettings::_getInstance()->isSynchronisationCacheEnabled()) {
181  return true;
182  }
183  if (!ilCalendarSettings::_getInstance()->getSynchronisationCacheMinutes()) {
184  return true;
185  }
186  return time() > ($this->ical_ctime + 60 * ilCalendarSettings::_getInstance()->getSynchronisationCacheMinutes());
187  }
188 
193  public function add()
194  {
195  global $DIC;
196 
197  $ilDB = $DIC['ilDB'];
198 
199  $this->createToken();
200 
201  $query = "INSERT INTO cal_auth_token (user_id,hash,selection,calendar) " .
202  "VALUES ( " .
203  $ilDB->quote($this->getUserId(), 'integer') . ', ' .
204  $ilDB->quote($this->getToken(), 'text') . ', ' .
205  $ilDB->quote($this->getSelectionType(), 'integer') . ', ' .
206  $ilDB->quote($this->getCalendar(), 'integer') . ' ' .
207  ')';
208  $ilDB->manipulate($query);
209 
210  return $this->getToken();
211  }
212 
217  protected function createToken()
218  {
219  $random = new \ilRandom();
220  $this->token = md5($this->getUserId() . $this->getSelectionType() . $random->int());
221  }
222 
227  protected function read()
228  {
229  global $DIC;
230 
231  $ilDB = $DIC['ilDB'];
232 
233  if (!$this->getToken()) {
234  $query = "SELECT * FROM cal_auth_token " .
235  "WHERE user_id = " . $ilDB->quote($this->getUserId(), 'integer');
236  } else {
237  $query = 'SELECT * FROM cal_auth_token ' .
238  'WHERE user_id = ' . $ilDB->quote($this->getUserId(), 'integer') . ' ' .
239  'AND hash = ' . $ilDB->quote($this->getToken(), 'text');
240  }
241 
242  $res = $ilDB->query($query);
243  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
244  $this->token = $row->hash;
245  $this->selection_type = $row->selection;
246  $this->calendar = $row->calendar;
247  $this->ical = $row->ical;
248  $this->ical_ctime = $row->c_time;
249  }
250  return true;
251  }
252 }
static _getInstance()
get singleton instance
isIcalExpired()
Check if cache is disabled or expired.
global $DIC
Definition: saml.php:7
user()
Definition: user.php:4
$a_type
Definition: workflow.php:92
foreach($_POST as $key=> $value) $res
$query
$row
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.
__construct($a_user_id, $a_token='')
Constructor.