Module: PHP
- Defined in:
- lib/php_serialize.rb
Defined Under Namespace
Classes: StringIOReader
Class Method Summary collapse
-
.serialize(var, assoc = false) ⇒ Object
Returns a string representing the argument in a form PHP.unserialize and PHP’s unserialize() should both be able to load.
-
.serialize_session(var, assoc = false) ⇒ Object
Like PHP.serialize, but only accepts a Hash or associative Array as the root type.
-
.unserialize(string, classmap = nil, assoc = false) ⇒ Object
Returns an object containing the reconstituted data from serialized.
Class Method Details
.serialize(var, assoc = false) ⇒ Object
Returns a string representing the argument in a form PHP.unserialize and PHP’s unserialize() should both be able to load.
string = PHP.serialize(mixed var[, bool assoc])
Array, Hash, Fixnum, Float, True/FalseClass, NilClass, String and Struct are supported; as are objects which support the to_assoc method, which returns an array of the form [[‘attr_name’, ‘value’]..]. Anything else will raise a TypeError.
If ‘assoc’ is specified, Array’s who’s first element is a two value array will be assumed to be an associative array, and will be serialized as a PHP associative array rather than a multidimensional array.
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 |
# File 'lib/php_serialize.rb', line 31 def PHP.serialize(var, assoc = false) # {{{ s = String.new case var when Array s << "a:#{var.size}:{" if assoc and var.first.is_a?(Array) and var.first.size == 2 var.each { |k,v| s << PHP.serialize(k, assoc) << PHP.serialize(v, assoc) } else var.each_with_index { |v,i| s << "i:#{i};#{PHP.serialize(v, assoc)}" } end s << '}' when Hash s << "a:#{var.size}:{" var.each do |k,v| s << "#{PHP.serialize(k, assoc)}#{PHP.serialize(v, assoc)}" end s << '}' when Struct # encode as Object with same name s << "O:#{var.class.to_s.bytesize}:\"#{var.class.to_s.downcase}\":#{var.members.length}:{" var.members.each do |member| s << "#{PHP.serialize(member, assoc)}#{PHP.serialize(var[member], assoc)}" end s << '}' when String, Symbol s << "s:#{var.to_s.bytesize}:\"#{var.to_s}\";" when Integer s << "i:#{var};" when Float s << "d:#{var};" when NilClass s << 'N;' when FalseClass, TrueClass s << "b:#{var ? 1 : 0};" else if var.respond_to?(:to_assoc) v = var.to_assoc # encode as Object with same name s << "O:#{var.class.to_s.bytesize}:\"#{var.class.to_s.downcase}\":#{v.length}:{" v.each do |k,v| s << "#{PHP.serialize(k.to_s, assoc)}#{PHP.serialize(v, assoc)}" end s << '}' else raise TypeError, "Unable to serialize type #{var.class}" end end s end |
.serialize_session(var, assoc = false) ⇒ Object
Like PHP.serialize, but only accepts a Hash or associative Array as the root type. The results are returned in PHP session format.
string = PHP.serialize_session(mixed var[, bool assoc])
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 |
# File 'lib/php_serialize.rb', line 99 def PHP.serialize_session(var, assoc = false) # {{{ s = String.new case var when Hash var.each do |key,value| if key.to_s.include?('|') raise IndexError, "Top level names may not contain pipes" end s << "#{key}|#{PHP.serialize(value, assoc)}" end when Array var.each do |x| case x when Array if x.size == 2 s << "#{x[0]}|#{PHP.serialize(x[1])}" else raise TypeError, "Array is not associative" end end end else raise TypeError, "Unable to serialize sessions with top level types other than Hash and associative Array" end s end |
.unserialize(string, classmap = nil, assoc = false) ⇒ Object
Returns an object containing the reconstituted data from serialized.
mixed = PHP.unserialize(string serialized, [hash classmap, [bool assoc]])
If a PHP array (associative; like an ordered hash) is encountered, it scans the keys; if they’re all incrementing integers counting from 0, it’s unserialized as an Array, otherwise it’s unserialized as a Hash. Note: this will lose ordering. To avoid this, specify assoc=true, and it will be unserialized as an associative array: [[key,value],…]
If a serialized object is encountered, the hash ‘classmap’ is searched for the class name (as a symbol). Since PHP classnames are not case-preserving, this must be a .capitalize()d representation. The value is expected to be the class itself; i.e. something you could call .new on.
If it’s not found in ‘classmap’, the current constant namespace is searched, and failing that, a new Struct(classname) is generated, with the arguments for .new specified in the same order PHP provided; since PHP uses hashes to represent attributes, this should be the same order they’re specified in PHP, but this is untested.
each serialized attribute is sent to the new object using the respective attribute=() method; you’ll get a NameError if the method doesn’t exist.
Array, Hash, Fixnum, Float, True/FalseClass, NilClass and String should be returned identically (i.e. foo == PHP.unserialize(PHP.serialize(foo)) for these types); Struct should be too, provided it’s in the namespace Module.const_get within unserialize() can see, or you gave it the same name in the Struct.new(<structname>), otherwise you should provide it in classmap.
Note: StringIO is required for unserialize(); it’s loaded as needed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/php_serialize.rb', line 158 def PHP.unserialize(string, classmap = nil, assoc = false) # {{{ if classmap == true or classmap == false assoc = classmap classmap = {} end classmap ||= {} ret = nil string = StringIOReader.new(string) while string.string[string.pos, 32] =~ /^(\w+)\|/ # session_name|serialized_data ret ||= {} string.pos += $&.size ret[$1] = PHP.do_unserialize(string, classmap, assoc) end ret || PHP.do_unserialize(string, classmap, assoc) end |