2 rcs_id('PHP Fortune - Made by henrik@aasted.org. HP: http://www.aasted.org');
3 rcs_id('$Id: fortune.php,v 1.2 2004-11-21 11:59:20 rurban Exp $');
7 Quotes from any of the fortune-files in the dir.
9 Quotes from the specific file.
11 Written by Henrik Aasted Sorensen, henrik@aasted.org
12 Read more at http://www.aasted.org/quote
16 function quoteFromDir($dir) {
20 if ( $handle = opendir($dir) ) {
21 while (false !== ($file = readdir($handle))) {
23 if ( strpos($file, ".dat") != false) {
25 if (substr($file, $len - 4) == ".dat"){
26 $number = $this->getNumberOfQuotes($dir . "/" . $file);
28 $quotes[$index] = $amount;
29 $files[$index] = $file;
36 srand((double)microtime()*1000000);
37 $index = rand(0, $amount);
40 while ($quotes[$i] < $index) {
44 return $this->getRandomQuote($dir . "/" .$files[$i]);
50 Reads the number of quotes in the file.
52 function getNumberOfQuotes($file) {
53 $fd = fopen($file, "rb");
54 $this->readLong($fd); // Just move over the first long. Might as well be fseek.
55 $len = $this->readLong($fd);
60 Picks quote number $index from the dat-file in $file.
62 function getExactQuote($file, $index) {
63 if (is_file($file) == false) {
64 echo "Input must be a file!<br/>";
68 if ( ($fd = fopen($file, "rb")) == false ) {
69 echo "Cannot open $file<br/>";
72 fseek($fd, 24 + 4 * $index);
74 $phys_index = $this->readLong($fd);
78 $quotefile = substr($file, 0, strlen($file) - 4);
80 if ( ($fd = fopen($quotefile, "rb")) == false ) {
81 echo "Cannot find file $quotefile!<br/>";
84 $res = $this->getQuote($fd, $phys_index);
91 Returns a random quote from $file.
93 function getRandomQuote($file) {
94 $number = $this->getNumberOfQuotes($file);
96 $index = rand(0, $number - 1);
98 return $this->getExactQuote($file, $index);
102 Reads a quote from the specified index.
104 function getQuote($fd, $index) {
109 $line = fgets($fd, 1024) . "<br>";
110 } while ( ($line[0] != "%") && (!feof($fd)) );
116 Gets indexes from the file pointed to by the filedescriptor $fd.
118 function getIndices($fd) {
119 fseek($fd, 24, SEEK_SET);
122 while ( feof($fd) == FALSE ) {
123 $res[$i] = readLong($fd);
129 function readLong($fd) {
130 $res = fread($fd, 4);
132 $l += ord($res[2]) << 8;
133 $l += ord($res[1]) << 16;
134 $l += ord($res[0]) << 24;
139 function createIndexFile($file) {
140 $fd = @fopen($file, "r");
153 if ($line == "%\n") {
154 $indices[$i] = ftell($fd);
156 if ($length > $longest)
159 if ($length < $shortest)
164 $length = $length + strlen($line);
170 $fd = @fopen($file . ".dat", "w");
173 echo "<!-- createIndexFile: Could not write to file....-->";
178 $this->writeLong($fd, 2);
179 $this->writeLong($fd, count($indices));
180 $this->writeLong($fd, $longest);
181 $this->writeLong($fd, $shortest);
182 $this->writeLong($fd, 0);
183 $this->writeLong($fd, 37 << 24);
185 for ($i = 0 ; $i < count($indices) ; $i++) {
186 $this->writeLong($fd, $indices[$i]);
192 function writeLong($fd, $l) {
193 fwrite($fd, chr ( ($l >> 24) & 255));
194 fwrite($fd, chr ( ($l >> 16) & 255));
195 fwrite($fd, chr ( ($l >> 8) & 255));
196 fwrite($fd, chr ( $l & 255));