~ overflow ~

Tag: xml

Convert Object to Array in php

by z3n on Dec.29, 2010, under Coding

Problem:
How to convert a generic object into an array?

Solution:
I’ve wrote this to convert simple xml objects into array, works pretty good.

function toArray($obj) {
	if (is_object($obj))
		$obj = (array)$obj;

	if (is_array($obj)) {
		foreach ($obj as &$val)
			$val = toArray($val);
	} else { // if this is not an array so it's a string
		$obj = (string)$obj;
	}

	return $obj;
}
Leave a Comment :, , , , more...

Flash policy-file-request

by z3n on Feb.09, 2010, under Coding, Tips & Hints

Problem:

So you developed an socket flash application using the newest Action Script 3.0, all good on tests and debug

Now when you put on production server, you see a request “<policy-file-request/>“  on the begginig of the connection, WTF is that man?!

Solution:

Let’s ignore all the documents and detailed sample-less (aka useless) explanations.

This is a new security feature introducted on flash 9+, you maybe aware of the crossdomain.xml file that should be present on your’s domain root folder, however this don’t work for socks server, only for other type of projects (streaming players for example).

First let’s see what we can’t see, <policy-file-request/> is not only <policy-file-request/>, there’s a null character after it, so if you are doing a $input == “<policy-file-request/>” you will go wrong, a null character is a chr(0) (zero), something like $input == “<policy-file-request/>”.chr(0) or substr($input,0,22) == “<policy-file-request/>” should solve this problem (in php of course) .

Sooo, for this first request (<policy-file-request/>) you should return this:

<?xml version=”1.0″?>
<!DOCTYPE cross-domain-policy SYSTEM “/xml/dtds/cross-domain-policy.dtd”>
<cross-domain-policy>
<site-control permitted-cross-domain-policies=”master-only”/>
<allow-access-from domain=”your_domain” to-ports=”666” />
</cross-domain-policy>

Where your_domain is your domain, doh, but you can also put * so it will work at any domain, if you have more than one domain using this, it will be usefull.

The other bold, 666, is the port that flash should be expecting data to come from, you can also put * here or a port range, or both, check official docs for more info, there are more options you can put on this xml, depending on your needs it will be usefull.

Note that there’s another trick here, you need to put a null character (chr(0)) in the end of this reply! Also, you don’t need to send any special headers like Content-Type.

If it’s all right, flash will disconnect and reconnect, if something is wrong it will just disconnect, make sure you do a handler on your socket server for this, it might ruin things like it did for me.

Sources:

Posting where i found about the chr(0)

Policy file changes in Flash Player 9 and Flash Player 10 (full article)

Leave a Comment :, , , , , , more...

The Ampersand (&) XML Problem

by z3n on Sep.26, 2009, under Coding

Problem:

Having ampersands (&) on a xml makes it non-compliant, causing errors on IE.

Solution:

I’ve been looking on this issue for a while, although I had the answer already I wanted an alternate. One said that you could turn the & into &amp; html entity or it’s ASCII code, but this just gets into another issue, since the & will still there – &amp; &#38; – In my case I had accents encoded as html entities, like áéíóú, which must be encoded (at least on my project) as entities to be passed as xml/json/etc. Other people noted that it’s possible to have specific entities declared on the html header, although this is a drawback, because xml will become quite big if you have too many different entities AND you will have to redeclare them on every reply.

None of the solutions I’ve found were good, so I’m posting mine.

A simple base64 encode would resolve the issue, and you only need to do it on replies, making things easier, so you send as base64 and the user with a little javascript decodes it back.

Base64 Javascript Source Code:

  1. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  2.  
  3. function _enc(input) {
  4.    var output = "";
  5.    var chr1, chr2, chr3;
  6.    var enc1, enc2, enc3, enc4;
  7.    var i = 0;
  8.    do {
  9.       chr1 = input.charCodeAt(i++);
  10.       chr2 = input.charCodeAt(i++);
  11.       chr3 = input.charCodeAt(i++);
  12.  
  13.       enc1 = chr1 >> 2;
  14.       enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  15.       enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  16.       enc4 = chr3 & 63;
  17.  
  18.       if (isNaN(chr2)) {
  19.          enc3 = enc4 = 64;
  20.       } else if (isNaN(chr3)) {
  21.          enc4 = 64;
  22.       }
  23.  
  24.       output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
  25.          keyStr.charAt(enc3) + keyStr.charAt(enc4);
  26.    } while (i < input.length);
  27.    
  28.    return output;
  29. }
  30.  
  31. function _dec(input) {
  32.  if (input == "NULL") { return ""; }
  33.    var output = "";
  34.    var chr1, chr2, chr3;
  35.    var enc1, enc2, enc3, enc4;
  36.    var i = 0;
  37.  
  38.    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
  39.    input = input.replace(/[^A-Za-z0-9\+\/\=]/g|>, "");
  40.  
  41.    do {
  42.       enc1 = keyStr.indexOf(input.charAt(i++));
  43.       enc2 = keyStr.indexOf(input.charAt(i++));
  44.       enc3 = keyStr.indexOf(input.charAt(i++));
  45.       enc4 = keyStr.indexOf(input.charAt(i++));
  46.  
  47.       chr1 = (enc1 << 2) | (enc2 >> 4);
  48.       chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  49.       chr3 = ((enc3 & 3) << 6) | enc4;
  50.  
  51.       output = output + String.fromCharCode(chr1);
  52.  
  53.       if (enc3 != 64) {
  54.          output = output + String.fromCharCode(chr2);
  55.       }
  56.       if (enc4 != 64) {
  57.          output = output + String.fromCharCode(chr3);
  58.       }
  59.    } while (i < input.length);
  60.  
  61.    return output;
  62. }
Leave a Comment :, , , , , , more...

jQuery + XML + IE = xmlDOM issue … or no??

by z3n on Aug.29, 2009, under Coding, Notes

Problem:

In the middle of the developing of a very complex script i figured out that IE was simply ignoring the xml documents i sent to it by ajax. Searching the web i’ve found this $.xmlDOM jQuery extension that is supposed to fix the IE issue with xmls. Although the extesion was clear and other people claims it work, it didn’t worked for me, how lucky is that?

Solution:

This took a while to solve, and i will skip all the boring process. Turns out that i didn’t needed the $.xmlDOM extension at all, the issue was on the xml. I’m developing this script in a language that has accents, i need to use `&aacute;` like html entities in order to avoid malfuncioning with data transport, so this little ampersand was breaking IE.

How nice is that?

Leave a Comment :, , , , more...

Ratio Master 1.7.5’s µTorrent 1.8.2 (14458) .client file

by z3n on Feb.24, 2009, under Tips & Hints, Uncategorized

<client name=”uTorrent 1.8.2 build (14458)” author=”z3n” version=”1.1″ processname=”utorrent”>
<query>info_hash={infohash}&peer_id={peerid}&port={port}&uploaded={uploaded}&downloaded={downloaded}&left={left}&key={key}{event}&numwant={numwant}&compact=1&no_peer_id=1</query>
<headers>Host: {host}_nl_User-Agent: uTorrent/1820_nl_Accept-Encoding: gzip_nl_</headers>
<peer_id prefix=”-UT1820-_1″ type=”random” length=”10″ urlencoding=”true” upperCase=”false” value=””/>
<key type=”hex” length=”8″ urlencoding=”false” upperCase=”true” value=””/>
<protocol value=”HTTP/1.1″/>
<hash upperCase=”false”/>
</client>

Leave a Comment :, , more...

Ratio Master 1.7.5’s µTorrent 1.8.1 (12639) .client file

by z3n on Feb.02, 2009, under Tips & Hints

I’ve been looking for this but i couldn’t find anything without having to signup for a stupid forum site, so i wrote my own:

<client name=”uTorrent 1.8.1 build (12639)” author=”z3n” version=”1.1″ processname=”utorrent”>
<query>info_hash={infohash}&amp;peer_id={peerid}&amp;port={port}&amp;uploaded={uploaded}&amp;downloaded={downloaded}&amp;left={left}&amp;key={key}{event}&amp;numwant={numwant}&amp;compact=1&amp;no_peer_id=1</query>
<headers>Host: {host}_nl_User-Agent: uTorrent/1810_nl_Accept-Encoding: gzip_nl_</headers>
<peer_id prefix=”-UT1810-_1″ type=”random” length=”10″ urlencoding=”true” upperCase=”false” value=”"/>
<key type=”hex” length=”8″ urlencoding=”false” upperCase=”true” value=”"/>
<protocol value=”HTTP/1.1″/>
<hash upperCase=”false”/>
</client>

Have fun!

Leave a Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!