ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCalendarAuthenticationToken.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
28  public const SELECTION_NONE = 0;
29  public const SELECTION_PD = 1;
30  public const SELECTION_CATEGORY = 2;
31  public const SELECTION_CALENDAR = 3;
32 
33  private int $user;
34 
35  private string $token = '';
36  private int $selection_type = self::SELECTION_NONE;
37  private int $calendar = 0;
38 
39  private string $ical = '';
40  private int $ical_ctime = 0;
41 
42  protected ilDBInterface $db;
43 
44  public function __construct(int $a_user_id, string $a_token = '')
45  {
46  global $DIC;
47 
48  $this->db = $DIC->database();
49 
50  $this->user = $a_user_id;
51  $this->token = $a_token;
52  $this->read();
53  }
54 
55  public static function lookupAuthToken(int $a_user_id, int $a_selection, int $a_calendar = 0): string
56  {
57  global $DIC;
58 
59  $ilDB = $DIC['ilDB'];
60  $query = "SELECT * FROM cal_auth_token " .
61  "WHERE user_id = " . $ilDB->quote($a_user_id, 'integer') . ' ' .
62  "AND selection = " . $ilDB->quote($a_selection, 'integer') . ' ' .
63  "AND calendar = " . $ilDB->quote($a_calendar, 'integer');
64  $res = $ilDB->query($query);
65  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
66  return $row->hash;
67  }
68  return '';
69  }
70 
71  public static function lookupUser(string $a_token): int
72  {
73  global $DIC;
74 
75  $ilDB = $DIC['ilDB'];
76  $query = "SELECT * FROM cal_auth_token " .
77  "WHERE hash = " . $ilDB->quote($a_token, 'text');
78  $res = $ilDB->query($query);
79  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
80  return (int) $row->user_id;
81  }
82  return 0;
83  }
84 
85  public function getSelectionType(): int
86  {
87  return $this->selection_type;
88  }
89 
90  public function getUserId(): int
91  {
92  return $this->user;
93  }
94 
95  public function setSelectionType(int $a_type): void
96  {
97  $this->selection_type = $a_type;
98  }
99 
100  public function setCalendar(int $a_cal): void
101  {
102  $this->calendar = $a_cal;
103  }
104 
105  public function getCalendar(): int
106  {
107  return $this->calendar;
108  }
109 
110  public function setIcal(string $ical): void
111  {
112  $this->ical = $ical;
113  }
114 
115  public function getIcal(): string
116  {
117  return $this->ical;
118  }
119 
120  public function getToken(): string
121  {
122  return $this->token;
123  }
124 
125  public function storeIcal(): void
126  {
127  $this->db->update(
128  'cal_auth_token',
129  array(
130  'ical' => array('clob', $this->getIcal()),
131  'c_time' => array('integer', time())
132  ),
133  array(
134  'user_id' => array('integer', $this->getUserId()),
135  'hash' => array('text', $this->getToken())
136  )
137  );
138  }
139 
144  public function isIcalExpired(): bool
145  {
146  if (!ilCalendarSettings::_getInstance()->isSynchronisationCacheEnabled()) {
147  return true;
148  }
149  if (!ilCalendarSettings::_getInstance()->getSynchronisationCacheMinutes()) {
150  return true;
151  }
152  return time() > ($this->ical_ctime + 60 * ilCalendarSettings::_getInstance()->getSynchronisationCacheMinutes());
153  }
154 
155  public function add(): string
156  {
157  $this->createToken();
158  $query = "INSERT INTO cal_auth_token (user_id,hash,selection,calendar) " .
159  "VALUES ( " .
160  $this->db->quote($this->getUserId(), 'integer') . ', ' .
161  $this->db->quote($this->getToken(), 'text') . ', ' .
162  $this->db->quote($this->getSelectionType(), 'integer') . ', ' .
163  $this->db->quote($this->getCalendar(), 'integer') . ' ' .
164  ')';
165  $this->db->manipulate($query);
166  return $this->getToken();
167  }
168 
169  protected function createToken(): void
170  {
171  $random = new \Random\Randomizer();
172  $this->token = md5($this->getUserId() . $this->getSelectionType() . $random->nextInt());
173  }
174 
175  protected function read(): bool
176  {
177  if (!$this->getToken()) {
178  $query = "SELECT * FROM cal_auth_token " .
179  "WHERE user_id = " . $this->db->quote($this->getUserId(), 'integer');
180  } else {
181  $query = 'SELECT * FROM cal_auth_token ' .
182  'WHERE user_id = ' . $this->db->quote($this->getUserId(), 'integer') . ' ' .
183  'AND hash = ' . $this->db->quote($this->getToken(), 'text');
184  }
185 
186  $res = $this->db->query($query);
187  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
188  $this->token = (string) $row->hash;
189  $this->selection_type = (int) $row->selection;
190  $this->calendar = (int) $row->calendar;
191  $this->ical = (string) $row->ical;
192  $this->ical_ctime = (int) $row->c_time;
193  }
194  return true;
195  }
196 }
$res
Definition: ltiservices.php:66
calendar()
description: > Example for rendring a calendar glyph.
Definition: calendar.php:41
isIcalExpired()
Check if cache is disabled or expired.
__construct(int $a_user_id, string $a_token='')
static lookupAuthToken(int $a_user_id, int $a_selection, int $a_calendar=0)
global $DIC
Definition: shib_login.php:22
Handles calendar authentication tokens for external calendar subscriptions.