123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- class Auth_OpenID_KVForm {
-
- static function toArray($kvs, $strict=false)
- {
- $lines = explode("\n", $kvs);
- $last = array_pop($lines);
- if ($last !== '') {
- 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) {
- if ($strict) {
- return false;
- }
- continue;
- }
- $key = $kv[0];
- $tkey = trim($key);
- if ($tkey != $key) {
- if ($strict) {
- return false;
- }
- }
- $value = $kv[1];
- $tval = trim($value);
- if ($tval != $value) {
- if ($strict) {
- return false;
- }
- }
- $values[$tkey] = $tval;
- }
- return $values;
- }
-
- static 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) {
- return null;
- }
- if (strpos($key, "\n") !== false) {
- return null;
- }
- if (strpos($value, "\n") !== false) {
- return null;
- }
- $serialized .= "$key:$value\n";
- }
- return $serialized;
- }
- }
|