Skip to content

Instantly share code, notes, and snippets.

View dbspringer's full-sized avatar

Derek Springer dbspringer

View GitHub Profile
@dbspringer
dbspringer / substr.c
Created September 13, 2011 00:05
Substring in C
#include <stdio.h>
#include <string.h>
/* If you're feeling sassy, a macro one-liner w/out checks */
#define substr(dest, src, dest_size, startPos, strLen) snprintf(dest, dest_size, "%.*s", strLen, src+startPos)
/* Python-like idiom of negative values to count from the end of the string. */
void substr (char *dest, const char *src, int startPos, size_t strLen) {
/* Cannot do anything with NULL. */
if (dest == NULL || src == NULL) return;
@dbspringer
dbspringer / scramble.py
Created October 6, 2011 18:25
A function to scramble the inner letters of a word/sentence.
#!/usr/bin/python
# -*- coding: utf-8 -*-
def scramble(unscrambled):
'''
Scrambles the word(s) in unscrambled such that the first and last letter remain the same,
but the inner letters are scrambled. Preserves the punctuation.
See also: http://science.slashdot.org/story/03/09/15/2227256/can-you-raed-tihs
'''
import string, random, re
@dbspringer
dbspringer / gist:1287873
Created October 14, 2011 18:17 — forked from fennb/gist:1283573
nginx microcaching config example
# Set cache dir
proxy_cache_path /var/cache/nginx levels=1:2
keys_zone=microcache:5m max_size=1000m;
# Virtualhost/server configuration
server {
listen 80;
server_name yourhost.domain.com;
# Define cached location (may not be whole site)
@dbspringer
dbspringer / fizzbuzz.py
Created November 1, 2011 21:35
FizzBuzz one liner
# FizzBuzz in one line (minus this comment)
print '\n'.join(['%d %s%s' % (i, 'Fizz'*(i%3==0), 'Buzz'*(i%5==0)) for i in xrange(101)])
@dbspringer
dbspringer / screen.scss
Last active December 19, 2015 00:39
Barebones example of of using Compass & SCSS to use sprites with pseudo selectors. In my icons directory I have a handful of icons (one called wrench.png) which I'm importing for .foo:before.
@import "icons/*.png";
.foo {
font-size: 20px;
color: red;
&:before {
@include icons-sprite(wrench);
width: 32px;
height: 32px;
@dbspringer
dbspringer / some_plugin.php
Created January 28, 2014 01:56
WordPress front-end uploader & current-user only media filter
<?php
class Some_WP_Plugin {
/**
* Init everything here
*/
public function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_filter( 'ajax_query_attachments_args', array( $this, 'filter_media' ) );
@dbspringer
dbspringer / badfunc.sh
Created September 23, 2014 01:56
Recurse through directories looking for bad functions
#!/usr/bin/env bash
dir="."
if [ $1 ]; then dir=$1; fi
bad_funcs="exec system passthru shell_exec escapeshellarg escapeshellcmd proc_close proc_open dl popen show_source ini_set"
for func in $bad_funcs
do
grep -R --include=*.php $func $dir
done
@dbspringer
dbspringer / delegate.php
Created January 2, 2015 18:56
How to add OpenID authentication for your self-hosted WordPress site.
add_action( 'wp_head', 'openid_delegate' );
function openid_delegate() {
echo <<<HTML
<!-- OpenID Delegation -->
<link rel="openid.server" href="https://yoursite.wordpress.com/?openidserver=1">
<link rel="openid.delegate" href="https://yoursite.wordpress.com/">
HTML;
}
@dbspringer
dbspringer / gist:a11b023982068881802d
Created February 12, 2015 18:53
Beer Recipe Table
<div class="beer-recipe">
<div class="beer-details">
<h3>Recipe Details</h3>
<table>
<thead>
<tr>
<th>Batch Size</th>
<th>Boil Time</th>
<th>IBU</th>
<th>SRM</th>
@dbspringer
dbspringer / plugin.php
Created May 7, 2015 21:33
Front-end Media Example plugin
<?php
/**
* Plugin Name: Front-end Media Example
* Plugin URI: http://derekspringer.wordpress.com
* Description: An example of adding the media loader on the front-end.
* Version: 0.1
* Author: Derek Springer
* Author URI: http://derekspringer.wordpress.com
* License: GPL-2.0+