SSH Keychain Access in Mac OSX Sierra

Prior to Sierra keys were persisted between logins – and with an update to ssh Keychain is no longer used by default. It can get frustrating to be prompted to enter your passphrase for a key after an upgrade and after each restart.

In your ~/.ssh/config file you will need the following lines:

Host *
    UseKeychain yes
    AddKeysToAgent yes
    IdentityFile ~/.ssh/id_rsa

Change ~/.ssh/id_rsa to the filename of your private key and if you utilise multiple keys also add an IdentityFile line for each of them.

You also need to initially add the key to save the passphrase to Keychain for the first time using:
ssh-add -K ~/.ssh/id_rsa

SourceTree broke my SSH keys

Sourcetree is a great free git GUI client for Mac and Windows. A recent update (2.4) really broke my workflow however and it took me longer than it should have to figure out what the issue was.

It seems Atlassian are enforcing a behavioural naming convention for ssh keys, which seems somewhat crazy. Rather than go through and change all my own naming conventions to align with theirs I simply created some symlinks. Problem solved.

The format needed is: yourbitbucketusername-BitBucket.pub

Given the syntax for creating a symbolic link:
ln -s /path/to/original/ /path/to/link
You’ll need something along the lines of:
ln -s ~/.ssh/id_rsa.pub ~/.ssh/boolean-BitBucket.pub

CodeIgniter Cart class product name restrictions

codeigniter logo

The CodeIgniter cart class has a non immediately apparent feature regarding special characters in the Product Name. Within the Cart library the line
var $product_name_rules = '\.\:\-_ a-z0-9'; // alpha-numeric, dashes, underscores, colons or periods will strip out any non defined special characters. While you could hack this line, the correct way to do this is to extend the library and create an over-ride.

Create a new file application/libraries/MY_Cart.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Cart extends CI_Cart {

function __construct() {
parent::__construct();

// Remove limitations in product names
$this->product_name_rules = '\d\D';
}
}

Note this will allow all characters, you may want to limit to specified special characters.

MySQL upgrade and planned outage announcement

mysql_logo

MySQL v5.1 Upgrade

Our upstream provider is upgrading MySQL on your server from version 5.0 to version 5.1 as part of their commitment to web hosting security and performance.

This is an important change to ensure continued compatibility with current and future software and to ensure the stability and security of services for all customers.

For most users this change will have minimal impact on websites hosted with us and the latest releases of popular software such as Joomla and WordPress are already compatible with this upgrade.

This is a good time for you to ensure your software installations are current because you could experience problems if it has been designed around earlier versions of MySQL. We also ask that you check to make sure that any custom code is compatible with the newer version of MySQL.

For more detailed information about this upgrade please visit the official MySQL page Upgrading from MySQL 5.0 to 5.1

The upgrade is scheduled to start between the 30th and 31st of July 2013. The upgrades will begin after 21:00hrs GMT and shouldn’t take any longer than 60 minutes.

Teensy powered Apple mechanical keyboard

I learned to type sometime in the 80’s on a manual typewriter and presumably this is why I am fixated on mechanical keyboards. If you’re not much of a touch typist (or keyboard geek) you’re probably not aware that modern (inexpensive) keyboards are an abomination unto typing.

The IBM Model M keyboard is perhaps the most famous clicky keyboard with manufacturing spanning 1984 until the late 90’s and a company purchased the manufacturing line and still produces a variant to this day. Their enduring popularity and sometimes surprising price tag on the second hand market is testimonial to their design and build quality.

Modern day mechanical keyboards are manufactured and popular with typists, gamers, and aficionados. Das Keyboard offer undeniable quality for Mac, PC and Linux. They even offer an uber-geeky blank keycap variant to totally confound your visual based two fingered typing coworkers. Due to their specialist and somewhat niche market however these keyboards cost twenty times that of a cheap membrane keyboard.

History lesson and personal typing preference aside however I’m also a fan of retro computers so when I learned of a project to modify a keyboard from the late Steve Jobs NeXT computer company into a modern functioning USB device I was more than interested.

As I didn’t have a somewhat rare NeXT keyboard in my junk box I tracked down an older Apple mechanical Extended Keyboard II for a tenth of the price of a second hand Model M.
NeXT used a proprietary communication protocol to link their keyboard and computer, and early Apple did exactly the same thing with Apple Desktop Bus (ADB) – an early ancestor to our modern day device independent USB protocol.
Whilst there have been various commercial ADB to USB adapters produced over the years building one from scratch proved to be a much more satisfying experience.

After purchasing a Teensy 2.0 USB development board I downloaded the necessary software

I cannot thank the Geekhack crew enough for providing this source code for others to utilise. They saved me hours of reverse engineering and hardware sniffing and made this project a breeze.

Unlike the super-friendly Arduino IDE I had to manually build and compile my own firmware from the command line before flashing it to the device using Teensy Loader. Presumably because I was using a newer version of the compiler than the author I received a build error:
make: *** [obj_adb_usb/common/xprintf.o] Error 1
which was rectified by modifying the code in config.h to include a new conditional
/* key combination for command */
#ifndef __ASSEMBLER__
#include "adb.h"
#include "matrix.h"
#define IS_COMMAND() ( \
matrix_is_on(MATRIX_ROW(ADB_POWER), MATRIX_COL(ADB_POWER)) \
)
#endif

Be sure to keep the original endif below the code block when pasting the patch.

Update 02/06/2014 Somehow I missed the Teensyduino extension which provides a more familiar GUI for those not wanting to experiment with the command line. Thanks Nantonos!

The Teensy board needed to be connected to the keyboard power 5v, ground, and signal. As I placed the board inside my keyboard case (now you see why size is important) I didn’t need any further modifications. Other people modifying this keyboard who placed a significant length of wire (such as an ADB cable) between the keyboard and Teensy needed a pullup resistor between the signal and power lines. I had to use a 1K resistor when testing using a breadboard and external cable, but after placing the Teensy inside the keyboard with virtually no wire length it became unnecessary.

Connect the keyboards ADB (data) pin 1 to Teensy pin F0, pin 3 to Teensy VCC (5v) and pin 4 to Teensy GND (ground). If you need a pullup resistor (in the 1-10k range, you may need to experiment) it will link ADB pins 1 and 3.

alps circuit board
High quality ALPS keyswitches are what make all the difference
alps circuit board
ADB socket removed, wires tapped in to the circuit board and heatshrunk ready to attach to the Teensy
Teensy installed
Teensy connected to USB and ready to be installed. I wrapped it in insulation and tucked it out of harms way.
Completed keyboard
47cm wide and weighing in at 1.7kg. Amusingly the MBP it is attached to weighs 2.02kg.

I love the coloured old apple logo top left and whilst it certainly creates the distinctive clacking sound of a mechanical keyboard it is not unbearably loud and coworker irritating. Perhaps Jethro Carr could have used one and avoided the banning of his beloved Model M’s.

If you would like to hear the sound of this keyboard head on over to Shawn Blanc’s article.

Edit 17/02/2014 I have just made another of these keyboards as I’ve become accustomed to using them and felt I was missing out at while I was at work. I had a bit of difficulty tracing the ADB pin outs this time as it was a slightly different keyboard (Apple Extended) A great reference is the ADB Apple Desktop Bus pinout

CodeIgniter routes and .htaccess

CodeIgniter has a robust routing module as part of its MVC architecture. A trap for the inexperienced arises when the default controller works (e.g. site.com) but anything else such as mysite.com/dashboard goes to a server based 404, not the CodeIgniter one.

Solution: in your config.php file ensure this line is present
$config['index_page'] = '';
and place the following code in an .htacess file
Options -Indexes
Options +FollowSymLinks

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>

# activate URL rewriting
RewriteEngine on

# do not rewrite links to the documentation, assets and public files (your folders here)
RewriteCond $1 !^(images|assets|uploads|js)

# do not rewrite for php files in the document root, robots.txt or the maintenance page etc
RewriteCond $1 !^([^\..]+\.php|robots\.txt|maintenance\.html)

# rewrite everything else
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>

# If we don't have mod_rewrite installed, all 404's can be sent to index.php, and everything works as normal.

ErrorDocument 404 index.php

</IfModule>

MySQL datatype for storing IP addresses

The best way to store a (version 4) IP address in a MySQL database is as an integer. This may sound strange but MySQL has two powerful functions to enable you to store an IP address as an unsigned INT. Searching an integer is much faster than searching a string and additionally integers take up less storage space which is great when working with large datasets.

mysql> SELECT INET_ATON('10.0.5.9');
---------------------
INET_ATON('10.0.5.9')
---------------------
167773449
---------------------
1 row in set (0.00 sec)

and the reverse function

mysql> SELECT INET_NTOA(167773449);
---------------------
INET_NTOA(167773449)
---------------------
10.0.5.9
---------------------
1 row in set (0.00 sec)

Speeding up PHP FOR loops

Refactoring some client code the other day I found a lot of inefficient logic. Whilst this example is written in PHP the concept is applicable in other programming languages.

Many people use for loops in the belief that they are faster than a foreach, and while this is often the case it can be slower and less flexible if you make some simple mistakes. Consider the following:

A slow FOR loop

$text="thequickbrownfoxjumpsoverthelazydog";
for($i=0; $i < strlen($text); $i++){ // Loop through the characters
echo ( substr($text,$i,1) == 'o' ) ? "Its an o" : "Not an o"; // Display true or false as relevant
}

A faster FOR loop

$text="thequickbrownfoxjumpsoverthelazydog";
$length = strlen($text); // obtain length of the string
for($i=0; $i < $length ; $i++){ // Loop through the characters
echo ( substr($text,$i,1) == 'o' ) ? "Its an o" : "Not an o"; // Display true or false as relevant
}

The second loop is faster because it does not evaluate the string length each time the loop runs strlen($text) as in the first example. Small differences like this can have a large effect on the speed of scripts especially if there are many loops or iterations.