Skip to content

Instantly share code, notes, and snippets.

@olimortimer
olimortimer / gist:e867231fd7b19d17780e
Created November 5, 2015 15:20
Shell: Compare file names only
# Compare file names only, without comparing the contents - good for a quick diff
diff <(cd folder1 && find . | sort) <(cd folder2 && find . | sort)
for f in *; do mv "$f" `echo $f | tr ' ' '_'`; done
@olimortimer
olimortimer / gist:38a65007235718482fed
Created October 12, 2015 11:02
PHP: Round X to the nearest Y
<?php
// Round 0.035 up to the nearest 0.01
echo ceil( 0.035 / 0.01 ) * 0.01;
// 0.04
@olimortimer
olimortimer / cloudflare_check.php
Last active January 1, 2021 11:52
PHP: Check IP is CloudFlare
<?php
function cidr_match($ip) {
foreach(file('https://www.cloudflare.com/ips-v4') as $cidr) {
list($subnet, $mask) = explode('/', $cidr);
if ((ip2long($ip) & ~((1 << (32 - $mask)) - 1) ) == ip2long($subnet)) {
return true;
@olimortimer
olimortimer / gist:948bc47d4034b2c7e682
Last active August 29, 2015 14:26
Shell: Magic Git
# Magic Git
function parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOUR="\[\033[0m\]"
@olimortimer
olimortimer / gist:b283d270a4ecce9d51f8
Last active August 29, 2015 14:23
htaccess snippets
# Force SSL
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Stop browsing of CDN
RewriteCond %{HTTP_HOST} ^cdn\.host\.co\.uk$ [NC]
RewriteCond $1 !\.(css|htc|less|js|js2|js3|js4|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|woff|xla|xls|xlsx|xlt|xlw|zip)$
RewriteRule ^(.*)$ https://www.host.co.uk/$1 [R=301,L]
@olimortimer
olimortimer / gist:3c675a367b3e3e9f0103
Last active August 29, 2015 14:22
JS: Pre-populate Fields
// Populate our field
function populateField($container, name, value) {
var $field = $container.find('[name="'+name+'"]');
// Skip if our field doesn't exist
if($field.length == 0) return false;
if($field.is(':radio')) {
$field.filter('[value="'+value+'"]').prop('checked', true);
@olimortimer
olimortimer / gist:75361ce5959045fd1f5f
Created May 29, 2015 08:37
Shell: Find unique file types
# Find all file types recursively within a directory
find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u
@olimortimer
olimortimer / cloudflare-api.local
Last active August 29, 2015 14:14
Shell: Fail2Ban WordPress Logins
# Ban IP via Cloudflare API: /etc/fail2ban/action.d/cloudflare-api.local
[Definition]
actionban = curl https://www.cloudflare.com/api_json.html -d 'a=ban' -d 'tkn=1234567890' -d 'email=cloudflare@email.com' -d 'key=<ip>'
actionunban = curl https://www.cloudflare.com/api_json.html -d 'a=nul' -d 'tkn=1234567890' -d 'email=cloudflare@email.com' -d 'key=<ip>'
@olimortimer
olimortimer / gist:e68ffe4aa8009a223c9d
Last active August 29, 2015 14:09
Shell: Cleaning Email Queue
# Delete emails from the queue where the addresses or TO or FROM;
mailq | tail -n +2 | awk 'BEGIN { RS = "" } / name@domain\.co\.uk$/ { print $1 }' | tr -d '*!' | postsuper -d -
mailq | tail -n +2 | awk 'BEGIN { RS = "" } / domain\.co\.uk$/ { print $1 }' | tr -d '*!' | postsuper -d -
mailq | tail -n +2 | awk 'BEGIN { RS = "" } / MAILER-DAEMON@mail\.domain\.com$/ { print $1 }' | tr -d '*!' | postsuper -d -
# Find all IP addresses against the sasl_username and put them into iplist.txt
grep "sasl_username=name@domain.co.uk" /var/log/mail.log | sort -u > iplist.txt
# Go through all the IPs and delete the emails from the queue
grep -wFf /iplist.txt | cut -f 6 -d ' ' | cut -f 1 -d ':' | postsuper -d -