95 lines
2.2 KiB
PHP
95 lines
2.2 KiB
PHP
|
<?php
|
|||
|
|
|||
|
/*
|
|||
|
Name: fortune.php
|
|||
|
Author: NicoSoft
|
|||
|
Cr<EFBFBD>ation: 07/07/01
|
|||
|
MAJ : 22/06/02
|
|||
|
*/
|
|||
|
|
|||
|
$FileName=$_GET["FileName"];
|
|||
|
|
|||
|
// plante la graine...
|
|||
|
mt_srand ((double) microtime() * 1000000);
|
|||
|
if (!isset($FortuneDir) || $FortuneDir == " ")
|
|||
|
$FortuneDir = "fortunes/";
|
|||
|
|
|||
|
if (!isset($FileName) || $FileName == "")
|
|||
|
$FileName = "random";
|
|||
|
|
|||
|
|
|||
|
/********************
|
|||
|
* gestion des fichiers *
|
|||
|
********************/
|
|||
|
if ($FileName == "random")
|
|||
|
{
|
|||
|
//echo "mode aleatoire:<br/>";
|
|||
|
$d = opendir($FortuneDir);
|
|||
|
|
|||
|
$Nb=0;
|
|||
|
|
|||
|
// r<>cup<75>re les noms des fichiers
|
|||
|
// (uniquement ceux sans points)
|
|||
|
while (false!==($file = readdir($d)))
|
|||
|
if (!strchr($file,"."))
|
|||
|
$FileNameNoDot[$Nb++] = $file;
|
|||
|
|
|||
|
// selectionne un fichier au hasard
|
|||
|
$Number= mt_rand(0, $Nb-1);
|
|||
|
|
|||
|
// ce fichier sera utilis<69> comme fichier par d<>faut
|
|||
|
$FileName=$FileNameNoDot[$Number];
|
|||
|
|
|||
|
closedir($d);
|
|||
|
}
|
|||
|
// v<>rifie si les fichier existent
|
|||
|
if (!file_exists("$FortuneDir$FileName"))
|
|||
|
die("$FortuneDir$FileName n'existe pas!!");
|
|||
|
else if (!file_exists("$FortuneDir$FileName.dat"))
|
|||
|
die("$FortuneDir$FileName.dat n'existe pas!!");
|
|||
|
else
|
|||
|
/********************
|
|||
|
* gestion des citations *
|
|||
|
********************/
|
|||
|
{
|
|||
|
$fd=fopen("$FortuneDir$FileName.dat", "rb");
|
|||
|
|
|||
|
// saute le num<75>ro de version (sans interet)
|
|||
|
$trash=fread($fd,4);
|
|||
|
|
|||
|
// r<>cup<75>re le nb de citations
|
|||
|
$str_numstr=hexdec(bin2hex(fread($fd,4)));
|
|||
|
|
|||
|
// on en prend une au hasard
|
|||
|
$Number= mt_rand(0, $str_numstr-1);
|
|||
|
|
|||
|
// recherhe la position
|
|||
|
$trash=fread($fd, 12);
|
|||
|
$str_delim=fread($fd,4);
|
|||
|
|
|||
|
for($i=0;$i<=$Number;$i++)
|
|||
|
$str_pointer=hexdec(bin2hex(fread($fd,4)));
|
|||
|
|
|||
|
fclose($fd);
|
|||
|
$CptChar = 0;
|
|||
|
// recherche dans le fichier texte la citation correspondante
|
|||
|
$fd=fopen("$FortuneDir$FileName", "r");
|
|||
|
fseek($fd, $str_pointer);
|
|||
|
|
|||
|
$Char=fgets($fd, 1024);
|
|||
|
while (strcmp($Char,"%\n")!=0 && !feof($fd))
|
|||
|
{
|
|||
|
if (feof($fd))
|
|||
|
fseek($fd,0);
|
|||
|
|
|||
|
echo htmlspecialchars(wordwrap($Char,30,"\n",1));
|
|||
|
echo "<br/>\n";
|
|||
|
|
|||
|
$Char=fgets($fd, 100);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
fclose($fd);
|
|||
|
}
|
|||
|
?>
|