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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
<?php
/**
* OpenID protocol key-value/comma-newline format parsing and
* serialization
*
* 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
*/
/**
* Container for key-value/comma-newline OpenID format and parsing
*/
class Auth_OpenID_KVForm {
/**
* Issue a warning when parsing KV form
*
* @static
* @access private
*/
function _warn($msg)
{
trigger_error($msg, E_USER_WARNING);
}
/**
* Convert an OpenID colon/newline separated string into an
* associative array
*
* @static
* @access private
*/
function toArray($kvs, $strict=false)
{
$lines = explode("\n", $kvs);
$last = array_pop($lines);
if ($last !== '') {
$msg = 'No newline at end of kv string:' . var_export($kvs, true);
Auth_OpenID_KVForm::_warn($msg);
array_push($lines, $last);
if ($strict) {
return false;
}
}
$values = array();
for ($lineno = 0; $lineno < count($lines); $lineno++) {
$line = $lines[$lineno];
$kv = explode(':', $line, 2);
if (count($kv) != 2) {
$msg = "No colon on line $lineno: " . var_export($line, true);
Auth_OpenID_KVForm::_warn($msg);
if ($strict) {
return false;
}
continue;
}
$key = $kv[0];
$tkey = trim($key);
if ($tkey != $key) {
$msg = "Whitespace in key on line $lineno:" .
var_export($key, true);
Auth_OpenID_KVForm::_warn($msg);
if ($strict) {
return false;
}
}
$value = $kv[1];
$tval = trim($value);
if ($tval != $value) {
$msg = "Whitespace in value on line $lineno: " .
var_export($value, true);
Auth_OpenID_KVForm::_warn($msg);
if ($strict) {
return false;
}
}
$values[$tkey] = $tval;
}
return $values;
}
/**
* Convert an array into an OpenID colon/newline separated string
*
* @static
* @access private
*/
function fromArray($values)
{
if ($values === null) {
return null;
}
ksort($values);
$serialized = '';
foreach ($values as $key => $value) {
if (is_array($value)) {
list($key, $value) = array($value[0], $value[1]);
}
if (strpos($key, ':') !== false) {
$msg = '":" in key:' . var_export($key, true);
Auth_OpenID_KVForm::_warn($msg);
return null;
}
if (strpos($key, "\n") !== false) {
$msg = '"\n" in key:' . var_export($key, true);
Auth_OpenID_KVForm::_warn($msg);
return null;
}
if (strpos($value, "\n") !== false) {
$msg = '"\n" in value:' . var_export($value, true);
Auth_OpenID_KVForm::_warn($msg);
return null;
}
$serialized .= "$key:$value\n";
}
return $serialized;
}
}
?>
|