Advertisement
matoni555

ICS.php

Dec 1st, 2021
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * ICS.php
  5.  * =======
  6.  * Use this class to create an .ics file.
  7.  *
  8.  * Usage
  9.  * -----
  10.  * Basic usage - generate ics file contents (see below for available properties):
  11.  *   $ics = new ICS($props);
  12.  *   $ics_file_contents = $ics->to_string();
  13.  *
  14.  * Setting properties after instantiation
  15.  *   $ics = new ICS();
  16.  *   $ics->set('summary', 'My awesome event');
  17.  *
  18.  * You can also set multiple properties at the same time by using an array:
  19.  *   $ics->set(array(
  20.  *     'dtstart' => 'now + 30 minutes',
  21.  *     'dtend' => 'now + 1 hour'
  22.  *   ));
  23.  *
  24.  * Available properties
  25.  * --------------------
  26.  * description
  27.  *   String description of the event.
  28.  * dtend
  29.  *   A date/time stamp designating the end of the event. You can use either a
  30.  *   DateTime object or a PHP datetime format string (e.g. "now + 1 hour").
  31.  * dtstart
  32.  *   A date/time stamp designating the start of the event. You can use either a
  33.  *   DateTime object or a PHP datetime format string (e.g. "now + 1 hour").
  34.  * location
  35.  *   String address or description of the location of the event.
  36.  * summary
  37.  *   String short summary of the event - usually used as the title.
  38.  * url
  39.  *   A url to attach to the the event. Make sure to add the protocol (http://
  40.  *   or https://).
  41.  */
  42.  
  43. class ICS {
  44.   const DT_FORMAT = 'Ymd\THis';
  45.  
  46.   protected $properties = array();
  47.   private $available_properties = array(
  48.     'description',
  49.     'dtend',
  50.     'dtstart',
  51.     'location',
  52.     'summary',
  53.     'url'
  54.   );
  55.  
  56.   public function __construct($props) {
  57.     $this->set($props);
  58.   }
  59.  
  60.   public function set($key, $val = false) {
  61.     if (is_array($key)) {
  62.       foreach ($key as $k => $v) {
  63.         $this->set($k, $v);
  64.       }
  65.     } else {
  66.       if (in_array($key, $this->available_properties)) {
  67.         $this->properties[$key] = $this->sanitize_val($val, $key);
  68.       }
  69.     }
  70.   }
  71.  
  72.   public function to_string() {
  73.     $rows = $this->build_props();
  74.     return implode("\r\n", $rows);
  75.   }
  76.  
  77.   private function build_props() {
  78.     // Build ICS properties - add header
  79.     $ics_props = array(
  80.       'BEGIN:VCALENDAR',
  81.       'VERSION:2.0',
  82.       'PRODID:-//hacksw/handcal//NONSGML v1.0//EN',
  83.       'CALSCALE:GREGORIAN',
  84.       'BEGIN:VEVENT'
  85.     );
  86.  
  87.     // Build ICS properties - add header
  88.     $props = array();
  89.     foreach($this->properties as $k => $v) {
  90.       $props[strtoupper($k . ($k === 'url' ? ';VALUE=URI' : ''))] = $v;
  91.     }
  92.  
  93.     // Set some default values
  94.     $props['DTSTAMP'] = $this->format_timestamp('now');
  95.     $props['UID'] = uniqid();
  96.  
  97.     // Append properties
  98.     foreach ($props as $k => $v) {
  99.       $ics_props[] = "$k:$v";
  100.     }
  101.  
  102.     // Build ICS properties - add footer
  103.     $ics_props[] = 'END:VEVENT';
  104.     $ics_props[] = 'END:VCALENDAR';
  105.  
  106.     return $ics_props;
  107.   }
  108.  
  109.   private function sanitize_val($val, $key = false) {
  110.     switch($key) {
  111.       case 'dtend':
  112.       case 'dtstamp':
  113.       case 'dtstart':
  114.         $val = $this->format_timestamp($val);
  115.         break;
  116.       default:
  117.         $val = $this->escape_string($val);
  118.     }
  119.  
  120.     return $val;
  121.   }
  122.  
  123.   private function format_timestamp($timestamp) {
  124.     $dt = new DateTime($timestamp);
  125.     return $dt->format(self::DT_FORMAT);
  126.   }
  127.  
  128.   private function escape_string($str) {
  129.     return preg_replace('/([\,;])/','\\\$1', $str);
  130.   }
  131. }
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement