File: Serialize.php

package info (click to toggle)
horde2 2.2.8-1sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,832 kB
  • ctags: 2,897
  • sloc: php: 12,784; sh: 954; sql: 149; makefile: 104; perl: 97; xml: 24; pascal: 6
file content (330 lines) | stat: -rw-r--r-- 10,397 bytes parent folder | download
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
320
321
322
323
324
325
326
327
328
329
330
<?php
/*
 * $Horde: horde/lib/Serialize.php,v 1.1.2.9 2003/01/17 10:22:15 jan Exp $
 *
 * Copyright 1999-2003 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.
 */

// Return codes
/** @constant SERIALIZEUNIT_OK Operation succeeded. */
define('SERIALIZEUNIT_OK', 0);

/** @constant SERIALIZEUNIT_ERROR Operation failed. */
define('SERIALIZEUNIT_ERROR', -1);

/** @constant SERIALIZEUNIT_ERROR_PARAMS Bad or missing parameters. */
define('SERIALIZEUNIT_ERROR_PARAMS', -2);

/** @constant SERIALIZEUNIT_ERROR_CONNECT Connection failure. */
define('SERIALIZEUNIT_ERROR_CONNECT', -3);

/** @constant SERIALIZEUNIT_ERROR_AUTH Authentication failure. */
define('SERIALIZEUNIT_ERROR_AUTH', -4);

/** @constant SERIALIZEUNIT_ERROR_EMPTY Empty retrieval result. */
define('SERIALIZEUNIT_ERROR_EMPTY', -5);

/** @constant SERIALIZEUNIT_ERROR_UNSUPPORTED Method not supported by
    driver. */
define('SERIALIZEUNIT_ERROR_UNSUPPORTED', -6);

// Kinds of serialization
/** @constant SERIALIZEUNIT_NONE no serialization */
define('SERIALIZEUNIT_NONE', 0);

/** @constant SERIALIZEUNIT_WDDX use WDDX */
define('SERIALIZEUNIT_WDDX', 1);

/** @constant SERIALIZEUNIT_BZIP use BZIP */
define('SERIALIZEUNIT_BZIP', 2);

/** @constant SERIALIZEUNIT_IMAP8 use IMAP 8b */
define('SERIALIZEUNIT_IMAP8', 3);

/** @constant SERIALIZEUNIT_IMAPUTF7 use IMAP UTF 7 */
define('SERIALIZEUNIT_IMAPUTF7', 4);

/** @constant SERIALIZEUNIT_IMAPUTF8 use IMAP UTF 8 */
define('SERIALIZEUNIT_IMAPUTF8', 5);

/** @constant SERIALIZEUNIT_BASIC use the basic method serialize from
    PHP */
define('SERIALIZEUNIT_BASIC', 6);

/** @constant SERIALIZEUNIT_GZ_DEFLAT GZ with deflate */
define('SERIALIZEUNIT_GZ_DEFLAT', 7);

/** @constant SERIALIZEUNIT_GZ_COMPRESS GZ with compress */
define('SERIALIZEUNIT_GZ_COMPRESS', 8);

/** @constant SERIALIZEUNIT_GZ_ENCOD GZ with encode */
define('SERIALIZEUNIT_GZ_ENCOD', 9);

/** @constant SERIALIZEUNIT_BASE64 Use Base64 */
define('SERIALIZEUNIT_BASE64', 10);

/** @constant SERIALIZEUNIT_SQLXML Use Sql2XML */
define('SERIALIZEUNIT_SQLXML', 11);

/** @constant SERIALIZEUNIT_RAW Raw url encode*/
define('SERIALIZEUNIT_RAW', 12);

/** @constant SERIALIZEUNIT_URL Url encode*/
define('SERIALIZEUNIT_URL', 13);

/** @constant SERIALIZEUNIT_UNKNOWN the mode is unknown */
define('SERIALIZEUNIT_UNKNOWN', -1);

// Specific default values
// BZIP
/** @constant SERIALIZEUNIT_BZIP_BLOCK define the block size 1-9 (the
    best) */
define('SERIALIZEUNIT_BZIP_BLOCK', 9);

/** @constant SERIALIZEUNIT_BZIP_FACTOR work factor - not used */
define('SERIALIZEUNIT_BZIP_FACTOR', 30);

/** @constant SERIALIZEUNIT_BZIP_SMALLMEM Try to minimize the load of
    memmory*/
define('SERIALIZEUNIT_BZIP_SMALLMEM', false);

// GZ
/** @constant SERIALIZEUNIT_GZ_DEFLAT_LEVEL level of compression -
    deflate 1-9(the best) */
define('SERIALIZEUNIT_GZ_DEFLAT_LEVEL', 9);

/** @constant SERIALIZEUNIT_GZ_DEFLAT_LENGTH max length of the
    uncompressed string*/
define('SERIALIZEUNIT_GZ_DEFLAT_LENGTH', 2048);

/** @constant SERIALIZEUNIT_GZ_COMPRESS_LEVEL level of compression -
    compress 1-9(the best) */
define('SERIALIZEUNIT_GZ_COMPRESS_LEVEL', 9);

/** @constant SERIALIZEUNIT_GZ_COMPRESS_LENGTH max length of the
    uncompressed string*/
define('SERIALIZEUNIT_GZ_COMPRESS_LENGTH', 2048);

/** @constant SERIALIZEUNIT_GZ_ENCOD_LEVEL level of compression -
    encode 1-9(the best) */
define('SERIALIZEUNIT_GZ_ENCOD_LEVEL', 9);

/**
 * Array (Kind of serialization => 2nd serialization)
 * It is possible to chain several serialization one after the other one
 * If a key is not found, the working process will stop, usefull
 * for SERIALIZEUNIT_NONE
 */
$__SERIALIZATION_CHAIN = array();

/**
 * This is a tool class, every method is static
 *
 * @author  Stephane Huther shuther@bigfoot.com
 * @version $Revision: 1.1.2.9 $
 * @since   Horde 2.0
 * @package horde
 */
class SerializeUnit {

    /**
     * Init $__SERIALIZATION_CHAIN to work properly with SerializeUnit after
     * @param array $insert content a parameter array that will be merged
     * @note for future compatibility raison, it is important to call this method
     */
    function init($insert=array())
    {
        global $__SERIALIZATION_CHAIN;

        if (!empty($insert))
        {
            $first=array();
            if (SerializeUnit::capability(SERIALIZEUNIT_GZ_COMPRESS)) {
                $first[SERIALIZEUNIT_WDDX]=SERIALIZEUNIT_GZ_COMPRESS;
                // $first[SERIALIZEUNIT_BASIC]=SERIALIZEUNIT_GZ_COMPRESS;
            }
            elseif (SerializeUnit::capability(SERIALIZEUNIT_BZIP)) {
                $first[SERIALIZEUNIT_WDDX]=SERIALIZEUNIT_BZIP;
                // $first[SERIALIZEUNIT_BASIC]=SERIALIZEUNIT_BZIP;
            }
            $__SERIALIZATION_CHAIN = array_merge($__SERIALIZATION_CHAIN, $first);
        }

        $__SERIALIZATION_CHAIN = array_merge($__SERIALIZATION_CHAIN,
                                              $insert);
    }

    /**
     * we serialize a value, it can be a string, an array (with the
     * method:SERIALIZEUNIT_WDDX), a DB::result (SERIALIZEUNIT_SQLXML)
     * or nearly everything (SERIALIZEUNIT_BASIC)
     *
     * @param mixed $var what are we going to serialize
     * @param integer $mode In which mode we will serialize, by default SERIALIZE
     * @return string the result or error (integer)
     */
    function &serializeUnit($var, $mode = 6)
    {
        global $__SERIALIZATION_CHAIN;

        switch ($mode) {
        case SERIALIZEUNIT_NONE:
            $out = $var;
            break;
        case SERIALIZEUNIT_BZIP:
            $out = bzcompress($var, SERIALIZEUNIT_BZIP_BLOCK, SERIALIZEUNIT_BZIP_FACTOR);
            break;
        case SERIALIZEUNIT_WDDX:
            $out = wddx_serialize_value($var);
            break;
        case SERIALIZEUNIT_IMAP8:
            $out = imap_8bit($var);
            break;
        case SERIALIZEUNIT_IMAPUTF7:
            $out = imap_utf7_encode($var);
            break;
        case SERIALIZEUNIT_IMAPUTF8:
            $out = imap_utf8($var);
            break;
        case SERIALIZEUNIT_GZ_DEFLAT:
            $out = gzdeflate($var, SERIALIZEUNIT_GZ_DEFLAT_LEVEL);
            break;
        case SERIALIZEUNIT_BASIC:
            $out = serialize($var);
            break;
        case SERIALIZEUNIT_GZ_COMPRESS:
            $out=gzcompress($var, SERIALIZEUNIT_GZ_COMPRESS_LEVEL);
            break;
        case SERIALIZEUNIT_BASE64:
            $out=base64_encode($var);
            break;
        case SERIALIZEUNIT_GZ_ENCOD:
            $out=gzencode($var, SERIALIZEUNIT_GZ_ENCOD_LEVEL);
            break;
        case SERIALIZEUNIT_RAW:
            $out=rawurlencode($var);
            break;
        case SERIALIZEUNIT_URL:
            $out=urlencode($var);
            break;
        case SERIALIZEUNIT_SQLXML:
            include_once ('DB.php');
            include_once('XML/sql2xml.php');
            $sql2xml = new xml_sql2xml();
            $out = $sql2xml->getXML($var);
            break;

        default:
            return SERIALIZEUNIT_ERROR_UNSUPPORTED;
        }

        if (isset($__SERIALIZATION_CHAIN[$mode]))
            return SerializeUnit::serializeUnit($out, $__SERIALIZATION_CHAIN[$mode]);
        return $out;
    }

    /*
     * we unserialize a value
     * @param mixed $var what are we going to unserialize
     * @param integer $mode In which mode we will unserialize, by default, the mode
     *                      is unknown, so we will detect it - not supported by now!
     * @return string the result
     */
    function &unSerializeUnit($var, $mode = 6)
    {
        global $__SERIALIZATION_CHAIN;

        if (empty($var)) {
            return $var;
        }
        if (isset($__SERIALIZATION_CHAIN[$mode])) {
            $var = SerializeUnit::deSerializeUnit($var, $__SERIALIZATION_CHAIN[$mode]);
        }

        switch ($mode) {
        case SERIALIZEUNIT_SQLXML:
        case SERIALIZEUNIT_NONE:
            $out = $var;
            break;
        case SERIALIZEUNIT_RAW:
            $out = rawurldecode($var);
            break;
        case SERIALIZEUNIT_URL:
            $out = urldecode($var);
            break;
        case SERIALIZEUNIT_WDDX:
            $out = wddx_deserialize($var);
            break;
        case SERIALIZEUNIT_BZIP:
            $out = bzdecompress($var, SERIALIZEUNIT_BZIP_SMALLMEM);
            break;
        case SERIALIZEUNIT_IMAPUTF7:
            $out = imap_utf7_decode($var);
            break;
        case SERIALIZEUNIT_BASIC:
            $out = unserialize($var);
            break;
        case SERIALIZEUNIT_GZ_DEFLAT:
            $out = gzinflate($var, SERIALIZEUNIT_GZ_DEFLAT_LENGTH);
            break;
        case SERIALIZEUNIT_BASE64:
            $out = base64_decode($var);
            break;
        case SERIALIZEUNIT_GZ_COMPRESS:
            $out = gzuncompress($var, SERIALIZEUNIT_GZ_COMPRESS_LENGTH);
            break;
        case SERIALIZEUNIT_UNKNOWN:
            $a = SerializeUnit::deSerializeUnit($strin, SERIALIZEUNIT_WDDX);
            return is_null($a) ? $a : SerializeUnit::deSerializeUnit($strin, SERIALIZEUNIT_NONE);
            break;

        default:
            return SERIALIZEUNIT_ERROR_UNSUPPORTED;
        }

        return $out;

    }

    /**
     * Check that a serialize system is supported or not
     * @param integer $mode Kind of serialization
     * @return boolean true of false
     */
    function capability($mode)
    {
        switch ($mode) {
        case SERIALIZEUNIT_BZIP:
            return extension_loaded('bz2');

        case SERIALIZEUNIT_WDDX:
            return extension_loaded('wddx');

        case SERIALIZEUNIT_IMAPUTF7:
        case SERIALIZEUNIT_IMAPUTF8:
        case SERIALIZEUNIT_IMAP8:
            return extension_loaded('imap');

        case SERIALIZEUNIT_GZ_DEFLAT:
        case SERIALIZEUNIT_GZ_COMPRESS:
        case SERIALIZEUNIT_GZ_ENCOD:
            return extension_loaded('zlib');

        case SERIALIZEUNIT_NONE:
        case SERIALIZEUNIT_BASIC:
        case SERIALIZEUNIT_BASE64:
        case SERIALIZEUNIT_SQLXML:
        case SERIALIZEUNIT_RAW:
        case SERIALIZEUNIT_URL:
            return true;

        default:
            return false;
        }
    }

}
?>