ical.php 11 KB

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