summaryrefslogtreecommitdiffstats
path: root/Auth/OpenID/KVForm.php
blob: 3f7c7cfa3c130f8c7111aa16907569adf9dc5d94 (plain)
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
<?php

/**
 * This is the KVForm module.
 *
 * PHP versions 4 and 5
 *
 * LICENSE: See the COPYING file included in this distribution.
 *
 * @access private
 * @package OpenID
 * @author JanRain, Inc. <openid@janrain.com>
 * @copyright 2005 Janrain, Inc.
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
 */

/**
 * Convert an array into an OpenID colon/newline separated string
 * @access private
 */
function Auth_OpenID_arrayToKV($values)
{
    if ($values === null) {
        return null;
    }

    $serialized = '';
    foreach ($values as $key => $value) {
        if (is_array($value)) {
            list($key, $value) = $value;
        }

        if (strpos($key, ':') !== false) {
            trigger_error('":" in key:' . addslashes($key), E_USER_WARNING);
            return null;
        }

        if (strpos($key, "\n") !== false) {
            trigger_error('"\n" in key:' . addslashes($key), E_USER_WARNING);
            return null;
        }

        if (strpos($value, "\n") !== false) {
            trigger_error('"\n" in value:' . addslashes($value),
                          E_USER_WARNING);
            return null;
        }
        $serialized .= "$key:$value\n";
    }
    return $serialized;
}

/**
 * Convert an OpenID colon/newline separated string into an
 * associative array
 * @access private
 */
function Auth_OpenID_kvToArray($kvs)
{
    $lines = explode("\n", $kvs);

    $last = array_pop($lines);
    if ($last !== '') {
        trigger_error('No newline at end of kv string:' . addslashes($kvs),
                      E_USER_WARNING);
        array_push($lines, $last);
    }

    $values = array();

    for ($lineno = 0; $lineno < count($lines); $lineno++) {
        $line = $lines[$lineno];
        $kv = explode(':', $line, 2);
        if (count($kv) != 2) {
            $esc = addslashes($line);
            trigger_error("No colon on line $lineno: $esc",
                          E_USER_WARNING);
            continue;
        }

        $key = $kv[0];
        $tkey = trim($key);
        if ($tkey != $key) {
            $esc = addslashes($key);
            trigger_error("Whitespace in key on line $lineno: '$esc'",
                          E_USER_WARNING);
        }

        $value = $kv[1];
        $tval = trim($value);
        if ($tval != $value) {
            $esc = addslashes($value);
            trigger_error("Whitespace in value on line $lineno: '$esc'",
                          E_USER_WARNING);
        }

        $values[$tkey] = $tval;
    }

    return $values;
}

?>