Skip to content

Instantly share code, notes, and snippets.

View cballou's full-sized avatar

Corey Ballou cballou

View GitHub Profile
@cballou
cballou / css3-font-face-bold-italic-support.css
Created March 24, 2012 21:29
CSS3 @font-face With Bold and Italic Support
/* normal case */
@font-face {
font-family: "DejaVu Sans";
src: url(‘path-to-font-directory/DejaVuSans.eot’);
src: local(‘☺’), url("path-to-font-directory/DejaVuSans.ttf") format("truetype");
}
/* bold */
@font-face {
font-family: "DejaVu Sans";
@cballou
cballou / youtube-vimeo-embed-urls.php
Created March 27, 2012 15:52
PHP Function to Convert Youtube and Vimeo URLs to Lightbox-Ready Equivalents
<?php
/**
* Given a string containing any combination of YouTube and Vimeo video URLs in
* a variety of formats (iframe, shortened, etc), each separated by a line break,
* parse the video string and determine it's valid embeddable URL for usage in
* popular JavaScript lightbox plugins.
*
* In addition, this handler grabs both the maximize size and thumbnail versions
* of video images for your general consumption. In the case of Vimeo, you must
* have the ability to make remote calls using file_get_contents(), which may be
@cballou
cballou / get-ip-address-optimized.php
Created March 26, 2012 00:51
PHP - Advanced Method to Retrieve Client IP Address
<?php
function get_ip_address() {
$ip_keys = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
foreach ($ip_keys as $key) {
if (array_key_exists($key, $_SERVER) === true) {
foreach (explode(',', $_SERVER[$key]) as $ip) {
// trim for safety measures
$ip = trim($ip);
// attempt to validate IP
if (validate_ip($ip)) {
@cballou
cballou / stripe-proration.php
Last active March 10, 2023 01:40
Calculate a proration percentage given the current timestamp and a current billing period
<?php
/**
* Handle calculating a percentage/fraction (proration) we should charge the
* user for based on the current day of the month before their next bill cycle.
* To use yourself, implement a getSubscription method which returns an object
* containing current_period_start and current_period_end DateTime objects.
*
* @access public
* @return float
@cballou
cballou / one-way-mysql.sql
Created March 25, 2012 13:33
Securing Your PHP Sessions with a Salt (old, use bcrypt)
CREATE TABLE `secure_login` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`email` VARCHAR(120) NOT NULL,
`password` VARCHAR(40) NOT NULL,
`session` VARCHAR(40) DEFAULT NULL,
`disabled` TINYINT(1) UNSIGNED DEFAULT 0,
`created_dt` DATETIME DEFAULT '0000-00-00 00:00:00',
`modified_ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE INDEX `uniq_idx` (`email`)
@cballou
cballou / medusa.sh
Created December 17, 2013 17:10
Bash script to output recent activity in /var/log/auth.log which may be useful for finding nefarious users.
#!/bin/bash
#####################################################
# To run, simply: chmod +x medusa.sh && ./medusa.sh #
#####################################################
# Successful publickey connections
echo '==== Successful SSH Public Key Connections ===='
CONNECTIONS=`grep "sshd.*Accepted publickey" /var/log/auth.log`
while read -r line; do
@cballou
cballou / one-way-mysql.sql
Created March 25, 2012 14:17
Securing Your PHP Sessions with a Random Salt (old, use bcrypt)
CREATE secure_login (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`email` VARCHAR(120) NOT NULL,
`salt` VARCHAR(8) NOT NULL,
`password` VARCHAR(40) NOT NULL,
`session` VARCHAR(40) DEFAULT NULL,
`disabled` TINYINT(1) UNSIGNED DEFAULT 0,
`created_dt` DATETIME DEFAULT '0000-00-00 00:00:00',
`modified_ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
@cballou
cballou / laravel-global-exception-handler.php
Last active September 12, 2021 10:45
Global handling of Laravel exceptions to better support AJAX requests.
<?php namespace App\Exceptions;
use Log;
use Mail;
use Config;
use Exception;
use Illuminate\Auth\Access\UnauthorizedException;
use Illuminate\Session\TokenMismatchException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
@cballou
cballou / example-FILES-array-fix-usage.php
Created March 25, 2012 12:57
Uploading Multiple Files in PHP – Fixing the Array Indices
<?php
// passed by reference
fixFilesArray($_FILES['fieldname']);
// all fixed
var_dump($_FILES['fieldname']);
@cballou
cballou / wordpress-multi-env-configphp-setup.php
Created August 15, 2011 13:54
Wordpress Multi-Environment wp-config.php Setup
<?php
/**
* This code is intended to be added to your wp-config.php file just below the top comment block.
* It should replace the existing define('DB_*') statements. You can add or remove sections
* depending on the number of environments you intend to setup. You should change all values of
* DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST for each environment, making them all distinct
* for security purposes.
*/
// determine the current environment