Skip to content
This repository has been archived by the owner on Jul 25, 2020. It is now read-only.

Support old format php session #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/de/ailis/pherialize/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import de.ailis.pherialize.exceptions.SerializeException;

Expand Down Expand Up @@ -90,6 +91,32 @@ public String serialize(final Object object)
serializeObject(object, buffer);
return buffer.toString();
}

/**
* Serializes the php session.
*
* @param object
* The object
* @return The serialized data
*/

public String serializeSession(final MixedArray array)
{
StringBuffer result = new StringBuffer();
StringBuffer buffer = new StringBuffer();
Iterator<Entry<Object, Object>> items = array.entrySet().iterator();
while(items.hasNext())
{
Entry thisEntry = (Entry) items.next();
String key = thisEntry.getKey().toString();

buffer.setLength(0);
serializeObject(thisEntry.getValue(), buffer);
result.append(key + "|" + buffer.toString());
}

return result.toString();
}


/**
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/de/ailis/pherialize/Unserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,30 @@ public Unserializer(final String data, Charset charset)
this.history = new ArrayList<Object>();
}

/**
* Unserializes the session in the data stream.
*
* @return The unserialized session
*/

public Mixed unserializeSession()
{
MixedArray array;
Mixed result;
int sep;

array = new MixedArray();
result = new Mixed(array);
while ((sep = this.data.indexOf('|', this.pos)) > -1)
{
String unencoded = this.data.substring(pos, sep);
Mixed key = new Mixed(encode(unencoded, charset));
this.pos = sep + 1;
Mixed value = unserializeObject();
array.put(key, value);
}
return result;
}

/**
* Unserializes the next object in the data stream.
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/de/ailis/pherialize/SerializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,23 @@ public void testSerializeCustomArray()
s2 = Pherialize.serialize(array);
assertEquals(s1, s2);
}

/**
* Test serializing array
*/

public void testSerializePhpSession()
{
MixedArray array;
String s1, s2;

array = new MixedArray();
array.put("flash_time", null);
array.put("created", 1522666956);
array.put("email", "[email protected]");

s1 = "flash_time|N;created|i:1522666956;email|s:15:\"[email protected]\";";
s2 = Pherialize.serializeSession(array);
assertEquals(s1, s2);
}
}
16 changes: 16 additions & 0 deletions src/test/java/de/ailis/pherialize/UnserializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ public void testUnserializeMap()
assertEquals(new Mixed(Boolean.TRUE), test.get("key2"));
}

/**
* Tests unserializing a session
*/

public void testUnserializeSession()
{
MixedArray test;

test = Pherialize.unserializeSession(
"login_ok|b:1;nome|s:4:\"sica\";inteiro|i:34;").toArray();
assertEquals(3, test.size());
assertEquals(new Mixed(Boolean.TRUE), test.get("login_ok"));
assertEquals(new Mixed("sica"), test.get("nome"));
assertEquals(new Mixed(Integer.valueOf(34)), test.get("inteiro"));
}


/**
* Tests unserializing a List
Expand Down