php-serialize 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d3c32a1fe43d8186acdc93c67320994c69ae8d4
4
+ data.tar.gz: 2fe820e15442213a58e8c084965583155cada091
5
+ SHA512:
6
+ metadata.gz: 42a2a80a57cf7c4b57a80f65804172a95407a2d27f5ae9d7daca1a4224ffe3eee926efda691161917639d4959b1870f2e28db9c32add604419855954e1879578
7
+ data.tar.gz: e656d868b8a3a1d87b8bcb5827021dd31b2fb729539388efab48e8f02938d4ee177046863b9950c7993b15c60a288387ded2b9b2acd9ac2fd3edb6a4ac691024
@@ -0,0 +1,2 @@
1
+ # Bunlder auto requires the gemname, so we redirect to the right file.
2
+ require "php_serialize"
@@ -1,55 +1,31 @@
1
- #!/usr/bin/env ruby
2
- # Copyright (c) 2003-2009 Thomas Hurst <tom@hur.st>
3
- #
4
- # Permission is hereby granted, free of charge, to any person obtaining a copy
5
- # of this software and associated documentation files (the "Software"), to deal
6
- # in the Software without restriction, including without limitation the rights
7
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
- # copies of the Software, and to permit persons to whom the Software is
9
- # furnished to do so, subject to the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be included in
12
- # all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- # SOFTWARE.
21
-
22
- # PHP serialize() and unserialize() workalikes
23
- #
24
- # Release History:
25
- # 1.0.0 - 2003-06-02 - First release.
26
- # 1.0.1 - 2003-06-16 - Minor bugfixes.
27
- # 1.0.2 - 2004-09-17 - Switch all {}'s to explicit Hash.new's.
28
- # 1.1.0 - 2009-04-01 - Pass assoc to recursive calls (thanks to Edward Speyer).
29
- # - Serialize Symbol like String.
30
- # - Add testsuite.
31
- # - Instantiate auto-generated Structs properly (thanks
32
- # to Philip Hallstrom).
33
- # - Unserialize arrays properly in assoc mode.
34
- # - Add PHP session support (thanks to TJ Vanderpoel).
35
- # - Release as tarball and gem.
36
- #
37
- # See http://www.php.net/serialize and http://www.php.net/unserialize for
38
- # details on the PHP side of all this.
1
+ require 'stringio'
2
+
39
3
  module PHP
40
- # string = PHP.serialize(mixed var[, bool assoc])
41
- #
42
- # Returns a string representing the argument in a form PHP.unserialize
43
- # and PHP's unserialize() should both be able to load.
44
- #
45
- # Array, Hash, Fixnum, Float, True/FalseClass, NilClass, String and Struct
46
- # are supported; as are objects which support the to_assoc method, which
47
- # returns an array of the form [['attr_name', 'value']..]. Anything else
48
- # will raise a TypeError.
49
- #
50
- # If 'assoc' is specified, Array's who's first element is a two value
51
- # array will be assumed to be an associative array, and will be serialized
52
- # as a PHP associative array rather than a multidimensional array.
4
+ class StringIOReader < StringIO
5
+ # Reads data from the buffer until +char+ is found. The
6
+ # returned string will include +char+.
7
+ def read_until(char)
8
+ val, cpos = '', pos
9
+ if idx = string.index(char, cpos)
10
+ val = read(idx - cpos + 1)
11
+ end
12
+ val
13
+ end
14
+ end
15
+
16
+ # Returns a string representing the argument in a form PHP.unserialize
17
+ # and PHP's unserialize() should both be able to load.
18
+ #
19
+ # string = PHP.serialize(mixed var[, bool assoc])
20
+ #
21
+ # Array, Hash, Fixnum, Float, True/FalseClass, NilClass, String and Struct
22
+ # are supported; as are objects which support the to_assoc method, which
23
+ # returns an array of the form [['attr_name', 'value']..]. Anything else
24
+ # will raise a TypeError.
25
+ #
26
+ # If 'assoc' is specified, Array's who's first element is a two value
27
+ # array will be assumed to be an associative array, and will be serialized
28
+ # as a PHP associative array rather than a multidimensional array.
53
29
  def PHP.serialize(var, assoc = false) # {{{
54
30
  s = ''
55
31
  case var
@@ -83,7 +59,7 @@ module PHP
83
59
  s << '}'
84
60
 
85
61
  when String, Symbol
86
- s << "s:#{var.to_s.length}:\"#{var.to_s}\";"
62
+ s << "s:#{var.to_s.bytesize}:\"#{var.to_s}\";"
87
63
 
88
64
  when Fixnum # PHP doesn't have bignums
89
65
  s << "i:#{var};"
@@ -95,7 +71,7 @@ module PHP
95
71
  s << 'N;'
96
72
 
97
73
  when FalseClass, TrueClass
98
- s << "b:#{var ? 1 :0};"
74
+ s << "b:#{var ? 1 : 0};"
99
75
 
100
76
  else
101
77
  if var.respond_to?(:to_assoc)
@@ -114,10 +90,10 @@ module PHP
114
90
  s
115
91
  end # }}}
116
92
 
117
- # string = PHP.serialize_session(mixed var[, bool assoc])
118
- #
119
- # Like PHP.serialize, but only accepts a Hash or associative Array as the root
120
- # type. The results are returned in PHP session format.
93
+ # Like PHP.serialize, but only accepts a Hash or associative Array as the root
94
+ # type. The results are returned in PHP session format.
95
+ #
96
+ # string = PHP.serialize_session(mixed var[, bool assoc])
121
97
  def PHP.serialize_session(var, assoc = false) # {{{
122
98
  s = ''
123
99
  case var
@@ -145,38 +121,38 @@ module PHP
145
121
  s
146
122
  end # }}}
147
123
 
148
- # mixed = PHP.unserialize(string serialized, [hash classmap, [bool assoc]])
149
- #
150
- # Returns an object containing the reconstituted data from serialized.
151
- #
152
- # If a PHP array (associative; like an ordered hash) is encountered, it
153
- # scans the keys; if they're all incrementing integers counting from 0,
154
- # it's unserialized as an Array, otherwise it's unserialized as a Hash.
155
- # Note: this will lose ordering. To avoid this, specify assoc=true,
156
- # and it will be unserialized as an associative array: [[key,value],...]
157
- #
158
- # If a serialized object is encountered, the hash 'classmap' is searched for
159
- # the class name (as a symbol). Since PHP classnames are not case-preserving,
160
- # this *must* be a .capitalize()d representation. The value is expected
161
- # to be the class itself; i.e. something you could call .new on.
162
- #
163
- # If it's not found in 'classmap', the current constant namespace is searched,
164
- # and failing that, a new Struct(classname) is generated, with the arguments
165
- # for .new specified in the same order PHP provided; since PHP uses hashes
166
- # to represent attributes, this should be the same order they're specified
167
- # in PHP, but this is untested.
168
- #
169
- # each serialized attribute is sent to the new object using the respective
170
- # {attribute}=() method; you'll get a NameError if the method doesn't exist.
171
- #
172
- # Array, Hash, Fixnum, Float, True/FalseClass, NilClass and String should
173
- # be returned identically (i.e. foo == PHP.unserialize(PHP.serialize(foo))
174
- # for these types); Struct should be too, provided it's in the namespace
175
- # Module.const_get within unserialize() can see, or you gave it the same
176
- # name in the Struct.new(<structname>), otherwise you should provide it in
177
- # classmap.
178
- #
179
- # Note: StringIO is required for unserialize(); it's loaded as needed
124
+ # Returns an object containing the reconstituted data from serialized.
125
+ #
126
+ # mixed = PHP.unserialize(string serialized, [hash classmap, [bool assoc]])
127
+ #
128
+ # If a PHP array (associative; like an ordered hash) is encountered, it
129
+ # scans the keys; if they're all incrementing integers counting from 0,
130
+ # it's unserialized as an Array, otherwise it's unserialized as a Hash.
131
+ # Note: this will lose ordering. To avoid this, specify assoc=true,
132
+ # and it will be unserialized as an associative array: [[key,value],...]
133
+ #
134
+ # If a serialized object is encountered, the hash 'classmap' is searched for
135
+ # the class name (as a symbol). Since PHP classnames are not case-preserving,
136
+ # this *must* be a .capitalize()d representation. The value is expected
137
+ # to be the class itself; i.e. something you could call .new on.
138
+ #
139
+ # If it's not found in 'classmap', the current constant namespace is searched,
140
+ # and failing that, a new Struct(classname) is generated, with the arguments
141
+ # for .new specified in the same order PHP provided; since PHP uses hashes
142
+ # to represent attributes, this should be the same order they're specified
143
+ # in PHP, but this is untested.
144
+ #
145
+ # each serialized attribute is sent to the new object using the respective
146
+ # {attribute}=() method; you'll get a NameError if the method doesn't exist.
147
+ #
148
+ # Array, Hash, Fixnum, Float, True/FalseClass, NilClass and String should
149
+ # be returned identically (i.e. foo == PHP.unserialize(PHP.serialize(foo))
150
+ # for these types); Struct should be too, provided it's in the namespace
151
+ # Module.const_get within unserialize() can see, or you gave it the same
152
+ # name in the Struct.new(<structname>), otherwise you should provide it in
153
+ # classmap.
154
+ #
155
+ # Note: StringIO is required for unserialize(); it's loaded as needed
180
156
  def PHP.unserialize(string, classmap = nil, assoc = false) # {{{
181
157
  if classmap == true or classmap == false
182
158
  assoc = classmap
@@ -184,33 +160,19 @@ module PHP
184
160
  end
185
161
  classmap ||= {}
186
162
 
187
- require 'stringio'
188
- string = StringIO.new(string)
189
- def string.read_until(char)
190
- val = ''
191
- while (c = self.read(1)) != char
192
- val << c
193
- end
194
- val
163
+ ret = nil
164
+ string = StringIOReader.new(string)
165
+ while string.string[string.pos, 32] =~ /^(\w+)\|/ # session_name|serialized_data
166
+ ret ||= {}
167
+ string.pos += $&.size
168
+ ret[$1] = PHP.do_unserialize(string, classmap, assoc)
195
169
  end
196
170
 
197
- if string.string =~ /^(\w+)\|/ # session_name|serialized_data
198
- ret = Hash.new
199
- loop do
200
- if string.string[string.pos, 32] =~ /^(\w+)\|/
201
- string.pos += $&.size
202
- ret[$1] = PHP.do_unserialize(string, classmap, assoc)
203
- else
204
- break
205
- end
206
- end
207
- ret
208
- else
209
- PHP.do_unserialize(string, classmap, assoc)
210
- end
171
+ ret ? ret : PHP.do_unserialize(string, classmap, assoc)
211
172
  end
212
173
 
213
- private
174
+ private
175
+
214
176
  def PHP.do_unserialize(string, classmap, assoc)
215
177
  val = nil
216
178
  # determine a type
@@ -279,7 +241,7 @@ private
279
241
 
280
242
  val = val.new
281
243
  rescue NameError # Nope; make a new Struct
282
- classmap[klass] = Struct.new(klass.to_s, *attrs.collect { |v| v[0].to_s })
244
+ classmap[klass] = val = Struct.new(klass.to_s, *attrs.collect { |v| v[0].to_s })
283
245
  val = val.new
284
246
  end
285
247
  end
@@ -311,4 +273,3 @@ private
311
273
  val
312
274
  end # }}}
313
275
  end
314
-
@@ -1,8 +1,9 @@
1
1
  #!/usr/local/bin/ruby
2
+ # encoding: utf-8
2
3
 
3
4
  require 'test/unit'
4
5
 
5
- $:.unshift "lib"
6
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
6
7
  require 'php_serialize'
7
8
 
8
9
  TestStruct = Struct.new(:name, :value)
@@ -57,9 +58,9 @@ class TestPhpSerialize < Test::Unit::TestCase
57
58
  test false, 'b:0;'
58
59
  test true, 'b:1;'
59
60
  test 42, 'i:42;'
60
- test -42, 'i:-42;'
61
+ test(-42, 'i:-42;')
61
62
  test 2147483647, "i:2147483647;", :name => 'Max Fixnum'
62
- test -2147483648, "i:-2147483648;", :name => 'Min Fixnum'
63
+ test(-2147483648, "i:-2147483648;", :name => 'Min Fixnum')
63
64
  test 4.2, 'd:4.2;'
64
65
  test 'test', 's:4:"test";'
65
66
  test :test, 's:4:"test";', :name => 'Symbol'
@@ -72,6 +73,10 @@ class TestPhpSerialize < Test::Unit::TestCase
72
73
  test TestClass.new("Foo", 65), 'O:9:"testclass":2:{s:4:"name";s:3:"Foo";s:5:"value";i:65;}',
73
74
  :name => 'Class'
74
75
 
76
+ # PHP counts multibyte string, not string length
77
+ def test_multibyte_string
78
+ assert_equal "s:6:\"öäü\";", PHP.serialize("öäü")
79
+ end
75
80
  # Verify assoc is passed down calls.
76
81
  # Slightly awkward because hashes don't guarantee order.
77
82
  def test_assoc
@@ -102,8 +107,11 @@ class TestPhpSerialize < Test::Unit::TestCase
102
107
  assert phps.include?(serialized)
103
108
  end
104
109
  end
105
- end
106
-
107
- require 'test/unit/ui/console/testrunner'
108
- Test::Unit::UI::Console::TestRunner.run(TestPhpSerialize)
109
110
 
111
+ def test_new_struct_creation
112
+ assert_nothing_raised do
113
+ phps = 'O:8:"stdClass":2:{s:3:"url";s:17:"/legacy/index.php";s:8:"dateTime";s:19:"2012-10-24 22:29:13";}'
114
+ PHP.unserialize(phps)
115
+ end
116
+ end
117
+ end
metadata CHANGED
@@ -1,54 +1,77 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: php-serialize
3
- version: !ruby/object:Gem::Version
4
- version: 1.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - Thomas Hurst
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
-
12
- date: 2009-04-01 00:00:00 +01:00
13
- default_executable:
14
- dependencies: []
15
-
16
- description: "This module provides two methods: PHP.serialize() and PHP.unserialize(), both of which should be compatible with the similarly named functions in PHP. It can also serialize and unserialize PHP sessions."
11
+ date: 2017-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: "\t\tThis module provides two methods: PHP.serialize() and PHP.unserialize(),
42
+ both\n\t\tof which should be compatible with the similarly named functions in PHP.\n\n\t\tIt
43
+ can also serialize and unserialize PHP sessions.\n"
17
44
  email: tom@hur.st
18
45
  executables: []
19
-
20
46
  extensions: []
21
-
22
47
  extra_rdoc_files: []
23
-
24
- files:
25
- - README
48
+ files:
49
+ - lib/php-serialize.rb
26
50
  - lib/php_serialize.rb
27
- has_rdoc: true
51
+ - test/php_serialize_test.rb
28
52
  homepage: http://www.aagh.net/projects/ruby-php-serialize
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
29
56
  post_install_message:
30
57
  rdoc_options: []
31
-
32
- require_paths:
58
+ require_paths:
33
59
  - lib/
34
- required_ruby_version: !ruby/object:Gem::Requirement
35
- requirements:
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
36
62
  - - ">="
37
- - !ruby/object:Gem::Version
38
- version: "0"
39
- version:
40
- required_rubygems_version: !ruby/object:Gem::Requirement
41
- requirements:
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
42
67
  - - ">="
43
- - !ruby/object:Gem::Version
44
- version: "0"
45
- version:
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
46
70
  requirements: []
47
-
48
71
  rubyforge_project:
49
- rubygems_version: 1.3.1
72
+ rubygems_version: 2.6.11
50
73
  signing_key:
51
- specification_version: 2
74
+ specification_version: 4
52
75
  summary: Ruby analogs to PHP's serialize() and unserialize() functions
53
- test_files:
54
- - test.rb
76
+ test_files:
77
+ - test/php_serialize_test.rb
data/README DELETED
@@ -1,34 +0,0 @@
1
- Ruby PHP Serializer
2
- ===================
3
-
4
- This module provides two methods: PHP.serialize() and PHP.unserialize(), both
5
- of which should be compatible with the similarly named functions in PHP.
6
-
7
- Basic usage:
8
-
9
- require 'php_serialize'
10
- in = {'foo' => 'bar'}
11
- php = PHP.serialize(in)
12
- # pass string to PHP unserialize() to get array('foo' => 'bar')
13
- out = PHP.unserialize(php) # => {'foo' => 'bar'}
14
-
15
-
16
- PHP.unserialize can also read PHP sessions, which are collections of named
17
- serialized objects. These can be reserialized using PHP.serialize_session(),
18
- which has the same semantics as PHP.serialize(), but which only supports
19
- Hash and associative Arrays for the root object.
20
-
21
-
22
- Acknowledgements
23
- ================
24
-
25
- TJ Vanderpoel, initial PHP serialized session support.
26
-
27
- Philip Hallstrom, fix for self-generated Structs on unserialization.
28
-
29
- Edward Speyer, fix for assoc serialization in nested structures.
30
-
31
-
32
-
33
- Author: Thomas Hurst <tom@hur.st>, http://hur.st/
34
- WWW: http://www.aagh.net/projects/ruby-php-serialize