Skip to content

Instantly share code, notes, and snippets.

View Andrewpk's full-sized avatar
🐶
wat

Andrew Kehrig Andrewpk

🐶
wat
  • wat
  • Royal Oak, Michigan
View GitHub Profile
@Andrewpk
Andrewpk / autoload.php
Last active August 29, 2015 14:00
Simple php autoloader. Checks for constant 'CLASS_DIR' that specifies base directory for classes, otherwise will simply look in PSR-0 style directory structure in current directory.
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// From all I can find on it - PSR states class methods must have opening brace on new line - not plain ol' functions //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function autoload($className) {
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
@Andrewpk
Andrewpk / Monolog\Handler\PDOHandler.php
Last active August 29, 2015 14:03
A PDO monolog handler implementation. Almost done (it needs tests). It also works with Postgres' json data type.
<?php
namespace AK\Monolog\Handler;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Monolog\Formatter\JsonFormatter;
class PDOHandler extends AbstractProcessingHandler
{
@Andrewpk
Andrewpk / Synology-DSM4.3-cleanConnectionLogs.sh
Created October 3, 2014 17:35
If you're running a synology with dsm v4 (you have a /var/log/synolog directory) and you have lots of traffic, your connection log will probably get full. For some reason this doesn't rotate, so put this script somewhere or add these commands to a scheduled task. If your connection log fills up your filesystem (all of /tmp is used) you won't be …
#!/bin/ash
rm /var/log/synolog/synoconn.log
rm /tmp/synolog*
/usr/syno/etc/rc.d/S22syslogng.sh restart
/usr/syno/etc/rc.d/S97apache-sys.sh restart
@Andrewpk
Andrewpk / mehRandomString.php
Last active August 29, 2015 14:12
PHP: pseudo-random string of defined length
<?php
function randString($length, $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-0123456789')
{
$str = '';
$count = strlen($charset) - 1;
while ($length--) {
$str .= $charset[mt_rand(0, $count)];
}
return $str;
@Andrewpk
Andrewpk / listen.sh
Created January 8, 2015 18:14
Ports being listened to on my machine
netstat -an | grep LISTEN | awk '{ print $4 }' | rev | cut -d: -f1 | rev
@Andrewpk
Andrewpk / gist:25bbc40c2f1ab9985eda
Created March 6, 2015 17:21
Simple grunt-contrib-connect options to modify cross-domain policy.
grunt.config(['connect'], {
options: {
port: 9000,
hostname: '*',
keepalive: true,
middleware: function (connect) {
return [
function (request, response, next) {
response.setHeader('Access-Control-Allow-Origin', '*');
return next();
@Andrewpk
Andrewpk / angularChainedSelect.html
Last active August 29, 2015 14:19
angular chained select copied based on http://jsfiddle.net/pythondave/JUZDf/
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>
<style>
/** setup simple styling **/
.ng-invalid { border: 1px solid red; }
body { font-family: Arial,Helvetica,sans-serif; }
body, td, th { font-size: 14px; margin: 0; }
table { border-collapse: separate; border-spacing: 2px; display: table; margin-bottom: 0; margin-top: 0; -moz-box-sizing: border-box; text-indent: 0; }
a:link, a:visited, a:hover { color: #5D6DB6; text-decoration: none; }
@Andrewpk
Andrewpk / uriparsing.java
Last active August 29, 2015 14:25
String + Uri.parse() vs Uri.parse().buildUpon(). I'm probably missing something, but why does Android 2.3 have problems with using Uri.parse().buildUpon() when every one of these methods has been around since api v1?
/**
* Works great on Android 4, and likely 3 but causes Android 2.3 to build a Uri
* with a string value of "geo:?q=" + zipCode
*/
Uri geoUri = Uri.parse("geo:0,0?").buildUpon()
.appendQueryParameter("q", zipCode).build();
/**
* Works great on Android 2.3+
*/
@Andrewpk
Andrewpk / addnvpair.xsl.diff
Created July 30, 2012 20:43
addnvpair.xsl diff - what IBM gives you, and what you probably want.
diff addnvpair.xsl.bak addnvpair.xsl
19c19,26
< <xsl:value-of select="normalize-space($value)"/>
---
> <xsl:choose>
> <xsl:when test="@name = 'Identifier'">
> <xsl:value-of select="$value"/>
> </xsl:when>
> <xsl:otherwise>
> <xsl:value-of select="normalize-space($value)"/>
@Andrewpk
Andrewpk / codeLOLpt1.php
Last active December 15, 2015 05:19
codeLOLpt1. This is funny because in order to adequately catch errors of this function, you either need to register a new error handler and check within your handler, or parse $php_errormsg. Simply checking if unserialize returned false is not adequate, since false is an acceptable value to be serialized. IMO: unserialize should probably throw a…
<?php
$origErrorTrackVal = ini_set('track_errors',1);
$oldReportingLevel = error_reporting(E_ALL); //least reporting necessary is E_NOTICE
$mixedUnserializedVal = unserialize(($someArr['someIndex']));
/**
* This next conditional is fun. At this point $mixedUnserializedVal can legally be any value except "unset"
* You still have to check the error messages though :-/
*/
if(isset($mixedUnserializedVal) && preg_match('/Undefined\sindex:\ssomeIndex/', $php_errormsg) === 0) {
//do work