[horde] Fwd:
Tim Gorter
email at teletechnics.com
Mon Aug 12 13:27:54 PDT 2002
Attached is fortunes.php a little script to get random messages from the
fortunes files as on unix machines.
It reads the fortune text files, and processes them, so I suggest not to
use all to big files, as it'll take too long to process.
But if people want to create their own they can follow the standard layout
and create their own quotes / messages etc. Or simply receive some of the
others available on the net.
I have put the following in the motd.php
include_once HORDE_BASE . '/lib/fortunes.php';
$f = new Fortunes;
$fortune_file = '/usr/bin/fortunes/fortune';
$fortune = $f->getRandomFortunes("$fortune_file");
echo "$fortune";
have fun,
tim.
-------------- next part --------------
<?php
/**
* Little scriblet to get a random message from the fortune files often available on
* unix based systems.
*
* Retrieves the fortune files with all fortune quotes, and randomly selects one.
* Does not require the associated .dat index file, and therefore new fortune
* files can easily be added.
* Fortune files are simple text files with every quote seperated by a % as the only
* character on its own, at the beginning of a new line.
*
* I do suggest to only use smaller fortune files, as larger ones will take some
* time to process.
*
* Fortune files are availbale at various databases around the internet.
* ie. http://freshmeat.net/browse/895/?topic_id=895 at freshmeat.net
*
* @author: Tim Gorter
* @version: 0.9
*
*/
class Fortunes {
/**
* Check the existance of the required fortunes file $file
*
* $file = file that contains the actual fortunes quotations
* must be without '.dat'
*/
function checkFortunes($file) {
if (!is_file($file)) {
$err_str = "$file" . _(' cannot be accessed!');
return $err_str;
}
return false;
}
/**
* Retrieve a Quote from the fortune file $file.
*
* $file = file that contains the actual fortunes quotations
* must be without '.dat'
*/
function getFortunes($file) {
$fp = fopen($file, "rb");
$file_contents = fread ($fp, filesize ($file));
$n = substr_count($file_contents, "\n%\n");
$n = rand(0, $n+1);
$fortune_array = preg_split ("[\n%\n]",$file_contents);
fclose($fp);
$fortune = $fortune_array[$n-1];
return $fortune;
}
/**
* Returns a random Fortune from file $file
*
* $file = file that contains the actual fortunes quotations
* must be without '.dat'
*/
function getRandomFortunes($file) {
if (!$err_str = $this->checkFortunes($file)) {
if (!$Fortunes = $this->getFortunes($file))
$Fortunes = _('Sorry, no message available');
} else {
$Fortunes = _('There seems to be an error - no Fortunes message could be
retrieved for your pleasure because ') . "$err_str";
}
return $Fortunes;
}
} // class Fortunes
?>
More information about the horde
mailing list