Problem in Moodle 1.9 and PHP 5.3

January 2, 2012 | PHP, Technical Stuffs | ,

Today someone ask me to check his Moodle installation in his website. He used to add resource(s) easily in any course he made and lately he’s unable to add any resource. In the resource page, Moodle just displays blank page. No error message at all.

After googling around, I found that the problem is (likely) caused by PHP upgrade from 5.2.x to 5.3.x. And thanks to Andrey who posted a script for a quick hack.

The trick is by adding a new function inside HTML_QuickForm_element class in moodle/lib/pear/HTML/QuickForm/element.php.

< ?php
public function __call($name, $args) {
    $name = str_replace('MoodleQuickForm_', '', $name);
    if ($name == 'passwordunmask') {
        $name = 'password';
    return call_user_func_array(array($this, 'HTML_QuickForm_'.$name), $args);

It works like a magic!

1 comment

SquirrelMail Login Without Domain

April 29, 2011 | PHP | ,

squirrelmailI was figuring out how to login to SquirrelMail without typing domain.

I got information that I could use a plugin named Vlogin and I also need to install Compatibility plugin in order to make Vlogin working.

After some minutes enabling both plugins, I still dunno how to login without typing my domain name in Squirrelmail. I did some RTFMs but still lost in the jungle. Yes I’m a lazy man, so I decided to do a little hack on Squirrelmail source code.

The only file I need to edit is redirect.php, located in src directory. I inserted following lines of code on line 57:

< ?php
      $login_username = $login_username.'';

Here’s the screenshoot of file src/redirect.php:
squirrelmail hack

And it’s working! And login with complete username ( is still possible.

If you try this method, please remember that this quick hack has some limitations:

  • It won’t work on SquirrelMail with multi-domain/subdomain installation.
  • Function ereg()/eregi() is deprecated! Use preg_match() instead.

Last but not least, use this hack at your own risk.

1 comment

Auto Retweet Bot for Twitter

July 24, 2010 | PHP | , , , , ,

This simple tutorial will show you how to create a simple (ro)bot that will re-tweet (RT) any particular tweets related to your search results in Twitter. In this example, my bot will re-tweet any tweet that contain “wahihi” word, so I name it Kingdom of Wahihi.

Before we start, we need following requirements:

  • Twitter accountYes, the bot needs its username and password.
  • PHP interpreterYou can put this script in your own machine or any web hosting services that supports PHP.
  • Cron JobCron is an application to execute a job (application/program) in schedule. Available on mostly any web hosting services.

As replacement for Cron Job, you can manually execute the script by visiting the page (open the script using web browser).

Step one, create the bot. I’ts a PHP script, if you’re an alien and never heard of this programming language before, I recommend you to skip this page and visit my other stories shown in the sidebar.

Let’s start with declaration of username and password of your Twitter account:

$user = 'kingdomofwahihi';
$pass = 'password';

Then grab the keyword:

$search = "";
$xml_source = file_get_contents($search);
$x = simplexml_load_string($xml_source);

Above script will fetch search results of “wahihi” from Twitter’s search. The output from Twitter is in Atom (XML) format, so it’s easier for us to read the data using simplexml_load_string() function.

Next, extract the data and retweet them:

foreach($x->entry as $item){
   // part one
   $author_name = $item->author->name;
   list($name, $mbuh) = explode (" ",$author_name);
   $author = trim($name);
   $msg = 'RT @'.$author. ': ' .$item->title;

   // part two
   $out = "POST HTTP/1.1rn"
   ."Host: twitter.comrn"
   ."Authorization: Basic ".base64_encode ($user.':'.$pass)."rn"
   ."Content-type: application/x-www-form-urlencodedrn"
   ."Content-length: ".strlen ("status=$msg")."rn"
   ."Connection: Closernrn"

   // part three
   $fp = fsockopen ('', 80);
   fwrite ($fp, $out);
   fclose ($fp);

I divide above lines of code into three parts because they have different task. Part one, the bot will grab author’s name/username and what he/she tweeted then join them into one variable. And add a RT sign as a re-tweet mark.

Part two, we are preparing the variable that we will sent to Twitter’s server, it contains raw HTTP header. I know this bot is gross, but is robust 😛

Part three, open socket to Twitter’s server on port 80 then send the raw HTTP header we have prepared on part two. Now check Twitter to see if your bot is successful.

Leave any comment if you have any questions, but question about how to execute the script won’t be answered :p

Update, September 3rd:
This script is no longer working since Twitter changed its authentication method. You can try this script with OAuth authentication, written by Nazieb.


Insert French Characters Into MySQL

June 4, 2010 | PHP | , , , ,

I was developing a bi-lingual website, so my custom CMS should be able to accept and display both English and non English characters. For English characters, it’s a piece of cake. But for French characters, I have pulling my hair for days to find out what went wrong with my script.

I have set the database to use UTF-8 encoding, but every time I inserted the text it’s messed up. French characters become a horror, unreadable for everyone.

After several days –literally– I have found the solution: I must set the character set into UTF-8 right before I insert the data!

< ?php
mysql_query("SET CHARACTER SET utf8"); //<--the key!
mysql_query("insert into data values ('$french_chars')");

SET CHARACTER SET utf8 will tell MySQL to store the inserted data using UTF-8 encoding. And it works for me.


Menampilkan random foto dari Flickr

April 22, 2008 | PHP

Baru saja saya menulis script kecil untuk mengambil gambar secara acak dari account Flickr saya. Script ini membutuhkan CURL dan tidak membutuhkan library XML apapun karena RSS feed dari Flickr saya perlakukan seperti file teks biasa.

Saya memanfaatkan fungsi yang pernah saya tulis sebelumnya, yaitu fungsi untuk mengambil konten suatu URL ke dalam suatu variabel:[code][/code]

Dan yang kedua adalah fungsi yang baru saja saya tulis. Fungsi ini akan membaca RSS feed dari account saya di Flickr lalu memilah-milah dan mengeluarkan satu link gambar saja:[code]‘,”,$image);
$pics[] = trim($image);

$out = array_rand($pics);
return $pics[$out];

Cara menggunakannya sangat mudah, hanya dengan memasukkan URL dari RSS feed saja. Silahkan Anda buka halaman Flickr Anda lalu view source dari halaman tersebut. Temukan bagian berikut:[code] [/code]

Nah, link menuju RSS feed adalah Mari kita gunakan dalam sampel:


Dan outputnya adalah, tinggal gunakan dalam tag IMG saja bukan?

Sebagai curhat colongan, script di atas saya gunakan dalam mainan saya yang baru di Desainnya ngambil dari desain web saya jaman jadul, (sudah mati). Selamat bersenang-senang ^_^


Kesalahan yang tak termaafkan

April 4, 2008 | PHP

Salah satu kesalahan nenek moyang yang tidak mungkin saya maafkan adalah karena mereka tidak ngeblog menuliskan cara membuat candi sehingga saya tidak bisa membuat candi seperti mereka.


Penyederhanaan PHP Youtube downloader

March 3, 2008 | PHP

Setelah lama tidak bisa berfungsi, akhirnya youtube downloader saya bisa digunakan lagi. Secara teknis ada penyederhanaan script yang saya gunakan. Sekarang tidak perlu pusing seandainya layout halaman berubah-ubah.

Berikut ini penyederhanaannya:[code][/code]

Have phun!


PHP code highlight

February 27, 2008 | PHP

This script will highlight string inside [code] and [/code] only.

< ?php
// function to call
function highlight_code($txt){
	$hasil = preg_replace_callback('{[code]((.|n)+?)[/code]}i',"replace_code", $txt);
	return $hasil;

// main function
function replace_code($ketemu){
	 $hasil = trim($ketemu[1], "n ");
	 return highlight_string($hasil, true);

// sample of usage:
$string = "It's PHP info: [ code ]  [ /code ]";
echo highlight_code($string);

The main function is the replace_code() function that will highlight the string. But we need to return the the highlighted string back into the full strings. So we use the callback, using highlight_code() function.

Good luck ^_^

No comment

Get DPI value of an image using PHP

February 19, 2008 | PHP

This is a simple function I wrote to get a DPI (dot per inch) value from an image using PHP only, without the need of ImageMagick nor GD library. The script was inspired from denisb post (the third post). At the forum, he described how to get the DPI value stored within the file.

< ?php
function get_dpi($filename){

    // open the file and read first 20 bytes.
    $a = fopen($filename,'r');
    $string = fread($a,20);

    // get the value of byte 14th up to 18th
    $data = bin2hex(substr($string,14,4));
    $x = substr($data,0,4);
    $y = substr($data,4,4);
    return array(hexdec($x),hexdec($y));


// output the result:

I have tried this function to get DPI value of an image that I generated using Photoshop and worked as expected but failed to get DPI value from an image from any digital camera. It returned weird value instead.

Any idea why this function failed to retrieve DPI value from image that generated by digital camera? Please share your opinion.


Anti spam sederhana menggunakan PHP

February 13, 2008 | PHP

Di beberapa postingan di blog ini sering banget diserang sepam. Salah satu ciri utama sepam tersebut adalah menuliskan URL ke dalam form komentar, kemungkinan untuk mendapatkan backlink agar situs dia naik posisi di search engine.

Berdasar sifat sepam tersebut, saya tulis script sederhana agar komentar yang mengandung empat kata ‘http://’ atau lebih (yang biasa dituliskan dalam link) akan ditolak dan tidak dimasukkan dalam database.

die(‘mas, nyepam ya?’);
// masukkan dalam database

Adakah yang sudah mencoba cara ini dan merasakan hasilnya?