[6] | 1 | <?php |
---|
| 2 | |
---|
| 3 | class str_tab |
---|
| 4 | { |
---|
| 5 | |
---|
| 6 | function tab_to_str($TAB) { return $this->_tab_to_str("", $TAB); } |
---|
| 7 | |
---|
| 8 | function _tab_to_str($pre, $TAB) |
---|
| 9 | { $content = ""; |
---|
| 10 | if($TAB) |
---|
| 11 | { foreach($TAB as $key => $value) |
---|
| 12 | { if(is_array($value)) $content .= $this->_tab_to_str($pre.strlen($key).":".$key, $TAB[$key]); |
---|
| 13 | else $content .= $pre.strlen($key).":".$key.strlen($value).":".$value."\n"; |
---|
| 14 | } |
---|
| 15 | } |
---|
| 16 | else return $pre."a:\n"; |
---|
| 17 | return $content; |
---|
| 18 | } |
---|
| 19 | |
---|
| 20 | function str_to_tab($content) |
---|
| 21 | { $TAB = array(); |
---|
| 22 | $n = 0; |
---|
| 23 | while($n < strlen($content)) |
---|
| 24 | { $LINE_FOUND = 0; |
---|
| 25 | $words = array(); |
---|
| 26 | while($n < strlen($content) && !$LINE_FOUND) |
---|
| 27 | { $length = ""; |
---|
| 28 | $word = ""; |
---|
| 29 | $ENDS_WITH_EMPTY_TAB = 0; |
---|
| 30 | while($n < strlen($content) && strcmp(substr($content, $n, 1), ":") != 0) { $length .= $content[$n]; $n++; } $n++; |
---|
| 31 | if(strcmp($length, "a") == 0) $ENDS_WITH_EMPTY_TAB = 1; |
---|
| 32 | else |
---|
| 33 | { for($k = 0; $k < $length; $k++) { $word .= $content[$n]; $n++; } |
---|
| 34 | $words[] = $word; |
---|
| 35 | } |
---|
| 36 | $LINE_FOUND = $ENDS_WITH_EMPTY_TAB || $n == strlen($content) || strcmp(substr($content, $n, 1), "\n") == 0; |
---|
| 37 | } |
---|
| 38 | $n++; |
---|
| 39 | if($words) $TAB = $this->affect($TAB, $words, $ENDS_WITH_EMPTY_TAB); |
---|
| 40 | } |
---|
| 41 | return $TAB; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | function affect($TAB, $words, $ENDS_WITH_EMPTY_TAB) |
---|
| 45 | { if(count($words) > ($ENDS_WITH_EMPTY_TAB ? 1 : 2)) |
---|
| 46 | { if(!isset($TAB[$words[0]]) || !is_array($TAB[$words[0]])) $TAB[$words[0]] = array(); |
---|
| 47 | $_words = array(); |
---|
| 48 | $FIRST = 1; |
---|
| 49 | foreach($words as $word) |
---|
| 50 | { if($FIRST) $FIRST = 0; |
---|
| 51 | else $_words[] = $word; |
---|
| 52 | } |
---|
| 53 | $TAB[$words[0]] = $this->affect($TAB[$words[0]], $_words, $ENDS_WITH_EMPTY_TAB); |
---|
| 54 | } |
---|
| 55 | else $TAB[$words[0]] = ($ENDS_WITH_EMPTY_TAB ? array() : $words[1]); |
---|
| 56 | return $TAB; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | function str_to_hex($str) |
---|
| 60 | { $hex = ""; |
---|
| 61 | $n = 0; |
---|
| 62 | while($n < strlen($str)) |
---|
| 63 | { $hex_char = dechex(ord($str[$n])); |
---|
| 64 | if(strlen($hex_char) == 1) $hex_char = "0".$hex_char; |
---|
| 65 | $hex .= $hex_char; |
---|
| 66 | $n++; |
---|
| 67 | } |
---|
| 68 | return $hex; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | function hex_to_str($hex) |
---|
| 72 | { $n = 0; |
---|
| 73 | $str = ""; |
---|
| 74 | while($n + 2 <= strlen($hex)) { $str .= urldecode("%".substr($hex, $n, 2)); $n += 2; } |
---|
| 75 | return $str; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | function request_tab($str) { return $this->str_to_tab($this->hex_to_str(utf8_decode($str))); } |
---|
| 79 | |
---|
| 80 | function response_str($TAB) { return $this->str_to_hex($this->tab_to_str($TAB)); } |
---|
| 81 | |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | ?> |
---|