PHP

Releasing Zend Framework QR / Matrixcode library

I had promised it for quite a while, and unfortunately things got delayed time after time, but finally I’ve managed to wrap things up and complete my Zend Framework module for generating QR codes in both raster and vector format.
In the end I refactored almost all the code to be more consistent with the architecture of the existing Zend_Barcode library (with a couple of differences however), so that took also a bit longer than anticipated.

I’ve released it as an open-source project on Google Code, so feel free to check it out:
http://code.google.com/p/zend-framework-matrixcode-module

You can also download the latest revision (r9) right here.

Any feedback is appreciated, and if you’d like to contribute, please let me know. It would be nice if other matrixcode formats could be added, f.i. Aztec Code or Data Matrix. The renderers do not need any changes as long as the 2D code format consists of square modules. Just pass them a two-dimensional array with boolean values and you should get the expected result.

Also be sure to check the Issues page as there is still a known bug with the encoding of a specific kind of string. Still need to figure that one out.

Hope you enjoy it.

AJAX requests and expired sessions

If you ever have developed secure web applications that make use of AJAX, you have most likely been confrontend with situations where an AJAX request occurs after the session has timed out. Where the server is supposed to return some HTML of JSON, you get a ’401 Unauthorized’ or an empty response depending on how you have it implemented. The question is then how to deal with that kind of responses. You could read the headers of the response and redirect when a 401 status is detected, but I can’t tell for sure that there isn’t/won’t be a browser that will catch those errors and handle it in a way that messes up the user experience. Also, you don’t want to check explicitly for such error after every AJAX request.

So the solution I implemented is explained below. I will show you how it can be done with Symfony and jQuery, but it should be straightforward to achieve the same with other frameworks (like with a Zend Framework Front Controller Plugin for example). What really matters is the idea behind the solution.

Server side:

class frontendConfiguration extends sfApplicationConfiguration
{
  	public function configure()
  	{
  		// add event listener
  		$this->dispatcher->connect('context.load_factories', array(
      			$this, 'isAuthenticatedAjaxRequest'
    		));
  	}

  	/**
  	 * This method checks if an Ajax request comes in and
  	 * if the session has not expired.
  	 * If so, a custom header is returned (a standard 401
  	 * might get trapped and handled by the browser)
  	 * and a javascript "ajaxSuccess" listener will detect this
  	 * header and redirect to the webroot.
  	 *
  	 * @param sfEvent $event
  	 */
  	public function isAuthenticatedAjaxRequest(sfEvent $event)
  	{
  		$context = $event->getSubject();
  		$request = $context->getRequest();
  		if($request->isXmlHttpRequest()) {
  			$user = $context->getUser();
  			if($user->isAuthenticated() === false) {
  				header('NOT_AUTHORIZED: 499');
 				exit();
  			}
  		}
  	}
}

So this is what we do here:
Before the request is being dispatched, we perform an additional check on the request. One way to implement this in Symfony is to connect a method to a certain event, in this case the “context.load_factories” event. The reason why we wait until the factories are loaded is because we will need the session data, which depends on the Storage factory.
In the triggered method, we check if concerns an AJAX request. If that’s the case, we verify if the user is authenticated and if not, we return a custom header ‘NOT_AUTHORIZED’ with status number 499. By using a custom header, we can be sure it will not be trapped by the browser.

Client side:

I will be using jQuery here basically because it’s my favorite Javascript framework.

$(function() {
	$('body').bind(
		'ajaxSuccess', 
		function(event, request, settings) {
		if (request.getResponseHeader('NOT_AUTHORIZED') === '499') {
   			window.location = '/';
		};
	});
});

In jQuery, by binding to the “ajaxSuccess” event, we can make sure that every AJAX response will trigger our function. All this function does is check if our custom header is set and if not, redirect the browser to the webroot. That would be a regular browser request which can be dealt with like any other normal request.

If you would have any remarks or feedback on how to further improve this, I’ll be happy to hear it from you.

The new Drupal 7 interface

I must confess, I’ve never been a huge fan op Drupal. I know it’s a powerful application framework. I know it is very extensible. I know there are lots of free modules around that fit almost every need. So what’s not to like then? I’ll tell you: the interface. I’ve used all major content management systems in the last couple of years, and Drupal is the clear winner in terms of clunky and annoying interface. It was clear that the user experience was not much of a concern for the Drupal team.
So for that simple reason, I never really considered Drupal when starting a new project.
But when Drupal 7 was announced, it got me interested again. A Drupal User Experience Project was set up to improve the, eeeehm… user experience, so it looked like they were aware of the problem and were getting ready to tackle it.
2 years of development later, Drupal 7 Alpha is finally released, so time to forget about the past and give it another try.
The install procedure is very nice and easy, and is pretty similar to other systems. Then, once installed, the interface showed up, and it was… a fail!
Maybe my expectations were too high. But, you see, I’m a hug fan of Metalab. They do fantastic jobs on web interfaces. Simple, beautiful, and efficient. Or take for example CampaignMonitor. It shines in simplicity and user friendlyness. If you are getting used to working with those kind of applications, Drupal 7 is pretty disappointing. I do realize that Drupal is a much more complex system then most other applications, since Drupal is more ‘generic’ and closer to a framework. But still, I believe they could have done a much better job. So I’m afraid that even the new interface will continue to scare beginners, and not much progress will be achieved in this field.
So for now I’m still not going to use Drupal 7 for regular websites, and maybe when the stable version is out I’ll give it another go. Until then I’ll stick with WordPress or Expression Engine.
P.S. I’m evaluating Drupal here primarily as a CMS system for websites, not so much as a framework for building applications. For websites, you just want it work out of the box, looking good and simple for the client, without the need for custom coding.

drupalI must admit, I’ve never been a huge fan op Drupal. I know it’s a powerful application framework. I know it is very extensible. I know there are lots of free modules around that fit almost every need. So what’s not to like then? I’ll tell you: the interface. I’ve used all major content management systems in the last couple of years, and Drupal is the clear winner in terms of clunky and annoying interface. It was clear that the user experience was not much of a concern for the Drupal team.
So for that simple reason, I didn’t consider Drupal that often when starting on a new website project.

But when Drupal 7 was announced, I got interested again. A Drupal User Experience Project was set up to improve the, eeeehm… user experience, so it looked like they were aware of the problem and were getting ready to tackle it.
2 years of development later, Drupal 7 Alpha is finally released, so time to forget about the past and give it another try.

The install procedure is very nice and easy, and is pretty similar to other systems. Then, once installed, the interface showed up and, it was… a fail!

Drupal 7 interface
They did a good job on better organizing the different actions in categories that are clear and make sense, but what keeps annoying me is the fact that the interface is still not intuitive and in my opionion a bit of a mess. I don’t know really. When I saw the new WordPress 2.7 interface some time ago, I was really excited by the huge progress they made. With Drupal 7, I can’t say I’m impressed.
Maybe my expectations were too high. But, you see, I’m a hug fan of Metalab. They do fantastic jobs on web interfaces. Simple, beautiful, and efficient. Or take for example the CampaignMonitor software. It shines in simplicity and user friendliness. If you are getting used to working with those kind of applications, the Drupal 7 interface is pretty disappointing. I do realize that Drupal is a much more complex system then most other applications, since Drupal is more ‘generic’ and closer to a framework. But still, I believe they could have done a much better job. So I’m afraid that even the new interface will continue to scare beginners, and not much progress will be achieved in this field.

So for now I’m still not going to use Drupal 7 for regular websites, and maybe when the stable version is out I’ll give it another go. Until then I’ll stick with one of the alternatives.

Final note:
I’m evaluating Drupal here primarily as a CMS system for websites, not so much as a framework for building applications. For websites, I just want it to work out of the box, looking good and simple for the client, without the need for custom coding.
I’m also aware that you’re free to design your own interface, but not having to do that looks as one of the major advantages to me.

QRcode for Zend Framework

I’ve been using QRcodes for quiet some time now on different projects, and so far I’ve always based my code generator on the PHP script written by Y.Swetake. It does the job pretty well, but there is definitely room for improvement:
- the code is very messy
- it’s almost impossible to figure out the meaning of each piece of code
- it’s not a class
- instead of using algorithms, arrays of predefined values are used for certain calculation
So I decided to write a QR matrixcode generator class for Zend Framework from scratch.
As there is not that much information to find on the internet about how a QR code is actually constructed, or what algorithms are used, I had to digg through all the relevant programming code I could find. One of the most valuable resources was a C++ library written by Kentaro Fukuchi. The code was much better documented than the PHP version, and both combined, together with online documentation, I started to figure it out piece by piece.
I must say it didn’t progress as fast as I hoped. I hadn’t been analyzing code like that for quite some time. Line by line, in different programming languages, jumping around from one function to the other trying to keep track. But you’ve gotta love that kind of challenges.
So once I had a much better idea of how a QR code is built, I could start writing my classes. I first checked if there was already a Zend Framework component that was related to QR codes or matrixcodes in general, but the only component that came close was Zend_Image_Barcode which is still a prototype and thus not part of the official library distribution.
I hoped I could base my own component on the structure of Zend_Image_Barcode, but there was something I didn’t like about it. A barcode is not necessarily an image. So to me it didn’t make much sense to extend the Zend_Image class. I did however plan to use the same methods like ‘render’ and ‘draw’ etc.
A QR code is in fact a matrixcode, just like Quick Code and Data Matrix. I was only going to implement the former for now, but it seemed to be a good idea to create a Matrixcode component that could contain all the different types of two dimentional codes. My QR code generator class will be one of them.
Because I didn’t want to limit the output format to png, gif and jpg, I decided to create an abstract rendering class that could be extended for each type of rendering output. In this specific case, I wanted png, gif, jpg, svg, and pdf (vectorial) to begin with.
So after a weekend of intense programming I managed to get a decent looking component. Since now I actually understand every part of it, I added some extra functionality like transparent backgrounds, adding a watermark, rendering in vector format, creating a border, etc.
There is still some work to do though. I would like to create an abstract matrixcode class, but I don’t really know yet in how far I could go with it. Well, I suppose I could keep it very, eehm… abstract. There is also one more algorithm to implement, some exception handling to do, etc.
But anyhow, if you’d be interested to check out the component like it is now, just leave me comment.

QR code I’ve been using QRcodes for quiet some time now on different projects, and so far I’ve always based my code generator on the PHP script written by Y.Swetake. It does the job pretty well, but there is definitely room for improvement:

  • the code is very messy
  • it’s almost impossible to figure out the meaning of each piece of code
  • it’s not object oriented
  • instead of using algorithms, arrays of predefined values are used for certain calculations

So I decided to write a QR matrixcode generator class for Zend Framework from scratch.

As there is not that much information to find on the internet about how a QR code is actually constructed, or what algorithms are used, I had to digg through all the relevant programming code I could find. One of the most valuable resources was a C++ library written by K. Fukuchi. The code was much better documented than the PHP version, and both combined, together with online documentation, I could start to figure it out piece by piece.

I must say it didn’t progress as fast as I hoped. I hadn’t been analyzing code like that for quite some time. Line by line, in different programming languages, jumping around from one function to the other trying to keep track. But you’ve gotta love that kind of challenges.

So once I had a much better idea of how a QR code is built, I started writing my own classes. I first checked if there was already a Zend Framework component that was related to QR codes or matrixcodes in general, but the only component that came close was Zend_Image_Barcode which is still a prototype and thus not part of the official library distribution.

I hoped I could base my own component on the structure of Zend_Image_Barcode, but there was something I didn’t like about it. A barcode is not necessarily an image. So to me it didn’t make much sense to extend the Zend_Image class. I did however plan to use the same methods like ‘render’ and ‘draw’ etc.

A QR code is in fact a matrixcode, just like Quick Code and Data Matrix. I was only going to implement the former for now, but it seemed to be a good idea to create a Matrixcode component that could hold all the different types of two dimentional codes. My QR code generator class is one of them, and an instance is created by the factory method of the Matrixcode class.

Because I didn’t want to limit the output format to png, gif and jpg, I decided to create an abstract rendering class that could be extended for each type of output rendering. In this specific case, I wanted png, gif, jpg, svg, and pdf (vectorial) to begin with.

So after a weekend of intense programming I managed to get a decent looking component. Since now I actually understand every part of it, I added some extra functionality like transparent backgrounds, adding a watermark, rendering in vector format, creating a border, etc.

There is still some work to do though. I would like to create an abstract matrixcode class, but I don’t really know yet in how far I could go with it. Well, I suppose I could keep it very, eehm… abstract. There is also one more algorithm to implement, some exception handling to do, etc.

But anyhow, if you’d be interested to check out the component like it is now, just leave me a comment.

UPDATE: Meanwhile the code has been released.

cakePHP 1.2 – custom scaffolding

Scaffolding in cakePHP is a very powerful and useful feature. It allows you to set up a prototype of your application within no time.
Although the manual of cakePHP states that’s you shouldn’t take customization of this scaffolding too far, it’s still something that offers a nice perspective in terms of flexible CMS-systems.
Today I did some experimenting with it, just to see how far I could take it.

I’m working on a rather big application: 6 websites managed by 1 CMS. Obviously with a setup like that, you can’t make use of cakes admin-routing anymore. You will need an independent CMS, running as a stand-alone application by itself.
Since the website is very dynamic, tables will come and go on a weekly basis.
In other words, the CMS will need to be very flexible and maintainable:
- controller, view and model subfolders to group related stuff
- controllers, views and models in different subfolders can have the same name (f.i. a dashboardController for each group)
- adding CRUD (Create, Read, Upate, Delete) functionality for a database table by using scaffolding
- choosing the table columns in the index view by setting an array in the controller
- search functionality and paging is taken care of automatically

Once everything will be set up, it should be possible to add the management of a new table in a matter of minutes, with paging, column selection, sorting, searching, and export to MS Excel, CSV and printer.

UPDATE (2008/09/22):

The whole setup is up and running. It required quite some tweaking of cake and the scaffolding more specifically, but eventually we managed to create a great CMS that fulfills all requirements above.
So are we happy we the result? Yes and no. The fact that we achieved what we wanted is very satisfying of course, but during the whole development process our enthusiasm about cakePHP slowly dropped to a rather low level.
The framework is pretty well documented, but the quality of the documentation is often poor, uncomplete, or outdated. Some modules just don’t work like they are supposed to, and because cake is based on conventions for the most part, it makes it often hard to figure out what is actually going on when things go wrong.

Don’t get me wrong, I’m still convinced it’s a powerful framework with some really great features. I just have the impression that you quickly meet the bounderies of the system once you start using it for more complicated stuff, and then the time consuming tweaking starts… So for now, I’ll let cakePHP mature a bit longer, see how it evolves, and then have another look at it.

Go to Top