| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\CalendarLinks\Generators; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Spatie\CalendarLinks\Generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Spatie\CalendarLinks\Link; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @see https://icalendar.org/RFC-Specifications/iCalendar-RFC-5545/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Ics implements Generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** @var string {@see https://www.php.net/manual/en/function.date.php} */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $dateFormat = 'Ymd'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     protected $dateTimeFormat = 'e:Ymd\THis'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function __construct(array $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->options = $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |     /** {@inheritdoc} */ | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public function generate(Link $link): string | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $url = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             'BEGIN:VCALENDAR', | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             'VERSION:2.0', | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             'BEGIN:VEVENT', | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             'UID:'.($this->options['UID'] ?? $this->generateEventUid($link)), | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             'SUMMARY:'.$this->escapeString($link->title), | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $dateTimeFormat = $link->allDay ? $this->dateFormat : $this->dateTimeFormat; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         if ($link->allDay) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             $url[] = 'DTSTART:'.$link->from->format($dateTimeFormat); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             $url[] = 'DURATION:P1D'; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             $url[] = 'DTSTART;TZID='.$link->from->format($dateTimeFormat); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             $url[] = 'DTEND;TZID='.$link->to->format($dateTimeFormat); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         if ($link->description) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             $url[] = 'DESCRIPTION:'.$this->escapeString($link->description); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         if ($link->address) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             $url[] = 'LOCATION:'.$this->escapeString($link->address); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $url[] = 'END:VEVENT'; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $url[] = 'END:VCALENDAR'; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         return $this->buildLink($url); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     protected function buildLink(array $propertiesAndComponents): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return 'data:text/calendar;charset=utf8;base64,'.base64_encode(implode("\r\n", $propertiesAndComponents)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** @see https://tools.ietf.org/html/rfc5545.html#section-3.3.11 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function escapeString(string $field): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return addcslashes($field, "\r\n,;"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** @see https://tools.ietf.org/html/rfc5545#section-3.8.4.7 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     protected function generateEventUid(Link $link): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return md5(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             '%s%s%s%s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $link->from->format(\DateTimeInterface::ATOM), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $link->to->format(\DateTimeInterface::ATOM), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $link->title, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $link->address | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  |