]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/fortune.php
Allow bold, italics or underlined for numbers
[SourceForge/phpwiki.git] / lib / fortune.php
1 <?php
2 // PHP Fortune - Made by henrik@aasted.org. HP: http://www.aasted.org
3
4 /*
5 Main methods to use:
6  quoteFromDir($dir):
7    Quotes from any of the fortune-files in the dir.
8  getRandomQuote($file):
9    Quotes from the specific file.
10
11  Written by Henrik Aasted Sorensen, henrik@aasted.org
12  Read more at http://www.aasted.org/quote
13 */
14 class Fortune
15 {
16
17     function quoteFromDir($dir)
18     {
19         $amount = 0;
20         $index = 0;
21         $quotes = array();
22         $files = array();
23
24         if ($handle = opendir($dir)) {
25             while (false !== ($file = readdir($handle))) {
26
27                 if (strpos($file, ".dat") != false) {
28                     $len = strlen($file);
29                     if (substr($file, $len - 4) == ".dat") {
30                         $number = $this->getNumberOfQuotes($dir . "/" . $file);
31                         $amount += $number;
32                         $quotes[$index] = $amount;
33                         $files[$index] = $file;
34                         $index++;
35                     }
36                 }
37             }
38
39             srand((double)microtime() * 1000000);
40             $index = rand(0, $amount);
41             $i = 0;
42
43             if ($amount)
44                 while ($quotes[$i] < $index) {
45                     $i++;
46                 }
47             if (!empty($files))
48                 return $this->getRandomQuote($dir . "/" . $files[$i]);
49         }
50         return -1;
51     }
52
53     /*
54      Reads the number of quotes in the file.
55     */
56     function getNumberOfQuotes($file)
57     {
58         $fd = fopen($file, "rb");
59         $this->readLong($fd); // Just move over the first long. Might as well be fseek.
60         $len = $this->readLong($fd);
61         fclose($fd);
62         return $len;
63     }
64
65     /*
66      Picks quote number $index from the dat-file in $file.
67     */
68     function getExactQuote($file, $index)
69     {
70         if (is_file($file) == false) {
71             echo "Input must be a file!<br/>";
72             return;
73         }
74
75         if (($fd = fopen($file, "rb")) == false) {
76             echo "Cannot open $file<br/>";
77             return;
78         }
79         fseek($fd, 24 + 4 * $index);
80
81         $phys_index = $this->readLong($fd);
82
83         fclose($fd);
84
85         $quotefile = substr($file, 0, strlen($file) - 4);
86
87         if (($fd = fopen($quotefile, "rb")) == false) {
88             echo "Cannot find file $quotefile!<br/>";
89         }
90
91         $res = $this->getQuote($fd, $phys_index);
92         fclose($fd);
93
94         return $res;
95     }
96
97     /*
98      Returns a random quote from $file.
99     */
100     function getRandomQuote($file)
101     {
102         $number = $this->getNumberOfQuotes($file);
103
104         $index = rand(0, $number - 1);
105
106         return $this->getExactQuote($file, $index);
107     }
108
109     /*
110      Reads a quote from the specified index.
111     */
112     function getQuote($fd, $index)
113     {
114         fseek($fd, $index);
115         $line = "";
116         $res = "";
117         do {
118             $res = $res . $line;
119             $line = fgets($fd, 1024) . "<br>";
120         } while (($line[0] != "%") && (!feof($fd)));
121
122         return $res;
123     }
124
125     /*
126      Gets indexes from the file pointed to by the filedescriptor $fd.
127     */
128     function getIndices($fd)
129     {
130         fseek($fd, 24, SEEK_SET);
131         $i = 0;
132
133         while (feof($fd) == FALSE) {
134             $res[$i] = readLong($fd);
135             $i++;
136         }
137         return $res;
138     }
139
140     function readLong($fd)
141     {
142         $res = fread($fd, 4);
143         $l = ord($res[3]);
144         $l += ord($res[2]) << 8;
145         $l += ord($res[1]) << 16;
146         $l += ord($res[0]) << 24;
147         return $l;
148     }
149
150     function createIndexFile($file)
151     {
152         $fd = @fopen($file, "r");
153         if ($fd == false) {
154             echo "File error!";
155             exit;
156         }
157
158         $i = 1;
159         $length = 0;
160         $longest = 0;
161         $shortest = 100000;
162         $indices[0] = 0;
163         while (!feof($fd)) {
164             $line = fgets($fd);
165             if ($line == "%\n") {
166                 $indices[$i] = ftell($fd);
167                 $i++;
168                 if ($length > $longest)
169                     $longest = $length;
170
171                 if ($length < $shortest)
172                     $shortest = $length;
173
174                 $length = 0;
175             } else {
176                 $length = $length + strlen($line);
177             }
178         }
179
180         fclose($fd);
181
182         $fd = @fopen($file . ".dat", "w");
183
184         if ($fd == false) {
185             echo "<!-- createIndexFile: Could not write to file....-->";
186             exit;
187         }
188
189         // Write header.
190         $this->writeLong($fd, 2);
191         $this->writeLong($fd, count($indices));
192         $this->writeLong($fd, $longest);
193         $this->writeLong($fd, $shortest);
194         $this->writeLong($fd, 0);
195         $this->writeLong($fd, 37 << 24);
196
197         for ($i = 0; $i < count($indices); $i++) {
198             $this->writeLong($fd, $indices[$i]);
199         }
200
201         fclose($fd);
202     }
203
204     function writeLong($fd, $l)
205     {
206         fwrite($fd, chr(($l >> 24) & 255));
207         fwrite($fd, chr(($l >> 16) & 255));
208         fwrite($fd, chr(($l >> 8) & 255));
209         fwrite($fd, chr($l & 255));
210     }
211 } // End of class