From 981bdb43c48756233d6661937d0aae265e7a6b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=CC=8Avard=20Pedersen?= Date: Tue, 14 Apr 2015 15:20:53 +0200 Subject: [PATCH] Initial commit --- bashquote | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 bashquote diff --git a/bashquote b/bashquote new file mode 100755 index 0000000..e5b44c9 --- /dev/null +++ b/bashquote @@ -0,0 +1,88 @@ +#!/usr/bin/php + array( + 'proxy' => str_replace("http://", "tcp://", getenv('http_proxy')), + 'request_fulluri' => true +))); + +$quotes = new BashOrgQuotes(); +$quote = $quotes->getQuote(); +$quote = $quotes::cleanupQuote($quote); +echo $quote."\n"; + +class Quotes { + + private $quotefile = ''; // Quote cache location + private $quotes = array(); // Quotes + + function __construct() { + $this->quotefile = getenv('HOME')."/.bashqdb"; + $this->quotefp = fopen($this->quotefile,"c+"); + // Only one instance accessing the cache at a time + if (!flock($this->quotefp, LOCK_EX)) + throw new Exception('Could not lock quote cache'); + $this->cacheRead(); + } + + function __destruct() { + $this->fillIfEmpty(); + $this->cacheWrite(); + flock($this->quotefp, LOCK_UN); + fclose($this->quotefp); + } + + function cacheRead() { + $cache = file($this->quotefile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES | FILE_TEXT); + if ($cache) { + $this->quotes = $cache; + } else { + //throw new Exception('Could not read quote cache'); + $this->quotes = array(); + } + } + + function cacheWrite() { + if (FALSE === file_put_contents($this->quotefile, implode("\n", $this->quotes))) { + throw new Exception('Could not write quote cache'); + } + } + + function fillIfEmpty() { + if (!$this->quotes) + $this->quotes = $this->fetchQuotes(); + } + + function getQuote() { + $quote = array_pop($this->quotes); + $this->fillIfEmpty(); + if (!$quote) { // This is not good enough error handling + $quote = array_pop($this->quotes); + $this->fillIfEmpty(); + } + return $quote; + } +} + +class BashOrgQuotes extends Quotes { + function fetchQuotes() { // */p[@class='qt'] + $matches = array(); + $source = file_get_contents("http://bash.org/?random1"); + // @todo Handle failure + preg_match_all("/

(.+?)<\/p>/si", $source, $matches); + $quotes = array(); + foreach ($matches[1] as $match) { + $quotes[] = str_replace("\n","",$match); + } + return $quotes; + } + + static function cleanupQuote($quote) { + $quote = html_entity_decode($quote); + $quote = str_replace("
","\n",$quote); + return $quote; + } +} +// http://www.qdb.us/api +?>