Tuesday, December 8, 2009

Less is More

My professor used to say "Less is more" especially when answer questions in exams. We interpret that he does not want to read through long answers. i.e. He's just lazy. :)

But there are some quotes that talks about this as well.
It seems that perfection is reached not when there is nothing left to add, but when there is nothing left to take away. - Antoine de Saint-Exupéry, Wind, sand and stars, 1939
Everything should be made as simple as possible, but not simpler. - Albert Einstein

So...I'm reading "Getting Real" written by the guys are 37signals. And the same concept of "less is more" is highlighted again. Of course, being the people who brought us Ruby On Rails. This concept should be the center of what they do everyday.

Interestingly, I received a phishing email asking me to login to 37signals to change my account password. I immediately realized that it was a phishing email and went to the real website to warn them about it. I guess the success of your web application depends on whether hackers are trying to phish your users data.

Friday, December 4, 2009

Singapore NRIC check

There are various nric check information pages, but most of them are giving you the algo. Here is one implemented in PHP.


<?php
function check_nric($nric) {
  if ( preg_match('/^[ST][0-9]{7}[JZIHGFEDCBA]$/', $nric) ) 
  { // NRIC
    $check = "JZIHGFEDCBA";
  } else if ( preg_match('/^[FG][0-9]{7}[XWUTRQPNMLK]$/', $nric) ) 
  { // FIN
    $check = "XWUTRQPNMLK";
  } else 
  {
    return false;
  }

  $total = $nric[1]*2
    + $nric[2]*7
    + $nric[3]*6
    + $nric[4]*5
    + $nric[5]*4
    + $nric[6]*3
    + $nric[7]*2;

  if ( $nric[0] == "T" OR $nric[0] == "G" ) 
  {
    // shift 4 places for after year 2000
    $total = $total + 4; 
  }

  if ( $nric[8] == $check[$total % 11] ) {
    return TRUE;
  } else {
    return FALSE;
  }
}
?>

Tuesday, November 17, 2009

phpBB3 with youtube

To allow users to embed youtube videos in your pbpBB3 forum, do the following:

1. Go to the Administration Control Panel (ACP)

2. Click on the "Posting" tab, click "Add new BBCode".

3. In BBCode usage, enter the following:
[youtube]http://www.youtube.com/watch?v={SIMPLETEXT}{TEXT}[/youtube]

4. In HTML replace, enter the following:
<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/{SIMPLETEXT}"><param name="wmode" value="transparent"><embed src="http://www.youtube.com/v/{SIMPLETEXT}" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object>

5. In Helpline, put anything you want, I use : Embed youtube links

6. Check "Display on posting page".

7. Submit.

With this, you can embed youtube links like this:
On some sites, the solution requires you to enter only the video id e.g. [youtube]ZGx2Mu2Yaig[/youtube], which is crazy. Which user will carefully select the id and paste it? Users will just copy everything in the address bar and paste it into the forum.

You may have tried other similar solution, and wondered why those do not work. The trick is in the bbcode, we have {SIMPLETEXT}{TEXT}. Often, a user will copy a youtube link with additional parameters (e.g. feature=channel ), which these parameters are handled by the {TEXT} and we throw these parameters away when we display the youtube object on screen. We only want the video id, which is supplied by the "v" parameter.

Friday, November 6, 2009

PHP - killing long running exec process

Was writing a php script to run in commandline to process some files. Part of it is to run a 'whois' command from within PHP, but it hang (ip is 74.220.96.0). I don't know why.

Anyway, I searched and found this:
<?
exec("command & sleep 3 ; kill $!");
?>

Saturday, August 29, 2009

S.M.A.R.T.

I'm attending the PMP (Project Management Professional) course in the mornings and the NCAP (National Coaching something) in the evenings.

One thing that came up in two very different courses is the concept of S.M.A.R.T. for setting goals.

S - Specific
M - Measurable
A - Achievable
R - Relevant
T - Time bound

Thursday, August 20, 2009

Threading?

So we have to process a huge amount of data and send them to a different machine. It seems that the sending part (the actual HTTP request in the SOAP call) is slow, and it was suggested to speed things up with multiple threads.

Firstly, my knowledge of PHP is limited on threading, and secondly it seems PHP don't have sophisicated threading APIs like Java or dotNET.

So I came up with a solution that makes multiple SOAP calls to the remote server, but not using threads.

In processing the data, the table has a row id. Simply decide how many concurrent processes you want, and pick out the data to process by the modulus. e.g. if you want to have 2 concurrent processes to send out SOAP messages, pick out the data as such
where id%2=0
and
where id%2=1
Two cron jobs are created, one to call each script with the different parameter (of course, we can do this via a bash script so only one cron job is created).

So there, no race-conditions, no hung threads (ok, maybe hung process), and no headaches.

Friday, July 31, 2009

PHP Read file execution time difference

If you need to read a file into a string, you can use:

$doc = implode(file($this->attachment_file));
or
$doc = file_get_contents($this->attachment_file);

Surprisingly, the first method executes faster. I don't know why.

Wednesday, July 29, 2009

Best thing for your CentOS server

If you have a centOS server with ssh exposed on port 22, you can put in a rate limit in the iptables like this (in /etc/sysconfig/iptables):

-A RH-Firewall-1-INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --name sshattack --set
-A RH-Firewall-1-INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --name sshattack --rcheck --seconds 60 --hitcount 3 -j LOG --log-prefix "SSH RATELIMIT: "
-A RH-Firewall-1-INPUT -p tcp --dport 22 -i eth0 -m state --state NEW -m recent --name sshattack --update --seconds 60 --hitcount 3 -j DROP

Basically after 3 failed attempts, iptables will block the hacker/robot trying to access your port 22.

Friday, July 10, 2009

CSS - :hover don't work in IE

I hate IE.

Spent a lot of time trouble shooting my my li:hover does not appear in IE6. Works in FF, Opera, Chrome, IE7.

Anyway, read this:

It points to this:
http://www.xs4all.nl/~peterned/

Now my css will not pass validation. Damn it.

Edit: Ok, went back to basics of using onmouseover and onmouseout. WTF.

Saturday, March 7, 2009

Aptana with cakePHP


Aptana is a pretty good IDE (http://www.aptana.com/) based on Ecplise.

The great thing is that it works with cakePHP.

Just one thing is that cakePHP v1.2 have the views with a ".ctp" extension and Aptana does not handle the code colouring, etc. Luckily this is an easy fix:

Click Window->Preferences...

Select General->Editors->File Associations

Click the "Add.." button and add in *.ctp

*.ctp will appear under "File Types". Click on *.ctp and click the "Add.." button for Associated Editors. Select Aptana PHP Editor.

Done.

Monday, February 23, 2009

Useful Javascript

It's been a while, but I'm doing a bit of javascript again. I have to perform some validation on the user's input and with regular expression it makes the validation much cleaner. The downside is the someone else who comes along and have no idea about regular expression is going to have problem maintaining the code. Oh well.

Validating Email
function validate_email(field) {
var email = document.getElementById(field).value;
return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)
}

Validating Mobile Number
function validate_mobile(field) {
var mobile_no = document.getElementById(field).value;
return /^[89][0-9]{7}$/.test(mobile_no);
}

Clean and simple.