php-serial 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/lib/php-serial.rb +144 -0
- data/lib/php-serial/version.rb +5 -0
- data/php-serial.gemspec +17 -0
- data/test/php_serial_test.rb +170 -0
- data/test/test_helper.rb +5 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jorge Urias
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Php::Serial
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'php-serial'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install php-serial
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/php-serial.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require "php-serial/version"
|
2
|
+
|
3
|
+
module Php
|
4
|
+
|
5
|
+
# Unserializes a PHP session
|
6
|
+
#
|
7
|
+
# Params:
|
8
|
+
# String data
|
9
|
+
# Returns: hash
|
10
|
+
def self.unserialize_session(data)
|
11
|
+
data = data.chomp
|
12
|
+
hash = {}
|
13
|
+
|
14
|
+
while !data.empty? do
|
15
|
+
key = extract_until!(data, "|")
|
16
|
+
hash[key] = unserialize(data)
|
17
|
+
end
|
18
|
+
hash
|
19
|
+
end
|
20
|
+
|
21
|
+
# Unserializes a string up to the first valid serialized instance
|
22
|
+
#
|
23
|
+
# Params:
|
24
|
+
# String data
|
25
|
+
# Returns: mixed
|
26
|
+
def self.unserialize(data='')
|
27
|
+
var_type = data.slice!(0)
|
28
|
+
data.slice!(0)
|
29
|
+
case var_type
|
30
|
+
when "N"
|
31
|
+
value = nil
|
32
|
+
when "b"
|
33
|
+
value = (extract_until!(data, ";") == '1')
|
34
|
+
when "s"
|
35
|
+
length = extract_until!(data, ":").to_i
|
36
|
+
extract_until!(data, '"')
|
37
|
+
value = data.slice!(0,length)
|
38
|
+
extract_until!(data, ";")
|
39
|
+
when "i"
|
40
|
+
value = extract_until!(data, ";").to_i
|
41
|
+
when "d"
|
42
|
+
value = extract_until!(data, ";").to_f
|
43
|
+
when "a"
|
44
|
+
value = {}
|
45
|
+
length = extract_until!(data, ":").to_i
|
46
|
+
extract_until!(data, '{')
|
47
|
+
length.times do
|
48
|
+
key = unserialize(data)
|
49
|
+
value[key] = unserialize(data)
|
50
|
+
end
|
51
|
+
extract_until!(data, "}")
|
52
|
+
# if keys are sequential numbers, return array
|
53
|
+
value = value.values if Array(0..value.length-1) == value.keys and !value.empty?
|
54
|
+
when "O"
|
55
|
+
value = {}
|
56
|
+
length = extract_until!(data, ":").to_i
|
57
|
+
extract_until!(data, '"')
|
58
|
+
value["class"] = data.slice!(0,length)
|
59
|
+
extract_until!(data, ':')
|
60
|
+
length = extract_until!(data, ":").to_i
|
61
|
+
extract_until!(data, '{')
|
62
|
+
length.times do
|
63
|
+
key = unserialize(data)
|
64
|
+
value[key] = unserialize(data)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
value
|
68
|
+
end
|
69
|
+
|
70
|
+
# Serializes a hash into PHP session
|
71
|
+
#
|
72
|
+
# Params:
|
73
|
+
# Hash hash
|
74
|
+
# Returns: string
|
75
|
+
def self.serialize_session(hash)
|
76
|
+
serialized_session = ''
|
77
|
+
hash.each do |key,value|
|
78
|
+
serialized_session += key.to_s + "|" + serialize(value)
|
79
|
+
end
|
80
|
+
serialized_session
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
# Serializes a ruby object into PHP serialized format
|
85
|
+
#
|
86
|
+
# Params:
|
87
|
+
# Mixed var
|
88
|
+
# Returns: string
|
89
|
+
def self.serialize(var)
|
90
|
+
val = ''
|
91
|
+
case var.class.to_s
|
92
|
+
when 'NilClass'
|
93
|
+
val = 'N;'
|
94
|
+
when 'Fixnum'
|
95
|
+
val = 'i:' + var.to_s + ';'
|
96
|
+
when 'Float'
|
97
|
+
val = 'd:' + var.to_s + ';'
|
98
|
+
when 'TrueClass'
|
99
|
+
val = 'b:1' + ';'
|
100
|
+
when 'FalseClass'
|
101
|
+
val = 'b:0' + ';'
|
102
|
+
when 'String', 'Symbol'
|
103
|
+
val = 's:' + var.length.to_s + ':' + '"' + var.to_s + '";'
|
104
|
+
when 'Array'
|
105
|
+
val = 'a:' + var.length.to_s + ':' + '{'
|
106
|
+
var.length.times do |index|
|
107
|
+
val += serialize(index) + serialize(var[index])
|
108
|
+
end
|
109
|
+
val += '}'
|
110
|
+
when 'Hash'
|
111
|
+
val = 'a:' + var.length.to_s + ':' + '{'
|
112
|
+
var.each do |item_key, item_value|
|
113
|
+
val += serialize(item_key) + serialize(item_value)
|
114
|
+
end
|
115
|
+
val += '}'
|
116
|
+
else
|
117
|
+
klass = var.class.to_s
|
118
|
+
val = "O:#{klass.length.to_s}:\"#{klass}\":#{var.instance_variables.length}:{"
|
119
|
+
var.instance_variables.each do |ivar|
|
120
|
+
ivar = ivar.to_s
|
121
|
+
ivar.slice!(0)
|
122
|
+
val += serialize(ivar) + serialize(var.send(ivar))
|
123
|
+
end
|
124
|
+
val += '}'
|
125
|
+
end
|
126
|
+
val
|
127
|
+
end
|
128
|
+
|
129
|
+
# Return all characters up to the first occurrence of char
|
130
|
+
# Truncates those characters from input string
|
131
|
+
#
|
132
|
+
# Params:
|
133
|
+
# String str
|
134
|
+
# String char
|
135
|
+
# Returns: string
|
136
|
+
def self.extract_until!(str, char)
|
137
|
+
extracted = ''
|
138
|
+
while (c = str.slice!(0))
|
139
|
+
break if c == char
|
140
|
+
extracted << c
|
141
|
+
end
|
142
|
+
extracted
|
143
|
+
end
|
144
|
+
end
|
data/php-serial.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/php-serial/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jorge Urias"]
|
6
|
+
gem.email = ["jurias@gmail.com"]
|
7
|
+
gem.description = %q{PHP serialization/unserialization with session support}
|
8
|
+
gem.summary = %q{PHP serialization/unserialization with session support}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "php-serial"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Php::Serial::VERSION
|
17
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe 'Serialize' do
|
4
|
+
it 'should correctly serialize strings' do
|
5
|
+
input = "This is a string"
|
6
|
+
expected = 's:16:"This is a string";'
|
7
|
+
Php.serialize(input).must_equal expected
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should correctly serialize symbols' do
|
11
|
+
input = "This is a symbol"
|
12
|
+
expected = 's:16:"This is a symbol";'
|
13
|
+
Php.serialize(input).must_equal expected
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should correctly serialize integers' do
|
17
|
+
input = 20
|
18
|
+
expected = 'i:20;'
|
19
|
+
Php.serialize(input).must_equal expected
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should correctly serialize floats' do
|
23
|
+
input = 3.14159
|
24
|
+
expected = 'd:3.14159;'
|
25
|
+
Php.serialize(input).must_equal expected
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should correctly serialize booleans' do
|
29
|
+
input = false
|
30
|
+
expected = 'b:0;'
|
31
|
+
Php.serialize(input).must_equal expected
|
32
|
+
|
33
|
+
input = true
|
34
|
+
expected = 'b:1;'
|
35
|
+
Php.serialize(input).must_equal expected
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should correctly serialize nil' do
|
39
|
+
input = nil
|
40
|
+
expected = 'N;'
|
41
|
+
Php.serialize(input).must_equal expected
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should correctly serialize arrays' do
|
45
|
+
input = [1,2,3,4,5]
|
46
|
+
expected = 'a:5:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;}'
|
47
|
+
Php.serialize(input).must_equal expected
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should correctly serialize hashes' do
|
51
|
+
input = {a: 1, b: "string", c: nil, d: true, e: 3.14159}
|
52
|
+
expected = 'a:5:{s:1:"a";i:1;s:1:"b";s:6:"string";s:1:"c";N;s:1:"d";b:1;s:1:"e";d:3.14159;}'
|
53
|
+
Php.serialize(input).must_equal expected
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should correctly serialize arbitrary class objects' do
|
57
|
+
class Person
|
58
|
+
attr_accessor :first_name, :last_name
|
59
|
+
end
|
60
|
+
person = Person.new
|
61
|
+
person.first_name = 'John'
|
62
|
+
person.last_name = 'Doe'
|
63
|
+
|
64
|
+
input = person
|
65
|
+
expected = 'O:6:"Person":2:{s:10:"first_name";s:4:"John";s:9:"last_name";s:3:"Doe";}'
|
66
|
+
Php.serialize(input).must_equal expected
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
describe 'Unserialize' do
|
72
|
+
it 'should correctly unserialize strings' do
|
73
|
+
input = 's:16:"This is a string";'
|
74
|
+
expected = "This is a string"
|
75
|
+
Php.unserialize(input).must_equal expected
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should correctly unserialize symbols' do
|
79
|
+
input = 's:16:"This is a symbol";'
|
80
|
+
expected = "This is a symbol"
|
81
|
+
Php.unserialize(input).must_equal expected
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'should correctly unserialize integers' do
|
85
|
+
input = 'i:20;'
|
86
|
+
expected = 20
|
87
|
+
Php.unserialize(input).must_equal expected
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should correctly unserialize floats' do
|
91
|
+
input = 'd:3.14159;'
|
92
|
+
expected = 3.14159
|
93
|
+
Php.unserialize(input).must_equal expected
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'should correctly unserialize booleans' do
|
97
|
+
input = 'b:0;'
|
98
|
+
expected = false
|
99
|
+
Php.unserialize(input).must_equal expected
|
100
|
+
|
101
|
+
input = 'b:1;'
|
102
|
+
expected = true
|
103
|
+
Php.unserialize(input).must_equal expected
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should correctly unserialize nil' do
|
107
|
+
input = 'N;'
|
108
|
+
expected = nil
|
109
|
+
Php.unserialize(input).must_equal expected
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'should correctly unserialize arrays' do
|
113
|
+
input = 'a:5:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;}'
|
114
|
+
expected = [1,2,3,4,5]
|
115
|
+
Php.unserialize(input).must_equal expected
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'should correctly unserialize hashes' do
|
119
|
+
input = 'a:5:{s:1:"a";i:1;s:1:"b";s:6:"string";s:1:"c";N;s:1:"d";b:1;s:1:"e";d:3.14159;}'
|
120
|
+
expected = {"a" => 1, "b" => "string", "c" => nil, "d" => true, "e" => 3.14159}
|
121
|
+
Php.unserialize(input).must_equal expected
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'should correctly unserialize objects' do
|
125
|
+
input = 'O:6:"Person":2:{s:10:"first_name";s:4:"John";s:9:"last_name";s:3:"Doe";}'
|
126
|
+
expected = {"class" => "Person", "first_name" => "John", "last_name" => "Doe"}
|
127
|
+
Php.unserialize(input).must_equal expected
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
describe 'Serialize Session' do
|
133
|
+
it 'should correctly serialize a hash into a php session' do
|
134
|
+
input = {"a" => 1, "b" => "string", "c" => nil, "d" => true, "e" => 3.14159}
|
135
|
+
expected = 'a|i:1;b|s:6:"string";c|N;d|b:1;e|d:3.14159;'
|
136
|
+
Php.serialize_session(input).must_equal expected
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'should correctly serialize a complex hash into a php session' do
|
140
|
+
input = {
|
141
|
+
"complex/key/A" => "A'A\";|$",
|
142
|
+
"complex/key/B" => [1,2,3,4,5,6,7,8,{},10],
|
143
|
+
"complex/key/C" => nil,
|
144
|
+
"complex/key/D" => {one: 1, "two" => "Two"},
|
145
|
+
"complex/key/E" => {index:{index:{index:{}}}}
|
146
|
+
}
|
147
|
+
expected = 'complex/key/A|s:7:"A\'A";|$";complex/key/B|a:10:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;i:6;i:7;i:7;i:8;i:8;a:0:{}i:9;i:10;}complex/key/C|N;complex/key/D|a:2:{s:3:"one";i:1;s:3:"two";s:3:"Two";}complex/key/E|a:1:{s:5:"index";a:1:{s:5:"index";a:1:{s:5:"index";a:0:{}}}}'
|
148
|
+
Php.serialize_session(input).must_equal expected
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe 'Unserialize Session' do
|
153
|
+
it 'should correctly unserialize a php session into a hash' do
|
154
|
+
input = 'a|i:1;b|s:6:"string";c|N;d|b:1;e|d:3.14159;'
|
155
|
+
expected = {"a" => 1, "b" => "string", "c" => nil, "d" => true, "e" => 3.14159}
|
156
|
+
Php.unserialize_session(input).must_equal expected
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'should correctly unserialize a complex php session' do
|
160
|
+
input = 'complex/key/A|s:7:"A\'A";|$";complex/key/B|a:10:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;i:5;i:6;i:6;i:7;i:7;i:8;i:8;a:0:{}i:9;i:10;}complex/key/C|N;complex/key/D|a:2:{s:3:"one";i:1;s:3:"two";s:3:"Two";}complex/key/E|a:1:{s:5:"index";a:1:{s:5:"index";a:1:{s:5:"index";a:0:{}}}}'
|
161
|
+
expected = {
|
162
|
+
"complex/key/A" => "A'A\";|$",
|
163
|
+
"complex/key/B" => [1,2,3,4,5,6,7,8,{},10],
|
164
|
+
"complex/key/C" => nil,
|
165
|
+
"complex/key/D" => {"one" => 1, "two" => "Two"},
|
166
|
+
"complex/key/E" => {"index" => {"index" => {"index" => {}}}}
|
167
|
+
}
|
168
|
+
Php.unserialize_session(input).must_equal expected
|
169
|
+
end
|
170
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: php-serial
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jorge Urias
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-14 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: PHP serialization/unserialization with session support
|
15
|
+
email:
|
16
|
+
- jurias@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/php-serial.rb
|
27
|
+
- lib/php-serial/version.rb
|
28
|
+
- php-serial.gemspec
|
29
|
+
- test/php_serial_test.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
homepage: ''
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.10
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: PHP serialization/unserialization with session support
|
55
|
+
test_files:
|
56
|
+
- test/php_serial_test.rb
|
57
|
+
- test/test_helper.rb
|