How to force TLS v1.0 in PHP

PHP 5.6+ Users

This is a new feature as documented on the PHP 5.6 OpenSSL Changes page.

At time of writing this, PHP5.6 is in Beta1 and thus this isn’t overly useful. People of the future – lucky you!

Everyone Else

The title of this post isn’t strictly accurate as this isn’t possible in PHP < 5.6. However, when working around a problem where a supplier’s API server wasn’t correctly negotiating TLSv1.2 down to its supported TLSv1.0, sending a small subset of ciphers seemed to allow negotiation to complete correctly. This was done by setting some SSL options in the stream context.

I created a context passing it the set of permitted ciphers in CIPHERS(1) format. Code below:

  1. [
  2. 'ssl' => [
  4. ],
  5. ]
  6. );


In a lot of cases, you can just use stream_context_set_default to set this info into the default stream context.

Special Note for SOAP Users

PHP’s SOAP client doesn’t seem to use the default stream context. As such, assign a stream context handle to a variable, as demonstrated above and pass this as a parameter to the SOAPClient constructor:

  1. $soap_client = new SOAPClient('', array('stream_context' => $context));


The list of ciphers above came from OpenSSL‘s supported cipher list on the server in question. The command openssl ciphers -v will give you all of the supported ciphers and which crypto protocols they apply to. I filtered out TLSv1.2 and passed this list to the stream_context_create in the above example.

The following shell command should give you what you require:

  1. openssl ciphers -v | grep -v 'TLSv1.2' | cut -d ' ' -f 1 | tr "\n" ':'


Raspberry Pi Project – A 1960s wallbox interfaced with Sonos

The inspiration for this project came from @nivnov pointing me to I had somewhat of a different take on the circuitry and software than it’s author, Steve Devlin. The pulse train of my Wallbox was also different. This is all explained below.

The Wallbox

A wallbox is a device dating from circa 1950s USA. It was placed on tables in diners to increase revenue from Jukeboxes. It contains no music, nor does it have a speaker. It could be considered a remote control for the diner’s main Jukebox.

I opted for a Seeburg Wall-O-Matic 100 Type 3W100. Wallboxes will be heavily mechanical with very basic circuitry. They will function such that a key combination will be output as a train of electrical pulses caused by an arm rotating over a set of electrical contacts.

Seeburg Wall-O-Matic 100

Seeburg Wall-O-Matic 100

Powering the Wallbox

The wallbox would have, traditionally, been powered from the jukebox. To use it standalone for this project, an AC-AC transformer was required. My Wallbox required 25VAC at a maximum of 3 amps. I used a Torodial transformer, mounted with its mounting washers, in a die-cast aluminium box. It kicks out a lot of heat so the box acts as a decent enough heatsink.

The wall boxes are traditionally unearthed. This seemed like a bad idea so I attached an earth to it via one of the unit’s screws.


Wallbox Transformer

Wallbox Transformer


Interfacing with the RasPi

Steve’s circuit is floating and I managed to fairly impressively melt a RasPi by inadvertently forcing 25vAC through it via the gnd. In the end, I opted for a circuit that isolated the Pi from the Wallbox. A diagram is below:

Raspberry Pi Wallbox Interfacing Circuit

Raspberry Pi Wallbox Interfacing Circuit

For the uninitiated, an Opto-isolator is an LED and a light activated transistor bundled into a single chip – it’s like a relay. In my case a DIP. I used a Vishay 4N25 but anything similar to that would be fine. The left of the circuit connects to the Wallbox’s 25vAC and Signal terminals; the right connects to the RasPi. I also hooked up 4 LEDs to the Pi to provide some kind of debug output in the event of software errors.

Raspberry Pi Seeburg Wallbox

Raspberry Pi Seeburg Wallbox

Decoding Pulses

As I eluded to above, the key combination is electrically pulsed by a rotating arm. Steve had the good fortune of having an oscilloscope to hand; I sadly didn’t. Rather, I started on the code that would decode the pulses and, initially, used it as a timer. It was clear, after filtering jitter, that the pulse train had a clear time gap in it. In Steve’s case, this gap represented a break between the letters and numbers. In my case, it represented the 20s in a base 20 numbering system.

The pulse train is sequential to represent A1 through to K0. If X represents the number of pulses before the gap and Y represents the number of pulses after the gap, X increments from 1 to 20 whilst Y stays at 1. Y then increments and X resets back to 1. The cycle repeats through to X=20, Y=4 for K0.

The maths was thus:

  • Decrement both the pre and post gap counts as 0 is represented by 1 pulse
  • The letter is (2 * post-gap-count) + (1 if pre-gap-count is > 10)
  • If letter was worked out to be > ‘H’, add 1 as the wallbox doesn’t have an I
  • The number is the pre-gap-count modulus 10 (i.e. pre-gap-count % 10)

Handling kernel level interrupts came with some problems of its own. Most notably, having to think sort-of-thread-safe when writing the code. The interrupt can… well… interrupt code processing at any time. If this is part way through a calculation, things get funky. This code is what I came up with.

Interfacing with Sonos

The Sonos “API” is a UPNP bastardization and is, frankly, horrible. PHP being my strongest language, I thought this the best way to tackle it (hence the C code’s interface with an external programme). I forked a pretty crappy Sonos class on GitHub and set about fixing/improving it. I’ve added multicast Sonos device detection and, most importantly for my requirements, Spotify support. The resulting code is on my GitHub account. Also here’s my usage of the class.

Track Cards

The Wallbox can handle 100 tracks. I had to get these into a parsable list for the Sonos code but also to print up some cards to go in the Jukebox. The Spotify API is also shocking. The best course of action I found was to use the Spotify play button generator to make an HTML page with the playlist data and then parse it with some throw-away code. Here’s the throw-away code. Don’t judge me…

Being a budding designer in my spare time, I knocked up some track card templates in Microsoft Paint. These are below:

Jukebox card template

Jukebox card template

Jukebox card template

Jukebox card template

I then used some more throw-away code to populate these with the track info. That throw-away code is here. Again, don’t judge!

Populated jukebox card

Populated jukebox card

I may have stumbled across a loophole in the Staples pricing system as they printed these on cards and individually cut them all out for not a lot of money.

The Finished Article

I mounted it on the wall and put the pi, circuitry, transformer, etc. on the other side of the wall to keep things clean. Picture below.

Mounted wall box

Mounted wall box

Here’s a video:

PHP: Interfacing with HeatMiser WiFi thermostats

PHP: Interfacing with HeatMiser WiFi thermostats

I’m quite a big fan of writing code that interfaces with physical data. It somehow feels more fulfilling to see or feel the results.

On top of my repertoire of the Foscam FI8908W Recorder and TK110 GPS Tracker Server, comes a PHP interface for HeatMiser WiFi Thermostats. This too can be found on GitHub. The code is fairly self explanatory and the docs should point you in the right direction. The library will read from and write to HeatMiser WiFi thermostats. It’s tested on the PRT-TS but should probably work on others. If not, fix it up and submit a pull request on Github (or comment here).

If you came looking for an insight into this Theromstat’s binary network protocol, there’s a decent guide released by HeatMiser on the topic. This can be found on their website. The guide can be a lot to get your head around so hopefully my code will be enough to aid you in this area. Failing that, or if you’re more of a camel, there’s a good Perl implementation on Google Code. The Perl implementation is more of an example than an interface-able library but it’s a great starting point and provided a lot of inspiration for my PHP implementation.


PHP: Find number of weeks in a given month

The following function finds the number of weeks in a given month, assuming Monday as the first day of the week.

  1. <?php
  2. function weeks_in_month($month, $year) {
  3. // Start of month
  4. $start = mktime(0, 0, 0, $month, 1, $year);
  5. // End of month
  6. $end = mktime(0, 0, 0, $month, date('t', $start), $year);
  7. // Start week
  8. $start_week = date('W', $start);
  9. // End week
  10. $end_week = date('W', $end);
  12. if ($end_week < $start_week) { // Month wraps
  13. return ((52 + $end_week) - $start_week) + 1;
  14. }
  16. return ($end_week - $start_week) + 1;
  17. }


PHP: Find the last day of the month

In PHP you can pass the t format character to the date() function in order to get the number of days in the month. Since this number also happens to be the date of the last day of the month, you can use this to your advantage:

Get the last day of the current month:

  1. <?php
  2. echo date('t F Y'); // e.g. 31 December 2011
  3. ?>

Get the last day of the month a timestamp falls in:

  1. <?php
  2. echo date('t F Y', 132456628); // e.g. 31 March 1974
  3. ?>