ical.php 10.0 KB

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