This repository was archived by the owner on Dec 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathICS.php
More file actions
284 lines (239 loc) · 5.14 KB
/
ICS.php
File metadata and controls
284 lines (239 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
use Illuminate\Filesystem\Filesystem;
class ICS
{
/* *
* Variables have the following structure:
* _ Scope (private and protected variables with underscore)
* (d) date/(s) string/(i) integer/(b) bool/(o) object Initials for the type
* name Name
*/
/**
* @var $_d_end
*/
private $_d_end;
/**
* @var $_d_start
*/
private $_d_start;
/**
* @var $_s_address
*/
private $_s_address;
/**
* @var $_s_description
*/
private $_s_description;
/**
* @var $_s_html
*/
private $_s_html;
/**
* @var $_s_uri
*/
private $_s_uri;
/**
* @var $_s_summary
*/
private $_s_summary;
/**
* @var $_s_output
*/
private $_s_output;
/**
* @var $_o_file
*/
private $_o_file;
/**
* @var $_s_file_path
*/
private $_s_file_path;
/**
* @var $_s_prodid
*/
private $_s_prodid;
/**
* Create a new Invite instance.
*
* @param string $prodid Identifier (ex.: //Company//Product//Language)
* @param array $attributes Predefined attributes
* @param bool $generate Generate markup
* @return void
*/
public function __construct($prodid, array $attributes = array())
{
if ( ! is_string($prodid) || $prodid === '')
{
throw new Exception('PRODID is required');
}
$this->_o_file = new Filesystem();
$this->_s_prodid = $prodid;
foreach ($attributes as $key => $value)
{
$this->$key = $value;
}
}
/**
* Get properties
*
* @param string $name startDate|endDate|address|summary|uri|description|path
* @param mixed $value Variable assignment
* @return ICS
* @throws Exception If $name is not recognized
*/
public function __set($name, $value)
{
switch ($name)
{
case 'startDate':
$this->_d_start = $value;
break;
case 'endDate':
$this->_d_end = $value;
break;
case 'address':
$this->_s_address = $value;
break;
case 'summary':
$this->_s_summary = $value;
break;
case 'uri':
$this->_s_uri = $value;
break;
case 'description':
$this->_s_description = $value;
break;
case 'html':
$this->_s_html = $value;
break;
case 'path':
$this->_s_file_path = $value;
break;
}
return $this;
}
/**
* Get properties
*
* @param string $name startDate|endDate|address|summary|uri|description|path
* @return mixed
* @throws Exception If $name is not recognized
*/
public function __get($name)
{
switch ($name)
{
case 'startDate':
return $this->_d_start;
break;
case 'endDate':
return $this->_d_end;
break;
case 'address':
return $this->_s_address;
break;
case 'summary':
return $this->_s_summary;
break;
case 'uri':
return $this->_s_uri;
break;
case 'description':
return $this->_s_description;
break;
case 'html':
return $this->_s_html;
break;
case 'path':
return $this->_s_file_path;
break;
}
}
/**
* Return the string file output
*
* @return string
*/
public function get()
{
($this->_s_output) ? $this->_s_output : $this->_generate();
return $this->_s_output;
}
/**
* Save the output to a file
*
* @param string $path Path to file or directory
* @param string $name File name
* @return mixed
* @throws Exception If path param is not a valid directory
*/
public function save($path = '', $name = '')
{
$this->path = ($path !== '') ? $path : $this->path;
$this->path .= ($name !== '') ? $name : $this->_createFileName();
return $this->_o_file->put($this->path, $this->get());
}
/**
* Delete the current generated file
*
* @return bool
*/
public function delete()
{
return $this->_o_file->delete($this->path);
}
/**
* Create a default file name
*
* @return string
*/
private function _createFileName()
{
return date('Ymd', time()).'.ics';
}
/**
* Generate ICS markup
*
* @return void
*/
private function _generate()
{
$this->_s_output = "BEGIN:VCALENDAR\n".
"VERSION:2.0\n".
"PRODID:-".$this->_s_prodid."\n".
"METHOD:REQUEST\n".
"BEGIN:VEVENT\n".
"DTSTART:".$this->_dateToCal($this->startDate)."\n".
"DTEND:".$this->_dateToCal($this->endDate)."\n".
"SUMMARY:New ".$this->_escapeString($this->summary)."\n".
"LOCATION:".$this->_escapeString($this->address)."\n".
"DESCRIPTION:".$this->_escapeString($this->description)."\n".
"X-ALT-DESC;FMTTYPE=text/html:".$this->_escapeString($this->html)."\n".
"URL;VALUE=URI:".$this->_escapeString($this->uri)."\n".
"UID:".uniqid()."\n".
"SEQUENCE:0\n".
"DTSTAMP:".$this->_dateToCal(time())."\n".
"END:VEVENT\n".
"END:VCALENDAR\n";
}
/**
* Generate the specific date markup for a ics file
*
* @param integer $timestamp Timestamp to be transformed
* @return string
*/
private function _dateToCal($timestamp)
{
return date('Ymd\THis\Z', ($timestamp) ? $timestamp : time());
}
/**
* Escape characters
*
* @param string $string String to be escaped
* @return string
*/
private function _escapeString($string)
{
return preg_replace('/([\,;])/','\\\$1', ($string) ? $string : '');
}
}