Category: Cool Webapps

host your <wordpress.com> blog anywhere, using this 10 lines of proxy script

q1. why the hell someone would do that?
a1. if you bought some interesting domain like i do (heh heh) http://blog.ofhas.in and want your existing blog to be available there as well, without spending hours to move data and to give it same look-n-feel – here’s your time-saving wizard. and beside that, i also dont want to lose my existing blog but i just want to avoid the hassle of re-hosting/migrating 🙂

q2. oh really?? how can I do that?
a2. here you go

<?php
$dataurl =$primaryurl = "http://hasin.wordpress.com";//old domain
$secondaryurl = "http://blog.ofhas.in"; //new domain
$path =array_keys($_GET);
if(!empty($path[0])) $dataurl = "{$primaryurl}/{$path[0]}";
$data = file_get_contents($dataurl);
$pattern = "~{$primaryurl}/([\d\S\/]+)~";
$data = preg_replace($pattern,"{$secondaryurl}/$1",$data);
$data = str_replace(array("<a href=\"{$primaryurl}","<form action=\"{$secondaryurl}"),array("<a href=\"{$secondaryurl}","<form action=\"{$primaryurl}"),$data);
echo $data;
?>

q3. is that all?
a3. nope, you need to modify/write .htaccess file in the root of this new domain


RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.+)
RewriteRule ^(.*)$ index.php?$1&%{QUERY_STRING}


RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

this script will also change the urls of all your post and pagination, behaves exactly same as you have hosted your blog in this new url. only drawback is submitting a comment will take you to your original domain, but you can easily fix that but simulating a POST request using curl/stream wrapper/fopen. there is one more vital drawback which is you may lose some IP specific traffic data

have a look at my actual blog http://hasin.wordpress.com and new one at http://blog.ofhas.in

happy weekend!

wrapper for google static map api

google maps is one of the very best mapping services available these days. using their api you can embed interactive maps of different types in your web pages. and there are many plugins to make your life easier to embed such maps in your web pages and applications.

to embed interactive google map, you need to include some external javascript files first. now when you are using some hosted solutions like wordpress, they may not allow you to include external javascript files in your blog/pages for different security pre-cautions. this is where google static map api comes handy.

google static maps api takes latitude and longitude of some places as parameter, along with other optional formatting params and delivers a static image in your expected image format (default is gif, but you can choose among gif, png and jpeg). so you dont need to bother about permission of including any other javascript files in your pages and you can embed such static maps by specifying the url as a “src” attribute of <img> tag.

there is still one problem that might knock you down. for example if you want to display “dhaka”, the capital or bangladesh in your static map, you need to know it’s latitude and longitude. so not everyone of us speaks in a language of ‘latitude’ and ‘longitude’ but onle the name of the places. like we all know about “university of berkeley” but only very few can figure out it’s lat-lon or they have to find it out from existing geocoding services.

yesterday night i was studying about this static map api and decided to build a geocoding wrapper so that you can pass only the name of a place and my wrapper class will convert it to latitude and longitude using geociding services and then generate a static map image for you. the whole wrapper will work around two objects – geocoding service and google static map api. for geocoding i prefer to use google because of it’s amazing precision but i must say that yahoo’s geocoding service is also pretty good.

here is my wrapper class and i am sure you can find it useful. now you can use it to embed google map to anywhere you would like to :). this class is released under LGPL.

there is a sample usage file (gmap.php which you will make use of) and a source object “staticgmap.php”. this class became a part of orchid framework and prothom alo blog. if you enjoy/use this class, dont forget to drop a line here in this post. the class and usage file is moderately commented, so you will not face any problem to understand. however the usage file is also pretty fine working.

please download the class from this page
download: http://svnpg.net/gmap/

supported features
1. geocoding service, both yahoo and google (default is yahoo)
2. support of zooming (0-19, default is 12)
3. support of image format (gif, jpeg and png. default is GIF)
4. maptype support (roadmap, satellite, mobile, hybrid and terrain where default is roadmap)
5. support of markers (just as plain location name)
6. variable dimension support (default size is 400×400)

sample maps

locate dhaka and moti jheel
http://svnpg.net/gmap/gmap.php?location=dhaka&markers=dhaka,moti jheel
dhaka and motijheel marked on a static google map

locate berkeley and emeryvlle
http://svnpg.net/gmap/gmap.php?location=berkeley&markers=berkeley,emeryville&zoom=11
berkeley and emeryville marked on google static map


place where i live – uttara 14 with custom zoom level and custom size

http://svnpg.net/gmap/gmap.php?location=uttara 14&zoom=16&size=400×300

uttara 14

Web scrapping in a smart way, making a "Today in History" object in PHP

There are thousands of services available on web who are presenting interesting as well as education information which you can really integrate in your web page or make a nice widget and let others use them seamlessly with their content delivery platforms. In this article I am going to show you how you can make a nice Today-in-History widget with the help of the data provided in Scopesys. You can use this code to make a nice widget or a trivia app or whatever. But before making your own scrappers from any services, please please please carefully note the copyright of that content. You shouldn’t violate copyright either way.

In this widget, we will strip the following content from the pages provided by scopesys and display them in different categories.
1. Today in history
2. Who’s born today
3. Who’s died today
4. Where is holiday today
5. Religious observance of today
6. Religious history of today

Lets go 😀


<?php
//todayinhistory.php
error_reporting(0);
define("MARKER_START","<H3>On this day...</h3>");
define("MARKER_END","<BR><BR><HR><h3>Holidays</h3>");
define("BIRTHDAY_START","</font></center></center>");
define("BIRTHDAY_END","<HR> <br><H3>Deaths which occurred on ".date("F d").":</H3>");
define("DEATH_START","<HR> <br><H3>Deaths which occurred on ".date("F d").":</H3>");
define("DEATH_END","<HR><IMG align=left SRC=\"http://www.scopesys.com/flag.gif\">");
define("HOLIDAYS_START",'<i>Note: Some Holidays are only applicable on a given <b>"day of the week"</b></i><br> <br>');
define("HOLIDAYS_END","<HR> <H3>Religious Observances</H3>");
define("RELIGIOUS_START","<HR> <H3>Religious Observances</H3>");
define("RELIGIOUS_END","<HR> <H3>Religious History </h3>");
define("RELHISTORY_START","<HR> <H3>Religious History </h3>");
define("RELHISTORY_END","<BR><BR><font color=red>");


echo "<h2>Today is ".Date("F d, Y")."</h2>";
$data = file_get_contents("http://www.scopesys.com/today");


if ($_GET['history']=='1'){
echo "<br/><h2 style='color: green' >Today in history</h2>";
$end = strpos($data,MARKER_END)-15;
$start = strpos($data,MARKER_START)+strlen(MARKER_START);
echo substr($data,$start,$end-$start);
}


if ($_GET['born']=='1'){
echo "<br/><h2 style='color: green' >Who's born today</h2>";
$end = strpos($data,BIRTHDAY_END);
$start = strpos($data,BIRTHDAY_START)+strlen(BIRTHDAY_START);
echo substr($data,$start,$end-$start);
}


if ($_GET['died']=='1'){
echo "<br/><h2 style='color: green' >Who died today</h2>";
$end = strpos($data,DEATH_END);
$start = strpos($data,DEATH_START)+strlen(DEATH_START);
echo substr($data,$start,$end-$start);
}


if ($_GET['holiday']=='1'){
echo "<br/><h2 style='color: green' >Where is holiday today</h2>";
$end = strpos($data,HOLIDAYS_END);
$start = strpos($data,HOLIDAYS_START)+strlen(HOLIDAYS_START);
echo substr($data,$start,$end-$start);
}


if ($_GET['religious']=='1'){
echo "<br/><h2 style='color: green' >Religious observance</h2>";
$end = strpos($data,RELIGIOUS_END);
$start = strpos($data,RELIGIOUS_START)+strlen(RELIGIOUS_START);
echo substr($data,$start,$end-$start);
}


if ($_GET['relhistory']=='1'){
echo "<br/><h2 style='color: green' >Religious history</h2>";
$end = strpos($data,RELHISTORY_END);
$start = strpos($data,RELHISTORY_START)+strlen(RELHISTORY_START);
echo substr($data,$start,$end-$start);
}
?>

Now if you want to find who born today, point your browser to todayinhistory.php?born=1. Mashup Mashup Mashup, that is what many successful web app are doing these days. And sometime this is how data collection is done behind the scene 🙂

Writing this code was really enjoyable as getting root canal done in your teeth with a rusty drill (I forgot where I’ve read such a nice quote), heh heh. But I am sure, you will enjoy it more than that 😉 – happy scrapping.

Making a jobsite using PHP

I was involved in making a job site few days ago. During the development, I have studied how easily anyone can develop a job site using PHP (language independent in true sense) . So I decide to write a blog post about my experience and here it goes. But note that this article is not about scaling or balancing the load on your site during heavy traffic, heh heh.

When you start making a job site, the most complex thing that I’ve found is making the resume-submission-system. Specially the resume editor is more complex than most other parts of a job site. Numbers of sub sections makes it really “not-so-lovely” to code . I am not going to foucs on resume-editor in this article but want to help you on “how easily you can create you can make/run a job site by your own”.

If you want a ready made job-board software which you can just plugin easily under your domain (zero effort, seriously) you can go for “job-a-matic” which is just register-setup-run type one. Please note that it doesn’t come with source code. Its just a customizable solution which you can run under your domain name.

If you love challenge but still want a ready made one with source code then you can go for “Jobberbase“. It is written in pure PHP and uses MySQL in back end. It comes with nice user interface and (more…)

We are close to release Orchid, the new Framework

orchid framework

Am I talking about another framework? Well yeah. I thought it will be real fun for web application developers to use orchid to build their apps. Orchid is still in preview state and we are planning to release it by the mid of January. But if you are interested you can check the orchid blog and checkout the nightly build version from svn repository. Orchid is very fast and lightweight and really painless to kickstart the development.

URL : http://orchid.phpxperts.net

Some of the core features of Orchid which I think are really helpful to develop real world applications, are listed here

1. Caching Engine with support of Memcache, MySQL and SQLite as Storage
2. MVC
3. RoR like Layout and segregated template components.
4. Partial Caching in Template
5. Very efficient object loading and dispatching
6. DAL for MySQL (Both MySQL and MySQLi), SQLite, PostgreSQL and MSSQL
7. Support for PDO
8. Builtin Unit Testing Library where you can write unit tests inside the controller without extra pain.
9. Internationalization using Language files
10. Session Manager (Regular and DB Based)
11. Bundled Google Map Library
12. Bundled Google Chart Library
13. Bundled JSON Library
14. Bundled Prototype, jQuery, Scriptaculous and SWFObject Scripts with on-demand gzip support
15. On demand gzipping for javascripts
16. Active Record
17. Excellent Benchmarking and Profiling support which you can use to profile your application
18. CSS based Button Manager (What is this???? — checkout)
19. AJAX Library

Beside many other features it also comes with an exceptional feature which is hard to find elsewhere 🙂 which is the following one

20. Bundled “Colorful Moments”

🙂

Orchid is planned to release by mid januray, as I told. Currently some of us are working on documentation. But you can still check out the code and sample controllers.

Ifthese sounds interesting – you can visit the official blog of orchid at http://orchid.phpxperts.net

List of RSS Feeds I read almost everyday

I am sharing a list of RSS feeds that I read almost everyday. And which one is my favorites RSS reader? Well, I use GoogleReader because of it’s excellent features like star and feed history. also I like it’s feed sharing feature.

1. Ajaxian : http://www.ajaxian.com/index.xml
2. Cow’s Blog : http://cow.neondragon.net/xml.php
3. IBM Developer Works (Web) : http://www.ibm.com/developerworks/views/web/rss/libraryview.jsp
4. IBM Developer Works (Open Source) : http://www.ibm.com/developerworks/views/opensource/rss/libraryview.jsp
5. Designer Folio : http://feeds.feedburner.com/dezinerfolio
6. Digg Technology : http://digg.com/rss/containertechnology.xml
7. DZone Latest Front Page Links : http://www.dzone.com/feed/frontpage/rss.xml
8. Freelance Switch : http://feeds.feedburner.com/FreelanceSwitch
9. HacksZine : http://hackszine.com/index.xml
10. International PHP Magazine News : http://www.php-mag.net/magphpde/psecom,id,26,noeid,26,.html
11. JSLabs High Performance Web Apps : http://feeds.feedburner.com/jaslabs
12. LifeHack : http://www.lifehack.org/feed/
13. Mashable : http://feeds.feedburner.com/Mashable
14. Maxdesign : http://www.maxdesign.com.au/feed/
15. Newsvine (PHP) : http://www.newsvine.com/_feeds/rss2/tag?id=php&d=v
16. PHP Freaks : http://www.phpfreaks.com/feeds/articles.xml
17. PHP Magazine : http://www.phpmagazine.net/syndicate.xml
18. PHP Coding Practise: http://php-coding-practices.com/feed/
19. PHP Developer : http://phpdeveloper.org/phpdev.rdf
20. PHP Geek : http://www.phpgeek.com/wordpress/feed
21. PHP Architct News : http://www.phparch.com/phpa.rss
22. Planet Ajaxian : http://planet.ajaxian.com/index.xml
23. Planet PHP : http://planet-php.org/rdf/
24. Programmable Web : http://feeds.feedburner.com/ProgrammableWeb
25. ROScripts : http://feeds.feedburner.com/ArticlesAndProgrammingTutorials
26. Sitepoint Blogs : http://www.sitepoint.com/blogs/feed/
27. Sitepoint News : http://www.sitepoint.com/recent.rdf
28. Smashing Magazine : http://www.smashingmagazine.com/wp-rss.php
29. Jonathan Snooks Blog : http://snook.ca/jonathan/index.rdf
30. TechCrunch : http://feeds.feedburner.com/Techcrunch
31. Technorati Javascript Links : http://feeds.technorati.com/search/Javascript
32. Technorati PHP Links: http://feeds.technorati.com/search/PHP
33. Veerle’s Blog : http://feeds.feedburner.com/veerlesblog
34. Web2List : http://feeds.feedburner.com/Web2list
35. Zen Habits : http://feeds.feedburner.com/zenhabits
36. Del.icio.us PHP Tags : http://del.icio.us/rss/tag/php
37. PHPExperts Forum : http://rss.groups.yahoo.com/group/phpexperts/rss
38. 456 Berea Street : http://www.456bereastreet.com/feed.xml
39. Particle Tree : http://feeds.feedburner.com/particletree
40. Simple Bits : http://simplebits.com/xml/rss.xml

Prelude to foundation: Its time to go for a better PHP Framework

I remember those old days when I had to write everything by myself. I wrote huge libraries to work with MySQL. Then I learned PostgreSQL and SQLite but didn’t rewrote my old library to work with those, I was running short of time. So I forsake the opportunity to write a db library which works with them. What I did was plain code relevant to database specific portions. Oh ya, that was a long time ago.

Soon after that I came to know adoDB which made my dream come true. I was so much happy getting my all db specific works done in a much more smarter way. I get rid of database portability issues. I was very happy that time.

I learned smarty soon after I realize that my codes are getting ugly with all the inline HTMLs and PHPs. Nothing could be smarter than separating the presentation logic from the business layer. I am a big smarty fan since that time. It saves my sleep for many nights.

But again I am recurrently suffering from maintainability issues. I was not surprised to find that my code is becoming huge unmanageable giant and it takes huge time for refactoring the application. I was very sad those days. Oh what a disaster that was.

When working with my team members located remote places, I fall into a deep shit. How can we manage and track the changes done by us? Even I was getting strange code in my routine which I bet was not written by me!! It was a terrific job (more…)

Vulnerable bug in CodeIgniter which took us hours to fix our corrupted database

We use codeigniter internally to develop our web solutions in somewhere in… net limited. Day before yesterday we suffered a terrible situation for an internal bug in code igniter which corrupted data inside some tables of our application database and then it tooks hours to find the origin of that bug, to fix it and to repair the corrupted data. Let me explain what happened.

Lets guess that we have one table named “users” with the following field

1. user_id
2. username
3. password
4. email

At some point, if you want to update the password field of this table, for a particular user, what will you write in your code?


$data = array("password"=>$new_password);
$this->db->where("user_id",$user_id);
$this->db->update("users", $data);

CodeIgniter’s ActiveRecord creates the query like the following

UPDATE users set password='{$new_password}’ where user_id='{$user_id}’;

Well, it’s ok and the quesry seems pretty fine. Now what should happen if you pass a valid user id to this code? Password of only that user will be updated. But what will happen when the passed $user_id is null?? Thats the most pathetic part that Codeigniter ActiveRecord plays. Instead of generating the following query,

UPDATE users set password='{$new_password}’ where user_id=”;

CodeIgniter’s ORM actually generates the following

UPDATE users set password='{$new_password}’ where user_id;

You find the difference of the above two queries right? one contains “where user_id=” ” and another contains just “where user_id” . Now if your backend database is MySQL and this query executes? You know what the hell will happen? It will replace all the user’s password with this new password instead of failing as MySQL count the “where user_id” part equals to false and returns all users. But If your Database is PostgreSQL, it fails, you are lucky.

So day before yesterday we suffered this problem against our commercial application which corrupts our user profile data. We immediate fixed the issue from our backup db (well, we lost 3 data) and then we started to find out what actually went wrong and found this vulnerable bug in CI.

So we suggest the CodeIgniter team to fix the issue immediately and change their ORM code so that it creates the query like the following if the value of passed argument is null. because it will fail to execute in all db. Otherwise the fellow user’s of code igniter, prepare for the dooms day.

UPDATE users set password='{$new_password}’ where user_id=”;

WordPress Blogrolls Importer – Opensource

Today I developed this tool to import wordpress blog rolls as XML document. You know when you export data from wordpress.com that doesn’t include the blog rolls data. So if you want to keep a backup of your blog rolls, you can use this tool to import your blog rolls data.. This one is developed using PHP and Curl

importer.gif

You can doewnload it and see the code in action here

Opensource WordPress Blogrolls Importer

################

The trick is lying here —
;

    

    $ch curl_init();

    
curl_setopt($chCURLOPT_COOKIEJAR“./login.jar”);

    

    

    
curl_setopt($chCURLOPT_RETURNTRANSFER,1);

    //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  // this line makes it work under https

    
curl_setopt($chCURLOPT_URL,“{$url}/wp-login.php”);

    curl_setopt($chCURLOPT_POST1);

    
curl_setopt($chCURLOPT_FOLLOWLOCATION,1);

    
curl_setopt($chCURLOPT_POSTFIELDS“log={$username}&pwd={$password}&&redirect_to=wp-admin/link-manager.php&wp_submit=”);

    

    $buffercurl_exec ($ch); // execute the curl command