ical.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. trait ICalHomepageItem
  3. {
  4. public function calendarDaysCheck($entryStart, $entryEnd)
  5. {
  6. $success = false;
  7. $entryStart = intval($entryStart);
  8. $entryEnd = intval($entryEnd);
  9. if ($entryStart >= 0 && $entryEnd <= 0) {
  10. $success = true;
  11. }
  12. return $success;
  13. }
  14. public function calendarStandardizeTimezone($timezone)
  15. {
  16. switch ($timezone) {
  17. case('CST'):
  18. case('Central Time'):
  19. case('Central Standard Time'):
  20. $timezone = 'America/Chicago';
  21. break;
  22. case('CET'):
  23. case('Central European Time'):
  24. $timezone = 'Europe/Berlin';
  25. break;
  26. case('EST'):
  27. case('Eastern Time'):
  28. case('Eastern Standard Time'):
  29. $timezone = 'America/New_York';
  30. break;
  31. case('PST'):
  32. case('Pacific Time'):
  33. case('Pacific Standard Time'):
  34. $timezone = 'America/Los_Angeles';
  35. break;
  36. case('China Time'):
  37. case('China Standard Time'):
  38. $timezone = 'Asia/Beijing';
  39. break;
  40. case('IST'):
  41. case('India Time'):
  42. case('India Standard Time'):
  43. $timezone = 'Asia/New_Delhi';
  44. break;
  45. case('JST');
  46. case('Japan Time'):
  47. case('Japan Standard Time'):
  48. $timezone = 'Asia/Tokyo';
  49. break;
  50. case('Western European Time');
  51. case('Western European Standard Time');
  52. case('Western European Summer Time');
  53. case('W. Europe Time');
  54. case('W. Europe Standard Time');
  55. case('W. Europe Summer Time');
  56. $timezone = 'Europe/Lisbon';
  57. break;
  58. }
  59. return $timezone;
  60. }
  61. public function getCalenderRepeat($value)
  62. {
  63. //FREQ=DAILY
  64. //RRULE:FREQ=WEEKLY;BYDAY=TH
  65. $first = explode('=', $value);
  66. if (count($first) > 1) {
  67. $second = explode(';', $first[1]);
  68. } else {
  69. return $value;
  70. }
  71. if ($second) {
  72. return $second[0];
  73. } else {
  74. return $first[1];
  75. }
  76. }
  77. public function getCalenderRepeatUntil($value)
  78. {
  79. $first = explode('UNTIL=', $value);
  80. if (count($first) > 1) {
  81. if (strpos($first[1], ';') !== false) {
  82. $check = explode(';', $first[1]);
  83. return $check[0];
  84. } else {
  85. return $first[1];
  86. }
  87. } else {
  88. return false;
  89. }
  90. }
  91. public function getCalenderRepeatCount($value)
  92. {
  93. $first = explode('COUNT=', $value);
  94. if (count($first) > 1) {
  95. return $first[1];
  96. } else {
  97. return false;
  98. }
  99. }
  100. public function file_get_contents_curl($url)
  101. {
  102. $ch = curl_init();
  103. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  104. curl_setopt($ch, CURLOPT_HEADER, 0);
  105. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  106. curl_setopt($ch, CURLOPT_URL, $url);
  107. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  108. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  109. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  110. $data = curl_exec($ch);
  111. curl_close($ch);
  112. return $data;
  113. }
  114. public function getIcsEventsAsArray($file)
  115. {
  116. $icalString = $this->file_get_contents_curl($file);
  117. $icsDates = array();
  118. /* Explode the ICs Data to get datas as array according to string ‘BEGIN:’ */
  119. $icsData = explode("BEGIN:", $icalString);
  120. /* Iterating the icsData value to make all the start end dates as sub array */
  121. foreach ($icsData as $key => $value) {
  122. $icsDatesMeta [$key] = explode("\n", $value);
  123. }
  124. /* Itearting the Ics Meta Value */
  125. foreach ($icsDatesMeta as $key => $value) {
  126. foreach ($value as $subKey => $subValue) {
  127. /* to get ics events in proper order */
  128. $icsDates = $this->getICSDates($key, $subKey, $subValue, $icsDates);
  129. }
  130. }
  131. return $icsDates;
  132. }
  133. /* funcion is to avaid the elements wich is not having the proper start, end and summary informations */
  134. public function getICSDates($key, $subKey, $subValue, $icsDates)
  135. {
  136. if ($key != 0 && $subKey == 0) {
  137. $icsDates [$key] ["BEGIN"] = $subValue;
  138. } else {
  139. $subValueArr = explode(":", $subValue, 2);
  140. if (isset ($subValueArr [1])) {
  141. $icsDates [$key] [$subValueArr [0]] = $subValueArr [1];
  142. }
  143. }
  144. return $icsDates;
  145. }
  146. public function getICalendar()
  147. {
  148. if (!$this->config['homepageCalendarEnabled']) {
  149. $this->setAPIResponse('error', 'iCal homepage item is not enabled', 409);
  150. return false;
  151. }
  152. if (!$this->qualifyRequest($this->config['homepageCalendarAuth'])) {
  153. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  154. return false;
  155. }
  156. if (empty($this->config['calendariCal'])) {
  157. $this->setAPIResponse('error', 'iCal URL is not defined', 422);
  158. return false;
  159. }
  160. $calendarItems = array();
  161. $calendars = array();
  162. $calendarURLList = explode(',', $this->config['calendariCal']);
  163. $icalEvents = array();
  164. foreach ($calendarURLList as $key => $value) {
  165. $icsEvents = $this->getIcsEventsAsArray($value);
  166. if (isset($icsEvents) && !empty($icsEvents)) {
  167. $timeZone = isset($icsEvents [1] ['X-WR-TIMEZONE']) ? trim($icsEvents[1]['X-WR-TIMEZONE']) : date_default_timezone_get();
  168. $originalTimeZone = isset($icsEvents [1] ['X-WR-TIMEZONE']) ? str_replace('"', '', trim($icsEvents[1]['X-WR-TIMEZONE'])) : false;
  169. unset($icsEvents [1]);
  170. foreach ($icsEvents as $icsEvent) {
  171. $startKeys = $this->array_filter_key($icsEvent, function ($key) {
  172. return strpos($key, 'DTSTART') === 0;
  173. });
  174. $endKeys = $this->array_filter_key($icsEvent, function ($key) {
  175. return strpos($key, 'DTEND') === 0;
  176. });
  177. if (!empty($startKeys) && !empty($endKeys) && isset($icsEvent['SUMMARY'])) {
  178. /* Getting start date and time */
  179. $repeat = isset($icsEvent ['RRULE']) ? $icsEvent ['RRULE'] : false;
  180. if (!$originalTimeZone) {
  181. $tzKey = array_keys($startKeys);
  182. if (strpos($tzKey[0], 'TZID=') !== false) {
  183. $originalTimeZone = explode('TZID=', (string)$tzKey[0]);
  184. $originalTimeZone = (count($originalTimeZone) >= 2) ? str_replace('"', '', $originalTimeZone[1]) : false;
  185. }
  186. }
  187. $start = reset($startKeys);
  188. $end = reset($endKeys);
  189. $totalDays = $this->config['calendarStart'] + $this->config['calendarEnd'];
  190. if ($repeat) {
  191. $repeatOverride = $this->getCalenderRepeatCount(trim($icsEvent["RRULE"]));
  192. switch (trim(strtolower($this->getCalenderRepeat($repeat)))) {
  193. case 'daily':
  194. $repeat = ($repeatOverride) ? $repeatOverride : $totalDays;
  195. $term = 'days';
  196. break;
  197. case 'weekly':
  198. $repeat = ($repeatOverride) ? $repeatOverride : round($totalDays / 7);
  199. $term = 'weeks';
  200. break;
  201. case 'monthly':
  202. $repeat = ($repeatOverride) ? $repeatOverride : round($totalDays / 30);
  203. $term = 'months';
  204. break;
  205. case 'yearly':
  206. $repeat = ($repeatOverride) ? $repeatOverride : round($totalDays / 365);
  207. $term = 'years';
  208. break;
  209. default:
  210. $repeat = ($repeatOverride) ? $repeatOverride : $totalDays;
  211. $term = 'days';
  212. break;
  213. }
  214. } else {
  215. $repeat = 1;
  216. $term = 'day';
  217. }
  218. $calendarTimes = 0;
  219. while ($calendarTimes < $repeat) {
  220. $currentDate = new DateTime ($this->currentTime);
  221. $oldestDay = new DateTime ($this->currentTime);
  222. $oldestDay->modify('-' . $this->config['calendarStart'] . ' days');
  223. $newestDay = new DateTime ($this->currentTime);
  224. $newestDay->modify('+' . $this->config['calendarEnd'] . ' days');
  225. /* Converting to datetime and apply the timezone to get proper date time */
  226. $startDt = new DateTime ($start);
  227. /* Getting end date with time */
  228. $endDt = new DateTime ($end);
  229. if ($calendarTimes !== 0) {
  230. $dateDiff = date_diff($startDt, $currentDate);
  231. $startDt->modify($dateDiff->format('%R') . (round(($dateDiff->days) / 7)) . ' weeks');
  232. $startDt->modify('+' . $calendarTimes . ' ' . $term);
  233. $endDt->modify($dateDiff->format('%R') . (round(($dateDiff->days) / 7)) . ' weeks');
  234. $endDt->modify('+' . $calendarTimes . ' ' . $term);
  235. } elseif ($calendarTimes == 0 && $repeat !== 1) {
  236. $dateDiff = date_diff($startDt, $currentDate);
  237. $startDt->modify($dateDiff->format('%R') . (round(($dateDiff->days) / 7)) . ' weeks');
  238. $endDt->modify($dateDiff->format('%R') . (round(($dateDiff->days) / 7)) . ' weeks');
  239. }
  240. $calendarStartDiff = date_diff($startDt, $newestDay);
  241. $calendarEndDiff = date_diff($startDt, $oldestDay);
  242. if ($originalTimeZone && $originalTimeZone !== 'UTC' && (strpos($start, 'Z') == false)) {
  243. $originalTimeZone = $this->calendarStandardizeTimezone($originalTimeZone);
  244. $dateTimeOriginalTZ = new DateTimeZone($originalTimeZone);
  245. $dateTimeOriginal = new DateTime('now', $dateTimeOriginalTZ);
  246. $dateTimeUTCTZ = new DateTimeZone(date_default_timezone_get());
  247. $dateTimeUTC = new DateTime('now', $dateTimeUTCTZ);
  248. $dateTimeOriginalOffset = $dateTimeOriginal->getOffset() / 3600;
  249. $dateTimeUTCOffset = $dateTimeUTC->getOffset() / 3600;
  250. $diff = $dateTimeUTCOffset - $dateTimeOriginalOffset;
  251. $startDt->modify('+ ' . $diff . ' hour');
  252. $endDt->modify('+ ' . $diff . ' hour');
  253. }
  254. $startDt->setTimeZone(new DateTimezone ($timeZone));
  255. $endDt->setTimeZone(new DateTimezone ($timeZone));
  256. $startDate = $startDt->format(DateTime::ATOM);
  257. $endDate = $endDt->format(DateTime::ATOM);
  258. if (new DateTime() < $endDt) {
  259. $extraClass = 'text-info';
  260. } else {
  261. $extraClass = 'text-success';
  262. }
  263. /* Getting the name of event */
  264. $eventName = $icsEvent['SUMMARY'];
  265. if (!$this->calendarDaysCheck($calendarStartDiff->format('%R') . $calendarStartDiff->days, $calendarEndDiff->format('%R') . $calendarEndDiff->days)) {
  266. break;
  267. }
  268. if (isset($icsEvent["RRULE"]) && $this->getCalenderRepeatUntil(trim($icsEvent["RRULE"]))) {
  269. $untilDate = new DateTime ($this->getCalenderRepeatUntil(trim($icsEvent["RRULE"])));
  270. $untilDiff = date_diff($currentDate, $untilDate);
  271. if ($untilDiff->days > 0) {
  272. break;
  273. }
  274. }
  275. $icalEvents[] = array(
  276. 'title' => $eventName,
  277. 'imagetype' => 'calendar-o text-warning text-custom-calendar ' . $extraClass,
  278. 'imagetypeFilter' => 'ical',
  279. 'className' => 'bg-calendar calendar-item bg-custom-calendar',
  280. 'start' => $startDate,
  281. 'end' => $endDate,
  282. 'bgColor' => str_replace('text', 'bg', $extraClass),
  283. );
  284. $calendarTimes = $calendarTimes + 1;
  285. }
  286. }
  287. }
  288. }
  289. }
  290. $calendarSources = $icalEvents;
  291. $this->setAPIResponse('success', null, 200, $calendarSources);
  292. return $calendarSources;
  293. }
  294. }