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