| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SRF\iCalendar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license GNU GPL v2+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @since 3.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @author mwjames | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class IcalFormatter { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * @var IcalTimezoneFormatter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	private $icalTimezoneFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	private $calendarName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	private $description; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 * @var [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	private $events = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * @since 3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 5 |  | 	public function __construct( IcalTimezoneFormatter $icalTimezoneFormatter ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 5 |  | 		$this->icalTimezoneFormatter = $icalTimezoneFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 5 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * @since 3.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * @param string $calendarName | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 4 |  | 	public function setCalendarName( $calendarName ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 4 |  | 		$this->calendarName = $calendarName; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * @since 3.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * @param string $description | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 4 |  | 	public function setDescription( $description ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 4 |  | 		$this->description = $description; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * @since 3.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 * @param array $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 4 |  | 	public function addEvent( array $params ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 4 |  | 		$event = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 4 |  | 		$event .= "BEGIN:VEVENT\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  | 		if ( isset( $params['summary'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 4 |  | 			$event .= "SUMMARY:" . $this->escape( $params['summary'] ) . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  | 		if ( isset( $params['url'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 4 |  | 			$event .= "URL:" . $params['url'] . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  | 			$event .= "UID:" . $params['url'] . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  | 		if ( array_key_exists( 'start', $params ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  | 			$event .= "DTSTART:" . $params['start'] . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  | 		if ( array_key_exists( 'end', $params ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  | 			$event .= "DTEND:" . $params['end'] . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  | 		if ( array_key_exists( 'location', $params ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 3 |  | 			$event .= "LOCATION:" . $this->escape( $params['location'] ) . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 4 |  | 		if ( array_key_exists( 'description', $params ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  | 			$event .= "DESCRIPTION:" . $this->escape( $params['description'] ) . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 4 |  | 		if ( isset( $params['timestamp'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 4 |  | 			$t = strtotime( str_replace( 'T', ' ', $params['timestamp'] ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 4 |  | 			$event .= "DTSTAMP:" . date( "Ymd", $t ) . "T" . date( "His", $t ) . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 4 |  | 		if ( isset( $params['sequence'] ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 4 |  | 			$event .= "SEQUENCE:" . $params['sequence'] . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 4 |  | 		$event .= "END:VEVENT\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 4 |  | 		$this->events[] = $event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @since 3.2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 4 |  | 	public function getIcal() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 4 |  | 		$result = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 4 |  | 		$result .= "BEGIN:VCALENDAR\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 4 |  | 		$result .= "PRODID:-//SMW Project//Semantic Result Formats\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 4 |  | 		$result .= "VERSION:2.0\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 4 |  | 		$result .= "METHOD:PUBLISH\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 4 |  | 		$result .= "X-WR-CALNAME:" . $this->calendarName . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 4 |  | 		if ( $this->description !== null ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 4 |  | 			$result .= "X-WR-CALDESC:" . $this->description . "\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 4 |  | 		$result .= $this->icalTimezoneFormatter->getTransitions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 4 |  | 		foreach ( $this->events as $event ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 4 |  | 			$result .= $event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 4 |  | 		$result .= "END:VCALENDAR\r\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 4 |  | 		$this->events = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 4 |  | 		return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	 * Implements esaping of special characters for iCalendar properties of type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	 * TEXT. This is defined in RFC2445 Section 4.3.21. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 4 |  | 	private function escape( $text ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 		// Note that \\ is a PHP escaped single \ here | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 4 |  | 		return str_replace( [ "\\", "\n", ";", "," ], [ "\\\\", "\\n", "\\;", "\\," ], $text ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  |