function ReturnICal($arParams)
{
$calendarId = $arParams['calendarId'];
$userId = $arParams['userId'];
$sign = $arParams['sign'];
$arParams['ownerType'] = mb_strtoupper($arParams['ownerType']);
$ownerType = (isset($arParams['ownerType']) && in_array($arParams['ownerType'], array('GROUP', 'USER'))) ? $arParams['ownerType'] : false;
$ownerId = isset($arParams['ownerId']) ? intval($arParams['ownerId']) : $this->ownerId;
$iblockId = (isset($arParams['iblockId']) && intval($arParams['iblockId']) > 0) ? intval($arParams['iblockId']) : $this->iblockId;
$GLOBALS['APPLICATION']->RestartBuffer();
if (!$this->CheckSign($sign, $userId, $calendarId))
return CEventCalendar::ThrowError(GetMessage('EC_ACCESS_DENIED'));
$this->bCurUserOwner = $ownerType != 'USER' || $ownerId == $userId;
$privateStatus = CECCalendar::GetPrivateStatus($iblockId, $calendarId, $ownerType);
if (!$this->arCalenderIndex[$calendarId]) // For get events check
$this->arCalenderIndex[$calendarId] = array('PRIVATE_STATUS' => $privateStatus);
if ($this->bCache)
{
$cache = new CPHPCache;
$cacheId = serialize(array($iblockId, $calendarId));
$cachePath = $this->cachePath.'ical/';
if ($cache->InitCache($this->cacheTime, $cacheId, $cachePath))
{
$res = $cache->GetVars();
$iCalEvents = $res['iCalEvents'];
}
}
if (!$this->bCache || empty($iCalEvents))
{
// Get iblock permissions
$arGroups = CUser::GetUserGroup($userId);
$arGroupPerm = CIBlock::GetGroupPermissions($iblockId);
$maxPerm = 'D';
foreach($arGroupPerm as $k => $perm)
if (in_array($k, $arGroups) && $perm > $maxPerm)
$maxPerm = $perm;
// Check permissions
if (($maxPerm < 'R') // iblock
||
(
$ownerType == 'USER' && // socnet: USER
(
!CSocNetFeatures::IsActiveFeature(SONET_ENTITY_USER, $ownerId, "calendar")
||
!CSocNetFeaturesPerms::CanPerformOperation($userId, SONET_ENTITY_USER, $ownerId, "calendar", 'view')
)
)
||
(
$ownerType == 'GROUP' && // socnet: GROUP
(
!CSocNetFeatures::IsActiveFeature(SONET_ENTITY_GROUP, $ownerId, "calendar")
||
!CSocNetFeaturesPerms::CanPerformOperation($userId, SONET_ENTITY_GROUP, $ownerId, "calendar", 'view')
)
)
)
return CEventCalendar::ThrowError(GetMessage('EC_ACCESS_DENIED'));
// === Fetch events from calendar ===
$r = CIBlockSection::GetList(Array(), Array("ID"=>$calendarId, "CHECK_PERMISSIONS"=>"N"));
if(!($arCal = $r->Fetch()))
return CEventCalendar::ThrowError('INCORRECT CALENDAR ID');
$arExport = CECCalendar::GetExportParams($iblockId, $calendarId, $ownerType, $ownerId); // LINK is incorrect, but we dont need for LINK...
if (!$arExport['ALLOW']) // Calendar is not accessible for export
return CEventCalendar::ThrowError(GetMessage('EC_ACCESS_DENIED'));
if ($arExport['SET'] == 'all') // Get all events
{
$bLoadAll =true;
$from_limit = $to_limit = false;
}
else // Get events from some period
{
$bLoadAll = false;
if ($arExport['SET'] == '3_9') // 3 month ago and 9 future
{
$ago = 3;
$future = 9;
}
else// if ($arExport['SET'] == '6_12')
{
$ago = 6;
$future = 12;
}
$from_limit = date(getDateFormat(false), mktime(0, 0, 0, date("m") - $ago, 1, date("Y")));
$to_limit = date(getDateFormat(false), mktime(0, 0, 0, date("m") + $future + 1, 1, date("Y")));
}
$arItems = $this->GetEvents(array(
'ownerType' => $ownerType,
'ownerId' => $ownerId,
'iblockId' => $iblockId,
'sectionId' => $calendarId,
'fromLimit' => $from_limit,
'toLimit' => $to_limit,
'bLoadAll' => $bLoadAll,
'arCalendarIds' => false,
'forExport' => true
));
$iCalEvents = $this->FormatICal($arCal, $arItems);
if ($this->bCache) // cache data
{
$cache->StartDataCache($this->cacheTime, $cacheId, $cachePath);
$cache->EndDataCache(array("iCalEvents" => $iCalEvents));
}
}
$this->ShowICalHeaders();
echo $iCalEvents;
exit();
}