All in vain

Some rants about technology, PHP, people, tools, web etc from a developers perspective. Feel free to copy ideas, code or anything from here.

Thursday, December 14, 2006

Guide to application development with PHP

No comments
I know it sounds too ambitious but couldn't help but dig it ;) . Well if you have dipped your toes in PHP and you are comfortable working with PHP this guide is for you. If you are unsatisfied with the current way of structuring your application then this guide might help you.

So lets start with the folder structure. The way you structure your application is crucial. Here is what i have come up with. This structure is only suggestive, you are free to modify it to suit your taste.



  • Classes: Store all your classes and helper code here.
  • Conf: This holds your configuration files needed for your application.
  • Cron: Holds all files which needs to run as cron job.
  • Includes: Store all your function files here.
  • Js: Javascript code plus any helper code like WYSIWYG editor etc.
  • Locale: Create folders for every language you plan to support and create language files which you can call depending upon the configuration settings.
  • Pages: All the pages which perform some directly visible action are stored here. Rest can be stored in the helper sub-directory.
  • Setup: Some scripts which can generate configuration files plus some SQL files needed for your application.
  • Style: All CSS + Images, can be organised using themes folder.
  • Templates: Divided in two sections. Forms stores all the form templates and layout stores what else layout templates.
  • Tmp: Store temporary files or stuff which is not private. You can use this folder to store cached files in your site as well.
Now the root folder would contain only three files, index.php, config.php and .htaccess. The index.php file acts as a router and translates the GET url to load the particular file. For example consider this URL:
http://yoursite.com/app/index.php?action=login

The index page catches the "action" parameter and loads the login.php file and depending upon the current state plus extra variables performs some action.

Lets get an overview of the index.php file
require_once( DIRNAME(__FILE__) .'/includes/app_init.php');
This line invokes the file which does all the initiation stuff, like loading the configuration file, loading the framework, loading the default template, defining some variables etc.

Tuesday, December 12, 2006

Sphinx mysql fulltext searching

No comments
A few months back i posted about a novel way of indexing data in your MySQL database. There is a small update on this as the developer has launched a dedicated site for that and also includes some API docs and help text. If you are frustrated with MySQL taking forever to index your data i suggest you take a look at this.

Friday, November 17, 2006

XML sucks

No comments
Well if you are a programmer you have to read this. Its awesome, it brought smile to my dry lips. What can i say "pure genius". Excerpts from the article.

And each language could be heard to mumble as it tromped and tromped and tromped, with complete and utter glee:

Have to parse XML, eh? Have to have an XML API, eh? Have to work with SOAP and XML-RPC and RSS and RDF, eh?

Well parse this, you little markup asshole.

The End.

Creating stunning graphs in php using artichow library

No comments
Those of you who are looking for a good library package to create visually appealing graphs or charts using GD library and wish some package was there which was GPL'ed then look no far. Artichow graph library is the perfect candidate which fits this description. While i like Jpgraph but the fact that its not GPL'ed makes me crib.

Since the library is in French i suggest you use Google translator to navigate the site.

Using GNU Netcat to access your POP3 mail account

No comments
This summary is not available. Please click here to view the post.

Thursday, November 09, 2006

Spooky blog

1 comment
Well this blog made an interesting read. I was in the process of killing some time and stumbled here looking for Orkut creator. This girl sure has a thing for everything spooky. But it is well worth the time, i am going to bookmark it and will try to check it out from time to time.

Tuesday, November 07, 2006

Converting Youtube flash videos to MPEG and extracting audio

No comments
Recently i was asked (rather commanded ;) ) by my friend to search for her favorite song. Well this song is by Jennifer Paige ("beautiful" OST Autumn in New York). I searched realy hard but could not find it. Then Finally i found a video on Youtube. Apparently this song was used as the background score of that video. So my mission was to get the audio-track from this video.

First of all you need to grab the video from youtube. Make sure you save it with .FLV extension. Now you are ready to go.
  1. Convert this flash video to mpeg format.
    ffmpeg -i video.flv -ab 56 -ar 22050 -b 500 -s 320x240 test.mpg (check out google for more options)
  2. Extract Audio from this MPEG video
    ffmpeg -i test.mpg -vn test.wav
  3. Convert this WAV(PCM audio) to MP3 format
    toolame test.wav test.mp3

A few notes about the above command. I have Xubuntu and i have ffmpeg and toolame encoders installed. You should use appropriate package managers to install them for you. Also i haven't had the time to play around with quality settings, i think it could have been done even better. So if you know a better process please do let me know.

Monday, November 06, 2006

A new type of captcha

No comments
Those of you who came late, a CAPTCHA stands for “Completely Automated Public Turing test to tell Computers and Humans Apart“ . Basicaly the system asks you to read an image and input the words exactly as written on it, before it lets you perform the desired operation. While i think no doubt it would slow down the spammers, but i have seen some people who claim to have the ability to read a captcha.

While going through this site i wondered is it possible to combine the idea of captcha with the human habit of reading a word partially. For example if we want the user to enter the word "glass" we can make the captcha to read "galss". Only a human being can infer that it is indeed the english word "glass".

Wednesday, November 01, 2006

Tabs galore: CSS tabs for you

No comments
Attention all you CSS lovers, get your free CSS tabs here. This one is from exploding boy, whatever that means ;) Well i have been facinated by css tabs myself because they make your site look much more professional than random links thrown here and there. Also if you are making some kind of an Web based application then its crucial that you create a logical navigation. Grouping similar functionality is best achieved with tabs.

The tabs on the above site uses images for that polished look. Though nothing is wrong with that but i think i like the pure css tabs more with no images used. And the best example i can think of can be found here. I have used them on more than one occasions and find them quite good. More links to other tabs can be found here.

Also one more css site worth visiting is this one. I learned quite a lot about css from this site; thanks paul. You can learn some css techniques here and pick up some layouts too.

Ubuntu 6.10 Edgy Eft released

No comments
It seems i am late on this, but wtf. I have been on ubuntu for almost 6 months now. Well not actually ubuntu but xubuntu. Since my PC is an old one, i am not able to use the latest KDE. So i installed XFCE as my desktop manager, its light and it works(TM) . The latest version of ubuntu promises to be much more polished and efficient.

From the site i can see that it has a lot of new features, like the newer init system. It seems upgrading it is not advised but some people have reported that it went ok for them. I think i would do a full install instead of the upgradation because i have experimented heavily with my current version and i just need to take a backup of my home dir and htdocs directory and i am good to go.

Now some caveats, from what i have read it seems the newer ubuntu has replaced bash with dash as their default shell. Some people are complaining about this issue and we have to just wait and watch for the repercussions.

Friday, October 20, 2006

I love Wine

No comments
Before you jump to any conclusion, let me clarify things a bit. The wine i am talking about is the wine program (Wine is not emulator) and it does leaves a good taste in your mouth. I was reading up on running wine on linux and i thought maybe it is a good idea afterall. While the installation was a no-brainer (I am on ubuntu and synaptic rocks!) and to install some app you need to find the setup file and open it with wine program.


In ubuntu, a new context menu item is added and i can right click on a "set.exe" file and select "run with wine". Anyways i could see it was emulating "c:\\program files\" and maybe there is some hidden registry file too. Well all in all it was intelligent enough to place a link to that app on my desktop. The first app i installed was 'gtalk' but it didnt work out that good. It was missing the buttons and i could only see the text boxes. Maybe i would look into them when i get more time. Then i tried Ultraedit editor and it did install good. Heres a screenshot to that!


Ok so it did go smooth, now i am on my way to install winamp and some other apps i liked on Windoze. Now you would be tempted to ask why did i install ultraedit instead of using linux editors like kate or bluefish. The thing is i like kate but it is not as refined as ultraedit. And even if this sounds lame i like it so you who cares if its a windoze app.

Cheers

Wednesday, October 18, 2006

OpenSource Basecamp replacement

No comments
Those of you who are looking for a replacement to basecamp project management tool can check out ActiveCollab. Though its not as feature rich (currently) as basecamp but i am sure it will develop into a worthy competitor.

Installing activecollab on your own server means your data is in your hands (both literaly and otherwise :) ) and also u can set up as many projects as you wish. The only roadblock i can see is the requirement of both PHP5 and MySQL with innodb support.


Balle balle band baaja time

1 comment
Yup its true. Its marriage season now in india. Unfortunately its not me who is getting married, one of my best friends (sumit) is getting married. In a few days (15 days to be exact) time he will loose his bachelorhood and join the ranks of married ones ;) .

So lets see how it works out for him, I mean girls are always carping the fact that guys shy away from commitment, but this guy seems to be too prepared for that. I wish him all the luck in his married life!!

Thursday, October 12, 2006

Distro surfing

No comments
Yeah i know what you are thinking. Was this dude stoned when he wrote this caption. Yeah i indulge in distro surfing from time to time. The thing is linux spoils you silly with so many options that it baffles many noobys. Ok so i have installed / tried lots and lots of distros. So many of them that i have forgot even there names. The thing is Distros can be categorised into main stream (Server and desktop) and private projects. Most of the smaller distros started as private projects and when people started to show support they grew into community projects.

Also, all of these distros take a well established main stream distro and does some value addition to it. Take the case of Elive, a live CD distro based upon debian and enlightenment as the Window manager which is a tough task itself, if u want to achieve that yourself. Let me make an inventory of all the distros i have tried till date:
  • Monkey linux
  • Corel linux
  • Debian (woody)
  • Redhat 7,8,9
  • Fedora 2,3,4,5
  • CentoOS 3, 4
  • FreeBSD
  • Elive
  • PCQ linux
  • Zenwalk
  • Slackware 9, 10
  • Ubuntu 5, 6
  • kubuntu 5
  • Xubuntu
  • Knoppix
  • Madriva
  • Damn Small Linux
  • Slax
  • Linspire
  • Vector linux
Phew that was a long list. Of all these distros i have tried, i have some personal favorites. First is debian; its rock solid though hardware detection is a problem with my computer but i manage to make it work somehow. Redhat or Fedora is ok but its quite bloated. After doing a full install it doesnt leave you much space to play around. Vector was blazing fast on my old computer, the only problem was Xfce used to crash quite reqularly, even KDE crashed quite often.

Currently i am using Xubuntu and it is working fine apart from a few small glitches. I am in love with the synaptic package management tool available on debian based distros. It put even yum to shame.

Earlier i used to configure the LAMPP setup by hand on each distro i used. But lately i wondered why go through all the pain, nowadays i just download xampp for linux and there you go. No time wasted in installing and configuring all the extensions and such.

A few days ago i stumbled upon Kororaa. It has more eye candy than vista folks. And come to think of it; its not hard on your hardware, lol. I always thought successive versions of OS should be more stable, less hungry for resources and more secure than the previous ones. Sadly this is not the case with Vista. Now lets not get into MS bashing here.

That just about does it for today. As usual comments are more than welcome.

Sandy

Tuesday, September 26, 2006

Nice site for CSS

No comments
Heres a site with some cool CSS menus and good tutorials on selectors and such. Very valuable resource if you are just starting with CSS. http://css.maxdesign.com.au/

Tuesday, August 29, 2006

Hardware issues in ubuntu/xubuntu

No comments
Recently i was trying xubuntu. I was facing some issues with my 4 year old PC. My realtek ethernet card and onboard sound card was not detected. Even `lspci` returned nothing. After banging my head against the wall for a few hours and searching on the net revealed that i have to add "pci=noacpi" to my kernel arguments(in grub press 'e' and at the end of the string append this).

After that everything worked like a charm. I dont know why this happens but it did gave me some problems cause i had to reinstall some distros cause my hardware was not getting detected properly.

Friday, August 25, 2006

Open standards

2 comments
Recently i was reading an article about ODF. Clearly open standards are what we should be looking for. Binding ourselves to a proprietary is going to harm us in the future. For example if i have a document i would want it to be accesible to me for eternity. Just because the software it was created in is no longer available or for a price shouldnt mean anything.

Heck i "own" that data. So the bottom line is softwares (like Open office) which support open document standards need to be encouraged.

Tuesday, August 22, 2006

Very nice thought

1 comment
I was for a long time anguished by my peers who used to rubbish PHP as a "hacking" language. Fit only for write once forget afterwards kinda scripts.

I always believed that if you have some self-descipline you can do pretty much anything* with your chosen language.

*Conditions apply

Heres a link to an interesting article from lesscode.org

Monday, June 12, 2006

Parsing large log files with PHP

2 comments
Today i was struggling to parse huge log files with PHP. Problem was the log file was huge in size and the new entries were being appended at the end. So PHP was taking forever to parse this file and extract last few records.

The solution? use AWK. Shell programmers swiss army knife :)

I knew every line had a unique time stamp (well almost) and i would store the timestamp of last line parsed. So i wrote a small command in AWK:


exec (" awk '/Jun 12 00:13:01/ {line_fnd = NR; }END {print NR,line_fnd}' OFS='=' /var/log/huge.log ", $arg1);



This returns the line number found and total number of lines separated by '=' symbol.

Then its quite easy to parse the output of this command and find the total new lines to grab. Then you can tail that many lines and redirect the output to a temporary file and parse the file line by line and do whatever you want with it and at last store the timestamp of the last line you parsed.

This shaved off quite a huge time of my script execution time :)

Anyone facing this problem might use this technique or if there is a better solution then please do let me know.

Tuesday, April 18, 2006

New company

No comments
Just joined my new company and it is a religious experience for me :0.

After working at my previous employers for more than two years, i feel like a religious convert here at my new company. Like a convert i feel fascinated about everything here ;-)

I am especially fascinated by the way this company works, every thing is planned. We were formally inducted and the process went like a ceremony :P, as u can see i am happy for my self (what a sod you might say haan ;) )

Okay would make it a habit to post atleast once a week, so bye for now.

Tuesday, February 28, 2006

Simply the best PHP framework

No comments
My search has ended. I think i have found the best PHP application development framework. After so many hours of searching, installing, configuring so many frameworks from sourceforge, freshmeat etc, i have found the best solution for my problems. Ok let me explain a few things here. I wanted a framework which:
  • Is flexible.
  • Is easy to use.
  • Doesnt force a design pattern.
  • Dont overdo the MVC, Ajax etc.
  • Provides a stable base for my applications.
And the winner is............. PHP2GO framework!

Using it is extremely simple. You can even use it with your existing applications. It doesnt forces itself on you. This framework in my opinion is the ideal for my needs. It makes so many day to day tasks so simple that it makes u think "why didnt i think of it".

I would rate it above some other frameworks i tried. For one its footwork is so small, you can strip it of unwanted functionality and it comes to around 800K.

Basic usage is like this:
  • U define a config file to suite your needs.
  • U include this file in any file you make.
  • Import classes u plan to use (Forms, templaes etc....)
  • initialise the classes and provide it with details and you are done.
Take a look at the example directory and u can find some very good examples. The most striking feature is how u can define your forms structure in XML and it generates form from it! Its so simple.

I would look into it more thoroughly but whatever time i have spent on it has made me cry with joy. OSS rules.

Saturday, February 18, 2006

PHP and SOAP

4 comments
I mentioned in one of my previous posts how i accessed the betfair api. The reason i am mentioning it again is because of this site. I owe quite a lot of things to this site. When i was banging my head against the wall to find a solution to my problems i found pattemplate to be quite helpful. Not that it has anything to do with SOAP.

Here is a summary of my problem, i needed to access the api through PHP and the documentation on their site was not helpful. Then i came across some code which used NuSOAP library. The response was very slow, and also the class i used was not sufficient to handle all the operations allowed in the API. To complicate matters further the Betfair people switched to a new version of their api and i was left on my own to find a solution. Then in one of the Perl forums i found a snippet where somebody (god bless him!) showed some code on how to send a raw request to a soap server.

I latched onto that chance and digged in PHP to find equivalent solution. Here is what i did. I converted the XML request data into templates and used Pat-template system to fill-in the values. Then i used the excellent Curl library to send raw POST request to the betfair SOAP server and tada! we got response.

//function to execute curl calls
function call_curl($xml)
{
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

$ch = curl_init(); // initialize curl handle
curl_setopt($ch, CURLOPT_URL, API_URL ); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // times out after 1 min
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml ); // add POST fields

if( LOCAL )
{
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}

curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

$header[] = "SOAPAction: ". API_URL;
$header[] = "MIME-Version: 1.0";
$header[] = "Content-type: text/xml; charset=utf-8";

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch); // run the whole process


if (curl_errno($ch))
{
print curl_error($ch);
return 0;
}
else
{

//now that we have got a result, parse this soap packet and return the XML doc
$parser = new soap_parser($result,'UTF-8','',TRUE);
$result = $parser->get_response();

//print_R($result);
curl_close($ch);

if($result["Result"]["header"]["errorCode"] != "NO_SESSION" )
{
update_session_key($result["Result"]["header"]["sessionToken"] );
}

return $result;
}

}//function ends



The rest as they say was history ;-) Btw if u dont know anything about Curl library i would advise u to give them a visit. It is an excellent tool. I have used it in various projects and here are a few of the uses i could find in my experience:
  • Form POSTing
  • Downloading a file and saving it to disk
  • Uploading files to a FTP server with authentication
  • Spidering!
  • Loggin in to a site and navigating through password protected areas and downloading feeds etc.
  • Telnet
  • Data transfer between servers
Thats it i guess for the moment, but before i go heres what u can expect in my coming posts: More about some other unix tools like NetCat, Dig, Socat etc and my new PHP development Framework.

Tuesday, February 07, 2006

I need RAM

No comments
I need RAM and lots of it :-) Just installed Kanotix and my system takes a dive every time i run XAMMP. For those of you came late, XAMMP is a all in one solution for a web server. So if you dread compiling and configuring Apache + MySQL + PHP and rest u need to take a look at this. It works on most systems.

So where was i?? Yes i need RAM My current rig runs AMD(what else ;-) ) duron 1.2 Ghz with 128Mb RAM. It was fine until i upgraded my distro to kanotix that i noticed my machine was thrashing while working. This made me search for distros which were not hard on configuration and would run fine with old systems. This took me to SLAMPP which is based on SLAX but comes with XAMPP preinstalled..

Lets see if it works otherwise i have to buy some RAM for my old PC. If you are interested in finding the right distro for u may i suggest distrowatch it lets you search on so many parameters.

BTW linux is the way to go if u want to learn about real computing. How things work underneath. And running linux is cool too. For developers it is the best, unless ofcourse you work on Micro$oft technologies. If u work on .NET u might be interested in MONO. This project makes it possible to run .NET on apache

hey did u visit my site on zeeblo. You should cause i have just installed JAWS CMS on it and it rocks.
See my site:

Tuesday, January 10, 2006

Evolution of a PHP programmer

No comments
Ok lets see what i did when i first "learned" how to hack PHP. Wrote some (lot!) scripts which said 'hello world' then did some counting then print some pyramids and what not. The next step was 'i was told' to make it show dynamic content. That is the good old print the date and time on some lame html.

Next came the file handling. PHP rocks on that one too. Its quite easy to work with files in PHP.

Then i guess came the guestbook and DB connectevity. After making lots of stray pages you realise its no good ;-) and you start to do "require header.php" etc. After lots of projects you realise HTML sucks. What with lots of inline coding. It becomes tedious to changel logic.

Then u realise u should separate the two of them. That is the presentation logic and the business logic. see: http://www.sitepoint.com/forums/showthread.php?t=123769&page=1&pp=25

This is when you pick up a template engine or two. BTW checkout pattemplate, it rocks IMHO. So you go back and realise its cool to use tempates and switch themes with changing the template folder.

After some point of time you get tired of the omnipresent FORMS. You say what the #$@#. These things suck up my whole time. Then u start to scratch your head and say "i need a method to my madness ©". You start to use form generators and stuff like that. Then time passes like a quiet river. And then bang you are back to the square one. Your project is a mess of classes (your own and some stolen). You find it hard to make new changes.

Then you decide to take things in your own hand. You start to look for a framework!!. After going through sourceforge.net and freashmeat you download a few of them and install them. After a few wasted days you realise all of them are total crap :-(. Not what you have imagined.

Then you think what every other serious php programmer thinks, why not my own web framework. This leads to another entry in the sourceforge and freashmeat etc. And you loose interest after some time and go back to the square one.

After some time u rediscover that frameworks can be good too:
http://stevenf.com/mt/2005/05/cake_is_good.php

Heck i cant help it, it seems to be my story. So please dont hold anyone responsible for my rants ;-).

See u soon.