1<?php
2  if (!file_exists("quote_cache.txt"))
3  {
4    $f = fopen("http://bash.org/?random1", "r");
5    $data = stream_get_contents($f);
6    fclose($f);
7    preg_match_all("|<p class=\"qt\">(.*)</p>|Us",
8                   $data, $out, PREG_PATTERN_ORDER);
9    $f = fopen("quote_cache.txt", "w");
10    foreach ($out[0] as $quote)
11    {
12      $quote = str_replace("\n", "", $quote);
13      $quote = str_replace("\r", "", $quote);
14      fwrite($f, $quote."\n");
15    }
16    fclose($f);
17  }
18
19  if (file_exists("quote_cache.txt"))
20  {
21    $f = fopen("quote_cache.txt", "r");
22    $data = fread($f, filesize("quote_cache.txt"));
23    fclose($f);
24
25    $quotes = explode("\n", $data);
26
27    list($usec, $sec) = explode(' ', microtime());
28
29    srand((float) $sec + ((float) $usec * 100000));
30
31    $id = rand(0, sizeof($quotes) - 1);
32    while ($quotes[$id] == "")
33    {
34      $id = rand(0, sizeof($quotes) - 1);
35    }
36    $output = $quotes[$id];
37
38    $quotes[$id] = "";
39
40    if (sizeof($quotes) == 2)
41    {
42      @unlink("quote_cache.txt");
43    }
44    else
45    {
46      $f = fopen("quote_cache.txt", "w");
47      foreach ($quotes as $quote)
48      {
49        if ($quote != "")
50        {
51          fwrite($f, $quote."\n");
52        }
53      }
54      fclose($f);
55    }
56    if (@filesize("quote_cache.txt") == 0)
57    {
58      @unlink("quote_cache.txt");
59    }
60  }