getQuote();
** or: (quote and author)
** $srv = new SnipSrv( "quotes.txt" );
** $item = $srv->getQuoteArr();
** echo "" . $item[0] . "" . $item[1] . "";
**
** with error checking:
** $srv = new SnipSrv( "filename.txt" );
** if( $srv->hasError() ) {
** echo "Error: " . $srv->errorString();
** exit;
** }
** echo $srv->getQuote();
**
** TODO: set it up to run from a database.
**
** $Id: snipsrv.php.txt,v 1.1.1.1 2004/02/18 13:29:07 jalal Exp $
***************************************************************/
class SnipSrv
{
var $type = "txt";
var $numQuotes = 0;
var $quotes = Array();
var $hadError = true;
var $errorString = "No file or db specified.";
function SnipSrv( $s = "", $type = "txt" )
{
srand( time() );
if( ($type = "txt") && (strlen($s) > 1) ) {
$this->read( $s );
}
else if( ($type = "db") && ( strlen($s) > 1) ) {
$this->connectToDb( $s );
}
}
function read( $filename )
{
$this->hadError = false;
if( ! file_exists( $filename) ) {
$this->hadError = true;
$this->errorString = "Unable to open file: " . $filename . "
\n";
return;
}
// read the file in and process it.
$lines = file( $filename );
$this->numQuotes = $this->process( $lines );
if( $this->numQuotes <= 0 ) {
$this->hadError = true;
$this->errorString = "File was apparently empty.";
return;
}
}
function connectDB( $db, $user, $pass )
{
}
function process( $lines )
{
// $lines is an array of lines read in from the file.
$lcount = count( $lines );
if( $lcount <= 0 ) {
return 0;
}
$section = 0;
$curr = new SnipItem();
for( $i = 0; $i < $lcount; $i++ ) {
if( $lines[$i][0] == ';' ) continue; // skip comment lines.
if( strlen($lines[$i]) < 1 ) continue; // and blank lines.
if( $lines[$i][0] == '=' && $lines[$i][1] == '=' ) { // its the end of a quote
array_push( $this->quotes, $curr );
$curr = new SnipItem();
$section = 0;
continue;
}
if( $lines[$i][0] == '-' && $lines[$i][1] == '-' ) { // its the author
$section = 1;
continue;
}
// otherwise its a string to add.
switch( $section ) {
case 0;
//echo "Adding to quote: " . $lines[$i] . "
\n";
$curr->addToQuote( $lines[$i] );
break;
case 1;
//echo "Adding to author: " . $lines[$i] . "
\n";
$curr->addToAuthor( $lines[$i] );
break;
default;
}// switch
}// for(...)
if( ! empty( $curr->quote[0] ) ) {
array_push( $this->quotes, $curr );
}
//echo " Number of quotes: " . count($this->quotes) . "
\n";
return 1;
}
// not designed to be called from outside
function _getItem( $idx ) {
if( $idx == -1 ) {
$idx = rand( 0, count($this->quotes)-1);
}
if( isset( $this->quotes[$idx] ) ) {
$item = $this->quotes[$idx];
} else {
$item = new SnipItem();
$item->addToQuote( "No index at " . $idx . " of " . count($this->quotes) );
$item->addToAuthor("Error");
}
return $item;
}
function getQuote( $idx = -1 )
{
$item = $this->_getItem($idx);
return $item->toString();
}
function getQuoteArr( $idx = -1 ) {
$item = $this->_getItem($idx);
$arr[0] = $item->quote();
$arr[1] = $item->author();
return $arr;
}
function hasError()
{
$tmp = $this->hadError;
$this->hadError = false;
return $tmp;
}
function lastErrorString()
{
return $this->errorString;
}
}
/**
* SnipItem is a private class, not to be used by clients
*/
class SnipItem
{
var $quote = Array();
var $author = Array();
function SnipItem()
{
}
function addToQuote( $str )
{
array_push($this->quote, $str);
}
function addToAuthor( $str )
{
array_push( $this->author, $str);
}
function author() {
return implode( $this->author, " " );
}
function quote() {
return implode( $this->quote, " " );
}
function toString()
{
$s = $this->quote();
$s .= "
\n";
$s .= $this->author();
return $s;
}
}
?>