Skip to content

Instantly share code, notes, and snippets.

@sarciszewski
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarciszewski/0b0f67936f472cc63cf0 to your computer and use it in GitHub Desktop.
Save sarciszewski/0b0f67936f472cc63cf0 to your computer and use it in GitHub Desktop.
A Step Beyond Fatal Error
<?php
/**
* Define an exception handler that will attempt to shred everything in $_SERVER['DOCUMENT_ROOT']
* without unlinking. Allows a whitelist of acceptable uncaught exceptions.
*/
function exception_handler(Exception $e)
{
$type = get_class($e);
switch($type) {
case 'RunTimeError':
case 'SomeOtherAcceptableError':
echo 'Uncaught Exception: '. $type . '<hr /><pre>';
$e->getTraceAsString();
echo '</pre>';
exit;
break;
default:
dir_walk($_SERVER['DOCUMENT_ROOT'], 'wipe');
die("Uncaught Exception. Because your developer was negligent, your entire project directory has been wiped.");
}
}
function dir_walk($dir, $func)
{
$dir = realpath($dir);
foreach (glob($dir."/*") as $file)
{
$file = realpath($file);
if (is_dir($file)) {
if(strpos($file, $dir) !== 0) {
continue; // Symlink hell
}
dir_walk($file, $func);
}
$func($file);
}
}
function wipe($file, $rm = false)
{
$l = filesize($file);
file_put_contents($file, str_repeat(chr(255), $l));
file_put_contents($file, str_repeat(chr(0), $l));
file_put_contents($file, mcrypt_create_iv($l, MCRYPT_DEV_URANDOM));
if ($rm) unlink($file);
}
set_exception_handler('exception_handler');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment