Category Archives: Web

Changing XenServer bonding mode back to default (or reducing bond switchover up-delay)

You need to get the pifs again (or ideally, find the lines you used to change the mode in your bash history) – see here for getting pifs.

Then use the following:
xe pif-param-set uuid=bd26334f-f7ff-3206-e5b2-4c017c51907c other-config:bond-mode=balance-slb other-config:bond-updelay=200

This will set your bond back to balance-slb, and reduce the up-delay time (see here for the issue – although I can’t actually confirm right now that the issue still exists in XenServer 5.6, will try and find out and post here if I do*).

*ps, just doing cat /proc/net/bonding/bond0 will tell you.

Pool memory errors in apache

I was getting this error a lot on a Drupal site:

Warning: include(): Unable to allocate memory for pool.

Turns out it’s not the apache or php settings, but APC (pecl module).

To fix, add the following to your php.ini file:

apc.shm_size = 64 (adjust value to your server capabilities)


Transparent tiled watermark using GD with PHP

This is a basic method for dynamically adding tiled watermarks to images on your site.  It uses the .htaccess file to give the images to the PHP script which returns the image plus watermarking.

1) Create the image you want to be the watermark as a png with the opacity you want to be used (we’re not lowering the opacity later), and make sure to add extra space around the image according to how big you want the tiles.

2) Add this script to your site as watermark.php:

$watermark = imagecreatefrompng(‘/path/to/your/watermark.png’);
$imagesrc = ‘/path/to/image/’ . $_GET[‘src’];
$type = mime_content_type($imagesrc);
  case ‘image/jpeg’:
    $image = imagecreatefromjpeg($imagesrc);
  case ‘image/png’:
    $image = imagecreatefrompng($imagesrc);
  case ‘image/gif’:
    $image = imagecreatefromgif($imagesrc);
    $image = null;
  # set dimensions
  $image_width = imagesx($image);
  $image_height = imagesy($image);
  imagealphablending($watermark, TRUE);

  # create the tile and overlay

  # output image
  header(‘content-type: image/jpeg’);

  #clean up


3) Add rewrite rules in .htaccess.  Notice the $_GET[‘src’] in the script above; we need to hand this variable to the script.  In this example, anything in my gallery folder, or with the filename starting with wmark- will be passed to the script:
RewriteRule ^(.*)wp-content/uploads/wmark-(.*) $1path/to/watermark.php?src=wp-content/uploads/wmark-$2
RewriteRule ^(.*)wp-content/uploads/image-gallery/main_gallery/(.*) $1path/to/watermark.php?src=wp-content/uploads/image-gallery/main_gallery/$2

# If you want to exclude a particular image (excludeme.jpg), add this ABOVE the other rules.  The [L] tells it to ignore other rules
RewriteRule ^wp-content/uploads/excludeme\.jpg$ – [L]


The script will only handle jpg/gif/png file types; if it’s possible that your rewrite rules will pass any other file types to the script, make sure to use (.*\.(gif|jpg|jpeg|png)) if it’s possible instead of just the basic (.*) in your rewrite rules.

WordPress Google Calendar Events plugin notes

I’ve just installed this awesome plugin onto a WP blog:

Had two issues I want to note:

1) Use the XML feed from Google, not the iCAL feed, or it will give errors

2) The timezone wasn’t working properly using default settings (it uses WP’s time); I realised this problem actually lies with WP – by default WP was using UTC+0 for my timezone (London), which is wrong, as now we’re on DST, which is UTC+1, changed it to London rather than the UTC offset and it’s working fine.
(Suprised I haven’t noticed this before, although I can’t actually think of any WP sites I’ve made where the post time is displayed – except this one at the moment, as I haven’t modified it at all yet – so it’s never been a problem.)

Drupal 7 custom template files per content type

Very useful – got this from here where you can also find the one for D6.

As the comment suggests, if you have a content type called ‘blog’, you can create a template file to apply to nodes with this content type only called page–blog.tpl.php.


function themeName_preprocess_page(&$vars, $hook) {
if (isset(
$vars['node'])) {
// If the node type is "blog" the template suggestion will be "page--blog.tpl.php".
$vars['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $vars['node']->type);


Drupal 7 functions during JOINs / matching the URL alias with the node ID using a dynamic query

While Drupal 7 dynamic query JOINs do accept functions (CONCAT at least), I had a tough time trying to figure out how to get them to work.

In my case I wanted to pull the URL alias out of the database along with a bunch of other information on a handful of nodes (don’t use this for lots of nodes, if you can help it, as MySQL performance will not be good).  This is easy if you already have the node ID before starting the query, however it can be tricky if you don’t as the node ID is not in the url_alias table.

The only way I could get it to work was like this:

$query->join(‘url_alias’, ‘ua’, ‘ua.source = CONCAT(\’node/\’,n.nid)’);

As you’ll notice, this will only work with taking out aliases for nodes, unfortunately.

I also tried the following which DO NOT WORK:

// substring instead of concat
$query->join(‘url_alias’, ‘ua’, ‘n.nid = SUBSTRING(ua.source FROM \’/\’)’);
// another version of substring
$query->join(‘url_alias’, ‘ua’, ‘n.nid = SUBSTRING(ua.source,5)’);
// and variations on the above, adding single, escaped single and double quotes in various places

// concat using double quotes
$query->join(‘url_alias’, ‘ua’, ‘ua.source = CONCAT(“node/”,n.nid)’);

// using concat / substring in placeholders
$query->join(‘url_alias’, ‘ua’, ‘ua.source = :node_source’, array(‘:node_source’ => ‘CONCAT(\’node/\’, n.nid)’));
$query->join(‘url_alias’, ‘ua’, ‘n.nid = :node_source’, array(‘:node_source’ => ‘SUBSTRING(ua.source,5)’));
// and variations on the above, using single/escaped single/double quotes, and different versions of SUBSTRING

// using the addExpression with a join
$query->addExpression(‘CONCAT(:source_pre,n.nid)’, ‘node_source’, array(‘:source_pre’ => ‘node/’));
$query->addExpression(‘CONCAT(\’node/\’,n.nid)’, ‘node_source’);
$query->join(‘url_alias’, ‘ua’, ‘:node_source = ua.source’, array(‘:node_source’ => ‘node_source’));
$query->join(‘url_alias’, ‘ua’, ‘node_source = ua.source’);

// I even tried adding an AND to the join, hoping it had some strange and magical usage
$query->join(‘url_alias’, ‘ua’, ‘n.nid = ua.source AND ua.source = :node_source’, array(‘:node_source’ => ‘CONCAT(\’node/\’, n.nid)’));


I’m sure there are some syntax issues with some of the above examples, but it still seems the dynamic database API is pretty rigid!

mp4 files not streaming over HTTP?

Probably because the metadata is in the wrong place (ie, not at the beginning of the file).

I downloaded two programs to move the metadata; MetaData Mover ( and MP4 Fast Start (, both of which killed my mp4 files, leaving them with 67kb per file.  Perhaps my mp4 files were encoded in a strange way?  They’re recorded using a fairly standard video camera.

In any case, after a long time of searching for the solution, turns out I only needed to do the following:

apt-get install mpeg4ip
mp4creator -optimize myfile.mp4

In my case I have a client that needs to be able to upload streaming videos to her WordPress site; she now just needs to upload the video files directly from her video camera, click a little button I’ve put on the WordPress site to search for unoptimized files and optimize them.  Excellent.

Custom WP plugin ‘Do not have sufficient permissions’ when accessing options page

If you’re using, for example, this tutorial from NetTuts:

Beware this bit of code:

function oscimp_admin_actions() {
add_options_page(“OSCommerce Product Display”, “OSCommerce Product Display”, 1, “OSCommerce Product Display”, “oscimp_admin”);

The fourth option in the array is the URI of the page; the spaces are taken out automatically by WordPress, but the case remains, so if you’re using a case-sensitive web server you’ll get permissions errors when trying to access the page.

Change it to something like ‘oscommerce_product_display’ and you should be fine.