[ Index ] |
|
Code source de Seagull 0.6.1 |
1 // {{{ HTML_AJAX_Serialize_PHP 2 /** 3 * PHP serializer 4 * 5 * This class can be used to serialize and unserialize data in a 6 * format compatible with PHP's native serialization functions. 7 * 8 * @version 0.0.3 9 * @copyright 2005 Arpad Ray <arpad@php.net> 10 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL 11 * 12 * See Main.js for Author/license details 13 */ 14 15 function HTML_AJAX_Serialize_PHP() {} 16 HTML_AJAX_Serialize_PHP.prototype = { 17 error: false, 18 message: "", 19 cont: "", 20 defaultEncoding: 'UTF-8', 21 contentType: 'application/php-serialized; charset: UTF-8', 22 // {{{ serialize 23 /** 24 * Serializes a variable 25 * 26 * @param mixed inp the variable to serialize 27 * @return string a string representation of the input, 28 * which can be reconstructed by unserialize() 29 * @author Arpad Ray <arpad@rajeczy.com> 30 * @author David Coallier <davidc@php.net> 31 */ 32 serialize: function(inp) { 33 var type = HTML_AJAX_Util.getType(inp); 34 var val; 35 switch (type) { 36 case "undefined": 37 val = "N"; 38 break; 39 case "boolean": 40 val = "b:" + (inp ? "1" : "0"); 41 break; 42 case "number": 43 val = (Math.round(inp) == inp ? "i" : "d") + ":" + inp; 44 break; 45 case "string": 46 val = "s:" + inp.length + ":\"" + inp + "\""; 47 break; 48 case "array": 49 val = "a"; 50 case "object": 51 if (type == "object") { 52 var objname = inp.constructor.toString().match(/(\w+)\(\)/); 53 if (objname == undefined) { 54 return; 55 } 56 objname[1] = this.serialize(objname[1]); 57 val = "O" + objname[1].substring(1, objname[1].length - 1); 58 } 59 var count = 0; 60 var vals = ""; 61 var okey; 62 for (key in inp) { 63 okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key); 64 vals += this.serialize(okey) + 65 this.serialize(inp[key]); 66 count++; 67 } 68 val += ":" + count + ":{" + vals + "}"; 69 break; 70 } 71 if (type != "object" && type != "array") val += ";"; 72 return val; 73 }, 74 // }}} 75 // {{{ unserialize 76 /** 77 * Reconstructs a serialized variable 78 * 79 * @param string inp the string to reconstruct 80 * @return mixed the variable represented by the input string, or void on failure 81 */ 82 unserialize: function(inp) { 83 this.error = 0; 84 if (inp == "" || inp.length < 2) { 85 this.raiseError("input is too short"); 86 return; 87 } 88 var val, kret, vret, cval; 89 var type = inp.charAt(0); 90 var cont = inp.substring(2); 91 var size = 0, divpos = 0, endcont = 0, rest = "", next = ""; 92 93 switch (type) { 94 case "N": // null 95 if (inp.charAt(1) != ";") { 96 this.raiseError("missing ; for null", cont); 97 } 98 // leave val undefined 99 rest = cont; 100 break; 101 case "b": // boolean 102 if (!/[01];/.test(cont.substring(0,2))) { 103 this.raiseError("value not 0 or 1, or missing ; for boolean", cont); 104 } 105 val = (cont.charAt(0) == "1"); 106 rest = cont.substring(1); 107 break; 108 case "s": // string 109 val = ""; 110 divpos = cont.indexOf(":"); 111 if (divpos == -1) { 112 this.raiseError("missing : for string", cont); 113 break; 114 } 115 size = parseInt(cont.substring(0, divpos)); 116 if (size == 0) { 117 if (cont.length - divpos < 4) { 118 this.raiseError("string is too short", cont); 119 break; 120 } 121 rest = cont.substring(divpos + 4); 122 break; 123 } 124 if ((cont.length - divpos - size) < 4) { 125 this.raiseError("string is too short", cont); 126 break; 127 } 128 if (cont.substring(divpos + 2 + size, divpos + 4 + size) != "\";") { 129 this.raiseError("string is too long, or missing \";", cont); 130 } 131 val = cont.substring(divpos + 2, divpos + 2 + size); 132 rest = cont.substring(divpos + 4 + size); 133 break; 134 case "i": // integer 135 case "d": // float 136 var dotfound = 0; 137 for (var i = 0; i < cont.length; i++) { 138 cval = cont.charAt(i); 139 if (isNaN(parseInt(cval)) && !(type == "d" && cval == "." && !dotfound++)) { 140 endcont = i; 141 break; 142 } 143 } 144 if (!endcont || cont.charAt(endcont) != ";") { 145 this.raiseError("missing or invalid value, or missing ; for int/float", cont); 146 } 147 val = cont.substring(0, endcont); 148 val = (type == "i" ? parseInt(val) : parseFloat(val)); 149 rest = cont.substring(endcont + 1); 150 break; 151 case "a": // array 152 if (cont.length < 4) { 153 this.raiseError("array is too short", cont); 154 return; 155 } 156 divpos = cont.indexOf(":", 1); 157 if (divpos == -1) { 158 this.raiseError("missing : for array", cont); 159 return; 160 } 161 size = parseInt(cont.substring(0, divpos)); 162 cont = cont.substring(divpos + 2); 163 val = new Array(); 164 if (cont.length < 1) { 165 this.raiseError("array is too short", cont); 166 return; 167 } 168 for (var i = 0; i < size; i++) { 169 kret = this.unserialize(cont, 1); 170 if (this.error || kret[0] == undefined || kret[1] == "") { 171 this.raiseError("missing or invalid key, or missing value for array", cont); 172 return; 173 } 174 vret = this.unserialize(kret[1], 1); 175 if (this.error) { 176 this.raiseError("invalid value for array", cont); 177 return; 178 } 179 val[kret[0]] = vret[0]; 180 cont = vret[1]; 181 } 182 if (cont.charAt(0) != "}") { 183 this.raiseError("missing ending }, or too many values for array", cont); 184 return; 185 } 186 rest = cont.substring(1); 187 break; 188 case "O": // object 189 divpos = cont.indexOf(":"); 190 if (divpos == -1) { 191 this.raiseError("missing : for object", cont); 192 return; 193 } 194 size = parseInt(cont.substring(0, divpos)); 195 var objname = cont.substring(divpos + 2, divpos + 2 + size); 196 if (cont.substring(divpos + 2 + size, divpos + 4 + size) != "\":") { 197 this.raiseError("object name is too long, or missing \":", cont); 198 return; 199 } 200 var objprops = this.unserialize("a:" + cont.substring(divpos + 4 + size), 1); 201 if (this.error) { 202 this.raiseError("invalid object properties", cont); 203 return; 204 } 205 rest = objprops[1]; 206 var objout = "function " + objname + "(){"; 207 for (key in objprops[0]) { 208 objout += "this." + key + "=objprops[0]['" + key + "'];"; 209 } 210 objout += "}val=new " + objname + "();"; 211 eval(objout); 212 break; 213 default: 214 this.raiseError("invalid input type", cont); 215 } 216 return (arguments.length == 1 ? val : [val, rest]); 217 }, 218 // }}} 219 // {{{ getError 220 /** 221 * Gets the last error message 222 * 223 * @return string the last error message from unserialize() 224 */ 225 getError: function() { 226 return this.message + "\n" + this.cont; 227 }, 228 // }}} 229 // {{{ raiseError 230 /** 231 * Raises an eror (called by unserialize().) 232 * 233 * @param string message the error message 234 * @param string cont the remaining unserialized content 235 */ 236 raiseError: function(message, cont) { 237 this.error = 1; 238 this.message = message; 239 this.cont = cont; 240 } 241 // }}} 242 } 243 // }}} 244
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 30 01:27:52 2007 | par Balluche grâce à PHPXref 0.7 |