ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
18  private $user = null;
19 
20  private $token = '';
21  private $selection_type = 0;
22  private $calendar = 0;
23 
24  private $ical = null;
25  private $ical_ctime = null;
26 
33  public function __construct($a_user_id,$a_token = '')
34  {
35  $this->user = $a_user_id;
36  $this->token = $a_token;
37 
38  $this->read();
39  }
40 
41  public static function lookupAuthToken($a_user_id, $a_selection,$a_calendar = 0)
42  {
43  global $ilDB;
44 
45  $query = "SELECT * FROM cal_auth_token ".
46  "WHERE user_id = ".$ilDB->quote($a_user_id,'integer').' '.
47  "AND selection = ".$ilDB->quote($a_selection,'integer').' '.
48  "AND calendar = ".$ilDB->quote($a_calendar,'integer');
49  $res = $ilDB->query($query);
50  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
51  {
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(DB_FETCHMODE_OBJECT))
70  {
71  return $row->user_id;
72  }
73  return 0;
74  }
75 
80  public function getSelectionType()
81  {
82  return $this->selection_type;
83  }
84 
85 
90  public function getUserId()
91  {
92  return $this->user;
93  }
94 
100  public function setSelectionType($a_type)
101  {
102  $this->selection_type = $a_type;
103  }
104 
110  public function setCalendar($a_cal)
111  {
112  $this->calendar = $a_cal;
113  }
114 
115  public function getCalendar()
116  {
117  return $this->calendar;
118  }
119 
120  public function setIcal($ical)
121  {
122  $this->ical = $ical;
123  }
124 
129  public function getIcal()
130  {
131  return $this->ical;
132  }
133 
134 
139  public function getToken()
140  {
141  return $this->token;
142  }
143 
148  public function storeIcal()
149  {
150  global $ilDB;
151 
152  $ilDB->update(
153  'cal_auth_token',
154  array(
155  'ical' => array('clob',$this->getIcal()),
156  'c_time' => array('integer',time())
157  ),
158  array(
159  'user_id' => array('integer',$this->getUserId()),
160  'hash' => array('text',$this->getToken())
161  )
162  );
163  }
164 
169  public function isIcalExpired()
170  {
171  include_once './Services/Calendar/classes/class.ilCalendarSettings.php';
172 
173  if(!ilCalendarSettings::_getInstance()->isSynchronisationCacheEnabled())
174  {
175  return true;
176  }
177  if(!ilCalendarSettings::_getInstance()->getSynchronisationCacheMinutes())
178  {
179  return true;
180  }
181  return time() > ($this->ical_ctime + 60 * ilCalendarSettings::_getInstance()->getSynchronisationCacheMinutes());
182  }
183 
188  public function add()
189  {
190  global $ilDB;
191 
192  $this->createToken();
193 
194  $query = "INSERT INTO cal_auth_token (user_id,hash,selection,calendar) ".
195  "VALUES ( ".
196  $ilDB->quote($this->getUserId(),'integer').', '.
197  $ilDB->quote($this->getToken(),'text').', '.
198  $ilDB->quote($this->getSelectionType(),'integer').', '.
199  $ilDB->quote($this->getCalendar(),'integer').' '.
200  ')';
201  $ilDB->manipulate($query);
202 
203  return $this->getToken();
204  }
205 
210  protected function createToken()
211  {
212  $this->token = md5($this->getUserId().$this->getSelectionType().rand());
213  }
214 
219  protected function read()
220  {
221  global $ilDB;
222 
223  $query = "SELECT * FROM cal_auth_token ".
224  "WHERE user_id = ".$ilDB->quote($this->getUserId(),'integer');
225 
226  $res = $ilDB->query($query);
227  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
228  {
229  $this->token = $row->hash;
230  $this->selection_type = $row->selection;
231  $this->calendar = $row->calendar;
232  $this->ical = $row->ical;
233  $this->ical_ctime = $row->c_time;
234  }
235  return true;
236  }
237 }
238 ?>