Skip to content

Instantly share code, notes, and snippets.

@thomas-p-wilson
thomas-p-wilson / RootPathDeterminer.php
Created March 2, 2011 19:42
Determines the path and url to the current file. Handles HTTPS and mod_rewrite.
<?php
// Define root path constant. $_SERVER['SCRIPT_FILENAME'] seems to survive
// redirects.
$root_path = dirname($_SERVER['SCRIPT_FILENAME']);
if(substr($root_path,-1) != '/') {
$root_path .= '/';
}
define('ROOT_PATH',$root_path);
unset($root_path);
@thomas-p-wilson
thomas-p-wilson / CheckRewrite.php
Created March 2, 2011 19:45
Determines whether or not mod_rewrite is working and enabled. Meant to be added to the index page of a site where all requests are redirected to the index page.
<?php
if(!isset($_GET['rewritecheck'])) {
$headers = get_headers(ROOT_URL.'/web/?rewritecheck',1);
define('REWRITE_ENABLED',strpos($headers[0],'301') !== false);
}
if(!defined('REWRITE_ENABLED')) {define('REWRITE_ENABLED',true);}
?>
@thomas-p-wilson
thomas-p-wilson / gist:1019681
Created June 10, 2011 20:20
Collatz Conjecture Solver
<?php
if(!isset($_GET['number']) || empty($_GET['number'])) {
echo '<h3>Please enter a number > 1</h3>';
} elseif(!is_numeric($_GET['number'])) {
echo '<h3>It has to be a number.</h3>';
}
?>
<form action="" method="get">
<input type="text" name="number">
<input type="submit" value="Go">
@thomas-p-wilson
thomas-p-wilson / array_stats.php
Created July 30, 2013 16:13
Calculates statistical information about an array of numbers, including upper and lower quartiles.
<?php
function array_stats(array $arr) {
sort($arr);
// Basic data
$result = array();
$result['cnt'] = count($arr);
$result['min'] = min($arr);
$result['max'] = max($arr);
$result['sum'] = array_sum($arr);
@thomas-p-wilson
thomas-p-wilson / iptables.rules.sh
Last active February 21, 2019 14:42
My basic IPTables configuration.
#!/bin/bash
# Get interface information
$LANIF="eth1"
$LANIP=$(ifconfig | awk "/$LANIF/,/inet addr/" | grep -oP "inet addr:[0-9\.]+" | cut -d':' -f2)
$EXTIF="eth0"
$EXTIF=$(ifconfig | awk "/$EXTIF/,/inet addr/" | grep -oP "inet addr:[0-9\.]+" | cut -d':' -f2)
# Clear all existing IPTables rules
echo " * Clearing old rules"
@thomas-p-wilson
thomas-p-wilson / gist:6514285
Created September 10, 2013 19:21
Fail2Ban Asterisk regexes for 1.8.x
failregex = NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - Wrong password
NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - No matching peer found
NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - No matching peer found
NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - Username/auth name mismatch
NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - Device does not match ACL
NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - Peer is not supposed to register
NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - ACL error (permit/deny)
NOTICE.* .*: Registration from '.*' failed for '<HOST>:.*' - Device does not match ACL
NOTICE.* .*: Registration from '\".*\".*' failed for '<HOST>:.*' - No matching peer found
NOTICE.* .*: Registration from '\".*\".*' failed for '<HOST>:.*' - Wrong password
@thomas-p-wilson
thomas-p-wilson / sentry
Created September 26, 2013 14:11
My init script for managing Sentry
#!/bin/bash
# /etc/init.d/sentry
# A debian-compatible sentry startup script
#
### BEGIN INIT INFO
# Provides: sentry
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
[composite:neutron]
use = egg:Paste#urlmap
/: neutronversions
/v2.0: neutronapi_v2_0
[composite:neutronapi_v2_0]
use = call:neutron.auth:pipeline_factory
noauth = request_id catch_errors extensions neutronapiapp_v2_0
keystone = request_id catch_errors authtoken keystonecontext extensions neutronapiapp_v2_0
@thomas-p-wilson
thomas-p-wilson / gitlab-jenkins-push.php
Created January 15, 2015 14:11
A script that takes push notifications from gitlab and runs a build in Jenkins on the branch that was pushed, assuming that the Jenkins project has a ${branch} parameter.
<?php
/* Change the following */
$job = urlencode("Project name as seen in Jenkins");
$token = "the build token you gave to the jenkins job";
$url = "https://ci.example.com/";
$json= file_get_contents('php://input');
$jsarr = json_decode($json,true);
$branch = substr($jsarr['ref'], strrpos($jsarr['ref'], '/') + 1);
@thomas-p-wilson
thomas-p-wilson / QueryStringUtils.java
Last active August 29, 2015 14:24
Querystring utility for parsing PHP-style querystrings in Java
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.