Version: 1.0.1

Type: Class

Category: File Management

License: GNU General Public License

Description: This is a PHP class that simulates the fortune program. It prints out a random epigram from a cookie fortune file.



<?php

/**
Author		:	Robbie B. :)
Version		:	1.0.1
Date		:	Mon Aug  7 14:15:36 CEST 2000
Last Mod	:	Mon Aug 20 21:24:45 CEST 2001
OS			:	Linux xenon 2.2.18pre21 #5 Sat May 19 11:16:27 CEST 2001 i686 unknown
File		:	phpmyfortune.php
**/


class phpMyFortune
{
	var $filename;	// filename which contains our cookie fortunes :)
	var $isError;
	var $errorString;
	
	function phpMyFortune()
	{
		// Initialize default values
		$this->filename="";
		$this->isError=false;
		$this->errorString="";
	}
	function setFileName($NF)
	{
		if (!file_exists($NF)) 
		{
			$this->isError=true;	
			$this->stringaErrore="File ".$NF. " not exist";
			$this->filename="";
		}
		else
		{
			$this->filename=$NF;
		}
	}
	
	function getFortune()
	{
		$dimFile;
		$numRandom;
		$fd;
		$Character;
		$StringTemp;
		$NumMaxLinea;

		$NumMaxLinea=1024;
		if ($this->filename=="")
		{
			$this->isError=true;
			$this->errorString="What is your fortune file ?";
			return $this->errorString;
		}
		$dimFile= filesize($this->filename);
		mt_srand( (double) microtime() * 1000000);
		$numRandom=mt_rand(0,$dimFile);

		$fd = fopen($this->filename, "r");
		if (!$fd)
		{
			$this->isError=true;
			$this->errorString="Unable to open fortune file";
			return $this->errorString;
		}
			
		
		fseek($fd, $numRandom);
		$Character="";
		$StringTemp="";
		while (strcmp($Character,"%\n")!=0 && !feof($fd))
		{
			$Character=fgets($fd, $NumMaxLinea);
		}
		if (feof($fd))
		{
			fseek($fd,1);
		}
		$Character="";
		$Character=fgets($fd, $NumMaxLinea);
		while (strcmp($Character,"%\n")!=0 )
		{
	        $StringTemp=$StringTemp.$Character."<br>";
			$Character=fgets($fd, $NumMaxLinea);
		}
		fclose($fd);
		return $StringTemp;
	}
}