IcalLib.php 3.7 KB

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