Zend Framework: Simple Acl Front Controller Plugin

access with Zend_Acl

Almost every web site need some abstraction over the access control list (ACL) to grant access of its users. As usual Zend Framework has quite good mechanism to deal with this – Zend_Acl.

Out in the web there are a lot of resources about Zend_Acl’s usage, so I ain’t going to cover it one more time, but simply copy/paste a very small front controller plugin implementing the basic usage of Zend_Acl.

Note that instead of defining the __construct() here is called preDispatch where the request is passed as a parameter. However only by copy pasting not every answer will be given. That’s why I’m going to write more about Zend_Acl in my future posts, for now only the source code:

<?php
 
class AclInit extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        // create new acl object
        $acl = new Zend_Acl();
 
        // define resources. typically there are
        // only four resources from the CRUD functionality
        // but there can be added more resources
        $acl->add(new Zend_Acl_Resource('index'))
            ->add(new Zend_Acl_Resource('create'))
            ->add(new Zend_Acl_Resource('read'))
            ->add(new Zend_Acl_Resource('update'))
            ->add(new Zend_Acl_Resource('delete'));
 
        // define roles
        $acl->addRole(new Zend_Acl_Role('guest'))
            ->addRole(new Zend_Acl_Role('admin'));
 
        // define privileges
        $acl->allow('guest', array('index', 'read'))
            ->allow('admin');
 
        // setup acl in the registry for more
        Zend_Registry::set('acl', $acl);
 
        // check permissions
        if (!$acl->isAllowed('guest', $request->getActionName())) {
            $request->setControllerName('error');
            $request->setActionName('error');
        }
    }
}
Posted in micro tutorial, PHP, zend framework | Tagged , , , , , , , , , , , | Leave a comment

document.forms['myform'].submit() is not a function?

.submit()

You want to submit the form by clicking on a link or some other element on the page and this is a simple task. You know that by simply adding something like document.forms['the-form-name'].submit() this will work.

<form method="post" name="myform">
    <input type="text" name="user" />
    <a href="#" onclick="document.forms['myform'].submit();return false">Submit</a>
</form>

However you’d like to submit the form not only by clicking on that particular element, but also by clicking on the Enter while the focus is on a input field, and this wont work! That’s because you don’t have a input type=”submit”.

OK, first thing is to add a hidden input type=”submit” – than by clicking both on the Enter keyboard button and on the link with the onclick=”document.blah.blah.blah” will submit the form.

<form method="post" name="myform">
    <input type="text" name="user" />
    <input type="submit" name="submit" value="submit" style="display:hidden" />
    <a href="#" onclick="document.forms['myform'].submit();return false">Submit</a>
</form>

But this is not true!

Than you’ll receive the following message:

document.forms['myform'].submit() is not a function

Why? This isn’t working, but ever line seems to be OK. The answer is – don’t name the input type=”submit” with the trivial – “submit”. Just give it another name:

<form method="post" name="myform">
    <input type="text" name="user" />
    <input type="submit" name="something" value="something" style="display:hidden" />
    <a href="#" onclick="document.forms['myform'].submit();return false">Submit</a>
</form>
Posted in micro tutorial, web development | Tagged , , , , | 3 Comments

PHP: preg_match Give Names to the Matches

So Called – Subpatterns

Patterns - preg_match

In PHP 5.2.2+ you can name the sub patterns returned from preg_match with a specific syntax.

Named subpatterns now accept the syntax (?<name>) and (?’name’) as well as (?P<name>). Previous versions accepted only (?P<name>)

This is extremely helpful, when dealing with long patterns. As you may know you can simply use the “old school” way and to call the matches by their number based index:

    $haystack = '01 Jan 1970';
    $pattern = '/(\d{1,2})\ (Jan|Feb)\ (19\d\d)/';
 
    preg_match($pattern, $haystack, $matches);
 
    print_r($matches);

Although it may look difficult to maintain, now you can simply name the sub patterns of preg_match and to call them with their associative array keys. This is more clear when writing code and it’s definitely more maintainable.

    $haystack = '01 Jan 1970';
    $pattern = '/(?<day>\d{1,2})\ (?<month>Jan|Feb)\ (?<year>19\d\d)/';
 
    preg_match($pattern, $haystack, $matches);
 
    print_r($matches);
 
    // now there's $matches['day'], $matches['month'] ...
Posted in micro tutorial, PHP | Tagged , , , , , , , , , | Leave a comment

Friday Algorithms: JavaScript Merge Sort

Merge Sort

This week I’m going to cover one very popular sorting algorithm – the merge sort. It’s very intuitive and simple as it’s described in Wikipedia:

  • If the list is of length 0 or 1, then it is already sorted. Otherwise:
  • Divide the unsorted list into two sublists of about half the size.
  • Sort each sublist recursively by re-applying merge sort.
  • Merge the two sublists back into one sorted list.

Here’s the Source

(JavaScript)

var a = [34, 203, 3, 746, 200, 984, 198, 764, 9];
 
function mergeSort(arr)
{
    if (arr.length < 2)
        return arr;
 
    var middle = parseInt(arr.length / 2);
    var left   = arr.slice(0, middle);
    var right  = arr.slice(middle, arr.length);
 
    return merge(mergeSort(left), mergeSort(right));
}
 
function merge(left, right)
{
    var result = [];
 
    while (left.length && right.length) {
        if (left[0] <= right[0]) {
            result.push(left.shift());
        } else {
            result.push(right.shift());
        }
    }
 
    while (left.length)
        result.push(left.shift());
 
    while (right.length)
        result.push(right.shift());
 
    return result;
}
 
console.log(mergeSort(a));

It’s interesting to see what happens in the Firebug’s console:

[34, 203, 3, 746] [200, 984, 198, 764, 9]
 
[34, 203] [3, 746]
 
[34] [203]
 
[3] [746]
 
[200, 984] [198, 764, 9]
 
[200] [984]
 
[198] [764, 9]
 
[764] [9]
 
[3, 9, 34, 198, 200, 203, 746, 764, 984]

Actually the tricky part in this algorithm is the merge function – it does all the work.

Posted in algorithms, javascript, web development | Tagged , , , , , , , , | Leave a comment

Replace the Broken Images with a Default Image with JavaScript

There is cool JavaScript trick that helps you catch broken images. You know that when the image doesn’t exist, the http path to the image returns 404 or the path is wrong, the browser doesn’t display nothing in the most cases. As MSIE is always different it displays an ugly icon saying that there is not an image to load

MSIE broken image icon

and that is really bad!

There’s a Quick Fix …

Simply add an onerror handler on the IMG tag

<img src="http://..../broken_url.jpg" onerror="this.src='path_to_default_image'" />
Posted in javascript, micro tutorial, web development | Tagged , , , , , | Leave a comment