Monday, December 31, 2007

Facebook Profile Badge

I have set up my Facebook Profile Badge.

EA Faisal's Facebook profile

Monday, December 24, 2007

My blog feed is now viewable from Facebook

I've set my blog feed in Facebook. Now people (that translate to those tagged as friends in Facebook), can easily view my blog feed from the Facebook interface.

Wednesday, December 19, 2007

PHP: built-in function round() on 32-bit and 64-bit machines

My customer was complaining about a weird rounding problem recently. I do my development work primarily on a 32-bit machine, but the production server is a 64-bit machine. When I tested to run round(), a PHP built-in function, 32-bit machine and 64-bit machine produced different results. So here is my quick fix to properly round a floating number.

<?php

define
('ROUND_HALF_DOWN', 1);
define('ROUND_HALF_EVEN', 2);
define('ROUND_HALF_UP', 3);

/**
* Round floating point number because the built-in round() function produces
* different results on 32-bit and 64-bit machines
*
* @param float $value Floating poing value
* @param integer $prec Precision
* @param integer $rounding Rounding option
*/
function myround($value, $prec=2, $rounding=2) {
list(
$b, $f) = explode('.', (string) $value);
$b = (int) $b;
if ((
$prec - strlen($f)) > 0) {
$f *= pow(10, ($prec - strlen($f)));
}
if (
strlen($f) > $prec) {
$f1 = (int) substr($f, 0, $prec);
$f2 = (int) substr($f, $prec, 1);
$f3 = (int) substr($f, $prec-1, 1);
if (
$rounding === ROUND_HALF_DOWN ||
(
$rounding === ROUND_HALF_EVEN && (($f3 & 1) === 0))) {
$f = ($f2 >= 6) ? $f1 + 1 : $f1;
} elseif (
$rounding === ROUND_HALF_UP ||
(
$rounding === ROUND_HALF_EVEN && (($f3 & 1) === 1))) {
$f = ($f2 >= 5) ? $f1 + 1 : $f1;
}
if (
$f === pow(10, $prec)) {
++
$b;
$f = 0;
}
}
$f = sprintf("%0{$prec}d", $f);
return (float) ((string)
$b . '.' . (string) $f);
}

?>

Then some quick tests:
<?php
echo "1.35: " . myround(1.35, 2, ROUND_HALF_UP) . "\n";
echo "1.425: " . myround(1.90*0.75, 2, ROUND_HALF_UP) . "\n";
echo "1.425: " . myround(1.90*0.75, 3, ROUND_HALF_UP) . "\n";
echo "1.425: " . myround(1.90*0.75, 2, ROUND_HALF_EVEN) . "\n";
echo "1.995: " . myround(1.995, 2, ROUND_HALF_UP) . "\n";
echo "1.995: " . myround(1.995, 2, ROUND_HALF_EVEN) . "\n";
echo "1.995: " . myround(1.995, 2, ROUND_HALF_DOWN) . "\n";
echo "1.015: " . myround(1.015, 2, ROUND_HALF_UP) . "\n";
echo "1.015: " . myround(1.015, 2, ROUND_HALF_EVEN) . "\n";
echo "1.000: " . myround(1.00, 2, ROUND_HALF_UP) . "\n";
echo "4.20: " . myround(4.20, 2, ROUND_HALF_UP) . "\n";
?>

Wednesday, April 18, 2007

Linux and Mac OS X are now playing nicely

After spending 2 hours of building and configuring Netatalk and Avahi on my Linux box, my wife's MacBook finally managed to browse the files on the Linux machine. All thanks to the guide on sharing directory using AFP at Gentoo-Wiki.

Tuesday, April 17, 2007

Making Linux and Mac OS X becomes good buddies

Last Friday I helped my wife to get her a MacBook. All the basic setup such as basic networking went pretty well. The hardware performed quite impressively - the only potential problem I foresee to pose a problem would be the heat.

Next would be attempting file sharing with Linux since my wife used and stored my Linux machine before MacBook came. I saw a potential product call Netatalk which look like a good candidate to do just that. May be later tonight or tomorrow night I'll attempt to build Netatalk and play around with it.

Monday, April 09, 2007

Smoking - An Islamic Perspective

I tuned to Radio IKIM.FM while driving this morning and the DJ was already half way discussing about smoking. It was an interesting discussion with a number of callers making their comments.

So when I reach my office, I immediately look for the fatwa or Islamic edict on smoking at eFatwa portal. Below are the fatwas I found:

Fatwa by the state of Kedah:

Merokok adalah haram
Ref: http://www.e-fatwa.gov.my/mufti/fatwa_search_result.asp?keyID=281
Fatwa by the state of Perlis:
Mesyuarat telah meneliti dan berbincang dengan panjang lebar serta memutuskan bahawa menanam tembakau/merokok dan memanfaatkan hasil tembakau adalah HARAM dengan berpandukan dalil-dalil serta nas-nas al-Quran, hadis dan pendapat para ulama yang muktabar.
Ref: http://www.e-fatwa.gov.my/mufti/fatwa_search_result.asp?keyID=350
Fatwa by the state of Selangor:
(1) Merokok adalah haram

(2) Oleh yang demikian , adalah haram bagi mana-mana orang Islam menghisap apaapa jenis rokok.

(3) Bagi maksud faiwa ini , “ rokokialah tembakau yang digulung dengan kertas atau daun nipah atau selainnya.
Ref: http://www.e-fatwa.gov.my/mufti/fatwa_search_result.asp?keyID=380
Fatwa by the state of Sarawak:
Merokok adalah haram dari pandangan Islam kerana padanya terdapat kemudaratan pada kesihatan dan pembaziran pada perbelanjaan
Ref: http://www.e-fatwa.gov.my/mufti/fatwa_search_result.asp?keyID=845
In summary, the Islamic scholars and Muftis from the 4 states all agreed beyond doubt and proclaimed that smoking is forbidden, or haram.

Thursday, March 29, 2007

Linux Printing with CUPS - sucked into dependency hell

After two days of struggling, I finally got my Epson C67 printer to work again. All in the name of upgrading GIMP-Print to Gutenprint which should provide better support for the printer, especially for photo printing.

The initial build process for Gutenprint was smooth - no error what so ever. But when I tried to print - nothing happen. After much reading and Google around, I found the problem was due to dependency issues. This simply means I had to re-build my Gimp and Ghostscript to newer versions (and also I took the opportunity to re-build CUPS). Only after all those re-built I managed to print again. I just wish there is an easier way to resolve dependency in the future.

Monday, March 26, 2007

A Simple Step-By-Step Guide of SSL Key Management With OpenSSL

This is a simplified step by step guide to manage SSL key using OpenSSL. For further information do man openssl.

1. Become a root certificate authority(CA)

  • $openssl req -new -x509 -keyout ca.key -out ca.crt -days 3650
  • This will request for (-)new self-signed(-x509) root certificate with private key(-keyout) named "ca.key") and certificate file(-out) named "ca.crt" which is certified for 10 years(-days 3650).

2. View the contents of the certificate
  • $openssl x509 -in ca.crt -noout -text
  • $openssl x509 -in ca.crt -noout -dates
  • $openssl x509 -in ca.crt -noout -purpose

3. Create the certificate signing request
  • This request is actually generated by those who want their certificate to be signed by root CA
  • $openssl req -new -nodes -keyout my.key -out my.csr -days 365
  • If paranoid, remove -nodes option to make the private key encrypted and password protected.

4. Signing a certificate
  • $openssl ca -out my.crt -in my.csr
  • Optionally, remove the human-readable portions of the certificate :
    • $openssl x509 -in my.crt -out my-nohuman-readable.crt

5. Deployment of certificates
  • The following certificates my.key, my.crt and ca.crt are needed by application using SSL.
  • Some applications require both the key and certificate in one file, which can be achived by running:
    • $cat server.key server.crt > key-cert.pem
  • For applications using SSL/TLS, the Diffie Hellman parameters may be required on the server side. Create the DH (using 2048 bits) by issuing:
    • $openssl dhparam -out dh2048.pem 2048

6. Certification Revocation List (CRL)
  • $openssl ca -gencrl -crldays 30 -out rootca.crl

7. Renewing and revoking certificates
  • Both root certificate and other signed certificates are subjected to expiry.
  • If root certificate expires, a new root CA certificate must be created and distributed. All other certificate that it signed must also be re-created and signed.
  • For other certificates, those certificates can be renewed by first revoking the old certificate, then re-signed the original request or do another round of request and sign procedure. To revoke a certificate, issue the following command:
    • $openssl ca -revoke expire.crt
  • Regenerate CRL again if necessary.

Tuesday, March 20, 2007

Wow! del.icio.us

Just open an account at del.icio.us :) It's a rather late entry into social bookmarking. I already formed an initial impression of social bookmarking - it's pretty cool.

You can view my bookmarks at http://del.icio.us/efaisal

Wednesday, March 14, 2007

Posting source code

I not really satisfied the way source code is being displayed. Currently I'm just putting the <pre> tag around the code. No fancy formatting, just plain <pre>. So, is there a better way to format source code? May be use <pre> tag with some cool CSS hacks?

Scheduleable Event Loop for asyncore/asynchat

Been a while since I write a blog. Below is the Python code which is quite usable to replace the default asyncore loop. It's based on Sam Rushing code which can be found at http://squirl.nightmare.com/medusa/async_sockets.html

import sys, time, asyncore

map = asyncore.socket_map

class EventLoop:
def __init__(self):
self.events = {}
self.poll = asyncore.poll3
self.__quit = None

def loop(self, timeout=30.0):
while map:
if self.__quit is True: break
now = int(time.time())
for k, v in self.events.iteritems():
if now >= k:
for o in v:
try:
o[0](*o[1][0], **o[1][1])
except:
type, value, tb = sys.exc_info()
parm = str(o[1])
logstr = "Error in Evenloop processing " + \
"scheduled task."
logstr = "Error details: %s: %s" % (type, value)
logstr = "Error details(parm): %s" % parm
logstr = "Error details(tb): %s" % \
tb.tb_frame.f_code
del self.events[k]
self.poll(timeout)

def schedule(self, delta, callback, *args, **kargs):
now = int(time.time())
if self.events.has_key(now + delta) is False:
self.events[now + delta] = []
self.events[now + delta].append((callback, (args, kargs)))

def unschedule(self, callback):
for k, v in self.events.iteritems():
for o in v:
if o[0] is callback:
del self.events[k][v][self.events[k][v].index(o)]
if len(v) == 0:
del self.events[k]

def quit(self):
self.__quit = True