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
 * OpenID protocol key-value/comma-newline format parsing and
5
 * serialization
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: See the COPYING file included in this distribution.
10
 *
11
 * @access private
12
 * @package OpenID
13
 * @author JanRain, Inc. <openid@janrain.com>
14
 * @copyright 2005 Janrain, Inc.
15
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
16
 */
17
 
18
/**
19
 * Container for key-value/comma-newline OpenID format and parsing
20
 */
21
class Auth_OpenID_KVForm {
22
    /**
23
     * Convert an OpenID colon/newline separated string into an
24
     * associative array
25
     *
26
     * @static
27
     * @access private
28
     */
29
    function toArray($kvs, $strict=false)
30
    {
31
        $lines = explode("\n", $kvs);
32
 
33
        $last = array_pop($lines);
34
        if ($last !== '') {
35
            array_push($lines, $last);
36
            if ($strict) {
37
                return false;
38
            }
39
        }
40
 
41
        $values = array();
42
 
43
        for ($lineno = 0; $lineno < count($lines); $lineno++) {
44
            $line = $lines[$lineno];
45
            $kv = explode(':', $line, 2);
46
            if (count($kv) != 2) {
47
                if ($strict) {
48
                    return false;
49
                }
50
                continue;
51
            }
52
 
53
            $key = $kv[0];
54
            $tkey = trim($key);
55
            if ($tkey != $key) {
56
                if ($strict) {
57
                    return false;
58
                }
59
            }
60
 
61
            $value = $kv[1];
62
            $tval = trim($value);
63
            if ($tval != $value) {
64
                if ($strict) {
65
                    return false;
66
                }
67
            }
68
 
69
            $values[$tkey] = $tval;
70
        }
71
 
72
        return $values;
73
    }
74
 
75
    /**
76
     * Convert an array into an OpenID colon/newline separated string
77
     *
78
     * @static
79
     * @access private
80
     */
81
    function fromArray($values)
82
    {
83
        if ($values === null) {
84
            return null;
85
        }
86
 
87
        ksort($values);
88
 
89
        $serialized = '';
90
        foreach ($values as $key => $value) {
91
            if (is_array($value)) {
92
                list($key, $value) = array($value[0], $value[1]);
93
            }
94
 
95
            if (strpos($key, ':') !== false) {
96
                return null;
97
            }
98
 
99
            if (strpos($key, "\n") !== false) {
100
                return null;
101
            }
102
 
103
            if (strpos($value, "\n") !== false) {
104
                return null;
105
            }
106
            $serialized .= "$key:$value\n";
107
        }
108
        return $serialized;
109
    }
110
}
111
 
112
?>