Skip to content

Instantly share code, notes, and snippets.

@ianthrive
ianthrive / filter-select-with-field.js
Last active August 29, 2015 14:02
Filter a <select> list with a <input type="text">.
// depends on jquery
function filterSelectWithField(selectList, searchField) {
selectList = $(selectList);
searchField = $(searchField);
options = selectList.find('option').clone();
timer = null;
function sanitize(str) {
return $.trim(str).replace(/\s+/g, ' ').toLowerCase();
@ianthrive
ianthrive / pgsql_audit.sql
Last active August 29, 2015 14:01
An audit changes on a PostgreSQL table.
CREATE TABLE customers_audit AS (
id SERIAL NOT NULL UNIQUE PRIMARY KEY,
created TIMESTAMPTZ NOT NULL DEFAULT NOW(),
before HSTORE,
after HSTORE
);
CREATE FUNCTION trigger_customers_audit RETURNS TRIGGER LANUAGE plpgsql AS $$
INSERT INTO customers_audit(before, after)
SELECT hstore(OLD), hstore(NEW);
@ianthrive
ianthrive / trigger_update.sql
Created May 13, 2014 15:57
A PostgreSQL trigger function to updated a timestamp column whenever a row is updated.
CREATE OR REPLACE FUNCTION trigger_updated RETURNS TRIGGER LANGUAGE plpgsql AS $$
BEGIN
NEW.updated = NOW();
RETURN NEW;
END;
$$
CREATE TRIGGER updated BEFORE UPDATE ON payment
OR EACH ROW WHEN (OLD.* IS DISTINCT FROM NEW.*)
EXECUTE PROCEDURE trigger_updated();
@ianthrive
ianthrive / createinstallmedia.sh
Created February 13, 2014 16:09
Create installer disk for Mac OS Mavericks
sudo /Applications/Install\ OS\ X\ Mavericks.app/Contents/Resources/createinstallmedia \
--volume /Volumes/Untitled \
--applicationpath /Applications/Install\ OS\ X\ Mavericks.app \
--nointeraction
@ianthrive
ianthrive / orthodrome_km.sql
Created January 23, 2014 09:36
Orthodrome ("great circle") distance between two latitude/longitude points. This function uses the spherical law of cosines formula, rather than the seemingly more popular (probably because it's older) "haversine" formula. It is assumed the earth is a perfect sphere with a radius of 6,371km.
CREATE OR REPLACE FUNCTION public.orthodrome_km(lat1 numeric, lon1 numeric, lat2 numeric, lon2 numeric)
RETURNS double precision
LANGUAGE plpgsql
AS $function$
BEGIN
return acos(sin(radians(lat1))*sin(radians(lat2))+cos(radians(lat1))*cos(radians(lat2))*cos(radians(lon2)-radians(lon1)))*6371;
END;
$function$
@ianthrive
ianthrive / postgresql-update-timestamp-trigger.sql
Created August 18, 2013 00:48
Automatically updated a timestamp column using a trigger in PostgreSQL.
CREATE OR REPLACE FUNCTION updated()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated = NOW();
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
CREATE TRIGGER updated BEFORE UPDATE ON projects FOR EACH ROW EXECUTE PROCEDURE updated();
@ianthrive
ianthrive / mjpeg-grab.php
Last active September 5, 2018 06:49
Grabs a single frame from a motion-JPEG stream. Useful for IP-cameras that have such capability.
<?
function mjpeg_grab_frame($url) {
$f = fopen($url, 'r');
if($f) {
$r = null;
while(substr_count($r, "\xFF\xD8") != 2) $r .= fread($f, 512);
$start = strpos($r, "\xFF\xD8");
$end = strpos($r, "\xFF\xD9", $start)+2;
$frame = substr($r, $start, $end-$start);
fclose($f);
@ianthrive
ianthrive / quicklook-text-selection.sh
Created February 20, 2013 13:13
Enable text selection in QuickLook in Mac OS X.
defaults write com.apple.finder QLEnableTextSelection -boolean YES
killall Finder
@ianthrive
ianthrive / cleanup-openwith-macos.sh
Created February 20, 2013 13:08
Removes duplicate items from the "Open with..." menu.
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user
killall Finder
<?php
function bcrypt($input, $salt=null, $rounds=12) {
if($rounds < 4 || $rounds > 31) $rounds = 12;
if(is_null($salt)) $salt = sprintf('$2a$%02d$', $rounds).substr(str_replace('+', '.', base64_encode(pack('N4', mt_rand(), mt_rand(), mt_rand(), mt_rand()))), 0, 22);
return crypt($input, $salt);
}
?>