00001 <?php 00002 00021 class Auth_OpenID_KVForm { 00029 function toArray($kvs, $strict=false) 00030 { 00031 $lines = explode("\n", $kvs); 00032 00033 $last = array_pop($lines); 00034 if ($last !== '') { 00035 array_push($lines, $last); 00036 if ($strict) { 00037 return false; 00038 } 00039 } 00040 00041 $values = array(); 00042 00043 for ($lineno = 0; $lineno < count($lines); $lineno++) { 00044 $line = $lines[$lineno]; 00045 $kv = explode(':', $line, 2); 00046 if (count($kv) != 2) { 00047 if ($strict) { 00048 return false; 00049 } 00050 continue; 00051 } 00052 00053 $key = $kv[0]; 00054 $tkey = trim($key); 00055 if ($tkey != $key) { 00056 if ($strict) { 00057 return false; 00058 } 00059 } 00060 00061 $value = $kv[1]; 00062 $tval = trim($value); 00063 if ($tval != $value) { 00064 if ($strict) { 00065 return false; 00066 } 00067 } 00068 00069 $values[$tkey] = $tval; 00070 } 00071 00072 return $values; 00073 } 00074 00081 function fromArray($values) 00082 { 00083 if ($values === null) { 00084 return null; 00085 } 00086 00087 ksort($values); 00088 00089 $serialized = ''; 00090 foreach ($values as $key => $value) { 00091 if (is_array($value)) { 00092 list($key, $value) = array($value[0], $value[1]); 00093 } 00094 00095 if (strpos($key, ':') !== false) { 00096 return null; 00097 } 00098 00099 if (strpos($key, "\n") !== false) { 00100 return null; 00101 } 00102 00103 if (strpos($value, "\n") !== false) { 00104 return null; 00105 } 00106 $serialized .= "$key:$value\n"; 00107 } 00108 return $serialized; 00109 } 00110 } 00111 00112 ?>