Category Archives: Programming

Ajaxify Form Validation

I’ve had trouble trying to validate jquery ajaxify forms using validation plugin

first I tried hacking onStart event but that would load the hash and fail at validation leaving me with a wrong hash in the URL bar

the thing that worked was hacking into the function that binds the event

I just asked there if the bind was for a form element and if so I called validate function
If it returned false I also returned false and effectively stopped form submission
hashes work fine, forms work fine

anyway, this is the piece of code for anyone interested

if(jQuery(this).is('form')) {
if (!jQuery(this).valid()) {
return false;
}
}

happy hacking :)

H1N1 Hack

I just read an interesting blog article about hacking H1N1

I’m a computer programmer so I actually found it quite interesting since the author made an analogy between computer and human viruses

you can learn a lot about flu virus, it’s program code, how it works and what can be done to make it more lethal or benign

here’s the link http://www.bunniestudios.com/blog/?p=353

Achievo ATK Framework

few days ago I was writing this php/ajax application for a client and then I realized I needed a way to manipulate the tables

some tables were simple but some had two or three relationships

I thought about writing a generic class for the simple tables and doing other tables “by hand”

then I realized… maybe someone else wrote something similar

and I’ve found just that

Achievo ATK framework makes it really simple to create /admin for your client so he can manipulate his tables by himself

but that simplicity comes only after you’ve spent some time getting familiar with it

at first, atk coding seemed cryptic but now I’m into it and it feels natural

atk will make your life easier because once you understand it you can easily manufacture table interfaces

it has support for cascading deletes so you don’t have to worry about orphaned records

one thing I’d like to see is full UTF-8 support

it currently uses iso-8859-1 character set by default (english language)

I got it here

Dynamic DNS Iptables Firewall Script

I’m using my computer as a development platform and I usually let people in by editing my firewall active table

but for those with dynamic ip addresses I had to figure out a different solution

so I made a small script in PHP to check for my friends dynamic ip address by looking up his dyndns.org hostname

it checks IP then goes sleeping for 5 minutes

when it detects an ip change it updates firewall by flushing my firends chain and adding a rule to let him access my webserver

this script works in my firewall iptables setup, it could work on yours but you have to be smart and figure that out for yourself, I’m not giving any guaranteess for this, use it as GPL V3 code :)
if you want me to help you set it up, just contact me

here’s the code:

#!/usr/bin/php
<?php
/*
use this code as GPL V3 licence says
Copyright (C) 2008, Miroslav Madzarevic, All Rights Reserved
*/
$old_ip = '';

while (1) {

    sleep (300);

    $host = 'friend.dyndns.org';

    if (!preg_match('/^'.$host.' has address ((?:\d{1,3}\.){3}\d{1,3})$/',
           exec ("/usr/bin/host $host"), $matches)) {
        echo "IP problems\n";
        continue;
    }

    $ip = $matches[1];

    $ip_parts = preg_split ('/\./', $ip, -1, PREG_SPLIT_NO_EMPTY);

    if (count($ip_parts) != 4) {
        echo "wrong ip parts count\n";
        exit;
    }

    $good = array();

    foreach ($ip_parts as $ip_part) {
        $ip_part = (int) $ip_part;
        if ($ip_part < 1 or $ip_part > 254) {
            echo "wrong ip part, $ip_part\n";
            exit;
        }
        $good[] = $ip_part;
    }

    $new_ip = join('.', $good);

    if ($old_ip != $new_ip) {

        `/sbin/iptables -F friend`;

        `/sbin/iptables -A friend -s $new_ip/32 -p tcp -m tcp --dport 80 -j ACCEPT`;

        $old_ip = $new_ip;
    }
}

?>

Firebug In Firefox 3 Ubuntu

with the new Ubuntu came Firefox beta 3, then the Firefox final 3 but Firebug just didn’t work

FYI Firebug is a great web development add on for Firefox

now after like a month or two ;) I found a way to make it working

just remove the Firebug extension if you have it that does not work

and then install Firebug from the packages

I used the command line

aptitude install firebug

have fun :)

C++ Hibernate Daemon

I use Fluxbox window manager on Ubuntu Linux where I don’t have all the bells and whistles of GNOME or KDE.

so I thought I’d make myself a daemon program that will automatically put my computer to hibernate when my laptop battery is almost empty

usually I take care that my battery doesn’t get to the red zone but this is for those few cases I forget

I wrote this small programm in C++

why c++

well because I don’t programm in c++ and I found it interesting to do so :)

main.cpp looks like this

so, you see the locations of the info and state file for the laptop battery, the percent at which we go to hibernation (I’ve set it to 12%), and the sleep interval before checking the battery state again (10 seconds)

I use my own hibernate script but you could use something like /usr/sbin/hibernate

you can change all of these

in the main function you can se that:

  • we work only as a root user
  • we daemonize (detach from the terminal and run in background)
  • instantiate a worker class object
  • and do our work :)

and what is our work? check out this picture

worker.cpp

well we get the battery total capacity and current capacity and then we calculate the percent

if the percent is less that what I want (12%) then I want my laptop to go to hibernaton

but I also made a security check (that you can remove if you like)

that makes sure my laptop doesn’t go twice into hibernation in succession

why did I do that?

well because I parse data from some files I have no influence over

and those files can change their format anytime

so this is just a precaution

I have a simple makefile that compiles this code, copies it to /usr/local/bin/medved (medved in my language means a bear and bears hibernate ;) ) and adds an entry to /etc/rc.local so that it starts next time you reboot your box automatically

you can also start it by hand and see how it works before the reboot

and that’s it, now I have a daemon that puts my laptop to hibernation before my battery runs dry

I’ve published the source code under GPL licence version 3

you can get source code here

HTML 5 First Public Working Draft By W3C

Today we got to see the first public working draft of HTML 5 here.

The most interesting for me were the differences between HTML 4 and HTML 5 and the improvements.

HTML 5 adds support for 2D drawing by using the new canvas element. There are also audio and video elements, new data presentational elements, new types for user inputs (like browser based calendars) etc.
Although this is still a draft  it looks promising. Check it out :)

BugLabs Lego Gadgets

Well these aren’t your tipical Lego parts. Here, a part is actually a piece of hardware that you can combine with other pieces to build something.

There are parts for a camera, gps and other stuff. And the interesting thing is that all of them communicate via HTTP, which makes it easy to use and programm.

I’ll go and read about these a lot more later. Don’t wait like me, check it out now.

VIM

vim is my favorite editor, I use it to programm, write texts, anything. On my desktop I use gvim, a graphical vim and I really enjoy it. But these days, I had to work remotelly via ssh and used vim from the console editing multiple files. It’s not as comfortable as the graphics version because there isn’t an easy way to use menus but I managed with :ls to list buffers and :b2 and others to change buffers.

Today I saw a Slashdot article about a book on vim, called Hacking VIM, maybe that’s just what the doctor ordered… :)