EasyPeasyICS.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * EasyPeasyICS Simple ICS/vCal data generator.
  4. * @author Marcus Bointon <phpmailer@synchromedia.co.uk>
  5. * @author Manuel Reinhard <manu@sprain.ch>
  6. *
  7. * Built with inspiration from
  8. * http://stackoverflow.com/questions/1463480/how-can-i-use-php-to-dynamically-publish-an-ical-file-to-be-read-by-google-calend/1464355#1464355
  9. * History:
  10. * 2010/12/17 - Manuel Reinhard - when it all started
  11. * 2014 PHPMailer project becomes maintainer
  12. */
  13. /**
  14. * Class EasyPeasyICS.
  15. * Simple ICS data generator
  16. * @package phpmailer
  17. * @subpackage easypeasyics
  18. */
  19. class EasyPeasyICS
  20. {
  21. /**
  22. * The name of the calendar
  23. * @var string
  24. */
  25. protected $calendarName;
  26. /**
  27. * The array of events to add to this calendar
  28. * @var array
  29. */
  30. protected $events = array();
  31. /**
  32. * Constructor
  33. * @param string $calendarName
  34. */
  35. public function __construct($calendarName = "")
  36. {
  37. $this->calendarName = $calendarName;
  38. }
  39. /**
  40. * Add an event to this calendar.
  41. * @param string $start The start date and time as a unix timestamp
  42. * @param string $end The end date and time as a unix timestamp
  43. * @param string $summary A summary or title for the event
  44. * @param string $description A description of the event
  45. * @param string $url A URL for the event
  46. * @param string $uid A unique identifier for the event - generated automatically if not provided
  47. * @return array An array of event details, including any generated UID
  48. */
  49. public function addEvent($start, $end, $summary = '', $description = '', $url = '', $uid = '')
  50. {
  51. if (empty($uid)) {
  52. $uid = md5(uniqid(mt_rand(), true)) . '@EasyPeasyICS';
  53. }
  54. $event = array(
  55. 'start' => gmdate('Ymd', $start) . 'T' . gmdate('His', $start) . 'Z',
  56. 'end' => gmdate('Ymd', $end) . 'T' . gmdate('His', $end) . 'Z',
  57. 'summary' => $summary,
  58. 'description' => $description,
  59. 'url' => $url,
  60. 'uid' => $uid
  61. );
  62. $this->events[] = $event;
  63. return $event;
  64. }
  65. /**
  66. * @return array Get the array of events.
  67. */
  68. public function getEvents()
  69. {
  70. return $this->events;
  71. }
  72. /**
  73. * Clear all events.
  74. */
  75. public function clearEvents()
  76. {
  77. $this->events = array();
  78. }
  79. /**
  80. * Get the name of the calendar.
  81. * @return string
  82. */
  83. public function getName()
  84. {
  85. return $this->calendarName;
  86. }
  87. /**
  88. * Set the name of the calendar.
  89. * @param $name
  90. */
  91. public function setName($name)
  92. {
  93. $this->calendarName = $name;
  94. }
  95. /**
  96. * Render and optionally output a vcal string.
  97. * @param bool $output Whether to output the calendar data directly (the default).
  98. * @return string The complete rendered vlal
  99. */
  100. public function render($output = true)
  101. {
  102. //Add header
  103. $ics = 'BEGIN:VCALENDAR
  104. METHOD:PUBLISH
  105. VERSION:2.0
  106. X-WR-CALNAME:' . $this->calendarName . '
  107. PRODID:-//hacksw/handcal//NONSGML v1.0//EN';
  108. //Add events
  109. foreach ($this->events as $event) {
  110. $ics .= '
  111. BEGIN:VEVENT
  112. UID:' . $event['uid'] . '
  113. DTSTAMP:' . gmdate('Ymd') . 'T' . gmdate('His') . 'Z
  114. DTSTART:' . $event['start'] . '
  115. DTEND:' . $event['end'] . '
  116. SUMMARY:' . str_replace("\n", "\\n", $event['summary']) . '
  117. DESCRIPTION:' . str_replace("\n", "\\n", $event['description']) . '
  118. URL;VALUE=URI:' . $event['url'] . '
  119. END:VEVENT';
  120. }
  121. //Add footer
  122. $ics .= '
  123. END:VCALENDAR';
  124. if ($output) {
  125. //Output
  126. $filename = $this->calendarName;
  127. //Filename needs quoting if it contains spaces
  128. if (strpos($filename, ' ') !== false) {
  129. $filename = '"'.$filename.'"';
  130. }
  131. header('Content-type: text/calendar; charset=utf-8');
  132. header('Content-Disposition: inline; filename=' . $filename . '.ics');
  133. echo $ics;
  134. }
  135. return $ics;
  136. }
  137. }