Contributing Open Source Arabization Features
Talk Description and Slides
Open source is the future and is a main driver for development on many levels including but not limited to technical, social, and economic development. In our part of the world, we’re not playing an important role in open source, we’re not benefiting from it to the maximum, and Arabized features are still lacking in many areas. My lightning talk will hopefully persuade developers to take action, contribute more and more to open source, and assist in taking the whole ecosystem to the next level.
Miscellaneous Tips and Resources
WordPress does not support a bilingual or multilingual blog out-of-the-box. There are however plugins developed by the WordPress community which will allow you to create a multilingual blog easily. The bad news is that relying on 3rd party plugins is not a very good idea. Sometimes those plugins don’t get supported anymore. Sometimes they have bugs or security issues. More info can be found here: http://codex.wordpress.org/Multilingual_WordPress
Drupal supports multilingual blogs and other types of websites/content out-of-the-box using the core content translation module. However, configuring this module is not easy. The good news is that you can build a solid solution with an easy-to-use interface for multilingual websites. More info here: http://drupal.org/documentation/modules/translation
In PHP, displaying Arabic numerals from 1 to 10 for example is not straightforward. You can’t just right a “for” loop and output the numbers. You have to loop with a counter and then transform that counter number from English to Arabic using this PHP class for example: http://www.phpclasses.org/package/6626
PHP’s unicode support is lacking for now. If you want to process Arabic characters and strings, you will have to rely on Multibyte String Functions – http://php.net/manual/en/ref.mbstring.php – and if you don’t you’re in for nasty surprises. For example, to count the number of characters in an Arabic string you have to use the “mb_strlen” function instead of the standard PHP “strlen” function cause some Arabic characters are encoded using more than 1 byte and you might get an incorrect count by using “strlen”.
Credits
The slides design of this talk where inspired by the excellent post Slide Design for Developers written by Zach Holman of GitHub. We have also used the Yanone Kaffeesatz Font which is distributed under an open-source license (well, specifically, under the Open Font License).
Force Secure Pages (SSL / HTTPS) with Zend Framework
If you have a PHP web application built using the Zend Framework, securing all its pages becomes very easy. You just go ahead and add the following tiny function to your Bootstrap file:
protected function _initForceSSL() {
if($_SERVER['SERVER_PORT'] != '443') {
header('Location: https://' . $_SERVER['HTTP_HOST'] .
$_SERVER['REQUEST_URI']);
exit();
}
What this does is that it captures any non-secure request (Over plain HTTP) and redirects it to HTTPS (HTTP Secure). As an added bonus, Zend Framework will now automatically include HTTPS in all the links that your application outputs (That is, if you’re using the ZF utility functions to create your links).
Of course, you’ll need to have an SSL certificate properly configured and installed for your domain name. This is another topic for another post and you’ll find plenty of resources on this one on the net. As always, I recommend using DreamHost, they make the whole process of SSL-related tasks a few clicks away and I couldn’t be happier with their service.
Generating Command Line Colors with PHP
Generating ANSI (or ASCII) command line colors, or terminal colors, or whatever you call them, is easy with PHP. As I have said in my previous post, ANSI Command Line Colors under Windows, having colored text in the command line is a great help for spotting error or success messages. So here’s a quick and dirty function in PHP to do just that:
function colorize($text, $status) {
$out = "";
switch($status) {
case "SUCCESS":
$out = "[42m"; //Green background
break;
case "FAILURE":
$out = "[41m"; //Red background
break;
case "WARNING":
$out = "[43m"; //Yellow background
break;
case "NOTE":
$out = "[44m"; //Blue background
break;
default:
throw new Exception("Invalid status: " . $status);
}
return chr(27) . "$out" . "$text" . chr(27) . "[0m";
}
echo colorize("Your command has successfully executed...", "SUCCESS");
References
http://en.wikipedia.org/wiki/ANSI_escape_code
http://php.net/manual/en/function.chr.php
https://wiki.archlinux.org/index.php/Color_Bash_Prompt
ANSI Command Line Colors under Windows
Having colored text in the command line is a great help for spotting error or success messages. Unfortunately, those of us developing under Windows do not have this feature by default. Here’s how to enable it:
- Download ANSICON from http://adoxa.110mb.com/ansicon/index.html or https://github.com/adoxa/ansicon
- Extract the proper files (Depending on if you have a 32 or 64 bit machine) to c:\ansicon\ (For example). I have a 32 bit machine and hence I extracted the files from inside the x86 folder.
- Open a command line prompt and go to c:\ansicon, and then type “ansicon -i” without the quotes
- Done
You can now enjoy the colored output of PHPUnit for example.
Note: I have installed ANSICON 1.3 under Windows 7. My best guess is that this process will work for other versions of Windows too.
Update: I have written a new post on how to Generate Command Line Colors with PHP
Changing the default Look and Feel of NetBeans
Why change the default Look and Feel?
Aesthetics apart, every developer should change the default Look and Feel of NetBeans. Why? because it has an annoying bug. Here’s what happens: you’re pumping code in the NetBeans code editor like crazy. You need to switch to another window for a quick copy / paste from your eternal library of super code. You press ALT + TAB, you go to your other window, you press CTRL + C, you press ALT + TAB, you’re in NetBeans again getting ready to kick your paste operation using CTRL + V… but… but… you’re no longer editing the code! You’re now navigating the menu! ANNOYING! SHOW STOPPER!
Luckily, it turns out this is a bug in the Windows Look and Feel of Java (Which NetBeans uses by default). Time to change it of course. Read on.
Note: I’m running NetBeans IDE 6.7.1 under Windows XP SP3 / Java SDK 1.6. If you have a different version, this bug might not manifest (Lucky you!).
8 Steps to change the default NetBeans Look and Feel
- Download the three .nbm files from NetBeans substance look and feel plugin
- Open NetBeans. Go to Tools -> Plugins -> Downloaded
- Click on Add Plugins and add the three downloaded .nbm files (You have to add them one by one)
- Make sure the three files are selected, click Install, and follow through
- Restart NetBeans
- Go to Tools -> Options -> Miscellaneous -> Look and Feel
- Choose your preferred Look and Feel and Restart NetBeans again
- Enjoy
References
ALT + TAB switching to/from netbeans on Windows
http://forums.netbeans.org/topic5465.html
NetBeans substance look and feel plugin (Project’s home page)
http://kenai.com/projects/nbsubstance/pages/Home
NetBeans IDE – Look and Feel
http://netbeanside61.blogspot.com/2008/05/netbeans-ide-look-and-feel.html
JTattoo
http://www.jtattoo.net
20+ Free Look and Feel Libraries for Java Swing
http://javabyexample.wisdomplug.com/component/content/article/37-core-java/65-20-free-look-and-feel-libraries-for-java-swings.html
Sending emails with Zend_Mail using Gmail or Google Apps
Zend Framework is currently one of the best MVC-based frameworks in the PHP world. Zend_Mail is part of Zend Framework and it provides the ability to easily send email messages. If you’re like me, most web applications you have developed are setup to use Google Apps as their email provider. Here’s how to send email messages via Gmail or Google Apps by using Zend_Mail.
public function send() {
//Initialize needed variables
$your_name = 'Mario Awad';
$your_email = 'your_email@your_domain.com'; //Or your_email@gmail.com for Gmail
$your_password = 'your_password';
$send_to_name = 'My Friend';
$send_to_email = 'myfriend@tempinbox.com';
//SMTP server configuration
$smtpHost = 'smtp.gmail.com';
$smtpConf = array(
'auth' => 'login',
'ssl' => 'ssl',
'port' => '465',
'username' => $your_email,
'password' => $your_password
);
$transport = new Zend_Mail_Transport_Smtp($smtpHost, $smtpConf);
//Create email
$mail = new Zend_Mail();
$mail->setFrom($your_email, $your_name);
$mail->addTo($send_to_email, $send_to_name);
$mail->setSubject('Hello World');
$mail->setBodyText('This is the body text of the email.');
//Send
$sent = true;
try {
$mail->send($transport);
}
catch (Exception $e) {
$sent = false;
}
//Return boolean indicating success or failure
return $sent;
}
In addition to the above code, please note the following:
- You must enable the “php_openssl” extension to use the SSL transport protocol (Which is needed for Gmail and Google Apps). All you have to do is open your “php.ini” file and uncomment the line that includes the “php_openssl” extension (Search for “php_openssl” and you’ll find it).
- I found many resources on the web stating that you should use the TLS transport protocol (They never mention how to use or setup the SSL transfer protocol). This didn’t work in my tests and always resulted in a timeout error.
- Yes, you must use “smtp.gmail.com” as your SMTP host even if you’re configuring the application for Google Apps. In other words, don’t use “smtp.yourdomain.com”.
I hope this small tutorial saves you some headaches. Cheers
Capture the output of var_dump in a string
Introduction
You are programming in PHP and you have an array variable that you’d like to explore at different execution paths. Of course, the best way is to use a PHP debugger like xdebug or Zend Debugger, but, what happens when you’re too lazy to install a debugger? What happens when you don’t want or can’t install a debugger and you just need to check the content of that array by dumping it in your log file? Well, you might think you’re stuck, but, read on…
What is var_dump?
The var_dump manual page states that var_dump displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.
The problem is that var_dump outputs its result directly to the browser, how can you capture its output in a string variable? read on…
Using output control functions for the solution
Output control functions can be used to capture and redirect the standard output. For more details, read the PHP manual on output control functions, of course.
Here’s the solution:
function varDumpToString ($var)
{
ob_start();
var_dump($var);
$result = ob_get_clean();
return $result;
}
//
//Example usage:
// $data = array('first', 'second', 'third');
// $result = varDumpToString($data);
//
All you have to do now is call the varDumpToString function. Happy coding
URL Rewriting for CodeIgniter
What is URL Rewriting?
URL rewriting provides shorter and more relevant-looking links to web pages on your site. This improves the readability and the search rankings of your URLs. For example, URL “a” can be rewritten as URL “b”.
a) http://example.com/index.php?section=casting
b) http://example.com/casting
There are many articles on the web discussing the benefits of shorter and more relevant URLs. Let me add to them that you get to hide the used technology from both search engines and users. You’ll also have a better time migrating to a different platform in case you need to. For example, if you build your web application on top of ASP.NET and use URL Rewriting, you can easily migrate to PHP (Recommended, of course) without changing your links and hence without losing all the search-ranking score for those links.
CodeIgniter Default URLs
By default, CodeIgniter uses a segment-based approach to represent URLs. Unfortunately, CodeIgniter includes the annoying “index.php” file name in the URL. For example:
http://example.com/index.php/products/view/shoes.
Now, the CodeIgniter manual mentions that it’s very simple to remove the “index.php” part from the URL using the following .htaccess file:
RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L]
Problem solved? Not really. This didn’t work on my local machine nor on my online hosting account (Dreamhost). Why? I don’t know. I don’t care. I don’t currently have the time to find out why.
Having already installed WordPress, I remembered that their .htaccess file works offline and online (at least in my case). I started playing around with it, checked some online resources, and devised two solutions. The local solution works on my local machine with the XAMPP server installed on it and the online solution works on my Dreamhost account.
The Local Solution
RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /ci/index.php/$1 [L]
The only change you need to make is to “ci” (on lines 2 and 5) which is the folder where you have your CodeIgniter application installed. In brief, this rewrite file tells your web server to apply the rewrite rule whenever a file or a directory is not found on the server. For example, if you invoke URL “c”, the “contact” folder is not found on your server (Since CodeIgniter files are in the “system” folder), and accordingly the URL is rewritten to “d”. This rewrite allows CodeIgniter to execute successfully (By using URL “d”) while giving you the benefits of shorter URLs (URL “c”).
c) http://localhost/ci/contact
d) http://localhost/ci/index.php/contact
The Online Solution
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
There are no changes that you need to make for this .htaccess file. However, there’s one important note to mention. The question mark after “index.php” on line 5 is needed on my online hosting account at Dreamhost. You might want to remove it if it doesn’t work on yours. Again, I didn’t have the time to investigate why this is the case. Please check the additional resources for more details.
Conclusion
I hope this post saves you a few headaches I had to go through to solve this problem. I would love to see such a solution coming out of the box with the next version of CodeIgniter. If you have any hints or additional information regarding URL rewriting in the context of CodeIgniter, please share them in the comments.
Additional Resources
CodeIgniter URLs
http://codeigniter.com/user_guide/general/urls.html
URL Rewriting for Beginners
http://www.addedbytes.com/apache/url-rewriting-for-beginners/
An easy way to test your RewriteRules against different URLs
http://civilolydnad.se/projects/rewriterule/
Dreamhost and CodeIgniter URLs
http://codeigniter.com/forums/viewthread/55620/
