Categories
PHP

Duplicating WordPress Blog: “The Hasin Vai” Method

Hasin Hayder, a ZCE and Open Source enthusiast shared a nice trick to duplicate your wordpress blog just using the simple combination of php and mod_rewrite of apache. Read his original blog post here. I am going to explain the mechanism step by step here.

The PHP Code:
Put this code inside the index.php on the target URL’s root directory.

<?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}/([dS/]+)~?;
$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;
?>

The URL REWRITING:
Put the following code inside a .htaccess file or in your apache httpd conf file.

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

Explanation:
······# The URL Rewriting portion processes the requests to index.php (with and without GET query strings) and other static directories and files (images dir and robots.txt)
······# index.php handles the incoming requests as redirected by the URL Rewriting mechanism.
······# First we define the two data source or URL.

<?php
$dataurl = $primaryurl = ?http://hasin.wordpress.com?;//old domain
$secondaryurl = ?http://blog.ofhas.in?; //new domain
These two lines define the URLs involved. The primary URL is the URL where your wordpress blog is really hosted. The secondary url is the URL of the new location where the blog will show up

······# We sort out the path to the data to display and then copy the data as string.

$path =array_keys($_GET);
if(!empty($path[0])) $dataurl = ?{$primaryurl}/{$path[0]}?;
$data = file_get_contents($dataurl);

······# We then use Regular Expression to change all referrences to our primary URL so that they point to our new location.

$pattern = ?~{$primaryurl}/([dS/]+)~?;
$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);
Here we change the URLs and Form Targets. But what about Images with relative URL? I think there’s something wrong with that. I will ask Hasin Vai.

······# Lets print out the modofied data. Close the php block.

echo $data;
?>

That’s all. Put the php code in index.php and the Rewriting Rules in a .htaccess. Place the files in your public_html directory to fire off the ground.

PS: I haven’t yet tested the codes myself. But I believe it would work except for relative image URL.

Categories
Uncategorized

Scripting with C

Normally scripting refers to the interpreted languages. You write a script that gets interpreted during runtime by interpreters. Python, PHP and Perl are some of the prominent scripting languages. On the other hand, with compiled languages the source code is first compiled then linked to related libraries and finally built into executables or other runtime components. C is a very good and well known example of compiled languages.

Interpreted languages are easy to debug because of their interpreted nature because code execution stops only when it encounters a bug. On the other side, with compiled languages the entire source is compiled before execution. Again, the same problem goes with prototyping. You make a change and go through the usual compile-link-build cycle over and over to reflect the changes. But with interpreted languages, changes in your scripts are immediately reflected in the next run.

C being a compiled language hardly supports scripting. Note that word, yeah, “hardly”, because we can script with C. How? Read on.

C source codes need to be compiled using compilers. You will find a good enough numbers of them to confuse you to decide which one to use. The GCC (GNU C Compiler) is one of the most popular. But the magic I am describing can be performed with a GCC like compiler that is somthing more than cool. The compiler is named TinyCC (aka TCC). It’s a hyper fast compiler. You can use it like a C interpreter to execute C codes on the fly like a script.

EXAMPLE:

How to Run?:

  1. Save the file as “hello.c”.
  2. Make sure TCC is added to your path.
  3. Run the command line tool and navigate it to the directory where hello.c is saved.
  4. Invoke TCC by typing: tcc -run hello.c.

Output: Hello World!

#!/usr/local/bin/tcc -run: Yeah, you can use it like a shell script as well (on linux).

The TCC compiler is so fast that you don’t even need to create makefiles for large projects. TCC has it’s own linker and assembler. TCC not only supports ANSI C, but also most of the new ISO C99 standard and many GNUC extensions including inline assembly. TCC can also automatically generate memory and bound checks while allowing all C pointers operations. TCC can do these checks even if non patched libraries are used. With libtcc, you can use TCC as a backend for dynamic code generation.

TCC mainly supports the i386 target on Linux and Windows. There are alpha ports for the ARM (arm-tcc) and the TMS320C67xx targets (c67-tcc). More information about the ARM port is available at http://lists.gnu.org/archive/html/tinycc-devel/2003-10/msg00044.html. But even after all these features, TCC is really small.

I am really enjoying my time with this “tiny” yet great tool 🙂

I would recommend all C programmers to give it a try 😀

Download Link: TCC Website

Categories
Personal

Khulna Zilla School: Torch Of Education

I wrote this article on the magazine published to celebrate the 120 years of Khulna Zilla School……..
=========================
Khulna Zilla School : Torch of Education

It was the year 1885 when the then English rulers established a school just by the river Rupsha. The area was considered to be the heart of Khulna city at that time. The English men wanted to light a torch of education that would remove darkness of illiteracy from this area that was soon going to be an important root of riverine communication. They were right with their thoughts and that school is now one of the best schools in the southern part of the country. Last year Khulna Zilla School was considered to be the number one school under jessore board as 69 of its students achieved GPA 5.0.  Time has passed by and this school has continued to adorn the nation with some talents who have been successful in every sphere of their life. They have brought pride not only for this school but also for the country. Khulna Zilla School by now symbolizes the pride of Khulna. This school has always been a generous guide who has guided the students to perfection. There is no doubt that the successful people, coming out of this school will admit the contribution of this school to their career. And on the year 2005 this educational institution is stepping on its 120th anniversary with the best wishes from millions of its students. The torch that was kindled 120 years ago is still providing light to the forth coming generation so that they can prove their quality in the fields of life just like they have done in their student life before. Khulna Zilla School has always been an icon of pure education, art and prosperity. “Bless this school”. That’s what everyone would pray to the Almighty.

Abu Ashraf Masnun
Class : IX,   Section : A,
Morning Shift, 2004.

=========================