New Site: randgame.info

Posted by shadow_of__soul | Posted in Uncategorized | Posted on 11-10-2009

0

Hi,

the other day i was thinking to make a site where you play a random game each time you visit.

so, i have registered a domain and set it up:

Rand Games

need to do a lot of more work on it, i’m still thinking some features i can add and a proper design for it (that need to be light and at the same time, look nice)

As soon i make some changes, i going to post it here :)

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Change of design

Posted by shadow_of__soul | Posted in Uncategorized | Posted on 09-10-2009

0

Hi,

as you see, i changed the style of the blog, i hope you like it :D

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

OAuth and REST with AJAX or FLASH

Posted by shadow_of__soul | Posted in Uncategorized | Posted on 06-10-2009

0

Hi,

the last days i was working on a API for project. for that, i was needed to set a OAuth system. all went perfect but i have a problem when i wanted to use the API in a AJAX app.

as you already know, AJAX it’s a technique where you use javascript to request info to a page and show it to the user. as you also know, javascript it’s executed in the user machine, and for OAuth it’s a problem :s

no matter what how it’s implemented the OAuth system in the API that you need to use, you need always to send the $consumer_key and the $consumer_secret encrypted in the signature, also depending of the type of request, also need to send the $token and $token_secret, for that, you can’t include in any javascript way (or in a swf) any of this vars why compromise the security of the consumer, why any with this data can access to the protected resources as you.

for that, the solution it’s to create a bridge between your AJAX/FLASH app and the API/OAuth server.

the process it’s simple, create a php with a code like this:

<?php

//check if the one who are making the call to the bridge it’s trusted

//for that you can use a filter ip, an authenticated session or any method that fits your needs

$consumer_key=”lalala”;

if($trusted){

//making the request

$result=$OAuth->request($url_of_the_server.$_GET['service'],

$consumer_key,$consumer_secret,$_SESSION['token'],$_SESSION['token_secret']);

echo $result;

}

?>

of course the code can be diferent depending of your client library and stuff, but basically that are the steps to make the bridge:

1)check that the one who call the bridge it’s trusted or ahiterized, checking a sesssion var or anything what you have

2)make the request using the $service var that define the service to call to the service

3)print the result of the call and it’s done :D

i hope this give you some help at the time to use OAuth and REST with ajax and flash

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

serialize array with javascript (php compatible, associative arrays and mootools)

Posted by shadow_of__soul | Posted in Uncategorized | Posted on 04-10-2009

0

Hi,

one of the task i was need to do, was to write a javascript funtion to serialize a array, so can be sent through ajax.

the task wasn’t easy, why searching on the web i only found partially working functions (some that only proccess numerical arrays, other even’t didn’t worked, and wasn’t recursive) but serching i come up with the perfect javascript serialize function ever :D

in resume it can:

  • parse numerical arrays
  • associative arrays
  • mixed arrays
  • multidimensional arrays
  • it detect if the index or values of the element it’s int or string and parse it accordingly
  • mootools compatible (read below what i mean with  this

so i dont want you wait anymore, here it’s the function:

function serializeArray(ArrayElem)
{
var cont=”";
var counts=0;
var jj;
var res = “”;

for(jj in ArrayElem)
{
//if mootools it’s included, this hack prevent that the function parse also the prototype objects
if(jj==”$family”){
res = ‘a:’+counts+’:{‘;
res += cont;
res += ‘}’;
return res;
}
if(isInt(jj)){
cont += ‘i:’+jj+’;';
}else{
cont += ’s:’+jj.length+’:”‘+jj+’”;’;
}
if(isArray(ArrayElem[jj])){
cont += serializeArray(ArrayElem[jj]);
}else{
if(isInt(ArrayElem[jj])){
cont += ‘i:’+ArrayElem[jj]+’;';
}else{
cont += ’s:’+ArrayElem[jj].length+’:”‘+ArrayElem[jj]+’”;’;
}
}
counts++;
}

res = ‘a:’+counts+’:{‘;
res += cont;
res += ‘}’;

return res;
}

function isArray(obj) {

return obj.constructor == Array;

}

function isInt(myNum) {
// get the modulus: if it’s 0, then it’s an integer
var myMod = myNum % 1;

if (myMod == 0) {
return true;
} else {
return false;
}
}

i going to explain some things about this function:

  1. the isInt() function it’s very simple, i dont know if it can fail with some weird value, but at least it’s fast and cross browser compatible
  2. i’m using a counter and not the length propertie of the array why with associative array it return 0
  3. as you see, there it’s a hack for mootools, that it’s it make it “compatible”, whiteout that hack the function also parse all the native properties and elements added at the moment when you add mootools to the document. i didn’t know any other form to stop it, so if you know something advise :D
  4. in the php that receive the serialized array, remember to use stripslashes if the magic quotes are ON and maybe you going to need to use html_entity_decode() (at least that was my case) to convert the doble quotes character to signs.

i hope it help you, it’s very easy to use and understand and can save you some hours of work :D

Regards,

Shadow

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Import big xml to oscommerce or mysql database

Posted by shadow_of__soul | Posted in Uncategorized | Posted on 02-10-2009

0

Hi,

recently i have needed to do a script that parse a big xml file and insert it to a oscommerce database. i wanted to write this post why can be useful if you are doing something like this and maybe with other CMS system or database schema :)

the script no only need to insert the products, also download the images, check if the product already ,xist and update the product with the info of the xml. i don’t going to describe the trivial process of check for the product, make the queries etc.. if you are seeing this post you are run with some of the 2 essential problem:

1)out of memory error

2)session timeout, php execution time limit or 500 internal error

so, here are the soltuion for your problems :)

1) out of memory error

this it’s happen why surely you are usng the xml_parse() to parse the xml. this function always going to give you a out of memory error why need to load all the document in the memory to use it.

the solution it’s simple, use XMLReader this going to allow to load all the xml file and let you review each element of the xml. the documentation it’s very clear of how to use it and with some of testing, in a few hours you should have working your parse function :) . the only “problem” can be it’s that’s only available from php version biggers that 5.1.2, i don’t think so but maybe you can run with some problem in some host :)

2)session timeout, php execution time limit or 500 internal error

well, this error can be fixed but going to be you more effort and ugly code but i didn’t thinked any other posible solution for this :s

if you have a php execution limit error, simply add: set_time_limit(0); this this going to let you run the script forever. of course this isn’t safe, why if you have a bug or something, can be running forever and consumming so much memory than you going to have your shosting account suspended, so be sure to test everything before you run it live :)

after you set the execution time to 0, if you still have a timeout error or a 500 internal error it’s surely happening by the mysql queries. the xml parse it’s very fast with XMLReader but the real time take the mysql queries and if you are using oscommerce and need to check too much thing going to be a nightmare :P

the only solution i come up for this, was to re-execute the script till i procesed all the xml. basically i have parse the xml into a multidimensional array like this:

$rows[$rownumber][elements]

so, i process all the xml (in this time i processed a 3MB xml) to a array, send it to a function that update the DB and processing it in 200 rows parts. after the part finished, i keep the number of rows processed and printed:

echo “processed…. $numberofrows”;

echo ‘<meta http-equiv=”refresh” content=”4;url=xmlimport.php?f=’.$filename2.’&cont=’.$continue.’&start=’.$finished.’” />’;

i used a meta and not a header() why with header it still crashed with a 500 internal error.

so, when the script load again in the function that parse the xml, i set:

if($_GET['cont']==true){

for($i=0;$i<$_GET['start'];$i++){

$xml->read();

}

}

with that, i have moved the pointer of the xmlReader object to the last inserted row, and then start to parse the rest. it continue till the rows to parse are none and the script stop :)

i hope this help someone to resolve this problem :)

Regards,

Shadow.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]