Subversion Repositories Applications.annuaire

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
42 aurelien 1
<?php
2
 
3
/**
4
 * URI normalization routines.
5
 *
6
 * @package OpenID
7
 * @author JanRain, Inc. <openid@janrain.com>
8
 * @copyright 2005 Janrain, Inc.
9
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
10
 */
11
 
12
require_once 'Services/Yadis/Misc.php';
13
 
14
// from appendix B of rfc 3986 (http://www.ietf.org/rfc/rfc3986.txt)
15
function Auth_OpenID_getURIPattern()
16
{
17
    return '&^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?&';
18
}
19
 
20
function Auth_OpenID_getAuthorityPattern()
21
{
22
    return '/^([^@]*@)?([^:]*)(:.*)?/';
23
}
24
 
25
function Auth_OpenID_getEncodedPattern()
26
{
27
    return '/%([0-9A-Fa-f]{2})/';
28
}
29
 
30
function Auth_OpenID_getUnreserved()
31
{
32
    $_unreserved = array();
33
    for ($i = 0; $i < 256; $i++) {
34
        $_unreserved[$i] = false;
35
    }
36
 
37
    for ($i = ord('A'); $i <= ord('Z'); $i++) {
38
        $_unreserved[$i] = true;
39
    }
40
 
41
    for ($i = ord('0'); $i <= ord('9'); $i++) {
42
        $_unreserved[$i] = true;
43
    }
44
 
45
    for ($i = ord('a'); $i <= ord('z'); $i++) {
46
        $_unreserved[$i] = true;
47
    }
48
 
49
    $_unreserved[ord('-')] = true;
50
    $_unreserved[ord('.')] = true;
51
    $_unreserved[ord('_')] = true;
52
    $_unreserved[ord('~')] = true;
53
 
54
    return $_unreserved;
55
}
56
 
57
function Auth_OpenID_getEscapeRE()
58
{
59
    $parts = array();
60
    foreach (array_merge(Services_Yadis_getUCSChars(),
61
                         Services_Yadis_getIPrivateChars()) as $pair) {
62
        list($m, $n) = $pair;
63
        $parts[] = sprintf("%s-%s", chr($m), chr($n));
64
    }
65
 
66
    return sprintf('[%s]', implode('', $parts));
67
}
68
 
69
function Auth_OpenID_pct_encoded_replace_unreserved($mo)
70
{
71
    $_unreserved = Auth_OpenID_getUnreserved();
72
 
73
    $i = intval($mo[1], 16);
74
    if ($_unreserved[$i]) {
75
        return chr($i);
76
    } else {
77
        return strtoupper($mo[0]);
78
    }
79
 
80
    return $mo[0];
81
}
82
 
83
function Auth_OpenID_pct_encoded_replace($mo)
84
{
85
    return chr(intval($mo[1], 16));
86
}
87
 
88
function Auth_OpenID_remove_dot_segments($path)
89
{
90
    $result_segments = array();
91
 
92
    while ($path) {
93
        if (Services_Yadis_startswith($path, '../')) {
94
            $path = substr($path, 3);
95
        } else if (Services_Yadis_startswith($path, './')) {
96
            $path = substr($path, 2);
97
        } else if (Services_Yadis_startswith($path, '/./')) {
98
            $path = substr($path, 2);
99
        } else if ($path == '/.') {
100
            $path = '/';
101
        } else if (Services_Yadis_startswith($path, '/../')) {
102
            $path = substr($path, 3);
103
            if ($result_segments) {
104
                array_pop($result_segments);
105
            }
106
        } else if ($path == '/..') {
107
            $path = '/';
108
            if ($result_segments) {
109
                array_pop($result_segments);
110
            }
111
        } else if (($path == '..') ||
112
                   ($path == '.')) {
113
            $path = '';
114
        } else {
115
            $i = 0;
116
            if ($path[0] == '/') {
117
                $i = 1;
118
            }
119
            $i = strpos($path, '/', $i);
120
            if ($i === false) {
121
                $i = strlen($path);
122
            }
123
            $result_segments[] = substr($path, 0, $i);
124
            $path = substr($path, $i);
125
        }
126
    }
127
 
128
    return implode('', $result_segments);
129
}
130
 
131
function Auth_OpenID_urinorm($uri)
132
{
133
    $uri_matches = array();
134
    preg_match(Auth_OpenID_getURIPattern(), $uri, $uri_matches);
135
 
136
    if (count($uri_matches) < 9) {
137
        for ($i = count($uri_matches); $i <= 9; $i++) {
138
            $uri_matches[] = '';
139
        }
140
    }
141
 
142
    $scheme = $uri_matches[2];
143
    if ($scheme) {
144
        $scheme = strtolower($scheme);
145
    }
146
 
147
    $scheme = $uri_matches[2];
148
    if ($scheme === '') {
149
        // No scheme specified
150
        return null;
151
    }
152
 
153
    $scheme = strtolower($scheme);
154
    if (!in_array($scheme, array('http', 'https'))) {
155
        // Not an absolute HTTP or HTTPS URI
156
        return null;
157
    }
158
 
159
    $authority = $uri_matches[4];
160
    if ($authority === '') {
161
        // Not an absolute URI
162
        return null;
163
    }
164
 
165
    $authority_matches = array();
166
    preg_match(Auth_OpenID_getAuthorityPattern(),
167
               $authority, $authority_matches);
168
    if (count($authority_matches) === 0) {
169
        // URI does not have a valid authority
170
        return null;
171
    }
172
 
173
    if (count($authority_matches) < 4) {
174
        for ($i = count($authority_matches); $i <= 4; $i++) {
175
            $authority_matches[] = '';
176
        }
177
    }
178
 
179
    list($_whole, $userinfo, $host, $port) = $authority_matches;
180
 
181
    if ($userinfo === null) {
182
        $userinfo = '';
183
    }
184
 
185
    if (strpos($host, '%') !== -1) {
186
        $host = strtolower($host);
187
        $host = preg_replace_callback(
188
                  Auth_OpenID_getEncodedPattern(),
189
                  'Auth_OpenID_pct_encoded_replace', $host);
190
        // NO IDNA.
191
        // $host = unicode($host, 'utf-8').encode('idna');
192
    } else {
193
        $host = strtolower($host);
194
    }
195
 
196
    if ($port) {
197
        if (($port == ':') ||
198
            ($scheme == 'http' && $port == ':80') ||
199
            ($scheme == 'https' && $port == ':443')) {
200
            $port = '';
201
        }
202
    } else {
203
        $port = '';
204
    }
205
 
206
    $authority = $userinfo . $host . $port;
207
 
208
    $path = $uri_matches[5];
209
    $path = preg_replace_callback(
210
               Auth_OpenID_getEncodedPattern(),
211
               'Auth_OpenID_pct_encoded_replace_unreserved', $path);
212
 
213
    $path = Auth_OpenID_remove_dot_segments($path);
214
    if (!$path) {
215
        $path = '/';
216
    }
217
 
218
    $query = $uri_matches[6];
219
    if ($query === null) {
220
        $query = '';
221
    }
222
 
223
    $fragment = $uri_matches[8];
224
    if ($fragment === null) {
225
        $fragment = '';
226
    }
227
 
228
    return $scheme . '://' . $authority . $path . $query . $fragment;
229
}
230
 
231
?>