Ehsanul Haque

Welcome to my personal site & blog

Archive for the 'Codes' Category

12 January
0Comments

Grab Yahoo class is in “coma”

Grab Yahoo class was one of my works that has been used by many people around the world. Since I released this class I did  not have a single month without comments or questions or suggestion about this class. Some people have copied the class, released it under their name (simply taking out my comments and added their own) to increase traffic to their site probably. But more importantly this class was appreciated by many. This was one of my finest contribution to the open source world.

How did I come up with this idea?

Well, I cannot remember which user group but in one of the groups I participate in had a discussion over if PHP can pull data off from places like Yahoo and use it for some purpose. Users on the group said this can be done (easily) with Java, but cannot be done with PHP. So I started researching over it. I found the cURL extension for PHP which seemed the good candidate to do the job. So I started working on it. After days of coding I finally wrote a script that will allow someone to login to their Yahoo account and fetch data from their address book. Initially I simply had a script but then I converted it to a Class allowing people to grab their address book, messenger list, number of new emails and calendar data.

What is happening now?

As per the title of this post this class is in “coma”. The reason is the update to Yahoo address book export mechanism. They have placed a CAPTCHA validation page in the export feature and, therefore, my Grab Yahoo class cannot go any further to grab the content it needs. Same thing happened with the LinkMeIn class which stopped working after LinkedIn added the CAPTCHA validation.

Why is this class in “coma”?

I have looked at the Yahoo address book export feature and found the CAPTCHA validation which will not allow the class to work. But I’ve not done my complete research on it yet, to make sure there is no other way to make this class functional. So, for the time being, this class is in “coma”. If I fail to revive this class I will probably officially pronounce it “dead”.

Can you help?

Yes, of course, you can. I hardly have time, these days, to sit with these side projects. If anyone from the community has time to research and help me revive this class it will be appreciated. Your name will go into the credit section of the class, well that is all I can offer!

16 September
3Comments

Wordpress Plugin: Recently Updated Pages

Today I’ve released a Wordpress plugin called “Recently Updated Pages” which is a Wordpress Widget that displays your recently updated page list on the sidebar.

I was looking for a similar plugin but after searching for a while I wanted to write my own plugin. I followed some instructions on couple of websites and created this plugin. It uses WP_Widget class from Wordpress core and extends it to build the widget. You can download it form Wordpress Plugin page at http://wordpress.org/extend/plugins/recently-updated-pages/

In case if you want to make a small donation you could do so at http://resource.bdwebwork.com/WordpressPlugins/RecentlyUpdatedPages/

Thanks

UPDATE AS OF SEPTEMBER 17, 2009:

I’ve updated the plugin and committed the latest version (1.0.1) to Wordpress plugin directory. The updated version will allow users to include list of blog Posts in the Recently Updated list. Originally the plugin only displayed list of updated Pages and now with this optional choice you can toggle between the option.

13 March
1Comment

“sed”: Find and Replace Command on Linux Command Line

Sometimes we use IDE to find and replace strings in our file(s). IDEs have features to search for a keyword in all the files in a directory and replace with a given string. When you’re on Linux and using command line you can easily do this using the command “sed”.

According to the linux manual (man sed) “sed” is a stream editor which is used to perform basic text transformations on an input stream. With this command you can efficiently find and replace text in one or multiple files or you can keep the original file and make a new file with the replacement text.

To find and replace text in the original file the command is:

sed -i 's/find/replace/g' original.php

In the command above, “-i” option instructs to edit file(s) in place. “s” command attempts to match the pattern space against the supplied “find” string (which is a regular expression pattern). If the match is successful, then that portion of the pattern space which was matched is replaced with “replace” text. “g” is used to specify a global change, i.e. the string will be changed everywhere in the file(s) where it finds them. As mentioned earlier the “find” string is basically a regular expression pattern, and the replacement may contain special character (&) or escapes (\1 through \9) to refer to the corresponding matching sub-expressions in the “find” regular expression.

To find text in a file and output to a different file with the replacement the command is:

sed 's/find/replace/g' original.php > replaced.php

In the last command if you do not specify the output filename, it will simply show the file content on the screen with replaced string. There’s another command (similar to “sed”) which can be used for the same purpose is from perl.

perl -pi -e 's/find/replace/g' original.php

This command will find the word “find” and replace with the word “replace” in original.php. For this command “-p” option assume a loop like “while (<>) { …. }” (which is similar to the option -n but it prints the lines as well). The following option “i” performs the same task as it does for “sed”. Then “-e” says anything after it is a online of program. According to the manual several “-e program” can be used.

31 December
2Comments

Inanis Glass theme for Wordpress: Set the clock to 24hr format

I have installed Inanis Glass theme (v. 1.2) for my Wordpress blog and it is really an amazing theme. It was very easy to install (unlike other themes where something is always missing or not fitting). I made small changes here and there to make it work the way I want. For example, it showed a message on the pages or articles where I turned off the comments that the comments are off etc. I modified it to remove that. Made few more CSS changes.

When I visited the author’s website I found few requests to show the clock in 24hrs format. When I looked at the javascript (functions.js) for the theme I found that the “clock” function is compressed by http://javascriptcompressor.com/. When I visited that site to see if they have any decompressor then I found a contribution on their forum which unpacks/decompresses the javascript – read about it on their forum http://javascriptcompressor.com/forums/p/9/42.aspx – and decompressed the “clock” function.

Decompressed/unpacked function looks like:

function init() {
   timeDisplay=document.createTextNode("");
   document.getElementById("clockhr").appendChild(timeDisplay);
   timeDisplay1=document.createTextNode("");
   document.getElementById("clockmin").appendChild(timeDisplay1);
   timeDisplay2=document.createTextNode("");
   document.getElementById("clockpart").appendChild(timeDisplay2)
}
function updateClock() {
   var currentTime=new Date();
   var currentHours=currentTime.getHours();
   var currentMinutes=currentTime.getMinutes();
   currentMinutes=(currentMinutes<10?"0":"")+currentMinutes;
   var timeOfDay=(currentHours<12)?"AM":"PM";
   currentHours=(currentHours>12)?currentHours-12:currentHours;
   currentHours=(currentHours===0)?12:currentHours;
   document.getElementById("clockhr").firstChild.nodeValue=currentHours;
   document.getElementById("clockmin").firstChild.nodeValue=currentMinutes;
   document.getElementById("clockpart").firstChild.nodeValue=timeOfDay
}

Basically the Javascript function used to find the current time – getHours() – returns the hour in 24hrs format. But to display the clock in 12hrs format the author has done some math. So if anyone wants to display 24hrs format clock comment out line number 14, 15, 16 and 19. To replace the function you can use the site above to re-compress the function or comment the existing function in functions.js file (clock function) and paste this decompressed version in the file.

Note: As I understand javascriptcompressor.com is used to pack/compress your javascript, not to encrypt it or hide it by any means. It is compressed so that it is either unreadable or smaller in size.

05 October
3Comments

PHP Function I frquently use

While developing the web applications with PHP I have made my own small function library. In this library I have few functions that I use very frequently. One of the functions that is used more often is echo_d(). The purpose of this function is to check a variable to see if it is empty or not set; then based on the check result it either prints a default value or the value of the variable.

This is a simple function that saves me a lot of time and keeps my main codes cleaner. So here it is.

function echo_d($var, $def) {
  $var = trim($var);
  echo ((isset($var)) && (!empty($var))) ? $var : $def;
}

When to use this function? One of the most common places I use this function is in the reports. When I am generating reports for anything I always have to check whether certain value is in the database; it prints a default value if it cannot find one. The usage of this function in the scenario is something like:

$lastLoginDate = $rowsFromDB['last_login_date'];
echo_d($lastLoginDate, "---");

This function can be expanded to make it more versatile but I have found this tiny little function very useful.


Thank you.

20 December
14Comments

Javascript: Select Multiple Dropdown Items

If you would like to have a multiple select dropdown and want to use the selection in a specified format, then here is the solution. Sometimes you may need to create a query string value or fill in a textbox or textarea with the selected values. Say for example you have a dropdown list where you can select multiple items. Upon selecting each option you want to fill in a textarea with the selected value.

Example:


HTML Part:

<form method=POST name='testing'>
  <select name='testsel' multiple onchange='showselection()'>
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
  </select>
  <textarea id="txtEditions"></textarea>
</form>

Javascript Part:

<script language='javascript'>
  function showselection()
  {
    var frm = document.testing
    var opt = frm.testsel

    var numofoptions = opt.length
    var selValue = new Array

    var j = 0
    for (i=0; i<numofoptions; i++)
    {
      if (opt[i].selected === true)
      {
        selValue[j] = opt[i].value
        j++
      }
    }

    selValue = selValue.join("+")

    document.getElementById("txtEditions").innerHTML = selValue
  }
</script>