EasyPeasyICS.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /* ------------------------------------------------------------------------ */
  3. /* EasyPeasyICS
  4. /* ------------------------------------------------------------------------ */
  5. /* Manuel Reinhard, manu@sprain.ch
  6. /* Twitter: @sprain
  7. /* Web: www.sprain.ch
  8. /*
  9. /* Built with inspiration by
  10. /" http://stackoverflow.com/questions/1463480/how-can-i-use-php-to-dynamically-publish-an-ical-file-to-be-read-by-google-calend/1464355#1464355
  11. /* ------------------------------------------------------------------------ */
  12. /* History:
  13. /* 2010/12/17 - Manuel Reinhard - when it all started
  14. /* ------------------------------------------------------------------------ */
  15. class EasyPeasyICS {
  16. protected $calendarName;
  17. protected $events = array();
  18. /**
  19. * Constructor
  20. * @param string $calendarName
  21. */
  22. public function __construct($calendarName=""){
  23. $this->calendarName = $calendarName;
  24. }//function
  25. /**
  26. * Add event to calendar
  27. * @param string $calendarName
  28. */
  29. public function addEvent($start, $end, $summary="", $description="", $url=""){
  30. $this->events[] = array(
  31. "start" => $start,
  32. "end" => $end,
  33. "summary" => $summary,
  34. "description" => $description,
  35. "url" => $url
  36. );
  37. }//function
  38. public function render($output = true){
  39. //start Variable
  40. $ics = "";
  41. //Add header
  42. $ics .= "BEGIN:VCALENDAR
  43. METHOD:PUBLISH
  44. VERSION:2.0
  45. X-WR-CALNAME:".$this->calendarName."
  46. PRODID:-//hacksw/handcal//NONSGML v1.0//EN";
  47. //Add events
  48. foreach($this->events as $event){
  49. $ics .= "
  50. BEGIN:VEVENT
  51. UID:". md5(uniqid(mt_rand(), true)) ."@EasyPeasyICS.php
  52. DTSTAMP:" . gmdate('Ymd').'T'. gmdate('His') . "Z
  53. DTSTART:".gmdate('Ymd', $event["start"])."T".gmdate('His', $event["start"])."Z
  54. DTEND:".gmdate('Ymd', $event["end"])."T".gmdate('His', $event["end"])."Z
  55. SUMMARY:".str_replace("\n", "\\n", $event['summary'])."
  56. DESCRIPTION:".str_replace("\n", "\\n", $event['description'])."
  57. URL;VALUE=URI:".$event['url']."
  58. END:VEVENT";
  59. }//foreach
  60. //Footer
  61. $ics .= "
  62. END:VCALENDAR";
  63. if ($output) {
  64. //Output
  65. header('Content-type: text/calendar; charset=utf-8');
  66. header('Content-Disposition: inline; filename='.$this->calendarName.'.ics');
  67. echo $ics;
  68. } else {
  69. return $ics;
  70. }
  71. }//function
  72. }//class