Skip to content

Instantly share code, notes, and snippets.

@thsutton
thsutton / ip_in_block.php
Created March 17, 2011 04:23
Function to check membership of an IPv4 address in an IPv4 CIDR block.
<?php
/**
* Check whether a IPv4 CIDR notation block contains an IP address.
*
* @param $cidr string
* An IPv4 CIDR block like "192.168.56.0/24".
* @param $ip string
* An IPv4 address like "192.168.56.101".
* @return bool
# modules/apache/manifests/init.pp
#
# Configure and manage Apache (in the Debian manner).
#
class apache {
# Configuration that might need to be tweaked.
$apache2_mods = "/etc/apache2/mods"
$apache2_sites = "/etc/apache2/sites"
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8 as C (pack, unpack)
import qualified Data.ByteString.Lazy as L (fromChunks)
import qualified Data.ByteString.Lazy.Char8 as LC (pack)
import Data.Digest.Pure.SHA (hmacSha1, showDigest)
import Data.List (sort)
import Network.URL (URL(..), importURL, exportURL, exportParams, add_param)
-- | Calculate the SHA-1 HMAC of the parameters and include it as the parameter "h".
signParameters :: ByteString -- | The key for HMAC.
@thsutton
thsutton / normalise-path.php
Created January 10, 2011 03:02
Normalise paths in PHP
<?php
/**
* Normalise a file path string so that it can be checked safely.
*
* Attempt to avoid invalid encoding bugs by transcoding the path. Then
* remove any unnecessary path components including '.', '..' and ''.
*
* @param $path string
* The path to normalise.
* @param $encoding string
@thsutton
thsutton / README.md
Created December 31, 2010 12:40
Example of looping in Heist templates

Looping tags in Heist templates

This is a small example demonstrating how to implement looping template tags ("splices") with the Heist template system that is the default with the Snap Framework.

The src/Site.hs file contains the interesting bits:

  • The loop handler which simply renders the [loop.tpl
project="$0"
if [ -z "$project" ]; then
project="project"
fi
mkdir "$project"
cd "$project"
# Create the new cabal-dev sandbox and install snap
cabal-dev install snap
@thsutton
thsutton / enumerator-filter.hs
Created December 15, 2010 09:42
A first attempt at making an enumeratee equivalent to the filter function for lists.
import Prelude hiding (sequence, map, head, filter)
import Control.Monad.Trans (lift, MonadIO, liftIO)
import Data.Enumerator
-- | Enumerator analogue of 'Data.List.filter'.
filter :: Monad m => (a -> Bool) -> Enumeratee a a m b
filter pred (Continue k) = do
x <- head
case x of
@thsutton
thsutton / response-header.js
Created November 6, 2010 09:17
Get the value of an HTTP response header in JavaScript
/**
* Read the value of a header in the current document.
*
* This uses a [single] XMLHTTPRequest to do a HEAD of the current document
* and fetch HTTP response header values. Note that the implementation is
* rather stupid in that it spins waiting for the XMLHTTPRequest to complete
* if it hasn't been called yet.
*
* @param name string
<?php
/** @file
*
* Allow users to save their progress and resume forms.
*
* @author Thomas Sutton <[email protected]>
* @copyright Bouncing Orange
* @created 2010-11-01
*/
Multiple Step Forms
===================
This module implements a techniques that can make your Drupal forms easier to
implement and easier to use: [breaking a form into multiple steps][1].
[1]: http://www.appnovation.com/create-multiple-step-form-drupal-6