-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVCard_Parser.php
More file actions
373 lines (311 loc) · 14.8 KB
/
VCard_Parser.php
File metadata and controls
373 lines (311 loc) · 14.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
require_once ('VCard.php');
/**
* Parser for vCards
*
* @author Daniel Theiss <[email protected]>
* @copyright Daniel Theiss
* @link https://github.com/dath/phpCalnCards/
*
* @package phpCalnCards
* @version 0.7
* @since 0.1
* @license http://www.gnu.org/licenses/gpl.html GNU GPL v3 or later
*
* @see http://www.ietf.org/rfc/rfc6350.txt RFC 6350: vCard Format Specification (-> vCard Version 4.0)
* @see http://www.ietf.org/rfc/rfc2426.txt RFC 2426: vCard MIME Directory Profile (-> vCard Version 3.0)
*
*/
class VCard_Parser {
protected $_content = null;
protected $_vcardCacheObject = null;
/**
* Constructor
*
* @param string filename path to vCard file or vCard as string
*/
public function __construct($filename) {
if (is_file($filename)) {
$this->_content = file_get_contents($filename);
} else {
$this->_content = $filename;
}
}
/**
* Parses vCards and returns vCard Object
*
* @return vCard object
*/
public function parse() {
$vcardObjectArray = array();
$keys = array("VERSION" => 'version',
"UID" => 'uid',
"SOURCE" => 'source',
"KIND" => 'kind',
"FN" => 'fullname',
"N" => 'name',
"NICKNAME" => 'nickname',
"PHOTO" => 'photo',
"BDAY" => 'birthday',
"ANNIVERSARY" => 'anniversary',
"GENDER" => 'gender',
"X-GENDER" => 'gender',
"LANG" => 'lang',
"ADR" => 'address',
"TEL" => 'telephone',
"EMAIL" => 'email',
"IMPP" => 'im',
"X-AIM" => 'im',
"X-GADUGADU" => 'im',
"X-GOOGLE-TALK" => 'im',
"X-ICQ" => 'im',
"X-JABBER" => 'im',
"X-MSN" => 'im',
"X-SKYPE" => 'im',
"X-SKYPE-USERNAME" => 'im',
"X-TWITTER" => 'im',
"X-YAHOO" => 'im',
"URL" => 'url',
"TITLE" => 'title',
"ROLE" => 'role',
"LOGO" => 'logo',
"ORG" => 'organization',
"TZ" => 'timezone',
"GEO" => 'geo',
"MAILER" => 'mailer',
"KEY" => 'key',
"SOUND" => 'sound',
"CATEGORIES" => 'categories',
"NOTE" => 'note',
"PRODID" => 'prodid',
"REV" => 'revision'
);
/* Unfolding like defined in RFC 6350 3.2 */
$this->_content = preg_replace("/\n(?:[ \t])/", "", $this->_content);
$lines = explode(VCard::LBREAK, $this->_content);
foreach ($lines as $line) {
$line = trim($line);
if (strtoupper($line) == "BEGIN:VCARD") {
$this->_vcardCacheObject = new VCard();
} elseif (strtoupper($line) == "END:VCARD") {
$vcardObjectArray[] = $this->_vcardCacheObject;
} elseif ($line != null) {
$type = '';
$value = '';
$group = null;
list ($type, $value) = explode(':', $line, 2);
$types = explode(';', $type);
// save group if exist
if (strpos($types[0],'.') !== false) {
list($group,$types[0]) = explode('.', $types[0], 2);
}
$key = strtoupper($types[0]);
array_shift($types);
$i = 0;
foreach ($types as $type) {
if (strpos(strtolower($type), 'base64')) {
$value = base64_decode($value);
unset($types[$i]);
} elseif (strpos(strtolower($type), 'encoding=b')) {
$value = base64_decode($value);
unset($types[$i]);
} elseif (strpos(strtolower($type), 'quoted-printable')) {
$value = quoted_printable_decode($value);
unset($types[$i]);
} elseif (strpos(strtolower($type), 'charset=') === 0) {
try {
$value = mb_convert_encoding($value, "UTF-8", substr($type, 8));
} catch (Exception $e) {
}
unset($types[$i]);
}
$i++;
}
if (in_array(strtoupper($key), array_keys($keys))) {
if ($keys[$key] == 'im') {
call_user_func(array($this, "_parse" . ucfirst($keys[$key])), $value, $key, $types, $group);
} elseif (isset($types[0]) && isset($group)) {
call_user_func(array($this, "_parse" . ucfirst($keys[$key])), $value, $types, $group);
} elseif (isset($types[0])) {
call_user_func(array($this, "_parse" . ucfirst($keys[$key])), $value, $types);
} elseif (isset($group)) {
call_user_func(array($this, "_parse" . ucfirst($keys[$key])), $value, array(), $group);
} else {
call_user_func(array($this, "_parse" . ucfirst($keys[$key])), $value);
}
} else {
if (isset($group)) {
call_user_func(array($this, "_parseOthers"), $key, $value, $types, $group);
} else {
call_user_func(array($this, "_parseOthers"), $key, $value, $types);
}
}
}
}
return $vcardObjectArray;
}
/*** Helper functions ***/
private static function _unescapeValue($string) {
return str_replace(array('\\\\','\,','\;'), array('\\',',',';'), $string);
}
private static function _explodeTypes($args) {
$types = array();
foreach ($args as $typeCache) {
$typeCache = strtoupper($typeCache);
if (strpos($typeCache, 'TYPE=') === 0) {
$types = array_merge($types, explode(',', substr($typeCache, 5)));
} else {
$types[] = $typeCache;
}
}
return $types;
}
/*** Parse functions for the different properties ***/
protected function _parseVersion($value) {
$this->_vcardCacheObject->changeVersion($value);
}
protected function _parseUid($value, $args = array()) {
$this->_vcardCacheObject->setUid(self::_unescapeValue($value));
}
protected function _parseSource($value, $args = array(), $group = null) {
$this->_vcardCacheObject->addSource($value, self::_explodeTypes($args), $group);
}
protected function _parseKind($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setKind($value, self::_explodeTypes($args));
$this->_vcardCacheObject->addToGrouping('kind', $group);
}
protected function _parseFullname($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setFullname(self::_unescapeValue($value));
$this->_vcardCacheObject->addToGrouping('fullname', $group);
}
protected function _parseName($value, $args = array(), $group = null) {
$value = explode(';', $value);
if (isset($value[0])) {
$this->_vcardCacheObject->setLastname(self::_unescapeValue($value[0]));
}
if (isset($value[1])) {
$this->_vcardCacheObject->setFirstname(self::_unescapeValue($value[1]));
}
if (isset($value[2])) {
$this->_vcardCacheObject->setAdditionalNames(self::_unescapeValue($value[2]));
}
if (isset($value[3])) {
$this->_vcardCacheObject->setNamePrefix(self::_unescapeValue($value[3]));
}
if (isset($value[4])) {
$this->_vcardCacheObject->setNameSuffix(self::_unescapeValue($value[4]));
}
$this->_vcardCacheObject->addToGrouping('name', $group);
}
protected function _parseNickname($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setNickname($value);
$this->_vcardCacheObject->addToGrouping('nickname', $group);
}
protected function _parsePhoto($value, $args = array(), $group = null) {
$this->_vcardCacheObject->addPhoto(self::_unescapeValue($value), self::_explodeTypes($args), $group);
}
protected function _parseBirthday($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setBirthday($value);
$this->_vcardCacheObject->addToGrouping('birthday', $group);
}
protected function _parseAnniversary($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setAnniversary($value);
$this->_vcardCacheObject->addToGrouping('anniversary', $group);
}
protected function _parseGender($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setGender($value);
$this->_vcardCacheObject->addToGrouping('gender', $group);
}
protected function _parseLang($value, $args = array(), $group = null) {
$this->_vcardCacheObject->addLang($value, self::_explodeTypes($args), $group);
}
protected function _parseAddress($value, $args = array(), $group = null) {
$type = self::_explodeTypes($args);
$value = explode(';', $value);
$this->_vcardCacheObject->addAddress(array(
'pobox' => (isset($value['0']) ? self::_unescapeValue($value['0']) : ''),
'extendedaddress' => (isset($value['1']) ? self::_unescapeValue($value['1']) : ''),
'street' => (isset($value['2']) ? self::_unescapeValue($value['2']) : ''),
'city' => (isset($value['3']) ? self::_unescapeValue($value['3']) : ''),
'region' => (isset($value['4']) ? self::_unescapeValue($value['4']) : ''),
'postalcode' => (isset($value['5']) ? self::_unescapeValue($value['5']) : ''),
'country' => (isset($value['6']) ? self::_unescapeValue($value['6']) : ''),
'type' => $type,
'group' => $group));
}
protected function _parseTelephone($value, $args = array(), $group = null) {
$this->_vcardCacheObject->addPhonenumber($value, self::_explodeTypes($args), $group);
}
protected function _parseEmail($value, $args = array(), $group = null) {
$this->_vcardCacheObject->addEmail(self::_unescapeValue($value), self::_explodeTypes($args), $group);
}
protected function _parseIm($value, $messenger, $args = array(), $group = null) {
$this->_vcardCacheObject->addInstantmessenger($value, $messenger, self::_explodeTypes($args), $group);
}
protected function _parseUrl($value, $args = array(), $group = null) {
$this->_vcardCacheObject->addUrl($value, self::_explodeTypes($args), $group);
}
protected function _parseTitle($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setTitle(self::_unescapeValue($value));
$this->_vcardCacheObject->addToGrouping('title', $group);
}
protected function _parseRole($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setRole(self::_unescapeValue($value));
$this->_vcardCacheObject->addToGrouping('role', $group);
}
protected function _parseLogo($value, $args = array(), $group = null) {
$this->_vcardCacheObject->addLogo(self::_unescapeValue($value), self::_explodeTypes($args), $group);
}
protected function _parseOrganization($value, $args = array(), $group = null) {
$value = explode(';', $value);
if (isset($value[0])) {
$this->_vcardCacheObject->setOrganization(self::_unescapeValue($value[0]));
$this->_vcardCacheObject->addToGrouping('organization', $group);
}
if (isset($value[1])) {
$this->_vcardCacheObject->setDepartment(self::_unescapeValue($value[1]));
}
if (isset($value[2])) {
$this->_vcardCacheObject->setSubDepartment(self::_unescapeValue($value[2]));
}
}
protected function _parseTimezone($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setTimezone($value);
$this->_vcardCacheObject->addToGrouping('timezone', $group);
}
protected function _parseGeo($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setGeolocation($value);
$this->_vcardCacheObject->addToGrouping('geolocation', $group);
}
protected function _parseMailer($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setMailer(self::_unescapeValue($value));
$this->_vcardCacheObject->addToGrouping('mailer', $group);
}
protected function _parseKey($value, $args = array(), $group = null) {
$this->_vcardCacheObject->addKey(self::_unescapeValue($value), self::_explodeTypes($args), $group);
}
protected function _parseSound($value, $args = array(), $group = null) {
$this->_vcardCacheObject->addSound(self::_unescapeValue($value), self::_explodeTypes($args), $group);
}
protected function _parseCategories($value, $args = array(), $group = null) {
$categories = explode(',', self::_unescapeValue($value));
foreach ($categories as $category) {
$this->_vcardCacheObject->addCategory($category);
}
$this->_vcardCacheObject->addToGrouping('categories', $group);
}
protected function _parseNote($value, $args = array(), $group = null) {
$this->_vcardCacheObject->setNote(self::_unescapeValue($value));
$this->_vcardCacheObject->addToGrouping('note', $group);
}
protected function _parseOthers($value, $key, $args = array(), $group = null) {
$this->_vcardCacheObject->addOthers($value, $key, self::_explodeTypes($args), $group);
}
protected function _parseProdid($value, $args = array()) {
$this->_vcardCacheObject->setProdid(self::_unescapeValue($value));
}
protected function _parseRevision($value, $args = array()) {
$this->_vcardCacheObject->setRevision($value);
}
}
?>