IcalLib.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. //$path = dirname(__FILE__);
  3. //require_once($path . DS . 'Vendor' . DS . 'ical'.DS.'ical.php');
  4. App::import('Vendor', 'Tools.ical', ['file' => 'ical/ical.php']);
  5. App::import('Vendor', 'Tools.icalobject', ['file' => 'ical/i_cal_object.php']);
  6. App::uses('TimeLib', 'Tools.Utility');
  7. /**
  8. * A wrapper for the Ical/Ics calendar lib
  9. * @see http://www.dereuromark.de/2011/11/21/serving-views-as-files-in-cake2 for details
  10. *
  11. * @author Mark Scherer
  12. * @license http://opensource.org/licenses/mit-license.php MIT
  13. */
  14. class IcalLib {
  15. public $Ical;
  16. public $Time;
  17. public function __construct() {
  18. $this->ICalObject = new ICalObject();
  19. }
  20. /**
  21. *
  22. * some automagic
  23. * - array urls are transformed in (full) absolute urls
  24. * - id => uid with @host
  25. * - start/end/timestamp to atom
  26. * - class to upper
  27. *
  28. * @param array $data
  29. * @param bool $addStartAndEnd
  30. * @return string icalContent (single vevent)
  31. */
  32. public function build($data, $addStartAndEnd = true) {
  33. $replacements = ['-', ':'];
  34. if (isset($data['timezone'])) {
  35. $replacements[] = 'Z';
  36. }
  37. if (isset($data['start'])) {
  38. $data['dtstart'] = TimeLib::toAtom($data['start']);
  39. $data['dtstart'] = str_replace($replacements, '', $data['dtstart']);
  40. unset($data['start']);
  41. }
  42. if (isset($data['end'])) {
  43. $data['dtend'] = TimeLib::toAtom($data['end']);
  44. $data['dtend'] = str_replace($replacements, '', $data['dtend']);
  45. unset($data['end']);
  46. }
  47. if (isset($data['timestamp'])) {
  48. $data['dtstamp'] = TimeLib::toAtom($data['timestamp']);
  49. $data['dtstamp'] = str_replace(['-', ':'], '', $data['dtstamp']);
  50. unset($data['timestamp']);
  51. }
  52. if (isset($data['timezone'])) {
  53. $data['tzid'] = $data['timezone'];
  54. unset($data['timezone']);
  55. }
  56. if (isset($data['id'])) {
  57. $data['uid'] = $data['id'] . '@' . env('HTTP_HOST');
  58. unset($data['id']);
  59. }
  60. if (isset($data['class'])) {
  61. $data['class'] = strtoupper($data['class']);
  62. }
  63. if (isset($data['url']) && is_array($data['url'])) {
  64. $data['url'] = Router::url($data['url'], true);
  65. }
  66. $res = $this->ICalObject->create($data);
  67. if ($addStartAndEnd) {
  68. $res = 'BEGIN:VEVENT' . PHP_EOL . trim($res) . PHP_EOL . 'END:VEVENT';
  69. }
  70. return $res;
  71. }
  72. /**
  73. * Start the file
  74. *
  75. * @param array $data
  76. * @return string
  77. */
  78. public function createStart($data = []) {
  79. $defaults = [
  80. 'version' => '2.0',
  81. 'prodid' => '-//' . env('HTTP_HOST'),
  82. 'method' => 'PUBLISH',
  83. ];
  84. $data = array_merge($defaults, $data);
  85. $res = [];
  86. $res[] = 'BEGIN:VCALENDAR';
  87. foreach ($data as $key => $val) {
  88. $res[] = strtoupper($key) . ':' . $val;
  89. }
  90. return implode(PHP_EOL, $res);
  91. }
  92. /**
  93. * End the file
  94. *
  95. * @return string
  96. */
  97. public function createEnd() {
  98. return 'END:VCALENDAR';
  99. }
  100. public function parse($url) {
  101. $context = stream_context_create(
  102. ['http' => ['header' => 'Connection: close']]);
  103. if (!file_exists($url) || !($res = file_get_contents($url, 0, $context))) {
  104. return false;
  105. }
  106. $this->Ical = new ical($url);
  107. if ($this->Ical->parse()) {
  108. return true;
  109. }
  110. return false;
  111. }
  112. /**
  113. * @return array
  114. */
  115. public function getCalendarInfos() {
  116. return $this->Ical->get_calender_data();
  117. }
  118. /**
  119. * Key => value with key as unixTimeStamp and value as summary
  120. *
  121. * @return array
  122. */
  123. public function getEventsAsList() {
  124. $res = [];
  125. $events = $this->getEvents();
  126. foreach ($events as $event) {
  127. $res[$event['DTSTART']['unixtime']] = $event['SUMMARY'];
  128. }
  129. return $res;
  130. }
  131. /**
  132. * @return array events or false on failure
  133. */
  134. public function getEvents() {
  135. return $this->Ical->get_sort_event_list();
  136. }
  137. /**
  138. * @return array todos or false on failure
  139. */
  140. public function getTodos() {
  141. return $this->Ical->get_todo_list();
  142. }
  143. }