Google calendar in Mozilla Thunderbird on Ubuntu 11.10

Via the Ubuntu Software Centre:

  1. Search for thunderbird
  2. Click on the search result “Thunderbird Email” and then on the “More Info” button
  3. Enable at least the “Calendar Extension for Thunderbird – Google Calendar support (xul-ext-gdata-provider)”
  4. Install the add-ons
  5. Open your Google Calendar in your web browser
  6. In the left-hand column, under “My calendars”, hover over the calendar of choice and click the down arrow that appears after the calendar name
  7. Select “Calendar settings”
  8. Close to the bottom of the page will be the “Calendar ID” (in my case it is my full email address. Make a note of this ID.
  9. Start up Thunderbird upon completion.
  10. File -> New -> Calendar
  11. Select “On the Network” and click Next
  12. Select “CalDAV”
  13. In the Location field enter: https://www.google.com/calendar/dav/calendar.id.noted.in.point.8/events
  14. Click Next then enter a name for the calendar and set an email account against it
  15. When prompted, enter your username and password for accessing this calendar

Fixing odd characters

One of the biggest complaints that web developers have is their lack of control over end users.

When will we be able to tell them a) not to paste Micosoft Word or b) if you must, paste it into something that doesn’t do formatting 1st.

It has a nasty habit of converting quotes into “smart” quotes as well as messing up characters such as the euro symbol €.

This is the fix I came up with that allows end users to continue to paste from such programs and bring their weird charset issues with them:

function fixChars($text)
{
  $chars = array(
    "/\xe2\x82\xac/" => "€",
    "/\xe2\x80\x99/" => "'",
    "/\xe2\x80\x9c/" => "“", // open quotes
    "/\xe2\x80\x9d/" => "”", // close quotes
    "/\xe2\x80\x93/" => "—",

    "/\x80/" => "€",
    "/\x92/" => "'",
    "/\x93/" => "“", // open quotes
    "/\x94/" => "”", // close quotes
    "/\x96/" => "—",
    "/\xa3/" => "£",
  );

  $find = array_keys($chars);
  $replace = array_values($chars);

  return preg_replace($find, $replace, $text);
}

Downloading SoundCloud Playlists

I like to have music while I’m coding. It breaks the silence while working alone at home.

When I have music playing, it’s best if I don’t have to think about setting up playlists, which is why I used to listen to the radio. The radio has it’s limits though, not enough Ninja Tune I think.

Speaking of Ninja Tune, I <3 their SolidSteel mixes http://soundcloud.com/ninja-tune/sets/solid-steel-radio-shows/

What I don’t like is having to listen via my web browser and their music player in Flash. A recourse hog and not great if you want their music on a device disconnected from the ‘net.

Thankfully, with SoundCloud tracks, you can download any file you wish. The downside to it is that there’s a lot to download. What if you want a whole playlist? A playlist of (currently) 151 tracks for instance?

The script below will download all of the tracks in a set to a directory and create a .m3u playlist at the same time.

Please excuse the justified text and syntax-highlighting gone wrong. View plain, or copy to clipboard and it’ll be fine.

$baseDir = "mp3s";
$soundcloudURL = "http://soundcloud.com/ninja-tune/sets/solid-steel-radio-shows/"; 

$urlBits = str_replace('http://', '', $soundcloudURL);
$urlBits = preg_replace('@/$@', '', $urlBits);
$urlBits = explode("/", $urlBits);

/**
 * http://soundcloud.com/ninja-tune/sets/solid-steel-radio-shows/
 * Array
 * (
 *     [0] => soundcloud.com
 *     [1] => ninja-tune
 *     [2] => sets
 *     [3] => solid-steel-radio-shows
 * )
 */

$storage_dir = $baseDir . "/" . $urlBits[1];
if (!file_exists($storage_dir))
{
  mkdir($storage_dir, 0755, true);
}

$playListFile = $baseDir . "/" . $urlBits[1] . "/" . $urlBits[count($urlBits) - 1] . ".m3u";
if (file_exists($playListFile))
{
  unlink($playListFile);
}

$file = getPage($soundcloudURL);
$rows = explode("\n", $file);

$urli = array();
$pattern = '@"/.+download"@i';
$baseURL = 'http://soundcloud.com';
foreach ($rows as $row)
{
  preg_match($pattern, $row, $matches);
  if (count($matches) > 0)
  {
    $uris[] = str_replace('"', '', $matches[0]);
  }
}

$i = 0;
$uriCount = count($uris);
foreach ($uris as $uri)
{
  $i++;
  echo "\nFetching file " . $i . " of " . $uriCount . "\n";
  $filename = preg_replace("@/" . $urlBits[1] . "/@", "", $uri);
  $filename = preg_replace("@/download@", "", $filename);

  file_put_contents($playListFile, $filename . ".mp3\n", FILE_APPEND);

  if (file_exists($storage_dir . "/" . $filename . ".mp3"))
  {
    echo "File exists, skipping.\n";
    continue;
  }

  $command = "wget " . $baseURL . $uri . " -O " . $storage_dir . "/" . $filename . ".mp3";
  `$command`;
}

function getPage($url, $referer = false, $timeout = 30, $header = false)
{
  $curl = curl_init();

  if(strstr($referer,"://")){
    curl_setopt ($curl, CURLOPT_REFERER, $referer);
  }

  curl_setopt ($curl, CURLOPT_URL, $url);
  curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout);
  curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
  curl_setopt ($curl, CURLOPT_HEADER, (int)$header);
  curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);

  $html = curl_exec ($curl);
  curl_close ($curl);

  return $html;
}

wp-block as a shortcode

In my last post I raved about the excellent plugin wp-blocks by Keir Whitaker and then went on to extend it a little.

Time to extend it a little further, this time by adding a shortcode.

Shortcodes are the handy square bracketed code snippets which conjure up more content and functionality direct from a page or post content.

The wp-blocks plugin is currently intended to work with the theme template files in PHP. Unfortunately (or fortunately?) PHP does not get parsed within the content areas. This does stop malicious code from being included, but it does limit things when you know what you’re doing.

The following code will give you a short code to work with which looks similar to the PHP code snippit you would include in your theme template file.

  /**
   * Gets the block data by 'name' and returns it for use in a content shortcode
   * Requires http://wordpress.org/extend/plugins/wp-blocks/
   *
   * @param string $name
   * @return string
   * @author Iain Cuthbertson
   */
  function get_wp_block_shortcode( $atts ) {
    extract( shortcode_atts( array(
      'name' => '',
    ), $atts ) );

    global $wpdb;
    $block_slug = trim($name);
    $data = (array)$wpdb->get_row("SELECT * FROM {$wpdb->prefix}wpb_content WHERE name = '{$name}' AND active = TRUE");
    if($data) return get_wrapped_block_content($data);
  }

  add_shortcode('get_wp_block', 'get_wp_block_shortcode');

Add that to your theme’s functions.php file and then you will be able to add blocks to your content with, for example:

[get_wp_block name="foo"]

Getting a random wp-block by regex

If you’re used to developing with a CMS such as CMS Made Simple, you would be quite used to having blocks of content separate from the main content system. These blocks can be re-used through-out existing content and within the theme templates.

One might even consider this a core function for a CMS. Sadly it’s missing from WordPress. Keir Whitaker has been working on this missing option and gives us wp-blocks.

It’s a great little plugin, though it currently has a couple of niggles:

  • Apostrophes are escaped in the output: \’
  • One can’t call a block from inside content, it has to be from the theme template.

Despite this, I’m glad that he’s made the plugin available for public use.

Now, time to extend it!

As with global content blocks in CMS Made Simple, wp-blocks does not offer a way to fetch a random content block. I fixed this in CMS Made Simple by writing the Random Global Content Block plugin.

Now I’ve done something similar for wp-blocks:

/**
 * Gets a random block data by 'regex'
 * Requires http://wordpress.org/extend/plugins/wp-blocks/
 *
 * @param string $block_slug_regex
 * @return string
 * @author Iain Cuthbertson
 */
function get_wp_block_random($block_slug_regex) {
  global $wpdb;
  $block_slug_regex = trim($block_slug_regex);
  $data = (array)$wpdb->get_results("SELECT id FROM {$wpdb->prefix}wpb_content WHERE name REGEXP '{$block_slug_regex}' AND active = TRUE"); 
  if (isset($data) && is_array($data) && count($data) > 0)
  {
    $arrayIndex = array();
    foreach ($data as $row)
    {
      $arrayIndex[] = $row->id;
    }
    $randomIndex = mt_rand(0, count($arrayIndex) - 1);

    return get_wp_block_by_id($arrayIndex[$randomIndex]);
  }
}

Copy that code into your theme’s functions.php and then call from within your template with, as an example:

<?php get_wp_block_random('^testimonial_'); ?>

This will then fetch a wp-block with a name starting with testimonial_.

Mimicking wordpress.com’s image resize URIs

To follow up from last night’s entry, I was determined to remove the intermediary step of my_resize_script.php.

I’ve acheived this goal in the mod_rewrite rule, file name and query string now get passed on to timthumb.php.

Updated rules:

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.(gif|png|jpg|jpeg)
RewriteCond %{QUERY_STRING} (w|h)=(.*)$
RewriteRule (.*) /full/path/to/timthumb.php?src=$1&%1=%2&%3=%4 [L]

This will pass both width and height variables into timthumb if they are declared.

Quick breakdown of the rules:

  1. Check that the URI exists as a file.
  2. Check that the URI in question is an image. Expand to other formats as needed.
  3. Check that one or both of width and height are being altered.
  4. Pass the file following to timthumb as variables:
    $1 = filename
    %1 = w or h
    %2 = value of w or h
    %3 = w or h
    %4 = value of w or h

If conditions 1 to 3 all return true, then the rewrite rule in 4 will be executed.

In summary, the URI
http://myrant.net/wp-content/uploads/2010/03/high_res_horsey.png?w=300&h=100

Will be passed on as
http://myrant.net/timtumb.php?src=wp-content/uploads/2010/03/high_res_horsey.png&w=300&h=100

Resize images on the fly without messing with image URLs

[EDIT: I've made this simpler with a follow up post: Mimicking wordpress.com’s image resize URIs]

Exporting a wordpress.com site for use on a standalone wordpress.org install is a joy to set up. The export and import system are simple to use and give little or no issues.

What is a problem is the automatic resizing of images that wordpress.com offers.

One can insert an image in the normal way and then append the image URI with some variables such as width (w) and height (h). This is also a d options, I assume this is for depth, but haven’t looked into it yet.

So an image URI might be http://myrant.net/wp-content/uploads/2010/03/high_res_horsey.png?w=300

This URI would display the image resized to 300px wide and retain the aspect ratio.

Currently, wordpress.org do not offer this facility (that I have seen). So I’ve been working on how it might be done:

1st off, we need to set up a mod_rewrite rule to cater for image URIs that have query string variables. This can be added to a vhost config or the site’s .htaccess file. I prefer to use .htaccess

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.(gif|png|jpg|jpeg)
RewriteCond %{QUERY_STRING} (w|h)=(.*)$
RewriteRule (.*) /my_resize_script.php?file=$1 [L]

Then we need a script (my_resize_script.php) to make use of the variables.

<?php
$urlBits = explode("?", $_SERVER['REQUEST_URI']);
$queryString = explode("&", $urlBits[1]);
$urlVars = array('file' => $_GET['file']);
foreach ($queryString as $bit)
{
	$varBits = explode("=", $bit);
	$urlVars[$varBits[0]] = $varBits[1];
}
?>
<pre><?php print_r($urlVars); ?></pre>

The output of this script as it is would be:

Array
(
    [file] => wp-content/uploads/2010/03/high_res_horsey.png
    [w] => 300
)

As you can see, I’m not actually making use of the variables yet. But all of the information that it does extract can be given to a proper resize script, such as TimThumb.

This is all a bit messy right now. I would much rather be able to pass variables directly into a resize script rather than have to use an intermediary one. If anybody wants to give some pointers, please do :)

Updating WordPress site URLs for different hosts

Why oh why does WordPress insist on storing the site URLs within the database?
Every other project I can think of either keeps it in a config file or doesn’t bother with one and just works with whatever URL it finds itself on.

This is most irritating when you have:

  • more than one developer working on the same project
  • more than one system for hosting the project (dev, test, live, etc)

One way to cope with this irritation is to have a collection of .sql files to handily update the database from the CLI.

For example, a WP site has the URL www.idophp.co.uk on a live host and uses idophp.iain on a dev host.

We would have 2 .sql files such as live_options.sql and dev_options.sql.

In live_options.sql:

UPDATE wp_options SET option_value = REPLACE(option_value, 'http://idophp.iain', 'http://www.idophp.co.uk') WHERE option_value LIKE 'http://idophp.iain%';

In dev_options.sql:

UPDATE wp_options SET option_value = REPLACE(option_value, 'http://www.idophp.co.uk', 'http://idophp.iain') WHERE option_value LIKE 'http://www.idophp.co.uk%';

These settings can be imported over the top of a database without altering anything but the site URLs using:

$ mysql -u idophp -p idophp < live_options.sql

How to get sfFormExtraPlugin working in Symfony 1.4 using Doctrine

I’m talking about this plugin: http://www.symfony-project.org/plugins/sfFormExtraPlugin

For some reason, getting the plugin to work took a lot of Googling and some trial and error.
So here is how I got it working. Hopefully I did this in the ‘correct’ way…

cd plugins
wget "http://plugins.symfony-project.org/get/sfFormExtraPlugin/sfFormExtraPlugin-1.1.3.tgz"
tar zxvf sfFormExtraPlugin-1.1.3.tgz
mv sfFormExtraPlugin-1.1.3 sfFormExtraPlugin
cd ..
./symfony plugin:publish-assets
cd web/js
wget "http://code.jquery.com/jquery-1.4.3.min.js"
wget "http://jqueryui.com/download/jquery-ui-1.8.5.custom.zip"
mkdir jquery-ui
cd jquery-ui
unzip ../jquery-ui-1.8.5.custom.zip
mv jquery-ui/css/smoothness ../css

Edit config/ProjectConfiguration.class.php to add sfFormExtraPlugin as an enabled plugin:

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    $this->enablePlugins(array('sfDoctrinePlugin', 'sfFormExtraPlugin'));
  }
}

Edit apps/<app_name>/config/view.yml to include the JS and CSS:

  stylesheets:    [main.css, smoothness/jquery-ui-1.8.5.custom.css]

  javascripts:    [jquery-1.4.3.min.js, jquery-ui/js/jquery-ui-1.8.5.custom.min.js]

Edit lib/form/doctrine/<your_module_name>Form.class.php to make use of the widgets you want. I’m interested in sfWidgetFormJQueryDate:

  public function configure()
  {
    $this->widgetSchema['created_at'] = new sfWidgetFormJQueryDate(array(
      'image' => '/images/silk_icons/calendar.png',
      'config' => '{}',
    ));
    $this->widgetSchema['updated_at'] = new sfWidgetFormJQueryDate(array(
      'image' => '/images/silk_icons/calendar.png',
      'config' => '{}',
    ));
  }

I have used the calendar.png icon from the famfamfam silk icon set. You can get the set here: http://www.famfamfam.com/lab/icons/silk/

I’ve used the date picker widget for filling in the created_at and updated_at fields. You can replace these values with the fields you need to manipulate in a user friendly way.

[Edit 17/10/2010:]
I’m not going mad, there is an issue with the ‘magic’ fields created_at and updated_at in that they have to be unset to become magic.
Found help here: http://levelx.me/technology/programming/symfony-1-4-doctrine-timestampable-behaviour/
Though the unset lines are better off being added just once to /lib/form/doctrine/BaseFormDoctrine.class.php, rather than each xxxForm.class.php file.

Which Windows applications are used by this web developer?

This list is partly to remind myself what essential apps need to be reinstalled after a system wipe.

But it does have me thinking, what do other web developers use on a daily basis?

Web browsers:

  1. Google Chrome
  2. FireFox
  3. IE
  4. Opera
  5. Safari

FireFox addons:

  1. Adblock Plus
  2. British English Dictionary
  3. ColorZilla
  4. Dummy Lipsum
  5. Firebug
  6. Html Validator
  7. Web Developer
  8. Xmarks

Code editing:

  1. PhpEd
  2. Eclipse PDT
  3. TextPad
  4. vim

Code tools:

  1. SQLyog
  2. WinMerge

System tools:

  1. PuTTY
  2. Vbox
  3. Synergy
  4. TightVNC

File tools:

  1. FileZilla
  2. DropBox
  3. Jungle Disk Workgroup Edition
  4. WinZip
  5. WinRar
  6. 7-Zip

Graphical manipulation:

  1. Paint.NET
  2. Adobe CS
  3. jRuler

“Productivity” apps:

  1. MS Office
  2. Google Apps Sync
  3. Foxit Reader

Communication:

  1. Skype
  2. Zoom phone adapter (use the Vista 64bit driver to get this working on 64bit Windows 7)
  3. X-Chat

CD/DVD tools:

  1. CDex
  2. DeepBurner

Audio/Visual:

  1. Spotify
  2. iTunes
  3. VLC

Dock gadgets:

  1. Digital Clock
  2. GMail Counter

Time off:

  1. Steam

Ace on DHBiT pointed me at http://ninite.com/, how very useful. Will make use of it next time :)