Last active
August 29, 2015 14:05
-
-
Save sarciszewski/0b0f67936f472cc63cf0 to your computer and use it in GitHub Desktop.
A Step Beyond Fatal Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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