Zend Framework, setting up a dynamic menu using a plugin

I wanted to set up the menu in layout.phtml using a dynamic call to the database.

I created a directory in library called plugins.

I put this in application.ini
autoloaderNamespaces[] = “Plugins”

I created a class in the plugins directory called Plugins_Layout with the file name Layout.php

class Plugins_Layout extends Zend_Controller_Plugin_Abstract
    public function preDispatch(Zend_Controller_Request_Abstract $request)
        $layout = Zend_Layout::getMvcInstance();
        $view = $layout->getView();

        $content_mapper = new Application_Model_ContentMapper();

        $view->menu = $content_mapper->getMenu();

I then added this to Bootstrap.php

protected function _initRoutes() {

       Zend_Controller_Front::getInstance()->registerPlugin(new Plugins_Layout());

I was then able to create the dynamic menu in layout.phtml

 menu as $url => $title) { ?>

All was working as I expected.

Zend framework, database connection

I set up a database connection in application/config/application.ini

resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params cialis preise holland.username = "username"
resources.db.params.password = "password"
resources.db.params.dbname = "montesnegro"

Next I created models:

zf create db-table Content content
zf create model ContentMapper
zf create model Content
zf create controller Content

I created the PHP for the Content model in application/models/content

class Application_Model_Content
    private $id;
    private $menu_id;
    private $content_text;
    private $update_date;

    function __construct($id = null,$menu_id = null,$content_text = null,$update_date = null)
        $this->id = $id;
        $this->menu_id = $menu_id;
        $this->content_text = $content_text;
        $this->update_date = $update_date;

    public function setContentText($content_text)
        $this->content_text = $content_text;
        return $this;

    public function getContentText()
        return $this->content_text;

    //other getters and setters.....

I created a ContentMapper class

class Application_Model_ContentMapper

     * @var Zend_Db_Table_Abstract
    protected $_dbTable;

    public function setDbTable($dbTable)
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        $this->_dbTable = $dbTable;
        return $this;

    public function getDbTable()
        if (null === $this->_dbTable) {
        return $this->_dbTable;

    public function save(Application_Model_Content $content)
        $data = array(
            'content_text'   => $content->getContentText(),
            'menu_id' => $content->getMenuId(),
            'update_text' => date('Y-m-d H:i:s'),

        if (null === ($id = $content->getId())) {
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $id));

    public function getMenu()
        $select = $this->getDbTable()->select()->order("order ASC ");
        $results = $this->getDbTable()->fetchAll($select);
        $menu   = array();
        foreach ($results as $row) {
            $menu[$row->url] = $row->title;
        return $menu;


    public function find($id, Application_Model_Content $content)
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
        $this->setContent($result->current(), $content);

    public function findByUrl($url,Application_Model_Content $content)
            $url = 'home';
        $select = $this->getDbTable()->select()->where("url = '$url' ");
        $result = $this->getDbTable()->fetchRow($select);
        if (0 == count($result)) {
        $this->setContent($result->toArray(), $content);

    private function setContent($row, Application_Model_Content $content)

    public function fetchAll()
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_Content();
            $this->setContent($row, $entry);
            $entries[] = $entry;
        return $entries;


And then I created the controller

class ContentController extends Zend_Controller_Action

    public function init()
        /* Initialize action controller here */

    public function indexAction()
        // action body
        $content_mapper = new Application_Model_ContentMapper();

        $url = $this->getRequest()->getParam('url');
        $content = new Application_Model_Content();
        $content_mapper->findByUrl($url, $content);
        $this->view->content = $content;



Finally, I created a MySQL table called content and put some stuff in there.

When I did http://localhost/content, all was well.

Zend framework set up

I’m currently using the Zend framework to set up a holiday rental web site in Spain. I am putting up a series of blog posts to act as a how to and to give a heads up on some of the gotchas I encountered on the way.

I downloaded the Zend framework and installed it on my local server. I’m running a Windows 7 PC and I have OSX laptop running Lion. Set up was the same on both, I download the framework and put the path in the php.ini.

My Windows php.ini had this


I ran

zf create project montesnegros

I then set up a virtual host (again the same process on Lion and Windows 7)

NameVirtualHost rachael.test:80

<VirtualHost rachael.test:80>
DocumentRoot "G:/data/WorkSites/rachael/montesnegros/public"
DirectoryIndex index.php
ServerName rachael.test

<Directory "G:/data/WorkSites/rachael/montesnegros/public">
Options +FollowSymLinks
AllowOverride All
#order allow,deny
allow from all

I checked the that application had been created with the correct file structure. I found a great resource for understanding the application.ini file at https://github.com/feibeck/application.ini/blob/master/application.ini

All was fine and when I restarted Apache, I got this

Apache OSX Lion 403 Forbidden Problem

I had to set up multiple development sites using the Apache virtual hosting. All were coming back with the dreaded 403 page.

I did a lot of research and nothing seemed to work.

Finally, I found something that did. My virtual hosts were all running within directories that I had set up outside the /Library/Webserver location. I didn’t want to move the directories there and I didn’t want to sym link my directories to there either.

I found a line in the http.conf file

User _www
Group _www

I changed it to

User kevinsaunders
Group staff

All fine, except that PHP wasn’t allowed to set a session file in /var/tmp.

sudo chown -R kevinsaunders:staff /var/tmp/*

That seemed to fix it all.

Image Magick bash script to resize and crop images

Resizing images is often something that any web developer has to do.movie Hacksaw Ridge 2016 trailer

This is a bash script which resizes and crops images to a perfect 72×72 square.


# script to resize and crop

cd "$dir$orig"
for k in `ls * cialis von lilly.jpg`
	h=`identify -format "%h" $k`
	w=`identify -format "%w" $k`
	#echo "$k, w:$w, h:$h"
	#If landscape
	if [ $w -ge $h ]
	   	 convert $k -resize x72 -crop 72x72+0+0  "$dir$resume/$k"
	     #echo "landscape"
		 convert $k -resize 72x -crop 72x72+0+0 "$dir$resume/$k"
	     #echo "portrait"

Recruitment emails, part 2

Another example of recruiter in a big hurry, and in this case, having difficulty in working out how the spell checker works

Hi Kevin,

I hope you a very well today.

i am currently recruiting for a position that i feel would be a fantastic opportunity. The job specification is below, have a look through and if you are interested please could you send me an updated version of your CV.

 C++ Software Developer (Award winning engineering team)

[I’ve never done C++ professionally. The consultant signs off: ]

If you are interested plese call me on 0121———

kind Reagrds, Lisa B——

Email attacks on websites

I’ve been lucky, in 10 years of web development I’ve only had two sites using phpmailer attacked by a spammer using an email injection attack.

The first site used the “To” address in the email form and substituted their own text.

The second was more devious and attacked through the subject line of the email. I had email verification on the form so they bluehost优惠码 couldn’t use a malformed email address but I did substitute a hidden form variable into the subject line and they attacked this.

Here is the how they did it (I have changed some of the email addresses to protect the site owners)

Return-path: 23423423
Envelope-to: 234234
Delivery-date: Sat, 27 Aug 20xx 05:19:55 +0100
Received: from 3423423424234
by store5.mail.uk.easynet.net with esmtp (Exim 4.32)
id 1E8sAd-000C9n-9v
for 234234234; Sat, 27 Aug 20xx 05:19:55 +0100
Received: from 234234234
by 2323423423324)
id 1E8sAT-000OE6-0P
for 234234324; Sat, 27 Aug 20xx 05:19:45 +0100
Received: (qmail 37339 invoked by uid 2527); 27 Aug 20xx 03:21:20 -0000
To: 234234
Subject: A new user has signed up for the
Content-Type: multipart/mixed; boundary=”===============0960820620==”
MIME-Version: 1.0 Subject: e6e85f17 To: bcc:
From:   This is a multi-part
message in MIME format.  –===============0960820620== Content-Type:
text/plain; charset=”us-ascii” MIME-Version: 1.0 Content-Transfer-Encoding:
7bit  pkamjstswf –===============0960820620==–  area
Date: Sat, 27 Aug 20xx 04:21:20 +0100
From: 2342342332
X-Priority: 3
X-Mailer: PHPMailer [version 1.72]
MIME-Version: 1.0
Content-Type: multipart/alternative;
Delivered-To: 42342342234234

Note the extended Subject:

For anybody using mail() in PHP I would suggest that you don’t use form variables which alter the email’s subject line as this can be a method of attack.

I would suggest that the next version of phpmailer does not allow anyone to set a new line in either the email “To” field or the email subject line. I would suggest that references to $this->Subject

are replaced by a method call

$this->getSubject() which then allows the class to strip out new lines and restrict the length of the subject.

Recruitment emails

Like many IT professionals, I’m on the radar of recruitment companies. I don’t mind, sometimes I get some interesting work through this route. I’m less keen on the recruitment companies who bombard me with emails about roles I’m not really suited for, it shows that they haven’t really done their homework.


A while ago, I got this email. It must be the ultimate catch all email.

Dear Kevin

We are currently searching for a JOB TITLE to work in CITY, COUNTRY for DURATION plus extensions. This is a fantastic contract opportunity for a large multi-national client.

The ideal candidate must have the following skills: SHORT JOB DESCRIPTION.

If you are interested in this position, please respond within 24 hours with a short motivation letter and your CV in Word format so MBA can contact you.

MBA do have a policy to fulfill job requests within 24 hours therefore a quick response is important.

Alternatively, you can call us on the number below.

MBA does apologise when this job does not match your profile. However, if you have any colleagues who may be interested in this contract opportunity; please forward this email to them. If they are successfully placed at our client site, we will award you with £150.00 (180 Euros).

Thank you for your time and we hope to hear from you soon,

It made feel all warm and fuzzy inside to know that the recruiter had actually read my CV and then sent me a carefully targeted position.


Which is faster? Java or PHP

I’ve written web sites in both PHP (around 50 sites) and Java (around 20 sites) for the last 7 years and I don’t think there’s much difference in terms of speed. It’s not true that one is faster than the other for heavy database use or that one will withstand a lot of site hits. If you write your app correctly and optimize for speed using strategies appropriate to that language then there’s very little difference.

I’ve found my Java apps easier to maintain (because you have to do OOP with Java) whereas the PHP apps tend to gradually disintegrate into a big ball of mud (too easy to hack loads of dodgy code together)

I’ve found Java much harder to run on shared servers (here in the UK most hosting companies will refuse to set up Tomcat because they don’t know how to do it) and I’ve also found that Java apps eat more memory than equivalent PHP apps. For most of my Java sites, I’ve ended up setting up the server myself whereas with all the PHP apps I’ve used off the shelf hosting.

Java is much easier to code because IDEs like Intellij, NetBeans or Eclipse are around 10 times easier than trying to use Dreamweaver or PHP Eclipse. I find that I can write Java a little quicker than PHP but there’s very little difference and anyway, you don’t have to write
as many lines of code in PHP.

I think that it depends on your preferences, your experience, your client’s preferences and budget.

Unless the site is a really small one, I prefer using Java. I’d definitely use Java if there are complex server side things to do (like bulk image processing or running Flash XML servers)