forked from hiorgserver/HiOrg-Joomla-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hiorg.php
123 lines (104 loc) · 2.65 KB
/
hiorg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* @package DPCalendar - HiOrg-Server
* @author HiOrg Server GmbH https://www.hiorg-server.de
* @copyright Copyright (c) HiOrg Server GmbH / Digital Peak. All rights reserved.
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*/
defined('_JEXEC') or die;
if (!JLoader::import('components.com_dpcalendar.helpers.dpcalendar', JPATH_ADMINISTRATOR))
{
return;
}
class PlgDPCalendarHiorg extends \DPCalendar\Plugin\SyncPlugin
{
/**
* Plugin identifier
*
* @var string
*/
protected $identifier = 'hiorg';
/**
* Retrun the ICal URL
*
* @param \stdClass $calendar
*
* @return string
*/
protected function getIcalUrl($calendar)
{
return 'https://www.hiorg-server.de/termine.php?ical=1&ov='. $calendar->params->get('uri');
}
/**
* Getting the sync token to determine if a full sync needs to be done.
*
* @param \stdClass $calendar
*
* @return boolean
*/
protected function getSyncToken($calendar)
{
$uri = $this->getIcalUrl($calendar);
if (!$uri)
{
return rand();
}
$internal = !filter_var($uri, FILTER_VALIDATE_URL);
if ($internal && strpos($uri, '/') !== 0)
{
$uri = JPATH_ROOT . DS . $uri;
}
$syncToken = rand();
if ($internal)
{
$timestamp = filemtime($uri);
if ($timestamp)
{
$syncToken = $timestamp;
}
}
else
{
$http = \JHttpFactory::getHttp();
$response = $http->head($uri);
if (key_exists('ETag', $response->headers))
{
$syncToken = $response->headers['ETag'];
}
elseif (key_exists('Last-Modified', $response->headers))
{
$syncToken = $response->headers['Last-Modified'];
}
}
return $syncToken;
}
/**
* Return the cleaned up content of the calendar
*
* @param integer $calendarId The calender Id
* @param JDate $startDate The start date
* @param JDate $endDate The end date
* @param JRegistry $options The options passed by the plugin
*
* @return string The content of the calendar
*/
protected function getContent($calendarId, JDate $startDate = null, JDate $endDate = null, JRegistry $options)
{
$calendar = $this->getDbCal($calendarId);
if (empty($calendar))
{
return '';
}
$content = \DPCalendarHelper::fetchContent($this->getIcalUrl($calendar));
if ($content instanceof \Exception)
{
$this->log($content->getMessage());
return '';
}
$content = str_replace("BEGIN:VCALENDAR\r\n", '', $content);
$content = str_replace("BEGIN:VCALENDAR\n", '', $content);
$content = str_replace("\r\nEND:VCALENDAR", '', $content);
$content = str_replace("\nEND:VCALENDAR", '', $content);
return "BEGIN:VCALENDAR\n" . $content . "\nEND:VCALENDAR";
}
}