2 rcs_id('PHP Fortune - Made by henrik@aasted.org. HP: http://www.aasted.org');
3 rcs_id('$Id: fortune.php,v 1.3 2007-01-02 13:21:47 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) {
22 if ( $handle = opendir($dir) ) {
23 while (false !== ($file = readdir($handle))) {
25 if ( strpos($file, ".dat") != false) {
27 if (substr($file, $len - 4) == ".dat"){
28 $number = $this->getNumberOfQuotes($dir . "/" . $file);
30 $quotes[$index] = $amount;
31 $files[$index] = $file;
37 srand((double)microtime()*1000000);
38 $index = rand(0, $amount);
42 while ($quotes[$i] < $index) {
46 return $this->getRandomQuote($dir . "/" .$files[$i]);
52 Reads the number of quotes in the file.
54 function getNumberOfQuotes($file) {
55 $fd = fopen($file, "rb");
56 $this->readLong($fd); // Just move over the first long. Might as well be fseek.
57 $len = $this->readLong($fd);
62 Picks quote number $index from the dat-file in $file.
64 function getExactQuote($file, $index) {
65 if (is_file($file) == false) {
66 echo "Input must be a file!<br/>";
70 if ( ($fd = fopen($file, "rb")) == false ) {
71 echo "Cannot open $file<br/>";
74 fseek($fd, 24 + 4 * $index);
76 $phys_index = $this->readLong($fd);
80 $quotefile = substr($file, 0, strlen($file) - 4);
82 if ( ($fd = fopen($quotefile, "rb")) == false ) {
83 echo "Cannot find file $quotefile!<br/>";
86 $res = $this->getQuote($fd, $phys_index);
93 Returns a random quote from $file.
95 function getRandomQuote($file) {
96 $number = $this->getNumberOfQuotes($file);
98 $index = rand(0, $number - 1);
100 return $this->getExactQuote($file, $index);
104 Reads a quote from the specified index.
106 function getQuote($fd, $index) {
111 $line = fgets($fd, 1024) . "<br>";
112 } while ( ($line[0] != "%") && (!feof($fd)) );
118 Gets indexes from the file pointed to by the filedescriptor $fd.
120 function getIndices($fd) {
121 fseek($fd, 24, SEEK_SET);
124 while ( feof($fd) == FALSE ) {
125 $res[$i] = readLong($fd);
131 function readLong($fd) {
132 $res = fread($fd, 4);
134 $l += ord($res[2]) << 8;
135 $l += ord($res[1]) << 16;
136 $l += ord($res[0]) << 24;
141 function createIndexFile($file) {
142 $fd = @fopen($file, "r");
155 if ($line == "%\n") {
156 $indices[$i] = ftell($fd);
158 if ($length > $longest)
161 if ($length < $shortest)
166 $length = $length + strlen($line);
172 $fd = @fopen($file . ".dat", "w");
175 echo "<!-- createIndexFile: Could not write to file....-->";
180 $this->writeLong($fd, 2);
181 $this->writeLong($fd, count($indices));
182 $this->writeLong($fd, $longest);
183 $this->writeLong($fd, $shortest);
184 $this->writeLong($fd, 0);
185 $this->writeLong($fd, 37 << 24);
187 for ($i = 0 ; $i < count($indices) ; $i++) {
188 $this->writeLong($fd, $indices[$i]);
194 function writeLong($fd, $l) {
195 fwrite($fd, chr ( ($l >> 24) & 255));
196 fwrite($fd, chr ( ($l >> 16) & 255));
197 fwrite($fd, chr ( ($l >> 8) & 255));
198 fwrite($fd, chr ( $l & 255));