forked from straup/php-lib-enplacify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_vcard.php
97 lines (67 loc) · 1.75 KB
/
lib_vcard.php
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
<?php
#
# $Id$
#
######################################################
$GLOBALS['vcard_valid_classes'] = array(
'fn org',
'tel',
'street-address',
'locality',
'region',
);
######################################################
function vcard_parse_html($html){
$html = mb_convert_encoding($html, 'html-entities', 'utf-8');
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$ok = $doc->loadHTML($html);
if (! $ok){
return array( 'ok' => 0, 'error' => 'Failed to parse HTML' );
}
# Just use XPath hooks instead ?
# Or just write a Expat parser which might be faster all the way around...
$tags = $doc->getElementsByTagName('div');
if (! $tags->length){
return array( 'ok' => 0, 'error' => 'No div tags' );
}
$vcard = array();
foreach ($tags as $tag){
if (! $tag->hasAttribute("class")){
continue;
}
$classes = explode(" ", $tag->getAttribute("class"));
if (! in_array("vcard", $classes)){
continue;
}
_vcard_parse_node($tag, $vcard);
break;
}
if (! count($vcard)){
return array( 'ok' => 0, 'error' => 'Failed to locate any vcard data' );
}
return array(
'ok' => 1,
'vcard' => $vcard,
);
}
######################################################
function _vcard_parse_node($node, &$vcard){
foreach ($node->childNodes as $kid){
if ($kid->nodeType != XML_ELEMENT_NODE){
continue;
}
if ($kid->hasAttribute("class")){
$class = $kid->getAttribute("class");
if (in_array($class, $GLOBALS['vcard_valid_classes'])){
$vcard[ $class ] = $kid->nodeValue;
}
}
if ($kid->hasChildNodes()){
_vcard_parse_node($kid, $vcard);
}
}
# Note the pass by ref
}
######################################################
?>