Programming


14
Mar 09

jQuery bsod plugin

So I was checking up on my twitter feed yesterday and saw a tweet that direceted me to this: http://devthought.com/wp-content/projects/mootools/BSOD/:

The script BSOD.js provides an easy-to-use class to boost the error reporting user experience of your websites.

To fully understand the characteristics of this technique please refer to this external article

I thought to myself… I can do that. I will use jQuery. So I spent most of yesterday after work learning up on how to make a jQuery extension (I wanted to be able to call it the same way as the moo-tools one)

$.bsod();

I got a bit carried away. I didn’t want just a bsod, I wanted each user to feel like the error was specifically made for them, so I added in os detection and os specific styles. I liked the way that he incorporated Spyjax (ajax for evil!), so I added that by default (you can turn it off of course if you want, it will blame a random site that is in the list). I also liked the colour changing, so that is in there too (it works only for the windows bsod though because linux and mac have images). Here are the options:

  • color: the background colour (win only, others are all black)
  • font_color: the font colour
  • error: the error message you want to display
  • os: the os (default is to guess the user’s os)
  • blame_url: boolean – true = blame a specific url
  • url_array: an array of urls you are looking to blame
  • error_array: a bunch of errors that you would like to use (one is randomly choosen)

Dependencies

Right now, the images and jQuery (tested with 1.3.1).

There is also a style that is compressed using cssTidy and inserted with the bsod.

Without further ado:

The demo (click on a word to see the bsod)

bsod.1.0.zip (with the dependencies included so you can use it right away!)

And some screen shots:

screen1 screen2 screen3 screen4

Tested in:

  • IE 8, IE 7
  • Firefox 3
  • Opera 9.63
  • Chrome 1.0.154.48

In case you missed it:

The demo (click on a word to see the bsod)

bsod.1.0.zip (with the dependencies included so you can use it right away!)


5
Feb 09

Templating

There are many ways of doing templating (for quick website development) especially in php.

One that I have a lot of experience with is Smarty.  So far I am not impressed too much with it. Rather than saving time, it seems to take up more of my time trying to get it to do what I want it to do. It also does not save me typing, which I thought was one of the points of templating.

For example in Smarty/html:

<input name="edit" type="checkbox" value="y" 
	{if $smarty.session.user and ( $user_type eq "editor" or $user_type eq "admin" )}
		checked="checked"
	{/if} /> edit

Alternatively in php/html:

<input name="edit" type="checkbox" value="y" 
<?php 
if($_SESSION['user'] && $user_type == "editor" || $user_type == "admin"){ ?>
	checked="checked"
<?php 
} ?> 
/> edit

Or in ‘pure php’ (CodeIgniter):

<?php
$this->load('form_helper');
if($_SESSION['user'] && $user_type== 'editor' || $usre_type == "admin"){
	$checked = false;
	if($checked = $this->input->get('checked');){
		$checked = true;
	}
	echo form_checkbox('edit', 'y', $checked);
}

I personally do not find it that difficult to understand either of the php examples I do understand that it may be simpler to use Smarty for very simple layouts… but when it comes to complicated layouts with many options, it is probably easier to resort to a purely php or html + php option rather than Smarty. Sure Smarty can be good if you want to let users change the layout of the page, but then, why not make it even simpler for them and have functions that create the elemets of your page and then they just have to move those around? You can even mark it with:

 =========== edit layout below =========== 

and change it so that instead of having to echo the form_checkbox helper function, you can override it and make it echo within the function itsself, then where ever you put it, it will be echoed out. In the end, this to me seems like the simpler answer to the whole thing, no real need to escape the variable, that can be done inside the function hiding it from the user as well avoiding things like:

{$variable|escape:"html"}

Which is probably more confusing to people who don’t know programming.

You may think that I am writting this because of my dislike for Smarty and all of the aggrevation that it causes me as a programmer. That is true, but there is also the problem that apparently Smarty is so rampant that you cannot get a php job without it in your resume. Oh… and look again at that if syntax:
{if isset($something) }
?>
Wait… where are the brackets? You might ask. Well… in Smarty they have done away with those brackets to make it simpler to read. If you happen to include those brackets by accident, you get Smarty compile errors. Even more inconsistancy comes up when you are trying to use arrays (you know, to group things together):
array_example_1.php

<?php
$smarty = new Smarty;
$smarty->assign('Contacts', array(
'fax' => '555-987-9876', 'email' => 'zaphod@slartibartfast.com'
);
$smarty->display('array_example_1.tpl');
?>

array_example_1.tpl

{$Contacts.fax|escape:"html"}
{$Contacts.email|escape:"html"}

Ok, so you might think that that is how arrays are handled in Smarty… and you would be right… sort of… because this is how arrays are handled when they are not hashed arrays:
array_example_2.php

<?php
$smarty = new Smarty;
$smarty->assign('Contacts', array(
'555-987-9876', 'zaphod@slartibartfast.com'
);
$smarty->display('array_example_2.tpl');
?>

array_example_2.tpl

{$Contacts[0]|escape:"html"}
{$Contacts[1]|escape:"html"}

So you can imagine the mess that is an array with hashes and other arrays inside it when you get to the layout:

array_example_3.tpl

{$Contacts[0].email.other[2]|escape:"html"}
{$Contacts.phone_number.home[1]|escape:"html"}

Don’t even get me started on the mess that is:

{literal} {/literal}

and:

{ldelim} {rdelim}

Be nice to your coders, and don’t treat your designers like idiots:

to keep designers away from PHP and to keep it safe, we should go for smarty. But is there really any designer who knows only smarty and no PHP? Is it really possible to write efficient code in smarty without knowing PHP? Nah, not at all. And there is {php} block which allow so called template designers to run unsafe PHP code inside it. Well, you can block executing {php} block from your side, but still smarty is not a solution to think that you are “secured”. And honestly, you should’t let your template designer write template code for you if they dont know PHP. And you should learn about XSS, SQL Injection and CSRF if you really want to live secured.

The Storyteller

let them use php functions and html instead of subjecting them to the bloated syntax soup that is Smarty.


13
Jan 09

Netbeans Subversion > Eclipse Subversion

For many reasons I prefer Netbeans to Eclipse for a lot of my programming. Since it has added in proper php support, which is more polished than Eclipse’s php extension, it has become my default IDE at work too. Another thing that is nice is that the subversion integration is very well done. One thing that is nice is that it knows that you are actually using subversion (the existance of .svn files is a good clue Eclipse…), and it will install the subversion client for you if you don’t have it installed so that you can actually use subversion. Fancy. The Plugins link is under Tools… not say… the Help menu, a much better place for it me thinks…

netbeans-annotationsOne of my favourite features is the Show Annotations for subversion… that way you can blame people for what they have done, or atleast see what changes they have made since you last looked at the file. In Eclipse it opens up 3 windows and just doesn’t seem very intuitive to me… but when you open it up in Netbeans, it meerly adds them to the left of the text, all of the lines changed for a specific commit have the name of the author is coloured blue and the comment (there is a comment right?) is shown at the bottom of the current tab.


15
Dec 08

Keeping database.yml dry

That would be dry as in Don’t Repeat Yourself

defaults: &defaults
    adapter: mysql
    encoding: utf8
    username: root
    password: root
    #Wherever mysqld.sock is located
    socket: /opt/local/var/run/mysql5/mysqld.sock
 
development:
    database: blog_development
    <<: *defaults
 
test:
    database: blog_test
    <<: *defaults
 
production:
    database: blog_production
    <<: *defaults

22
Oct 08

Making isset Recursive

At work I was told to add in the magic method __isset() (which is technically done by overloading the function…). Essentially it lets you call isset() on private and protected variables in your classes:

 
/**  As of PHP 5.1.0  */
 
class MyClass{
 
private $private;
 
public $public;
 
protected $protected;
 
public function __isset($name) {
        echo "Is '$name' set? ";
        return isset($this->name);
    }
 
}
 
$myClass = new MyClass();
 
echo isset($myClass->private); #Is 'private' set? false

echo isset($myClass->protected); #Is 'protected' set? false

echo isset($myClass->public); #false

This function will be called whenever the variable is unreachable by a normal isset call (private and protected variables)

Nice and easy if you know exactly what you want. Unfortunately sometimes people have the habit of adding underscores to the start of their variables, but not always. So to make this work, I decided it would be a good idea to make it check to see if the variable was there, or if maybe there should have been an underscore at the begining. So it became:

 
/**  As of PHP 5.1.0  */
 
class MyClass{
 
private $_private;
 
public $public;
 
protected $protected;
 
public function __isset($name) {
    if(isset($this-&gt;$name)){
        return true;
    }
 
    $name = "_".$name;
        return isset($this->$name);
    }
 
}
 
$myClass = new MyClass();
 
echo isset($myClass->_private); #Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 134136 bytes) in isset_r.php on line 20

Curious, so I changed it a bit to see what was actually happening in there…

__private___private____private_____private______private_______private________private_________private

__________private___________private____________private_____________private______________private___

____________private________________private_________________private__________________private______

_____________private____________________private_____________________private______________________

private_______________________private________________________private_________________________priv

ate__________________________private___________________________private__________________________

__private_____________________________private______________________________private_______________

________________private________________________________private_________________________________

private__________________________________private___________________________________private______

______________________________private_____________________________________private______________

________________________private_______________________________________private__________________

______________________private_________________________________________private__________________

________________________private___________________________________________private______________

______________________________private_____________________________________________private______

________________________________________private_______________________________________________

private________________________________________________private

Fun stuf… the lesson is: name things a standard way and you are good to go, if you don’t it will screw things up royally (but it might make a pretty design).