Posts Tagged ‘code’

Unpacking Packed JavaScript Code

// December 30th, 2011 // 1 Comment » // Code Codex

I’m sure if you are reading this, you too, as I have many times, come across packed JavaScript code that you needed either to check for maliciousness, modification, curiosity, or perhaps even something more sinister. Don’t judge me, I won’t judge you. Either way, you’ve probably had the same cringing look on your face the first time you saw this.

So, of course, I went about to figure out how to decipher, unscramble, or generally “decrypt” this annoyance. I’ve come across several methods while surfing the Internet, and I’d like to share what I’ve found.

As there are many different methods for JS packing, your mileage may vary greatly on the usefulness of the functionality here for unpacking. However, there are only so many ways to skin a cat and invariably most people have their code packed with the same method.

There are several ways to go about unpacking the code, and I’ll go ahead and list my quick and dirty favorites.

Method 1

The following method (by the guys at http://www.strictly-software.com/unpacker), of course their code needs to be decoded itself by looking at their reformat.js

function unpack()
{
var p = G('txtPacked').value, c="";
if (p != "")
{
c = p;
if (/eval\(+function\(/.test(c))
{
var _e = eval;
var s = "eval = function(v) { c = v; };" + c + "; eval = _e;";
eval(s);
}
c = R(c,{indent_size: 1, indent_char: '\t'});
}

G('txtUnpacked').value = unescape(c);
}

Method 2

Another method supposed is to replace the eval() function call with a document.write() call


eval(code);

to

document.write(code);

Of course, that is just enough to get you started, but it was enough for me, and I hope it helps out others. Good luck, happy hunting & happy hacking!

Resolving Prototype Scriptaculous jQuery Conflictions with noConflict() Example

// April 2nd, 2011 // 1 Comment » // Code Codex

There is a good chance if you are reading this, you were under the same problem I myself had a short while ago…

You’re using Scriptaculous or something else built on top of Prototype and everything works fine when one fine day or hour, you decide you want to incorporate a widget or some code that uses jQuery.

So you do what I did, search around only to have people mention with a high and mighty, auspicious sounding response that the answer to all your problems, the magical cure-all for your what ails ya:

jQuery.noConflict();

Of course they mention that all you have to do is “call this code” after importing the jquery script.. and possibly that you should call it like this ‘$.noConflict();’ or perhaps ‘jQuery.noConflict();’ or laughingly ‘$jQuery.noConflict();’

Some of the smarter (but no more helpful) ones tell you correctly to use assign the noConflict to a variable

like so:
$var = jQuery.noConflict();

Yet they then expect you to just know what to do from there. Great. Thanks. So you spend a couple hours thinking that maybe if you call the statement before the script is imported or right after or before that one or after that one, and then you start to change some of the references in your code.

No luck?

The thing is that all references to $ need to be changed.

Let’s go over an example. I was using a script for a neat little widget on a site that I was working on and it sat on top of Scriptaculous (and of course Prototype). When I dropped in the jQuery code… things went all to hell.

Courage Wolf Says its Better to Have Tried and Failed than to Never Try At All

He may be right, but we're not going to fail this time!

The Scriptaculous is not important, we do not have to make any changes to any of that code at all to get your new jQuery code to work. In my example I dropped in a modalbox, one of my favorites called “Sexylightbox“.

I placed my new modalbox code after my old code.

It required these lines to work:


Import/Src these scripts:


jquery.js
jquery.easing.js
sexylightbox.v2.3.jquery.js

Then it also needed to be initialized:

<script type=”text/javascript>

$(document).ready(function(){

SexyLightbox.initialize({color:’black‘, dir: ‘../../images/sexyimages‘});

});
</script>

Then I had to decide what to name my noConflict variable.. in this case I decided to go with $sexy.

So I changed

 

<script type=”text/javascript>

$(document).ready(function(){

SexyLightbox.initialize({color:’black‘, dir: ‘../../images/sexyimages‘});

});
</script>

to

 

<script type=”text/javascript>

$sexy(document).ready(function(){

SexyLightbox.initialize({color:’black‘, dir: ‘../../images/sexyimages‘});

});
</script>

This is just the first step! I made sure to edit sexylightbox.v2.3.jquery.js and I added in this line to the top of the file:


$sexy = jQuery.noConflict();

Then, I completed it with my finish move and in my editor VI I replaced every instance of the $ character with the $sexy string using a global search and replace command while in Escape mode. If you’re more of the GUI type and using BlueFish or SlickEdit or something else, then you should have a “Replace” and then “Replace All Instances of” option under the Find menubar option.

Then as crazy as it may sound, everything just started working perfectly.

The important thing to keep in mind is that every single instance of where jQuery code is using the $, you have to replace it with the new variable you’ve created with the noConflict function. I wish someone had spelled it out that simply for me.. would have saved me hours of wasted time and frustration.

Converting XML to Associative Array in PHP

// March 2nd, 2011 // No Comments » // Code Codex

Sometimes there are situations where I have had code that is expecting an Associate Array with its corresponding values to deal with. However, as can often stand the case when propagating over to XML, sometimes its not possible (or digitally healthy) to convert everything all at once and I end up in a situation where I have all my data in single layer XML objects but need to interact with it on an associate array level.

So, as a quick to fix I wrote this small function entitled xml2AssArray() to deal with the children of a single layer XML object and turn it into an array with the children’s names used as the key-names of the array.

function xml2AssArray($xmlobj)
{
    $assarray = array(); // Initialize the Array
    // Loop through the XML object's children
    foreach ($xmlobj->children() as $child)
    {
        // Assign the childs name as the key for the next entry while assigning the value itself as well
        $assarray[$child->getName()] = $child;
    }
    return $assarray; // Return the new Array
}

Let’s take this XML object (called $myxml which we create using SimpleXML) for example:

<?xml version="1.0"?>
<Lead>
    <ID>1213</ID>
    <SSN>123450000</SSN>
    <FullName>Israel Smith</FullName>
</Lead>

When we run it through our new function, the array that is returned, is now an associate array:

> $myarray = xml2AssArray($myxml);
> echo $myarray['FullName'];
Israel Smith
>

Huzzah!