Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
var_serialise.inc
1 <?php
36 ####################### THIS NEEDS TO BE FINISHED - JS NEEDS TO BE UPDATED
37 
38 $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR'] = chr(27);
39 $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_FROM_LIST'] = Array("\r", "\n", '/', '<', '>');
40 $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_TO_LIST'] = Array(
41  $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR'].'r',
42  $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR'].'n',
43  $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR'].'/',
44  $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR'].'<',
45  $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR'].'>',
46  );
47 
58 function var_serialise($var, $string_escape=false)
59 {
60  $str = _var_serialise($var, '');
61  if ($str !== false && $string_escape) {
62  $str = str_replace("\n", "\\n", addslashes($str));
63  }
64  return $str;
65 
66 }//end var_serialise()
67 
68 
77 function _var_serialise(&$var, $indent)
78 {
79 
80  $str = '';
81  $type = strtolower(gettype($var));
82 
83  switch ($type) {
84  // normal vars
85  case 'string' :
86  if (strpos($var, $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR']) !== FALSE) {
87  trigger_error('Your data contained the escape character (ASCII Code '.ord($GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR']).'), this has been removed', E_USER_NOTICE);
88  $var = str_replace($GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR'], '', $var);
89  }
90  $var = str_replace($GLOBALS['VAR_SERIALISE_STRING_ESCAPE_FROM_LIST'], $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_TO_LIST'], $var);
91  // fall through on purpose
92  case 'integer' :
93  case 'double' :
94  $str .= '<val_type>'.$type.'</val_type><val>'.$var."</val>\n";
95  break;
96 
97  case 'null' :
98  $str .= '<val_type>'.$type."</val_type><val></val>\n";
99  break;
100 
101  case 'boolean' :
102  $str .= '<val_type>'.$type.'</val_type><val>'.(($var) ? 1 : 0)."</val>\n";
103  break;
104 
105  // recursive vars
106  case 'array' :
107  $str .= '<val_type>'.$type."</val_type>\n";
108  for (reset($var); NULL !== ($k = key($var)); next($var)) {
109  $sub_str = _var_serialise($var[$k], $indent.' ');
110  if ($sub_str === false) return false;
111  if (is_string($k)) {
112  if (strpos($k, $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR']) !== FALSE) {
113  trigger_error('Your data contained the escape character (ASCII Code '.ord($GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR']).'), this has been removed', E_USER_NOTICE);
114  $k = str_replace($GLOBALS['VAR_SERIALISE_STRING_ESCAPE_CHAR'], '', $k);
115  }
116  $k = str_replace($GLOBALS['VAR_SERIALISE_STRING_ESCAPE_FROM_LIST'], $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_TO_LIST'], $k);
117  }
118  $str .= $indent.' <name_type>'.strtolower(gettype($k)).'</name_type><name>'.$k.'</name>'.$sub_str;
119  }//end for
120 
121  break;
122 
123  default :
124  trigger_error('Unable to serialise a var of type "'.$type.'"', E_USER_WARNING);
125  return false;
126  }//end switch
127 
128  return $str;
129 
130 }//end _var_serialise()
131 
132 
142 function var_unserialise($str)
143 {
144  $str = str_replace(Array("\r\n", "\r"), "\n", $str);
145  $lines = explode("\n", trim($str));
146  $i = 0;
147  list($var, $name) = _var_unserialise($lines, $i, '');
148  return $var;
149 }//end var_unserialise()
150 
151 
162 function _var_unserialise(&$lines, &$i, $indent)
163 {
164  $str = &$lines[$i];
165 
166  // if it's blank then return null
167  if ($str == $indent) return Array(null, null);
168 
169  $name_type = '';
170  $name = null;
171 
172  $e = '/^'.$indent.'<name_type>(.*)<\/name_type><name>(.*)<\/name>(.*)$/';
173  if (preg_match($e, $str, $matches)) {
174  $name_type = $matches[1];
175  $name = $matches[2];
176  if ($name_type == 'string') {
177  $name = str_replace($GLOBALS['VAR_SERIALISE_STRING_ESCAPE_TO_LIST'], $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_FROM_LIST'], $name);
178  }
179  $str = $matches[3];
180  settype($name, $name_type);
181  }//end if
182 
183  // OK so it's an array
184  if ($str == '<val_type>array</val_type>'){
185  $indent_len = strlen($indent);
186  $i++;
187  $val = Array();
188  // while the indent is still the same unserialise our contents
189  while (isset($lines[$i]) && $indent.' ' == substr($lines[$i], 0, $indent_len + 1)) {
190  list($var, $key) = _var_unserialise($lines, $i, $indent.' ');
191  $val[$key] = $var;
192  $i++;
193  }//end while
194  $i--;
195 
196  return Array($val, $name);
197 
198  }//end if
199 
200  $val_type = '';
201  $val = null;
202 
203  $e = '/^<val_type>(.*)<\/val_type><val>(.*)<\/val>$/';
204  if (preg_match($e, $str, $matches)) {
205 
206  $val_type = $matches[1];
207  $val = $matches[2];
208  settype($val, $val_type);
209 
210  // if this is a string then we need to reverse the escaping process
211  if ($val_type == 'string') {
212  $val = str_replace($GLOBALS['VAR_SERIALISE_STRING_ESCAPE_TO_LIST'], $GLOBALS['VAR_SERIALISE_STRING_ESCAPE_FROM_LIST'], $val);
213  }//end if
214 
215  }//end if
216 
217  return Array($val, $name);
218 
219 }//end _var_unserialise()
220 
221 ?>