Archive for the ‘PHP Programming’ Category

RoundCube email hangs on “Sending message…”

Sunday, August 16th, 2009

While configuring one of my clients RoundCube installation in combination with Plesk, it would work almost OK, except that when sending mails RoundCube would hang at the spinner with “Sending message…”.
After some digging it turns out that the Plesk-bundled PEAR module directory was before the RoundCube bundled PEAR modules directory. Both contain Mail_Mime, but the Plesk version was older and did not contain the getMessage() function.
Something similar to this will pop up in apache errorlog:
PHP Fatal error: Call to undefined method rcube_mail_mime::getMessage() in /usr/share/psa-horde/program/steps/mail/ on line 492

This was fixed by changing the include_path so that the RoundCube modules come first for this specific vhost.

Don’t know what else will break further up the chain however ;-)

Fixing the Apache “unable to include…” localised errors

Monday, November 10th, 2008

Apache includes a nice feature that displays localised errors. This makes use of Server Side Includes (SSI).
When you use PHP and have it also parse .html documents, your localised errors are broken. In fact, because the initial error will generate new errors, it becomes a mess. You typically see this in your error log:

unable to include "include/top.html" in parsed file /usr/share/apache2/error/HTTP_NOT_FOUND.html.var
unable to include "include/bottom.html" in parsed file /usr/share/apache2/error/HTTP_NOT_FOUND.html.var

I fixed this by using a new extension .err for the error documents.
Snippet from my apache2.conf:

    AllowOverride None
    Options FollowSymlinks Includes

    AddType text/html err
    AddOutputFilter Includes err

    AddHandler type-map var
    Order allow,deny
    Allow from all
    LanguagePriority en cs de es fr it nl sv pt-br ro
    ForceLanguagePriority Prefer Fallback

Then go into /usr/share/apache2/error and rename all files to have .err.var extension (instead of .html.var):

rename 's/\.html\.var/\.err\.var/g' *

Rename the tree files in includes dir:

rename 's/\.html/\.err/g' includes/*

Finally recursively replace the strings in the type-map files too:

perl -pi -e 's/\.html/\.err/g' *

You /usr/share/apache2/error tree now looks something like this:


Now your localised errors should be back again, and you logs will not be flooded anymore.

Array sorting but without articles

Saturday, March 31st, 2007

Sometimes you want to sort an array in PHP, but exclude certain words.
I once needed this to sort a list of artists and bands. It is very common for bandnames to have articles in front of them.
When sorting those, it feels more natural to leave those words out, so that “The Doors” would end up at the letter D, and not T.
This little PHP function will do natcasesort, but will leave out the english articles a, an, the, and the dutch articles de, het and een:

function sort_na($a, $prefix= '/^((d|th)e|an?|een|het)s*/i') {
    foreach($a as $entry){
    $cmp=create_function('$x, $y',
        'return strnatcasecmp($x["name"], $y["name"]);');
    usort($b, $cmp);
    foreach($b as $entry) {
    return $c;