Created
November 29, 2023 09:01
-
-
Save akalongman/78865b0f2c6f5f1d183cdb5e328ba30c to your computer and use it in GitHub Desktop.
Parse PHP Serialized object in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.json.*; | |
public class Main { | |
public static void main(String[] args) { | |
String str = ";{\"uuid\":\"d1d06498-294d-430e-a7ca-b21a01e5a5dd\",\"displayName\":\"App\\\\Jobs\\\\SendSmsOneJob\",\"job\":\"Illuminate\\\\Queue\\\\CallQueuedHandler@call\",\"maxTries\":null,\"maxExceptions\":null,\"failOnTimeout\":false,\"backoff\":null,\"timeout\":null,\"retryUntil\":null,\"data\":{\"commandName\":\"App\\\\Jobs\\\\SendSmsOneJob\",\"command\":\"O:22:\\\"App\\\\Jobs\\\\SendSmsOneJob\\\":2:{s:4:\\\"data\\\";s:303:\\\"{\\\"subject\\\":\\\"Photocenter\\\",\\\"message\\\":\\\"ბოლო მესიჯი გამოგზავნილია 11:45\\\",\\\"phone\\\":\\\"995555555\\\",\\\"ignore_blacklist\\\":true,\\\"company_id\\\":115,\\\"type\\\":\\\"transactional_messages\\\",\\\"ip_address\\\":\\\"213.217.16.42\\\",\\\"uuid\\\":\\\"55021d6f-20ec-49db-9269-f51b7166b35d\\\",\\\"sms_type\\\":\\\"a2p\\\"}\\\";s:5:\\\"queue\\\";s:7:\\\"a2p_one\\\";}\"},\"sentry_baggage_data\":\"sentry-trace_id=03203501f21e4a829e635323149697c5,sentry-public_key=2f8202af13c248234321378c4734e3ae,sentry-environment=production\",\"sentry_trace_parent_data\":\"03203501f21e4awerwereww9697c5-54fbe1ed5cba40c4\",\"id\":\"02a52217-4329-4329-b497-24dc410a6da6\"}"; | |
JSONObject parsed = parseSmppPayload(str); | |
System.out.println(parsed.getString("uuid")); // 55021d6f-20ec-49db-9269-f51b7166b35d | |
} | |
private static JSONObject parseSmppPayload(String payload) | |
{ | |
JSONObject obj = new JSONObject(payload.substring(1)); | |
String command = obj.getJSONObject("data").getString("command"); | |
SerializedPhpParser serializedPhpParser = new SerializedPhpParser(command); | |
SerializedPhpParser.PhpObject result = (SerializedPhpParser.PhpObject) serializedPhpParser.parse(); | |
String data = result.attributes.get("data").toString(); | |
JSONObject parsed = new JSONObject(data); | |
return parsed; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (c) 2007 Zsolt Sz�sz <zsolt at lorecraft dot com> | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of this software and associated documentation files (the | |
"Software"), to deal in the Software without restriction, including | |
without limitation the rights to use, copy, modify, merge, publish, | |
distribute, sublicense, and/or sell copies of the Software, and to | |
permit persons to whom the Software is furnished to do so, subject to | |
the following conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
import java.util.HashMap; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import java.util.regex.Pattern; | |
/** | |
* Deserializes a serialized PHP data structure into corresponding Java objects. It supports | |
* the integer, float, boolean, string primitives that are mapped to their Java | |
* equivalent, plus arrays that are parsed into <code>Map</code> instances and objects | |
* that are represented by {@link PhpObject} instances. | |
* <p> | |
* Example of use: | |
* <pre> | |
* String input = "O:8:"TypeName":1:{s:3:"foo";s:3:"bar";}"; | |
* SerializedPhpParser serializedPhpParser = new SerializedPhpParser(input); | |
* Object result = serializedPhpParser.parse(); | |
* </pre> | |
* | |
* The <code>result</code> object will be a <code>PhpObject</code> with the name "TypeName" and | |
* the attribute "foo" = "bar". | |
*/ | |
public class SerializedPhpParser { | |
private final String input; | |
private int index; | |
private boolean assumeUTF8 = true; | |
private Pattern acceptedAttributeNameRegex = null; | |
public SerializedPhpParser(String input) { | |
this.input = input; | |
} | |
public SerializedPhpParser(String input, boolean assumeUTF8) { | |
this.input = input; | |
this.assumeUTF8 = assumeUTF8; | |
} | |
public Object parse() { | |
char type = input.charAt(index); | |
switch (type) { | |
case 'i': | |
index += 2; | |
// Patch Integer/Double for the PHP x64. | |
Object tmp; | |
tmp = parseInt(); | |
if (tmp == null) { | |
tmp = parseFloat(); | |
} | |
return tmp; | |
// End of Patch Integer/Double for the PHP x64. | |
case 'd': | |
index += 2; | |
return parseFloat(); | |
case 'b': | |
index += 2; | |
return parseBoolean(); | |
case 's': | |
index += 2; | |
return parseString(); | |
case 'a': | |
index += 2; | |
return parseArray(); | |
case 'O': | |
index += 2; | |
return parseObject(); | |
case 'N': | |
index += 2; | |
return NULL; | |
default: | |
throw new IllegalStateException("Encountered unknown type [" + type | |
+ "]"); | |
} | |
} | |
private Object parseObject() { | |
PhpObject phpObject = new PhpObject(); | |
int strLen = readLength(); | |
phpObject.name = input.substring(index, index + strLen); | |
index = index + strLen + 2; | |
int attrLen = readLength(); | |
for (int i = 0; i < attrLen; i++) { | |
Object key = parse(); | |
Object value = parse(); | |
if (isAcceptedAttribute(key)) { | |
phpObject.attributes.put(key, value); | |
} | |
} | |
index++; | |
return phpObject; | |
} | |
private Map<Object, Object> parseArray() { | |
int arrayLen = readLength(); | |
Map<Object, Object> result = new LinkedHashMap<Object, Object>(); | |
for (int i = 0; i < arrayLen; i++) { | |
Object key = parse(); | |
Object value = parse(); | |
if (isAcceptedAttribute(key)) { | |
result.put(key, value); | |
} | |
} | |
index++; | |
return result; | |
} | |
private boolean isAcceptedAttribute(Object key) { | |
if (acceptedAttributeNameRegex == null) { | |
return true; | |
} | |
if (!(key instanceof String)) { | |
return true; | |
} | |
return acceptedAttributeNameRegex.matcher((String)key).matches(); | |
} | |
private int readLength() { | |
int delimiter = input.indexOf(':', index); | |
int arrayLen = Integer.valueOf(input.substring(index, delimiter)); | |
index = delimiter + 2; | |
return arrayLen; | |
} | |
/** | |
* Assumes strings are utf8 encoded | |
* | |
* @return | |
*/ | |
private String parseString() { | |
int strLen = readLength(); | |
int utfStrLen = 0; | |
int byteCount = 0; | |
while (byteCount != strLen) { | |
char ch = input.charAt(index + utfStrLen++); | |
if (assumeUTF8) { | |
if ((ch >= 0x0001) && (ch <= 0x007F)) { | |
byteCount++; | |
} else if (ch > 0x07FF) { | |
byteCount += 3; | |
} else { | |
byteCount += 2; | |
} | |
} else { | |
byteCount++; | |
} | |
} | |
String value = input.substring(index, index + utfStrLen); | |
index = index + utfStrLen + 2; | |
return value; | |
} | |
private Boolean parseBoolean() { | |
int delimiter = input.indexOf(';', index); | |
String value = input.substring(index, delimiter); | |
if (value.equals("1")) { | |
value = "true"; | |
} else if (value.equals("0")) { | |
value = "false"; | |
} | |
index = delimiter + 1; | |
return Boolean.valueOf(value); | |
} | |
private Double parseFloat() { | |
int delimiter = input.indexOf(';', index); | |
String value = input.substring(index, delimiter); | |
index = delimiter + 1; | |
return Double.valueOf(value); | |
} | |
private Integer parseInt() { | |
int delimiter = input.indexOf(';', index); | |
// Let's store old value of the index for the patch Integer/Double for the PHP x64. | |
int index_old=index; | |
String value = input.substring(index, delimiter); | |
index = delimiter + 1; | |
// Patch Integer/Double for the PHP x64. | |
try { | |
return Integer.valueOf(value); | |
} catch (Exception ex) { | |
index=index_old; | |
} | |
return null; | |
// End of Patch Integer/Double for the PHP x64. | |
} | |
public void setAcceptedAttributeNameRegex(String acceptedAttributeNameRegex) { | |
this.acceptedAttributeNameRegex = Pattern.compile(acceptedAttributeNameRegex); | |
} | |
public static final Object NULL = new Object() { | |
@Override | |
public String toString() { | |
return "NULL"; | |
} | |
}; | |
/** | |
* Represents an object that has a name and a map of attributes | |
*/ | |
public static class PhpObject { | |
public String name; | |
public Map<Object, Object> attributes = new HashMap<Object, Object>(); | |
@Override | |
public String toString() { | |
return "\"" + name + "\" : " + attributes.toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment