Skip to content

Instantly share code, notes, and snippets.

View davebarnwell's full-sized avatar

Dave Barnwell davebarnwell

View GitHub Profile
@davebarnwell
davebarnwell / StrCSV.php
Last active April 24, 2018 16:18
get and put CSVs from a string instead of a file
<?php
class StrCSV
public function strPutCsv($input, $delimiter = ',', $enclosure = '"')
{
// Open a memory "file" for read/write...
$fp = fopen('php://memory', 'r+');
// ... write the $input array to the "file" using fputcsv()...
fputcsv($fp, $input, $delimiter, $enclosure);
// ... rewind the "file" so we can read what we just wrote...
rewind($fp);
@davebarnwell
davebarnwell / window.console.js
Created January 5, 2018 16:12
Stub out window.console object for old browsers
// Avoid `console` errors in browsers that lack a console.
(function() {
var method;
var noop = function () {};
var methods = [
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
'timeStamp', 'trace', 'warn'
];
@davebarnwell
davebarnwell / REST_HTTP_best_practice.md
Last active May 23, 2018 19:06
HTTP RESTful best practice

HTTP RESTful best practice as bullet point

collected wisdom on REST API best practice.

Use HTTP status codes

  • 200 : OK – Eyerything is working
  • 201 : OK – New resource has been created
  • 204 : OK – The resource was successfully deleted
  • 304 : Not Modified – The client can use cached data
@davebarnwell
davebarnwell / jsError.php
Last active January 3, 2018 15:49
catch and log javascript errors to your server with no dependancy on 3rd party libs
<?php
// Do something more robust in production to check it's a valid post etc
// also use Monolog or similar to log, but hey this is an example :)
$logMessage = sprintf(
'JS_ERROR[%s] URL[%s] on file[%s:%s] UA[%s]',
$_POST['errorMessage'],
$_POST['url'],
$_POST['file'],
$_POST['lineNumber'],
@davebarnwell
davebarnwell / gulpfile.js
Created August 8, 2017 20:30 — forked from franksmule/gulpfile.js
gulp.js that does SASS, JS Concatenation Watching - Tutorial -> http://omcfarlane.co.uk/install-gulp-js-windows/
//*********** IMPORTS *****************
var gulp = require('gulp');
var sass = require('gulp-ruby-sass');
var gutil = require('gulp-util');
var rename = require("gulp-rename");
var map = require("map-stream");
var livereload = require("gulp-livereload");
var concat = require("gulp-concat");
var uglify = require('gulp-uglify');
var watch = require('gulp-watch');
@davebarnwell
davebarnwell / twig_file_date_extension.php
Created August 8, 2017 07:34 — forked from Stoffo/twig_file_date_extension.php
Twig Function to get the file mtime in template for HTTP Caching
<?php
$function_filedate = new Twig_SimpleFunction(
'fileDate',
/**
* @param $file_path
* This function generates a new file path with the last date of filechange
* to support better better client caching via Expires header:
* i.e:
@davebarnwell
davebarnwell / dump_and_die_debug.php
Last active August 10, 2017 12:39
PHP dump args in pre tag and die
<?php
/**
* dump args surrounded in html pre block and die
*
* @param mixed $args,... variable number of arguments
*/
function dd() {
echo '<pre>';
call_user_func_array('var_dump',func_get_args());
echo '</pre>';
@davebarnwell
davebarnwell / README.md
Created July 27, 2017 15:52
use openssl to encrypt and decrypt with a password (keys are better, but this is quick)

Encrypt Archive.zip saving as Archive.zip.dat, you'll be prompted to enter a password and confirm it

openssl enc -aes-256-cbc -in Archive.zip >Archive.zip.dat

Decrypt Archive.zip.dat saving as Archive.zip, you'll be prompted to enter the password you used to encrypt it

@davebarnwell
davebarnwell / git-export.sh
Created July 27, 2017 11:04
Export/Copy the current git branch as pure files to a directory path
#!/usr/bin/env bash
TARGET_DIR=~/temp_dir git archive --format=tar HEAD | (cd $TARGET_DIR && tar xf -)
@davebarnwell
davebarnwell / php_copy_to_paste_board.php
Last active July 30, 2017 16:33
PHP to copy a string to the mac os x paste board
<?php
function copy2clipboard($string){
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "a.txt", "a") // stderr is a file to write to
);
$process = proc_open('pbcopy', $descriptorspec, $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $string);