Giter Club home page Giter Club logo

phpserialization's Introduction

PHPSerialization for PowerShell

This project started out of a random Freelancer.com project post. The customer was requesting to convert a php serialized string to json and he wanted the project to be in C#. The project also referenced a C# PHP Serialization a.k.a Sharp Serialization Library that might offer some insight to achieve his request.

Since the Sharp Serialization Library was already written in C#, I simply converted the C# class to a PowerShell class to practice writing classes in PowerShell. And I was bored.

As it turned out, I tested a few serialized php arrays and they worked. After doing some more research, I came across another article by Gordon Breuer, who decided to use List<> objects (instead of Arraylists) and Dictionary<> object (instead of Hashtables) to cover all basis. I decided to use with his code instead.

There are a few things to note in my preliminary testing:

  • I haven't tried to serialized php data; only deserialize/unserialize
  • This only works on simple data types and structures such as arrays/hashtables. Any nested hashtables, jagged/multi-dimensional arrays will have to be done with additional code.

Examples

Example 1: A serialized PHP array containing strings, an integer and a bool response

To use the PHPSerialization PowerShell Class, download the .psm1 file and either put it in one of your module locations or your desired location. Import-Module does not work. Instead, you will have to use the keyword "Using" and "Module" to get PS to recognize the class:

Using Module "C:\Users\Nicos\Documents\WindowsPowerShell\Modules\PHPSerialization\PHPSerializationClass.psm1"

Next, take a php serialized code and wrap it in a here string:

$phpstring = @"
a:9:{s:8:"playerId";s:12:"omp-id-84560";s:4:"type";s:5:"audio";s:6:"player";s:5:"html5";s:9:"timestamp";N;s:4:"page";s:59:"https://fakeaudiosite.com/view.php?id=123456789&section=1.0";s:3:"src";s:57:"https://fakeaudiosite/pluginfile.php/22639/14j_aug041.mp3";s:7:"browser";s:6:"chrome";s:8:"isMobile";b:0;s:5:"value";i:10;}"
"@

To see the results outputted directly to PS, type:

[PHPSerialization]::new().Deserialize($phpstring) 

If you want to sort it by key:

$phpdata = [PHPSerialization]::new().Deserialize($phpstring)
$phpdata.GetEnumerator() | Sort Key

Finally, if you want to convert the data to JSON:

ConvertTo-Json -InputObject $phpdata

Final Thoughts

Again, this came out of me trying my first crack at converting a C# class to a PowerShell class as well as having a strong belief that PowerShell can be used for more than just "Windows Administrative" tasks as many would like to believe. Many thinks to the original C# author Matt Palmerlee and Gordon Breuer for the PHP Serialization.

phpserialization's People

Contributors

nicoeat614 avatar

Stargazers

Gerrit Geeraerts avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.