ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilCalendarRemoteAccessHandler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
30 {
31  protected const LIMITED_QUERY_PARAM = 'limited';
32 
34  protected ?Refinery $refinery = null;
35  protected ?HTTPServices $http = null;
36  protected ?ilLogger $logger = null;
37  protected ?ilLanguage $lng = null;
38  protected bool $limit_enabled;
39 
40  public function __construct()
41  {
42  }
43 
45  {
46  return $this->token_handler;
47  }
48 
52  public function parseRequest(): void
53  {
54  // before initialization: $_GET and $_COOKIE is required is unavoidable
55  // in the moment.
56  if ($_GET['client_id']) {
57  $_COOKIE['ilClientId'] = $_GET['client_id'];
58  } else {
59  $path_info_components = explode('/', $_SERVER['PATH_INFO']);
60  $_COOKIE['ilClientId'] = $path_info_components[1];
61  }
62  }
63 
64  public function handleRequest(): bool
65  {
66  session_name('ILCALSESSID');
67  $this->initIlias();
68  $logger = $GLOBALS['DIC']->logger()->cal();
69  $this->initTokenHandler();
70  $this->initLimitEnabled();
71 
72  if (!$this->initUser()) {
73  $logger->warning('Calendar token is invalid. Authentication failed.');
74  return false;
75  }
76 
77  if ($this->getTokenHandler()->getIcal() and !$this->getTokenHandler()->isIcalExpired()) {
78  $GLOBALS['DIC']['ilAuthSession']->logout();
79  ilUtil::deliverData($this->getTokenHandler()->getIcal(), 'calendar.ics', 'text/calendar');
80  exit;
81  }
82 
83  if ($this->getTokenHandler()->getSelectionType() == ilCalendarAuthenticationToken::SELECTION_CALENDAR) {
84  #$export = new ilCalendarExport(array($this->getTokenHandler()->getCalendar()));
86  $cats->initialize(ilCalendarCategories::MODE_REMOTE_SELECTED, $this->getTokenHandler()->getCalendar());
87  $export = new ilCalendarExport($cats->getCategories(true), $this->limit_enabled);
88  } else {
91  $export = new ilCalendarExport($cats->getCategories(true), $this->limit_enabled);
92  }
93 
94  $export->export();
95 
96  $this->getTokenHandler()->setIcal($export->getExportString());
97  $this->getTokenHandler()->storeIcal();
98 
99  $GLOBALS['DIC']['ilAuthSession']->logout();
100  ilUtil::deliverData($export->getExportString(), 'calendar.ics', 'text/calendar');
101  exit;
102  }
103 
104  protected function initTokenHandler(): void
105  {
106  global $DIC;
107 
108  $this->http = $DIC->http();
109  $this->refinery = $DIC->refinery();
110 
111  $token = '';
112  if ($this->http->wrapper()->query()->has('token')) {
113  $token = $this->http->wrapper()->query()->retrieve(
114  'token',
115  $this->refinery->kindlyTo()->string()
116  );
117  }
118  $this->logger->info('Authentication token: ' . $token);
119  $this->token_handler = new ilCalendarAuthenticationToken(
121  $token
122  );
123  }
124 
125  protected function initLimitEnabled(): void
126  {
127  $this->limit_enabled = (bool) ($_GET[self::LIMITED_QUERY_PARAM] ?? false);
128  }
129 
130  protected function initIlias(): void
131  {
132  include_once './Services/Context/classes/class.ilContext.php';
134 
135  include_once './Services/Authentication/classes/class.ilAuthFactory.php';
137 
138  include_once './Services/Init/classes/class.ilInitialisation.php';
140 
141  global $DIC;
142 
143  $this->lng = $DIC->language();
144  $this->lng->loadLanguageModule('dateplaner');
145  $this->logger = $DIC->logger()->cal();
146  }
147 
148  protected function initUser(): bool
149  {
150  global $DIC;
151 
152  if (!$this->getTokenHandler() instanceof ilCalendarAuthenticationToken) {
153  $this->logger->info('Initialisation of authentication token failed');
154  return false;
155  }
156  if (!$this->getTokenHandler()->getUserId()) {
157  $this->logger->info('No user id found for calendar synchronisation');
158  return false;
159  }
160  if (!ilObjUser::_exists($this->getTokenHandler()->getUserId())) {
161  $this->logger->notice('No valid user id found for calendar synchronisation');
162  return false;
163  }
164 
165  $GLOBALS['DIC']['ilAuthSession']->setAuthenticated(true, $this->getTokenHandler()->getUserId());
167 
168  if (!$DIC->user() instanceof ilObjUser) {
169  $this->logger->debug('No user object defined');
170  } else {
171  $this->logger->debug('Current user is: ' . $DIC->user()->getId());
172  }
173  return true;
174  }
175 }
exit
Definition: login.php:28
warning(string $a_message)
parseRequest()
Fetch client id, the chosen calendar...
$path_info_components
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: webdav.php:21
static deliverData(string $a_data, string $a_filename, string $mime="application/octet-stream")
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: feed.php:28
static initILIAS()
ilias initialisation
static _exists(int $id, bool $reference=false, ?string $type=null)
checks if an object exists in object_data
static http()
Fetches the global http state from ILIAS.
$_GET['client_id']
Definition: saml1-acs.php:21
$token
Definition: xapitoken.php:70
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
Export calendar(s) to ical format
const CONTEXT_ICAL
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
static _getInstance($a_usr_id=0)
get singleton instance
static setContext(int $a_context)
set context
static init(string $a_type)
Init context by type.
const CONTEXT_CALENDAR_TOKEN
Calendar authentication with auth token.
Handles calendar authentication tokens for external calendar subscriptions.
Handles requests from external calendar applications
static initUserAccount()
Init user with current account id.
$_COOKIE[session_name()]
Definition: xapitoken.php:54