forked from kiang/sahana-tw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oid_parse.php
executable file
·125 lines (93 loc) · 3.98 KB
/
oid_parse.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
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
<?php
/*****
PHP OpenID - OpenID consumer and server library
Copyright (C) 2005 Open Source Consulting, SA. and Dan Libby
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Reciprocal linking. The author humbly requests that if you should use
PHP-OpenID on your website to provide an OpenID enabled service that you
place a link to the author's website ( http://videntity.org ) somewhere
that your users can discover it. You are however under no obligation to
do so.
More info about PHP OpenID:
openid@videntity.org
http://videntity.org/openid/
More info about OpenID:
http://www.openid.net
*****/
class oidParse {
// static, public
// parses html data and returns <link> tag attributes.
//
// The return value is an array where each element
// is a sub-array containing all attribute/value
// pairs from a single <link> tag.
function parseLinkAttrs($data) {
$tidy_ver = phpversion('tidy');
if( (int)$tidy_ver == 2 ) {
return oidParse::_parseLinkAttrsWithTidy2( $data );
}
// Todo: We could also have a method that uses Tidy1. volunteers?
else {
return oidParse::_parseLinkAttrsWithPreg( $data );
}
// Todo: We could also have a method that uses ereg. volunteers?
}
// static, private
// parses all link attrs from an html page using perl regex ( preg/pcre )
function _parseLinkAttrsWithPreg( $text ) {
$tag_list = array();
// find all tags
preg_match_all('/<[^>]+>/s',$text,$tags);
foreach ($tags[0] as $tag) {
$gotten = preg_match('/^<\s*link\s*(.*)>/i',$tag,$alist);
if ($gotten) {
// print "<b>$alist[1]</b><br>";
$cleaned = preg_replace('/\s+=\s+/','=',$alist[1]);
preg_match_all('/(?:^|\s)(\w+)="([^">]+)"/',$cleaned,$qatts);
preg_match_all('/(?:^|\s)(\w+)=([^"\s>]+)/',$cleaned,$patts);
$allatts = array_merge($patts[1],$qatts[1]);
$allvals = array_merge($patts[2],$qatts[2]);
$attrs = array();
for ($k=0; $k<count($allatts); $k++) {
$attrs[$allatts[$k]] = $allvals[$k];
}
$tag_list[] = $attrs;
}
}
return $tag_list;
}
// static, private
function _parseLinkAttrsWithTidy2Worker( $node, &$link_tags ) {
$link_tags = $link_tags ? $link_tags : array();
if(isset($node->id)) {
if($node->id == TIDY_TAG_LINK ) {
// $node->attribute is actually a mapped array with
// key/val pairs for each of the nodes attributes.
$link_tags[] = $node->attribute;
}
}
if( $node->hasChildren() ) {
foreach($node->child as $next) {
oidParse::_parseLinkAttrsWithTidy2Worker($next, $link_tags );
}
}
return $link_tags;
}
// static, private
function _parseLinkAttrsWithTidy2( $html ) {
$tree = tidy_parse_string( $html );
$tags = null;
return oidParse::_parseLinkAttrsWithTidy2Worker( $tree->html(), $tags );
}
};
?>