Skip to content

Instantly share code, notes, and snippets.

@ianthrive
ianthrive / sql-list-months-days.sql
Created October 19, 2012 11:07
SQL: list all days in a month given a date within that month
PREPARE test AS
WITH RECURSIVE T(n) AS (
SELECT date_trunc('month', $1::date)::date AS start
UNION
SELECT n+1 FROM T WHERE n < date_trunc('month', $1)::date + '1 month'::interval - '1 day'::interval
)
SELECT * FROM T;
EXECUTE test('15 nov 2012');
@ianthrive
ianthrive / dir-listing.php
Last active May 2, 2020 16:48
Directory listing of file repository on bluffdale.com
if(is_dir($currentPath)) {
?><table class="file-list"><?
$scan = scandir($currentPath);
array_shift($scan);
array_shift($scan);
foreach($scan as $name) {
if($name[0] != '.' && $name[0] != '_') {
$path = $currentPath.'/'.$name;
$icon = 'unknown';
if(is_dir($path)) {
@ianthrive
ianthrive / safari-validate-popup.js
Created October 28, 2012 04:06
Make HTML5 form validation appear the same way in all recent browsers as Safari
window.addEventListener('load', function(event) {
// for each form on the page
for(i=0; i < document.forms.length; i++) {
f = document.forms[i];
// add a 'submit' event listener
f.noValidate = true;
// except for safari, the event never triggers without noValidate=true
f.addEventListener('submit', function(event) {
// if form is not valid don't let it submit
if(!event.target.checkValidity()) {
@ianthrive
ianthrive / bcrypt-example.php
Last active October 13, 2015 12:37 — forked from rk/bcrypt.php
Simple bcrypt object to wrap crypt() with.
<?php
// Originally by Andrew Moore
// Src: http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php/6337021#6337021
//
// Heavily modified by Robert Kosek, from data at php.net/crypt
class Bcrypt {
private $rounds;
private $prefix;
@ianthrive
ianthrive / macos-save-new-documents-to-cloud.sh
Created February 19, 2013 11:12
Disable/enable the default iCloud selection in the save window of new documents.
defaults write NSGlobalDomain NSDocumentSaveNewDocumentsToCloud -bool false
defaults write NSGlobalDomain NSDocumentSaveNewDocumentsToCloud -bool true
<?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);
}
?>
@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
@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 / 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 / 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();