php:5.6-apache docker container with GD (and jpeg) compiled

I was trying to get the official php:5.6-apache docker container compiled with GD for a project. GD would install but the JPEG support was missing. I managed to figure out the missing libraries and config in the end.

This is what my docker file looks like now, with the important bits in bold:

FROM php:5.6-apache

RUN apt-get update \
    && apt-get install libpng12-dev libfreetype6-dev libjpeg62-turbo-dev -qy \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install gd

Getting the raw POST or PUT data of a request in ZF2 MVC

If you’re sending raw data to ZF2 over http (perhaps a json document), you can use the “getContent()” method of Zend\Http\PhpEnvironment\Request.

How does this work in practice?

In a controller you might want to do something like this:

1
2
3
4
5
6
7
8
9
<?php
namespace Application\Controller;</code>
 
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
 
class IndexController extends AbstractActionController {
    public function indexAction() {
        $data = $this->getRequest()->getContent();

But if you’re always expecting a certain format, say JSON, a controller plugin could give you back the array or object rather than a lot of json_decode calls in your controllers.

The plugin:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
 
namespace My\Mvc\Controller\Plugin;
 
use Zend\Mvc\Controller\Plugin\Params as ZendParams;
 
class Params extends ZendParams {
 
    public function fromJson() {
        $body = $this->getController()->getRequest()->getContent();
        if (!empty($body)) {
            $json = json_decode($body, true);
            if (!empty($json)) {
                return $json;
            }
        }
 
        return false;
    }
 
}

And then its as simple as:

1
2
public function indexAction() {
    $data = $this->params()->fromJson();

Help! Sometimes getContent() doesn’t return my data.

The method uses file_get_contents(‘php://input’) to get the raw data from the HTTP request. Unfortuantely, the php://input stream can only be read once per process. I did have a plug-in that called file_get_contents(‘php://input’) too, and if $request->getContent() gets called after this, it will be empty!

PhpMyDiff v0.0.3

I had to use PhpMyDiff at work today for this first time in a while and managed to fix up a few bugs. I’ve also implemented a new way of comparing databases if the tables use the MyISAM engine. Rather than having to grab every row from the table and compare row by row, mysql has a CHECKSUM function, which quickly returns the table checksum. If the checksums are different the original process (of grabbing the data) comes back in to play but for databases with only a few table changes, its a huge speed increase.

Given time I’d like to introduce a similar row by row checksum, which should be easy enough for tables with a primary key. PhpMyDiff is still a long way from complete, but it may slowly get there!

Oh, it should also be possible to compare any database type supported by Zend_Db now, although I haven’t had the chance to test this at all. I’m sure something will break, as a few of my queries are probably MySQL specific!!

Feedback would be great if anyone gets the time… check it out at http://code.google.com/p/phpmydiff/

Deleting duplicate mdir emails using php

In an attempt to speed up my imap access I moved old emails in to folders based on year. This speed up process majorly backfired when imap/mdir decided to make 7 copies of each email in the same folder. I ended up with 27,000 emails in my 2006 folder!

With my mailbox quota full I needed a quick solution… and couldn’t find one! Thunderbird has a plugin that will search for and delete duplicate messages but it runs over imap which crippled the server trying to handle all the requests.

Using Google I stumbled across this solution for finding and deleting duplicate messages using reformail but after getting reformail installed I found it to be very slow and the number of messages to delete didn’t add up so I had to abandon this approach.

In the end I decided to write my own PHP script that would cycle through the specific mail directory, search for duplicate messages based on the Message-Id (or a checksum of the email if not available) and then delete the unnecessary, duplicate emails. It worked a treat, and went through the 27,000 emails in less than 5 minutes! If anybody wants the code, its below!

Continue reading “Deleting duplicate mdir emails using php”