Skip to content

Instantly share code, notes, and snippets.

@jkoop
jkoop / PurifyHtml.php
Created March 8, 2024 15:03
Laravel 10: middleware: prevent HTML and Markdown attacks
<?php
namespace App\Http\Middleware;
use HTMLPurifier;
use HTMLPurifier_HTML5Config;
use Illuminate\Foundation\Http\Middleware\TransformsRequest;
/**
* Use HTMLPurifier to purify all inputs with names matching `/[-_]html\s*$/i`.
@jkoop
jkoop / SetLocale.php
Created March 5, 2024 21:23
Laravel 10; set App and User's locale based on browser's preferred language and server's `lang/` directory; @requires `punic/punic`
<?php
/**
* For Laravel 10.
* Automatically set App and User's locale based on browser's preferred
* language and server's `lang/` directory.
* @copyright 2024 Joe Koop
* @license MIT
*/
@jkoop
jkoop / SetLocale.php
Created February 29, 2024 20:36
Automatically set app locale based on browser's preferred language and server's `lang/` directory
<?php
/**
* For Laravel 10.
* Automatically set app locale based on browser's preferred language and server's `lang/` directory.
* @copyright 2024 Joe Koop
* @license MIT
*/
namespace App\Http\Middleware;
@jkoop
jkoop / numbers.h
Last active November 22, 2023 15:08
terse shorthands for numeric types in C
/**
* numbers.h - Terse shorthands for numeric types
* Copyright (c) 2023 Joe Koop
* License: The MIT License (MIT)
*/
#include <stdint.h>
// E for "exact"
typedef int8_t i8e;
@jkoop
jkoop / readable-dates.php
Last active March 15, 2024 15:34 — forked from scr4bble/readable-dates.php
Adminer plugin that replaces UNIX timestamps with human-readable dates.
<?php
/** This plugin replaces UNIX timestamps with human-readable dates in your local timezone.
* Mouse click on the date field reveals timestamp back.
*
* @link https://www.adminer.org/plugins/#use
* @author Anonymous
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
@jkoop
jkoop / wifi-geotracking-to-traccar.sh
Created May 1, 2023 13:46
Get location via WiFi (with Google API) and report it to your Traccar server
#!/bin/bash
# Run this as root
GOOGLE_API_KEY=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
TRACCAR_URL='https://traccar.example.com'
INTERFACE=wlan0
DEVICE_ID=123456
json=$(sudo iwlist $INTERFACE scan | egrep -o 'Address: .*|Channel:.*|Signal level=.*|Last beacon:.*' | sed -E 's/Address: (.*)/\{"macAddress":"\1",/g' | sed -E 's/Channel:(.*)/"channel":\1,/g' | sed -E 's/Signal level=(.*) dBm/"signalStrength":\1,/g' | sed -E 's/Last beacon: (.*)ms ago/"age":\1}/g')
json=$(echo "$json" | head -n 1; echo "$json" | sed -E 's/\{/,{/g' | tail -n +2)
@jkoop
jkoop / get-list-of-clients-from-aerohive-aps.php
Created April 11, 2023 02:30
Get a list of all Wifi clients from AeroHive APs
#!/usr/bin/php
<?php
/**
* This script SSHes into all of my AeroHive Wifi APs, gets a list of current
* connected clients from each if them, assembles the list into one, and writes
* that list to a JSON file.
*
* This is intended to be run as a minutely `cron` job. As such, the path of the
* output file is the date, formatted to not put a million files in one folder:
@jkoop
jkoop / where-clause.sql
Created March 27, 2023 21:33
13 digit ISBN checker for SQLite3
(10 - (
substr(isbn, 1, 1) * 1 +
substr(isbn, 2, 1) * 3 +
substr(isbn, 3, 1) * 1 +
substr(isbn, 4, 1) * 3 +
substr(isbn, 5, 1) * 1 +
substr(isbn, 6, 1) * 3 +
substr(isbn, 7, 1) * 1 +
substr(isbn, 8, 1) * 3 +
substr(isbn, 9, 1) * 1 +
/**
* Converts a number to A,B,C,...,X,Y,Z,AA,AB,AC,...,ZZ,AAA,AAB, etc.
*
* @param int $int must be at least 1
* @return string
*/
function spreadsheetLetters(int $int): string {
if ($int < 0) throw new TypeError('$int must be at least 0');
$numeric = $int % 26;
@jkoop
jkoop / git-blame-count.sh
Last active May 2, 2023 19:47
Who gets blamed for the most lines in a git project?
#! /bin/sh
git ls-files -z | \
xargs -0n1 git blame -w | \
grep -o --binary-files=text '^[^(]*([^)]*)' | \
grep -o '([^0-9]*' | \
grep -o '[^(]*' | \
sed 's/\s*$//g' | \
sort -f | \
uniq -c | \