Skip to content

Instantly share code, notes, and snippets.

@dlueth
dlueth / mdnsresponder.sh
Last active April 16, 2022 03:02
Replace Yosemite's DNS discoveryd with the former mDNSResponder.
#!/bin/bash
if [ ! $(whoami) = 'root' ]; then
echo "This script should be run as root." > /dev/stderr
exit 1
fi
spinner()
{
local pid=$!
@dlueth
dlueth / macports.sh
Last active November 30, 2015 08:04
Macports install, remove and update script
#!/bin/bash
if [ ! $(whoami) = 'root' ]; then
echo "This script should be run as root." > /dev/stderr
exit 1
fi
if [ ! -f "/opt/local/bin/port" ] ; then
echo "This script requires macports to be installed"
exit 1
@dlueth
dlueth / .htaccess
Created July 18, 2013 08:06
Assets for Qoopido.js shrinkimage showing how to convert images to .shrunk files via PHP and how to include it in your .htaccess
AddType application/json .shrunk
AddType application/javascript .shrunk.jsonp
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+.q(?:[0-9]+).shrunk)$ shrinkimage.php?file=$1 [QSA,L]
RewriteRule ^(.+.q(?:[0-9]+).shrunk.jsonp)$ shrinkimage.php?file=$1&jsonp=1 [QSA,L]
</IfModule>
<Ifmodule mod_deflate.c>
@dlueth
dlueth / getFiles.php
Created June 27, 2013 21:36
Fetch files matching an optional regex-pattern recursively from any given directory
getFiles($directory, $recursive = false, $pattern = NULL, $absolute = false) {
$return = array();
$iterator = ($recursive === false) ? new \FilesystemIterator($directory, \FilesystemIterator::SKIP_DOTS) : new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory), \RecursiveIteratorIterator::SELF_FIRST);
$iterator = ($pattern !== NULL) ? new \RegexIterator($iterator, '/' . $pattern . '/i') : $iterator;
$directory = preg_quote($directory . '/', '/');
foreach($iterator as $path) {
if($path->isFile() === true) {
$return[] = ($absolute !== false) ? $path->getPathname() : preg_replace('/^' . $directory . '/', '', $path->getPathname());
}
@dlueth
dlueth / convertCharset.php
Created June 27, 2013 21:33
PHP modifier to convert the encoding of any given string (requires mb_string)
function convertCharset($value, $characterset = NULL) {
return (mb_check_encoding($value, $characterset) === true) ? $value : mb_convert_encoding($value, $characterset);
}
@dlueth
dlueth / sanitize.php
Created June 27, 2013 21:32
PHP modifier to sanitize (remove any hostile characters) from any given string
function sanitize($value) {
$value = preg_replace('/[\x00-\x1f\?*:";|\/°^!§$%&\\()=´`+#\':,<>]/', '', trim($value));
$value = preg_replace('/^(?:PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d)(?:\.*)(.*)/', '\1', $value);
return $value;
}
@dlueth
dlueth / sluggify.php
Created June 27, 2013 21:31
PHP modifier to sluggify (make url-safe) any given string
function sluggify($value) {
return trim(strtolower(preg_replace('/([^\w]|-)+/', '-', trim(strtr(str_replace('\'', '', trim($value)), array(
'À'=>'A','Á'=>'A','Â'=>'A','Ã'=>'A','Å'=>'A','Ä'=>'A','Æ'=>'AE',
'à'=>'a','á'=>'a','â'=>'a','ã'=>'a','å'=>'a','ä'=>'a','æ'=>'ae',
'Þ'=>'B','þ'=>'b','Č'=>'C','Ć'=>'C','Ç'=>'C','č'=>'c','ć'=>'c',
'ç'=>'c','Ď'=>'D','ð'=>'d','ď'=>'d','Đ'=>'Dj','đ'=>'dj','È'=>'E',
'É'=>'E','Ê'=>'E','Ë'=>'E','è'=>'e','é'=>'e','ê'=>'e','ë'=>'e',
'Ì'=>'I','Í'=>'I','Î'=>'I','Ï'=>'I','ì'=>'i','í'=>'i','î'=>'i',
'ï'=>'i','Ľ'=>'L','ľ'=>'l','Ñ'=>'N','Ň'=>'N','ñ'=>'n','ň'=>'n',
'Ò'=>'O','Ó'=>'O','Ô'=>'O','Õ'=>'O','Ø'=>'O','Ö'=>'O','Œ'=>'OE',
@dlueth
dlueth / transliterate.php
Created June 27, 2013 21:29
PHP modifier to transliterate a given string (requires iconv, mb_string)
function transliterate($value, $characterset) {
return iconv(mb_detect_encoding($value, NULL, true), $characterset . '//IGNORE//TRANSLIT', trim($value));
}
@dlueth
dlueth / isValidPath.php
Created June 27, 2013 21:28
PHP validator that checks if a given string is a valid/allowed filesystem path across operating systems
function isValidPath($value) {
return (preg_match('/^(\/(?:(?:(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*)(?:\/(?:(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\\-_.!~*\'():\@&=+\$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*))*))$/', (string) $value) > 0);
}
@dlueth
dlueth / isValidFilename.php
Created June 27, 2013 21:27
PHP validator that checks if a given string is a valid/allowed filename across operating systems
function isValidFilename($value) {
return (strlen($value) <= 255 && preg_match('/^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\?*:"";|\/]+$/', $value) > 0);
}