locutusjs / locutus

Bringing stdlibs of other programming languages to JavaScript for educational purposes

Home Page:https://locutus.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unserialize is throwing unhandled exception.

JefferyHus opened this issue · comments

const php = require("locutus/php/var");

var ser = php.unserialize('a:10:{s:8:"security";s:2:"on";s:4:"mode";s:6:"active";s:8:"behavior";s:7:"nothing";s:8:"requests";a:2:{s:8:"interval";s:8:"1 minute";s:5:"count";i:100;}s:7:"package";a:2:{s:5:"start";O:13:"Carbon\Carbon":3:{s:4:"date";s:26:"2017-07-19 21:18:31.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/London";}s:3:"end";O:13:"Carbon\Carbon":3:{s:4:"date";s:26:"2017-08-18 21:18:31.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/London";}}s:7:"actions";a:4:{i:0;s:7:"captcha";s:4:"0_25";s:7:"captcha";s:4:"0_50";s:5:"block";s:4:"0_75";s:5:"block";}s:7:"captcha";N;s:4:"http";a:0:{}s:5:"pages";a:0:{}s:5:"token";a:0:{}}');

This code throws this exception SyntaxError: Unknown / Unhandled data type(s): o, even if the PHP unserialize works perfectly.

for anyone having the same problem here is a fix:

      case 'o':
        ccount = readUntil(data, dataoffset, ':');
        dataoffset += ccount[0] + 2;

        ccount = readUntil(data, dataoffset, '"');
        dataoffset += ccount[0] + 2;

        readdata = {};

        keyandchrs = readUntil(data, dataoffset, ':');
        chrs = keyandchrs[0];
        keys = keyandchrs[1];
        dataoffset += chrs + 2;

        for (i = 0; i < parseInt(keys, 10); i++) {
          kprops = _unserialize(data, dataoffset);
          kchrs = kprops[1];
          key = kprops[2];
          dataoffset += kchrs;

          vprops = _unserialize(data, dataoffset);
          vchrs = vprops[1];
          value = vprops[2];
          dataoffset += vchrs;

          readdata[key] = value;
        }
        dataoffset += 1;
        break;

put this code in the switch..case code and it will work just fine.

Thanks for the find & patch! Continuing in #349