IcalLib.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. //$path = dirname(__FILE__);
  3. //require_once($path . DS . 'Vendor' . DS . 'ical'.DS.'ical.php');
  4. App::import('Vendor', 'Tools.ical', array('file' => 'ical/ical.php'));
  5. App::import('Vendor', 'Tools.icalobject', array('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 MIT
  13. * @cakephp 2.x
  14. */
  15. class IcalLib {
  16. public $Ical;
  17. public $Time;
  18. public function __construct() {
  19. $this->ICalObject = new ICalObject();
  20. }
  21. /** BUILDING **/
  22. /**
  23. *
  24. * some automagic
  25. * - array urls are transformed in (full) absolute urls
  26. * - id => uid with @host
  27. * - start/end/timestamp to atom
  28. * - class to upper
  29. *
  30. * @param array $data
  31. * @param boolean $addStartAndEnd
  32. * @return string icalContent (single vevent)
  33. */
  34. public function build($data, $addStartAndEnd = true) {
  35. $replacements = array('-', ':');
  36. if (isset($data['timezone'])) {
  37. $replacements[] = 'Z';
  38. }
  39. if (isset($data['start'])) {
  40. $data['dtstart'] = TimeLib::toAtom($data['start']);
  41. $data['dtstart'] = str_replace($replacements, '', $data['dtstart']);
  42. unset($data['start']);
  43. }
  44. if (isset($data['end'])) {
  45. $data['dtend'] = TimeLib::toAtom($data['end']);
  46. $data['dtend'] = str_replace($replacements, '', $data['dtend']);
  47. unset($data['end']);
  48. }
  49. if (isset($data['timestamp'])) {
  50. $data['dtstamp'] = TimeLib::toAtom($data['timestamp']);
  51. $data['dtstamp'] = str_replace(array('-', ':'), '', $data['dtstamp']);
  52. unset($data['timestamp']);
  53. }
  54. if (isset($data['timezone'])) {
  55. $data['tzid'] = $data['timezone'];
  56. unset($data['timezone']);
  57. }
  58. if (isset($data['id'])) {
  59. $data['uid'] = $data['id'] . '@' . env('HTTP_HOST');
  60. unset($data['id']);
  61. }
  62. if (isset($data['class'])) {
  63. $data['class'] = strtoupper($data['class']);
  64. }
  65. if (isset($data['url']) && is_array($data['url'])) {
  66. $data['url'] = Router::url($data['url'], true);
  67. }
  68. $res = $this->ICalObject->create($data);
  69. if ($addStartAndEnd) {
  70. $res = 'BEGIN:VEVENT' . PHP_EOL . trim($res) . PHP_EOL . 'END:VEVENT';
  71. }
  72. return $res;
  73. }
  74. /**
  75. * Start the file
  76. *
  77. * @param array $data
  78. * @return string
  79. */
  80. public function createStart($data = array()) {
  81. $defaults = array(
  82. 'version' => '2.0',
  83. 'prodid' => '-//' . env('HTTP_HOST'),
  84. 'method' => 'PUBLISH',
  85. );
  86. $data = array_merge($defaults, $data);
  87. $res = array();
  88. $res[] = 'BEGIN:VCALENDAR';
  89. foreach ($data as $key => $val) {
  90. $res[] = strtoupper($key) . ':' . $val;
  91. }
  92. return implode(PHP_EOL, $res);
  93. }
  94. /**
  95. * End the file
  96. *
  97. * @return string
  98. */
  99. public function createEnd() {
  100. return 'END:VCALENDAR';
  101. }
  102. /** PARSING **/
  103. public function parse($url) {
  104. if (!file_exists($url) || !($res = file_get_contents($url))) {
  105. return false;
  106. }
  107. $this->Ical = new ical($url);
  108. if ($this->Ical->parse()) {
  109. return true;
  110. }
  111. return false;
  112. }
  113. /**
  114. * @return array
  115. */
  116. public function getCalendarInfos() {
  117. return $this->Ical->get_calender_data();
  118. }
  119. /**
  120. * Key => value with key as unixTimeStamp and value as summary
  121. * @return array
  122. */
  123. public function getEventsAsList() {
  124. $res = array();
  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. }