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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
<?php
define('SERIALIZE_UNKNOWN', -1);
define('SERIALIZE_NONE', 0);
define('SERIALIZE_WDDX', 1);
define('SERIALIZE_BZIP', 2);
define('SERIALIZE_IMAP8', 3);
define('SERIALIZE_IMAPUTF7', 4);
define('SERIALIZE_IMAPUTF8', 5);
define('SERIALIZE_BASIC', 6);
define('SERIALIZE_GZ_DEFLATE', 7);
define('SERIALIZE_GZ_COMPRESS', 8);
define('SERIALIZE_GZ_ENCOD', 9);
define('SERIALIZE_BASE64', 10);
define('SERIALIZE_SQLXML', 11);
define('SERIALIZE_RAW', 12);
define('SERIALIZE_URL', 13);
define('SERIALIZE_UTF7', 14);
/** Use SERIALIZE_UTF7 and SERIALIZE_BASIC stacked. */
define('SERIALIZE_UTF7_BASIC', 15);
/**
* The Serialize:: class provides various methods of encapsulating data.
*
* $Horde: framework/Serialize/Serialize.php,v 1.25.10.9 2006/01/01 21:28:34 jan Exp $
*
* Copyright 2001-2006 Stephane Huther <shuther@bigfoot.com>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Stephane Huther <shuther@bigfoot.com>
* @since Horde 2.0
* @package Horde_Serialize
*/
class Horde_Serialize {
/**
* Serialize a value.
*
* See the list of constants at the top of the file for the serializing
* techniques that can be used.
*
* @param mixed $data The data to be serialized.
* @param mixed $mode The mode of serialization. Can be either a single
* mode or array of modes. If array, will be
* serialized in the order provided.
* @param mixed $params Any additional parameters the serialization method
* requires.
*
* @return string The serialized data.
* Returns PEAR_Error on error.
*/
function serialize($data, $mode = array(SERIALIZE_BASIC), $params = null)
{
if (!is_array($mode)) {
$mode = array($mode);
}
/* Return now if no data. */
if (empty($data) && !is_array($data)) {
return $data;
}
/* Parse through the list of serializing modes. */
foreach ($mode as $val) {
/* Check to make sure the mode is supported. */
if (!Horde_Serialize::hasCapability($val)) {
return PEAR::raiseError('Unsupported serialization type');
}
Horde_Serialize::_serialize($data, $val, $params);
if (is_a($data, 'PEAR_Error')) {
break;
}
}
return $data;
}
/**
* Serialize data.
*
* @access private
*
* @param mixed &$data The data to be serialized.
* @param mixed $mode The mode of serialization. Can be
* either a single mode or array of modes.
* If array, will be serialized in the
* order provided.
* @param mixed $params Any additional parameters the serialization method
* requires.
*/
function _serialize(&$data, $mode, $params = null)
{
switch ($mode) {
case SERIALIZE_NONE:
break;
case SERIALIZE_BZIP:
$data = bzcompress($data, 9, 30);
break;
case SERIALIZE_WDDX:
$data = wddx_serialize_value($data);
break;
case SERIALIZE_IMAP8:
$data = imap_8bit($data);
break;
case SERIALIZE_IMAPUTF7:
$data = imap_utf7_encode($data);
break;
case SERIALIZE_IMAPUTF8:
$data = imap_utf8($data);
break;
case SERIALIZE_GZ_DEFLATE:
$data = gzdeflate($data, 9);
break;
case SERIALIZE_BASIC:
$data = serialize($data);
break;
case SERIALIZE_GZ_COMPRESS:
$data = gzcompress($data, 9);
break;
case SERIALIZE_BASE64:
$data = base64_encode($data);
break;
case SERIALIZE_GZ_ENCOD:
$data = gzencode($data, 9);
break;
case SERIALIZE_RAW:
$data = rawurlencode($data);
break;
case SERIALIZE_URL:
$data = urlencode($data);
break;
case SERIALIZE_SQLXML:
require_once 'DB.php';
$sql2xml = &new xml_sql2xml();
$data = $sql2xml->getXML($data);
break;
case SERIALIZE_UTF7:
require_once 'Horde/String.php';
$data = String::convertCharset($data, $params, 'utf-7');
break;
case SERIALIZE_UTF7_BASIC:
Horde_Serialize::_serialize($data, SERIALIZE_UTF7, $params);
Horde_Serialize::_serialize($data, SERIALIZE_BASIC, $params);
break;
}
}
/**
* Unserialize a value.
*
* See the list of constants at the top of the file for the serializing
* techniques that can be used.
*
* @param mixed $data The data to be unserialized.
* @param mixed $mode The mode of unserialization. Can be either a
* single mode or array of modes. If array, will be
* unserialized in the order provided.
* @param mixed $params Any additional parameters the unserialization
* method requires.
*
* @return string The unserialized data.
* Returns PEAR_Error on error.
*/
function unserialize($data, $mode = SERIALIZE_BASIC, $params = null)
{
if (!is_array($mode)) {
$mode = array($mode);
}
/* Return now if no data. */
if (empty($data)) {
return $data;
}
/* Parse through the list of unserializing modes. */
foreach ($mode as $val) {
/* Check to make sure the mode is supported. */
if (!Horde_Serialize::hasCapability($val)) {
return PEAR::raiseError('Unsupported unserialization type');
}
Horde_Serialize::_unserialize($data, $val, $params);
if (is_a($data, 'PEAR_Error')) {
break;
}
}
return $data;
}
/**
* Unserialize data.
*
* @access private
*
* @param mixed &$data The data to be unserialized.
* @param mixed $mode The mode of unserialization. Can be either a
* single mode or array of modes. If array, will be
* unserialized in the order provided.
* @param mixed $params Any additional parameters the unserialization
* method requires.
*/
function _unserialize(&$data, $mode, $params = null)
{
switch ($mode) {
case SERIALIZE_NONE:
case SERIALIZE_SQLXML:
break;
case SERIALIZE_RAW:
$data = rawurldecode($data);
break;
case SERIALIZE_URL:
$data = urldecode($data);
break;
case SERIALIZE_WDDX:
$data = wddx_deserialize($data);
break;
case SERIALIZE_BZIP:
$data = bzdecompress($data, false);
break;
case SERIALIZE_IMAPUTF7:
$data = imap_utf7_decode($data);
break;
case SERIALIZE_BASIC:
$data = @unserialize($data);
break;
case SERIALIZE_GZ_DEFLATE:
$data = gzinflate($data);
break;
case SERIALIZE_BASE64:
$data = base64_decode($data);
break;
case SERIALIZE_GZ_COMPRESS:
$data = gzuncompress($data);
break;
case SERIALIZE_UTF7:
require_once 'Horde/String.php';
$data = String::convertCharset($data, 'utf-7', $params);
break;
case SERIALIZE_UTF7_BASIC:
Horde_Serialize::_unserialize($data, SERIALIZE_BASIC, $params);
Horde_Serialize::_unserialize($data, SERIALIZE_UTF7, $params);
break;
}
}
/**
* Check whether or not a serialization method is supported.
*
* @param integer $mode The serialization method.
*
* @return boolean True if supported, false if not.
*/
function hasCapability($mode)
{
switch ($mode) {
case SERIALIZE_BZIP:
return Util::extensionExists('bz2');
case SERIALIZE_WDDX:
return Util::extensionExists('wddx');
case SERIALIZE_IMAPUTF7:
case SERIALIZE_IMAPUTF8:
case SERIALIZE_IMAP8:
return Util::extensionExists('imap');
case SERIALIZE_GZ_DEFLATE:
case SERIALIZE_GZ_COMPRESS:
case SERIALIZE_GZ_ENCOD:
return Util::extensionExists('zlib');
case SERIALIZE_SQLXML:
return @include_once 'XML/sql2xml.php';
case SERIALIZE_NONE:
case SERIALIZE_BASIC:
case SERIALIZE_BASE64:
case SERIALIZE_RAW:
case SERIALIZE_URL:
case SERIALIZE_UTF7:
case SERIALIZE_UTF7_BASIC:
return true;
default:
return false;
}
}
}
|