Path: blob/master/src/applications/calendar/controller/PhabricatorCalendarExportICSController.php
12256 views
<?php12final class PhabricatorCalendarExportICSController3extends PhabricatorCalendarController {45public function shouldRequireLogin() {6// Export URIs are available if you know the secret key. We can't do any7// other kind of authentication because third-party applications like8// Google Calendar and Calendar.app need to be able to fetch these URIs.9return false;10}1112public function handleRequest(AphrontRequest $request) {13$omnipotent = PhabricatorUser::getOmnipotentUser();1415// NOTE: We're using the omnipotent viewer to fetch the export, but the16// URI must contain the secret key. Once we load the export we'll figure17// out who the effective viewer is.18$export = id(new PhabricatorCalendarExportQuery())19->setViewer($omnipotent)20->withSecretKeys(array($request->getURIData('secretKey')))21->executeOne();22if (!$export) {23return new Aphront404Response();24}2526if ($export->getIsDisabled()) {27return new Aphront404Response();28}2930$author = id(new PhabricatorPeopleQuery())31->setViewer($omnipotent)32->withPHIDs(array($export->getAuthorPHID()))33->needUserSettings(true)34->executeOne();35if (!$author) {36return new Aphront404Response();37}3839$mode = $export->getPolicyMode();40switch ($mode) {41case PhabricatorCalendarExport::MODE_PUBLIC:42$viewer = new PhabricatorUser();43break;44case PhabricatorCalendarExport::MODE_PRIVILEGED:45$viewer = $author;46break;47default:48throw new Exception(49pht(50'This export has an invalid mode ("%s").',51$mode));52}5354$engine = id(new PhabricatorCalendarEventSearchEngine())55->setViewer($viewer);5657$query_key = $export->getQueryKey();58$saved = id(new PhabricatorSavedQueryQuery())59->setViewer($omnipotent)60->withEngineClassNames(array(get_class($engine)))61->withQueryKeys(array($query_key))62->executeOne();63if (!$saved) {64$saved = $engine->buildSavedQueryFromBuiltin($query_key);65}6667if (!$saved) {68return new Aphront404Response();69}7071$saved = clone $saved;7273// Mark this as a query for export, so we get the correct ghost/recurring74// behaviors. We also want to load all matching events.75$saved->setParameter('export', true);76$saved->setParameter('limit', 0xFFFF);7778// Remove any range constraints. We always export all matching events into79// ICS files.80$saved->setParameter('rangeStart', null);81$saved->setParameter('rangeEnd', null);82$saved->setParameter('upcoming', null);8384// The "month" and "day" display modes imply time ranges.85$saved->setParameter('display', 'list');8687$query = $engine->buildQueryFromSavedQuery($saved);8889$events = $query90->setViewer($viewer)91->execute();9293return $this->newICSResponse(94$viewer,95$export->getICSFilename(),96$events);97}9899}100101102