Archive for February, 2010

Swap values of two variables

Wednesday, February 24th, 2010

Here is a cool little thing, we use xor to change values of two variables, without a temp. variable, function or anything else:

php > $a = 'a'; $b = 'b';
php > $a = $a ^ $b;
php > $b = $a ^ $b;
php > $a = $a ^ $b;
php > print $a;
b
php > print $b;
a

MySQL escaping

Tuesday, February 23rd, 2010

Every now and then I see some sites that fails to input/output data from the database, like: don\’t.
My heart hurts every time, and it’s not just small pages, it’s big community/news pages as well.

The reason why this happens, is that people forget to check if magic_quotes_gpc are true. magic_quotes_gpc is a setting in php.ini and will escape input automaticly from GET, POST and COOKIE. If this setting is on (and in many cases it is) and then use mysql_real_escape_string as well, it will result in double escaping. On output some people tend to forget to strip slashes.

I have made these filter functions to show how it could be done, but there are always better alternatives.

<?php

function mysql_in($data) {
    if(get_magic_quotes_gpc()) {
        $data = stripslashes($data);
    }
    return mysql_real_escape_string($data);
}

function mysql_out($data) {
    $data = stripslashes($data);
    //$data = htmlspecialchars($data);
    //$data = nl2br($data);
    return $data;
}

?>

Why Cal Henderson hates Django

Tuesday, February 23rd, 2010

Cal Henderson talks about why he hates Django. It’s very entertaining and good learning :)

IRC on numb3rs

Saturday, February 20th, 2010

“IRC! Internet Relay Chat. It’s how hackers talk when they don’t want to be overheard.”

“Can we see what they’re saying?”
“In leet speak. Luckily, I speak leet.”
“Thats so hot.”

I laughed :)

NetCom USB Modem (ZTE MF636) on Ubuntu

Thursday, February 18th, 2010

This guide is to make the ZTE MF636 USB modem work with Ubuntu 9.10 (“Karmic”). I made this guide of many other guides out there, but only with the combination of them all I got it to work for me. I have the Norwegian NetCom operator, but it should work with others as well.

Step 1

In Linux, the USB-modem is considered as a storage device, so we need to make sure that it is seen as a modem instead.

First download gtkterm:

sudo apt-get install gtkterm

Find the correct USB device:

ls /dev | grep ttyUSB

The last device listed is probably the one. Now edit the config file.
Make sure to replace the X in USBX you got earlier:

gedit ~/.gtktermrc

Copy / paste:

[default]
port = /dev/ttyUSBX
speed = 115200
bits = 8
stopbits = 1
parity = none
flow = none
wait_delay = 0
wait_char = -1
echo = False
crlfauto = False
font = "Nimbus Mono L, 14"
term_transparency = False
term_show_cursor = True
term_rows = 80
term_columns = 25
term_visual_bell = True
term_foreground_red = 43253
term_foreground_blue = 43253
term_foreground_green = 43253
term_background_red = 0
term_background_blue = 0
term_background_green = 0
term_background_saturation = 0,500000

Open gtkterm (Accessories->Serial port terminal) and go to configuration and check ‘Local echo’. Then write:

AT+ZOPRT=5
AT+ZCDRUN=8

You will get “Close autorun state result(0:FAIL 1:SUCCESS):1″ if it went well.

Step 2

Install Gnome-ppp:

sudo apt-get install gnome-ppp

Edit the following config file:

gedit /etc/wvdial.conf

Copy / paste: (Here also change the X)

[Dialer Defaults]
Modem = /dev/ttyUSBX
ISDN = 0
Modem Type = Analog Modem
Baud = 460800
Init1 = ATZ
Init2 = AT&F &D2 &C1
Init3 = ATS7=60 S30=0 S0=0
Phone = *99#
Dial Attempts = 1
Dial Command = ATM1L3DT
Ask Password = off
Password = netcom
Username = netcom
Auto Reconnect = off
Stupid Mode = 1

Edit another config file:

sudo gedit /etc/init.d/rc.local

add:

modprobe -r usbserial
modprobe usbserial vendor=0x19d2 product=0x0031

Unplugg modem. Reboot. Plugin modem.

Now we need to run gnome-ppp with root access:

sudo wvdial

If everything went well, you will get something like this:

echofish@ubuntu:/dev$ sudo wvdial
--> WvDial: Internet dialer version 1.60
--> Cannot get information for serial port.
--> Initializing modem.
--> Sending: ATZ
ATZ
OK
--> Modem initialized.
--> Sending: ATM1L3DT*99#
--> Waiting for carrier.
ATM1L3DT*99#
CONNECT
--> Carrier detected.  Starting PPP immediately.
--> Starting pppd at Sat Nov  7 21:35:46 2009
--> Pid of pppd: 5709
--> Using interface ppp0
--> local  IP address 89.9.97.35
--> remote IP address 10.64.64.64
--> primary   DNS address 212.169.123.67
--> secondary DNS address 212.45.188.254</pre>

Happy surfing!

(To restore the modem to default, just write AT+ZCDRUN=9 to it.)