Friday, October 8, 2010

Codeigniter Loading Singleton

I tried many times to register at the codeigniter forum, but the captcha is not letting me through. So I'll do my update here.

From this: http://codeigniter.com/forums/viewthread/150117/
Ketama has an example to load a library that is a singleton. However, if you are on PHP 5.2 and lower, you will have a problem. You will get a "Paamayim Nekudotayim" error.


Basically, before PHP 5.3, you cannot call a static method where the class is a variable. So the line below will give you an error.
$CI->$classvar = $name::getInstance($config);

$name cannot be used to make a call to the static method of getInstance. There are two ways around this. First is to use the eval() command, but this is deemed to be up to 10 times slower (read this somewhere...no link), while the second solution of being 3 times slower seems to be the acceptable method. It is to do this:

$CI->$classvar = call_user_func_array(array($name,'getInstance'), $config);

By using the call_user_func_array() function, this will work with PHP versions older than PHP 5.3.

I've also made some minor changes to the code proposed to more suit my liking (in MY_Loader.php [CI version 1.72], subclass from CI_Loader and copy and paste the _ci_init_class() function and make the following change).
// Instantiate the class
$CI =& get_instance();
// modification start
if ($config !== NULL)
{
  if (isset($config['singleton']) ? 
    $config['singleton'] === TRUE : FALSE)
  {
    // for PHP 5.3 and above
    //$CI->$classvar = $name::getInstance($config); 
    $CI->$classvar = call_user_func_array(array($name,'getInstance'), $config);
  } else
  {
    $CI->$classvar = new $name($config);
  }//End If. Added support for singleton Libraries
} else
{
  $CI->$classvar = new $name;
}
// modification ends

To use, put the MY_Loader.php and your singletone file both into your system/applications/libraries/ directory. Then in your controller put in the following:

$this->load->library('MYLogger', array('singleton'=>TRUE));

That's all!

Thursday, February 11, 2010

Centralised Location-based Engine (CLBE)

With the new money being pumped into Wireless@SG, one of the new feature is providing LBS (Location Based Services).

The press release is here

From there you can goto the FAQ page.

So the question is that would you pay $200/mth for this?

Friday, January 22, 2010

Creating ico files

To put it simply, this don't work
http://msdn.microsoft.com/en-us/library/ms997636.aspx

This works.
http://egressive.com/tutorial/creating-a-multi-resolution-favicon-including-transparency-with-the-gimp

Case closed. Wasted 1/2 day following the official advise thinking I did something wrong.

Automating ssh, ftp, scp

A really good way to automate ssh, ftp or scp is to use "Expect" (http://expect.nist.gov/). To kick it off, just use "autoexpect" which records your key stroke into an expect script and you just need to run the expect script and it can be cron. Easy as that.

Then again, if everything is so easy, we would lose our jobs. More often than not, there is some customization required. Expect is based on tcl, so if you are familiar with tcl, you are good. If not just search for what you want to do in tcl and usually there should be someone who has already came across the problem and someone else has helped.

In my case, I need to scp all the files in one directory into a remote server. In autoexpect, I used the following command.

autoexpect scp /somedir/* user@remoteserver:/somewhere/

The problem is that the /somedir/* is expanded to list out all the files in the directory. In the generated expect file it looks like this

spawn scp /somedir/f1 /somedir/f1 /somedir/f3 user@remoteserver:/somewhere/

Now we know that the files will change so this won't work. In this case, a bit of scripting is needed:

set files [glob -directory /home/koopt/user_backups/ *]
set source ""


foreach f $files {
  set source "$source $f"
}
spawn scp $source user@remoteserver:/somewhere/

The function "glob" will list out all the files in full path and we just loop through the files to get a long list of files to scp to the remote server.

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.