Skip to content

Instantly share code, notes, and snippets.

View jonsherrard's full-sized avatar
💭
Working

Jon Sherrard jonsherrard

💭
Working
View GitHub Profile
@jonsherrard
jonsherrard / MY_Controller.php
Created April 25, 2012 14:51
CodeIgniter MY_Controller template. Requires ion_auth spark. Redirects user if not logged in. Stores the user's previous page in session data.
<?php
class MY_Controller extends CI_Controller {
function __construct () {
parent::__construct();
if (!$this->ion_auth->logged_in())
@jonsherrard
jonsherrard / PHP_Sanitize.php
Created April 25, 2012 14:54
Clean any string to make it URL friendly. Removes characters and whitespace
<?php
function sanitize($string, $force_lowercase = true, $anal = false) {
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
"}", "\\", "|", ";", ":", "\"", "'", "&#8216;", "&#8217;", "&#8220;", "&#8221;", "&#8211;", "&#8212;",
"—", "–", ",", "<", ".", ">", "/", "?");
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "-", $clean);
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
return ($force_lowercase) ?
@jonsherrard
jonsherrard / String_Generator.php
Created April 25, 2012 14:59
Random string generator a-la imgur urls etc.
<?php
function generate_string($length) {
$chars = "0123456789abcdefghijklmnopqrstuvwxyz";
$string = "";
for ($i = 0; $i < $length; $i++) {
$string .= $chars[mt_rand(0, strlen($chars) - 1)];
}
@jonsherrard
jonsherrard / salusbury_email_1.html
Created May 18, 2012 12:29
salusbury_email_1.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- Facebook sharing information tags -->
<meta property="og:title" content="*|MC:SUBJECT|*" />
<title>Salusbury Rooms Newsletter #1</title>
<style type="text/css">
@jonsherrard
jonsherrard / .bash_profile
Created July 30, 2012 00:05
.bash_profile
## ALIASES
alias home='cd ~/'
alias ls='ls -ls -G'
alias webfaction='ssh web***.webfaction.com -l ************'
alias htdocs='cd /Applications/MAMP/htdocs'
##
CDPATH='.:~:/Applications/MAMP/htdocs'
@jonsherrard
jonsherrard / hosts
Created July 30, 2012 00:35
Hosts File
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
@jonsherrard
jonsherrard / backbone-cakefile-snippet.coffee
Created September 17, 2012 06:04
Cakefile with delay
# Double check file structure before use
task 'build:coffee', 'build src/client.js file from source files', (options) ->
files = content = []
finished = {}
fs.readFile 'src/client/coffee/base.coffee', 'utf8', (err, contents) ->
content.push contents
for dir in ['views', 'collections', 'models'] then do (dir) ->
delay = (ms, func) -> setTimeout func, ms
if dir is 'models'
@jonsherrard
jonsherrard / distance.coffee
Created September 20, 2012 17:04
Coffeescript to convert to latitudes and longitudes into a distance
distance = (lat1, lon1, lat2, lon2) ->
R = 6371 # km (change this constant to get miles)
dLat = (lat2 - lat1) * Math.PI / 180
dLon = (lon2 - lon1) * Math.PI / 180
a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2)
c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
d = R * c
if d > 1
return d + "km"
else return Math.round(d * 1000) + "m" if d <= 1
@jonsherrard
jonsherrard / horrible.sql
Created September 21, 2012 14:49
horrible sql
Select *,
acos(sin($lat)*sin(radians(event_latitude)) + cos($lat)*cos(radians(event_latitude))*cos(radians(event_longitude)-$lon))*$R As distance
From (
Select search_view.*, images.image_url
From search_view, images
Where search_view.event_image_id = images.image_id
AND event_start > ?
And event_latitude>$min_lat And event_latitude<$max_lat
And event_longitude>$min_lon And event_longitude<$max_lon
) As FirstCut