Created
November 8, 2011 12:13
-
-
Save tskrynnyk/1347607 to your computer and use it in GitHub Desktop.
Random BOFH excuse.
This file contains hidden or 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 | |
| /* | |
| Random BOFH excuse. | |
| Credit to Jeff Ballard <ballard%cs.wisc.edu> for the original | |
| "The Bastard Operator From Hell"-style excuse server. | |
| See http://www.cs.wisc.edu/~ballard/bofh/ for more info. | |
| */ | |
| //defined('SYSPATH') or die('No direct script access.'); | |
| class BOFHexcuse { | |
| public $header; | |
| public $body; | |
| private $excuse; | |
| const EXCUSES_FILE = '/var/local/bofh-excuses.txt'; | |
| const FORTUNE_CMD = '/usr/games/fortune -s -u'; | |
| const FORTUNE_PATH = '/usr/share/games/fortunes/bofh-excuses'; | |
| function __construct($from = '') { | |
| $this->excuse = array(); | |
| if ($from == 'fortune') { | |
| $this->fromFortune(); | |
| } else { | |
| $this->fromFile(); | |
| } | |
| $this->header = htmlspecialchars($this->excuse[0]); | |
| $this->body = htmlspecialchars($this->excuse[1]); | |
| } | |
| private function fromFile() { | |
| try { | |
| if (!$excuses = @file(self::EXCUSES_FILE)) | |
| throw new Exception("Error: Couldn't read file."); | |
| $i = mt_rand(0, count($excuses)-1); | |
| $this->excuse[0] = 'BOFH excuse #' . ($i+1) . ':'; | |
| $this->excuse[1] = $excuses[$i]; | |
| } catch (Exception $e) { | |
| echo $e->getMessage(); | |
| } | |
| } | |
| private function fromFortune() { | |
| /* You must have fortunes and fortunes-bofh-excuses in your system. */ | |
| try { | |
| exec(self::FORTUNE_CMD . ' ' . self::FORTUNE_PATH, $f, $err); | |
| if($err > 0) { | |
| throw new Exception("Error while executing command ($err)."); | |
| } | |
| $this->excuse = array_values(array_filter($f)); | |
| } catch (Exception $e) { | |
| echo $e->getMessage(); | |
| } | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment