When I say simple, I mean simple! Its current functionality is that of only being able to create a new blog post. Since this is only the class, and not the client code, there is no formatting or HTML output going on.
Please tell me what you think.
/*
Description: This class is able to write data to a text file (assuming
write permissions). It can not edit or delete entrys.
*/
class FFDBBlog //FFDBBlog stands for "Flat File DataBase weB log"
{
/* 'private' memebers */
var $error; //array of error messages
/* 'public' members */
var $strFirstPostPath;
var $strBlogArchivePath;
/** Constructors **/
function FFDBBlog($strMostCurrentPath="", $strBlogArhive="")
{
/***\
Description: Constructor for $this' memebrs
Precondition: $strMostCurrentPath must be a real ABSOLUTE path on the server.
Postcondition: NULL
\***/
$this->strFirstPostPath = $strMostCurrentPath;
$this->strBlogArchivePath = $strBlogArhive;
$this->error = array();
}
/** Accessors **/
function getFPP()
{
return $this->strFirstPostPath;
}
function getBAP()
{
return $this->strBlogArchivePath;
}
function getError()
{
return $this->error;
}
/** (Basic) Modifiers **/
function setFPP($newFPP)
{
$this->strFirstPostPath = $newFPP;
}
function setBAP($newBAP)
{
$this->strBlogArchivePath = $newBAP;
}
function setError($newErrorArr)
{//[Precondition]: $newErrorArr is an array
if(is_array($newErrorArr))
{
$this->error = $newErrorArr;
}
else
{
$this->addError("FFDBBlog: [setError()] - $newErrorArr is not an array\n");
}
}
/** other methods **/
function addError($strErrString)
{
/***\
Description: adds an error to the error array
Precondition: NULL
Postcondition: $strErrString is added as a new index at the end of the error array
\***/
$this->error[] = $strErrString."\n";
}
function resetError()
{
/***\
Description: purges error array
Precondition: NULL
Postcondition: $this->error === array()
\***/
foreach ($this->error as $i => $value) {
unset($this->error[$i]);
}
if(count($this->error) < 1)
{
return true;
}
return false;
}
function errorString()
{
foreach($this->error as $i => $value)
{ //add new lines to end of error strings
$this->error[$i] .= "\r\n";
}
return join("", $this->error);
}
function numErrors()
{
return count($this->error);
}
function errors()
{
if(count($this->error) > 0)
return true;
return false;
}
function p4_file_put_contents($strFileName, $data, $append=false)
/***\
Description: This function does the same thing as the PHP5 function file_put_contents,
but with less functionality (only option for append, as opposed
for multiple possilbe options)
Precondition:
Postcondition: $data has been written to the file $strFileName if $append is
set, $data will be appended to the file. if it is false, or
not set, the file will be truncated to 0 bytes, and then $data
will be written to the beginning of the file.
\***/
{
clearstatcache();
if(!file_exists($strFileName))
{
$this->addError('FFDBBlog: p4_file_put_contents: file does not exist');
return false;
}
if(!is_writable($strFileName))
{
$this->addError('FFDBBlog: p4_file_put_contents: file is NOT writable');
return false;
}
if($append === false)
{ //overwrite file
$fp = @fopen($strFileName, "w") or $this->addError('FFDBBlog: p4_file_put_contents: unable to open file for writing (overwrite)');
@flock($fp, LOCK_EX) or $this->addError('FFDBBlog: p4_file_put_contents: unable to obtain an exclusive lock on file (overwrite)');
@fwrite($fp, $data, strlen($data)) or $this->addError('FFDBBlog: p4_file_put_contents: unable to write file contents (overwrite)');
@flock($fp, LOCK_UN) or $this->addError('FFDBBlog: p4_file_put_contents: unable to unlock file (overwrite)');
@fclose($fp) or $this->addError('FFDBBlog: p4_file_put_contents: unable to close file pointer (overwrite)');
}
else
{ //append to file
$fp = @fopen($strFileName, "a") or $this->addError('FFDBBlog: p4_file_put_contents: unable to open file for writing (append)');
@flock($fp, LOCK_EX) or $this->addError('FFDBBlog: p4_file_put_contents: unable to obtain an exclusive lock on file (append)');
@fwrite($fp, $data, strlen($data)) or $this->addError('FFDBBlog: p4_file_put_contents: unable to write file contents (append)');
@flock($fp, LOCK_UN) or $this->addError('FFDBBlog: p4_file_put_contents: unable to unlock file (append)');
@fclose($fp) or $this->addError('FFDBBlog: p4_file_put_contents: unable to close file pointer (append)');;
}
if(!($this->errors()))
{
return true;
}
return false;
}
function newPost($postContents)
/***\
Description: Creates a new post to the FFDBBlog
Precondition: NULL
Postcondition: the value of $contents is added to the FFDBBlog
On error, returns false, returns true otherwise
\***/
{
clearstatcache();
if(file_exists($this->strFirstPostPath))
{
if( ! ( is_readable($this->strFirstPostPath) && is_readable($this->strBlogArchivePath) ) )
{
$this->addError("FFDBBlog: newPost: Unable to continue, files NOT readable");
return false;
}
if( ! ( is_writable($this->strFirstPostPath) && is_writable($this->strBlogArchivePath) ) )
{
$this->addError("FFDBBlog: newPost: Unable to continue, files NOT writable");
return false;
}
$strOldNAPost = file_get_contents($this->strFirstPostPath) or $this->addError("FFDBBlog: newPost: (0.0.0) Unable to get first post contents for storage");
$strArchive = file_get_contents($this->strBlogArchivePath) or $this->addError("FFDBBlog: newPost: (0.0.1) Unable to get existing archive contents for storage");
/* truncate first post file */
$fp = @fopen($this->strFirstPostPath, "w") or $this->addError("FFDBBlog: newPost: (0.0.1.0) Unable to open/truncate first post contents");
@fclose($fp) or $this->addError("FFDBBlog: newPost: (0.0.1.1) Unable to close file stream");
/* end truncation */
$this->p4_file_put_contents($this->strFirstPostPath, $postContents) or $this->addError("FFDBBlog: newPost: (0.0.2) Unable to write new data into first post file"); //write new first post data to the first post file
$this->p4_file_put_contents($this->strBlogArchivePath, $strOldNAPost) or $this->addError("FFDBBlog: newPost: (0.0.3) Unable to archive old first post"); //make the old first post part of the archive
$this->p4_file_put_contents($this->strBlogArchivePath, $strArchive, true) or $this->addError("FFDBBlog: newPost: (0.0.4) Unable to write existing archive contents"); //append the existing archive to the file
}
else
{
$tmpArchiveContents = file_get_contents($this->strBlogArchivePath) or $this->addError("FFDBBlog: newPost: (0.1.0) Unable to store contents of archive file"); //get current archive file
$this->p4_file_put_contents($this->strBlogArchivePath, $postContents) or $this->addError("FFDBBlog: newPost: (0.1.1) Unable to write new post to archive file"); //overwrite archive file with new post data
$this->p4_file_put_contents($this-strBlogArchivePath, $tmpArchiveContents, true) or $this->addError("FFDBBlog: newPost: (0.1.2) Unable to append existing archive contents to archive file"); //append archive file with existing archive contents
}
if(!($this->errors()))
return true; //went off w/o a hitch, yay!!!
return false; //something went wrong
}
function toString()
{
$string = "Most recent post path: " . $this->getMBP();
$string .= "\nBlog archive path: " . $this->getBAP();
$string .= "\n<errors>\n\n" . $this->errorString() . "\n\n</errors>";
return $string;
}
}