File: fortune.php

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (199 lines) | stat: -rw-r--r-- 5,096 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
rcs_id('PHP Fortune - Made by henrik@aasted.org. HP: http://www.aasted.org');
rcs_id('$Id: fortune.php,v 1.2 2004/11/21 11:59:20 rurban Exp $');
/* 
Main methods to use:
 quoteFromDir($dir):
   Quotes from any of the fortune-files in the dir.
 getRandomQuote($file):
   Quotes from the specific file.
   
 Written by Henrik Aasted Sorensen, henrik@aasted.org
 Read more at http://www.aasted.org/quote
*/
class Fortune {

    function quoteFromDir($dir) {
        $amount = 0;
        $index = 0;

        if ( $handle = opendir($dir) ) {
            while (false !== ($file = readdir($handle))) {
		
                if ( strpos($file, ".dat") != false) {
                    $len = strlen($file);
                    if (substr($file, $len - 4) == ".dat"){
                        $number = $this->getNumberOfQuotes($dir . "/" . $file);
                        $amount += $number;
                        $quotes[$index] = $amount;
                        $files[$index] = $file;
                        $index++;					
                    }
                }			
		
            }
	
            srand((double)microtime()*1000000);
            $index = rand(0, $amount);
            $i = 0;
	
            while ($quotes[$i] < $index)  {
                $i++;	
            }

            return $this->getRandomQuote($dir . "/" .$files[$i]);
        }
        return -1;
    }

    /*
     Reads the number of quotes in the file. 
    */
    function getNumberOfQuotes($file) {
        $fd = fopen($file, "rb");
        $this->readLong($fd); // Just move over the first long. Might as well be fseek.
        $len =  $this->readLong($fd);
        fclose($fd);
        return $len;
    }
    /*
     Picks quote number $index from the dat-file in $file.
    */
    function getExactQuote($file, $index) {
        if (is_file($file) == false) {
            echo "Input must be a file!<br/>";
            return;
        }
	
        if ( ($fd = fopen($file, "rb")) == false ) {
            echo "Cannot open $file<br/>";	
            return;
        }
        fseek($fd, 24 + 4 * $index);
	
        $phys_index = $this->readLong($fd);
	
        fclose($fd);
	
        $quotefile = substr($file, 0, strlen($file) - 4);

        if ( ($fd = fopen($quotefile, "rb")) == false ) {
            echo "Cannot find file $quotefile!<br/>";
        }		
	
        $res = $this->getQuote($fd, $phys_index);
        fclose($fd);
	
        return $res;	
    }

    /*
     Returns a random quote from $file.
    */
    function getRandomQuote($file) {
        $number = $this->getNumberOfQuotes($file);

        $index = rand(0, $number - 1);

        return $this->getExactQuote($file, $index);
    }	

    /*
     Reads a quote from the specified index.
    */
    function getQuote($fd, $index) {
        fseek($fd, $index);
        $line=""; $res = "";
        do {
            $res = $res . $line;		
            $line = fgets($fd, 1024) . "<br>";
        } while ( ($line[0] != "%") && (!feof($fd)) );

        return $res;
    }

    /*
     Gets indexes from the file pointed to by the filedescriptor $fd.
    */
    function getIndices($fd) {
        fseek($fd, 24, SEEK_SET);
        $i = 0;
	
        while ( feof($fd) == FALSE ) {
            $res[$i] = readLong($fd);
            $i++;
        }
        return $res;
    }

    function readLong($fd) {
        $res = fread($fd, 4);
        $l = ord($res[3]);
        $l += ord($res[2]) << 8;
        $l += ord($res[1]) << 16;
        $l += ord($res[0]) << 24;
        return $l;
    }


    function createIndexFile($file) {
        $fd = @fopen($file, "r");
        if ($fd == false) {
            echo "File error!";
            exit;
        }

        $i = 1;
        $length = 0;
        $longest = 0;
        $shortest = 100000;
        $indices[0] = 0;
        while (!feof($fd)) {
            $line = fgets($fd);
            if ($line == "%\n") {
                $indices[$i] = ftell($fd);
                $i++;
                if ($length > $longest)
                    $longest = $length;
	
                if ($length < $shortest)
                    $shortest = $length;

                $length = 0;
            } else {
                $length = $length + strlen($line);
            }
        }

        fclose($fd);

        $fd = @fopen($file . ".dat", "w");

        if ($fd == false) {
            echo "<!-- createIndexFile: Could not write to file....-->";
            exit;
        }

        // Write header.
        $this->writeLong($fd, 2);
        $this->writeLong($fd, count($indices));
        $this->writeLong($fd, $longest);
        $this->writeLong($fd, $shortest);
        $this->writeLong($fd, 0);
        $this->writeLong($fd, 37 << 24);

        for ($i = 0 ; $i < count($indices) ; $i++) {
            $this->writeLong($fd, $indices[$i]);
        }

        fclose($fd);
    }

    function writeLong($fd, $l) {
        fwrite($fd, chr ( ($l >> 24) & 255));
        fwrite($fd, chr ( ($l >> 16) & 255));
        fwrite($fd, chr ( ($l >> 8) & 255));
        fwrite($fd, chr ( $l & 255));
    }
} // End of class
?>