IcalLib.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 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. /** BUILDING **/
  21. /**
  22. *
  23. * some automagic
  24. * - array urls are transformed in (full) absolute urls
  25. * - id => uid with @host
  26. * - start/end/timestamp to atom
  27. * - class to upper
  28. *
  29. * @param array $data
  30. * @param bool $addStartAndEnd
  31. * @return string icalContent (single vevent)
  32. */
  33. public function build($data, $addStartAndEnd = true) {
  34. $replacements = array('-', ':');
  35. if (isset($data['timezone'])) {
  36. $replacements[] = 'Z';
  37. }
  38. if (isset($data['start'])) {
  39. $data['dtstart'] = TimeLib::toAtom($data['start']);
  40. $data['dtstart'] = str_replace($replacements, '', $data['dtstart']);
  41. unset($data['start']);
  42. }
  43. if (isset($data['end'])) {
  44. $data['dtend'] = TimeLib::toAtom($data['end']);
  45. $data['dtend'] = str_replace($replacements, '', $data['dtend']);
  46. unset($data['end']);
  47. }
  48. if (isset($data['timestamp'])) {
  49. $data['dtstamp'] = TimeLib::toAtom($data['timestamp']);
  50. $data['dtstamp'] = str_replace(array('-', ':'), '', $data['dtstamp']);
  51. unset($data['timestamp']);
  52. }
  53. if (isset($data['timezone'])) {
  54. $data['tzid'] = $data['timezone'];
  55. unset($data['timezone']);
  56. }
  57. if (isset($data['id'])) {
  58. $data['uid'] = $data['id'] . '@' . env('HTTP_HOST');
  59. unset($data['id']);
  60. }
  61. if (isset($data['class'])) {
  62. $data['class'] = strtoupper($data['class']);
  63. }
  64. if (isset($data['url']) && is_array($data['url'])) {
  65. $data['url'] = Router::url($data['url'], true);
  66. }
  67. $res = $this->ICalObject->create($data);
  68. if ($addStartAndEnd) {
  69. $res = 'BEGIN:VEVENT' . PHP_EOL . trim($res) . PHP_EOL . 'END:VEVENT';
  70. }
  71. return $res;
  72. }
  73. /**
  74. * Start the file
  75. *
  76. * @param array $data
  77. * @return string
  78. */
  79. public function createStart($data = array()) {
  80. $defaults = array(
  81. 'version' => '2.0',
  82. 'prodid' => '-//' . env('HTTP_HOST'),
  83. 'method' => 'PUBLISH',
  84. );
  85. $data = array_merge($defaults, $data);
  86. $res = array();
  87. $res[] = 'BEGIN:VCALENDAR';
  88. foreach ($data as $key => $val) {
  89. $res[] = strtoupper($key) . ':' . $val;
  90. }
  91. return implode(PHP_EOL, $res);
  92. }
  93. /**
  94. * End the file
  95. *
  96. * @return string
  97. */
  98. public function createEnd() {
  99. return 'END:VCALENDAR';
  100. }
  101. /** PARSING **/
  102. public function parse($url) {
  103. $context = stream_context_create(
  104. array('http' => array('header' => 'Connection: close')));
  105. if (!file_exists($url) || !($res = file_get_contents($url, 0, $context))) {
  106. return false;
  107. }
  108. $this->Ical = new ical($url);
  109. if ($this->Ical->parse()) {
  110. return true;
  111. }
  112. return false;
  113. }
  114. /**
  115. * @return array
  116. */
  117. public function getCalendarInfos() {
  118. return $this->Ical->get_calender_data();
  119. }
  120. /**
  121. * Key => value with key as unixTimeStamp and value as summary
  122. *
  123. * @return array
  124. */
  125. public function getEventsAsList() {
  126. $res = array();
  127. $events = $this->getEvents();
  128. foreach ($events as $event) {
  129. $res[$event['DTSTART']['unixtime']] = $event['SUMMARY'];
  130. }
  131. return $res;
  132. }
  133. /**
  134. * @return array events or false on failure
  135. */
  136. public function getEvents() {
  137. return $this->Ical->get_sort_event_list();
  138. }
  139. /**
  140. * @return array todos or false on failure
  141. */
  142. public function getTodos() {
  143. return $this->Ical->get_todo_list();
  144. }
  145. }