<?php
/*
 *
 * sflog!
 * Copyright (C) 2005 Alberto Sfolcini <a.sfolcini@gmail.com>
 *
 * This file is a part of sflog!.
 *
 * sflog! is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * sflog! is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

require_once(dirname(__FILE__)."/../common/Ini.class.php");
require_once(dirname(__FILE__)."/../cms/BlogBar.class.php");
require_once(dirname(__FILE__)."/../cms/Constants.inc.php");
require_once(dirname(__FILE__)."/../common/utils.func.php");

	
/**
 * @title			: Post
 * @description		: Manages Posts   
 * @date			: 7-Nov-2005   
 * @author			: Alberto Sfolcini  <a.sfolcini@gmail.com>
 */
class Post {

	var $postID			= "";
	var $postHeader 	= "";
	var $postTitle		= "";
	var $postContent	= "";
	var $postSection	= "";
	var $postVisible	= false;
	var $footVisible	= true;

	var $ini;
	
	var $blogID			= "";

	/**
	 * Constructor 1
	 */
	function Post($blogID){
		$this->blogID = $blogID;
	}

	/**
	 * New Post
	 * @param $postID string
	 * @param $postHeader string
	 * @param $postTitle string
	 * @param $postContent string
	 * @param $postSection string 
	 * @param $postVisible boolean
	 * @param $footVisible boolean 
	 * @return boolean
	 */
	function addPost($postID,$postHeader,$postTitle,$postContent,$postSection,$postVisible,$footVisible){
		if (strcasecmp(trim($postID),"")==0)
			$postID = date("Ymd");
		$this->postID	= $postID;
				
		$this->postHeader = $postHeader;
		$this->postTitle  = $postTitle;
		$this->postContent	= $postContent;
		$this->postSection	= $postSection;
		$this->postVisible = $postVisible;
		if (strcasecmp(trim($footVisible),"")==0)
			$footVisible = 0;
		else
			$footVisible = true;
		$this->footVisible = $footVisible;
		
		$ret = true;
				
		// write file in $postSection directory
		//if (strcasecmp(trim($this->postSection),HOME_BLOG_SECTION)==0)
		//	$this->postSection = "";		
		// set filename as yyyymmdd, data ISO format
		$fn = $this->postID;

		/**
		 * Write Header
		 */		
		$file_header = new WriteFile(BLOG_PATH.$this->blogID.BLOG_DATA.$this->postSection.'/'.$fn.'_header',REWRITE);
		$file_header->writeStr('<H2>'.$postHeader.'</H2>');
		$file_header->closeFile();
		
		/**
		 * write Title 
		 */
		$file_title = new WriteFile(BLOG_PATH.$this->blogID.BLOG_DATA.$this->postSection.'/'.$fn.'_title',REWRITE);
		if (strcasecmp(trim($postTitle),"")!=0)
			$file_title->writeStr('<H3>'.$postTitle.'</H3>');
		$file_title->closeFile();
		
		/**
		 * Write Content 
		 */
		if ($this->postVisible==false)
			$tfn = $fn.'_hide';
		else
			$tfn = $fn;
			
		$tmpfn = BLOG_PATH.$this->blogID.BLOG_DATA.$this->postSection.'/'.$fn;
		if (file_exists($tmpfn.'_hide'))
			unlink($tmpfn.'_hide');
		if (file_exists($tmpfn))
			unlink($tmpfn);		
		$file_content = new WriteFile(BLOG_PATH.$this->blogID.BLOG_DATA.$this->postSection.'/'.$tfn,REWRITE);
		$file_content->writeStr($postContent);
		$file_content->closeFile();

		/**
		 * Write Footer
		 */
		$file_footer = new WriteFile(BLOG_PATH.$this->blogID.BLOG_DATA.$this->postSection.'/'.$fn.'_footer',REWRITE);
		$file_footer->writeStr('<p class="footer">');
		$file_footer->writeStr('<?$id='.$postID.'?>');
		$file_footer->writeStr('<?$fv='.$this->footVisible.'?>');
		if ($this->footVisible){
			$file_footer->writeStr(date('G:i a T'));
			if (file_exists(SITEDOMAIN))
				$domain =  file_get_contents(SITEDOMAIN);
			else
				$domain = '';
			$file_footer->writeStr('| <a href="?blog='.$this->blogID.'&section='.$this->postSection.'&permalink='.$postID.'">permalink</a>');
			//$file_footer->writeStr('| <a href="">comments (0)</a>');
			$file_footer->writeStr('| <a href="http://www.google.com/blogsearch?q=related: '.$this->postTitle.'"><img src="images/googleicon.gif" height="16" width="16" border="0" align="middle" alt="links to this post" /></a>');
			
		}
		if (strcasecmp(trim($this->postSection),'')==0)
			$file_footer->writeStr('<?include(dirname(__FILE__)."/../../../includes/footer.inc.php")?>');
		else
			$file_footer->writeStr('<?include(dirname(__FILE__)."/../../../../includes/footer.inc.php")?>');
		$file_footer->writeStr('</p>');
		$file_footer->closeFile();
		
		// check if all steps above were ok, if yes return true
		return $ret;
	}
	
	/**
	 * delete given post $id
	 * @param $id
	 * @param $sectionID
	 */
	function deletePost($id,$sectionID){
		// delete filesystem
		$fn = BLOG_PATH.$this->blogID.BLOG_DATA.$sectionID.'/'.$id;
		if (file_exists($fn)){
			unlink($fn);
		}
		if (file_exists($fn.'_hide')){
			unlink($fn.'_hide');
		}
		if (file_exists($fn.'_header')){
			unlink($fn.'_header');
		}
		if (file_exists($fn.'_title')){
			unlink($fn.'_title');
		}
		if (file_exists($fn.'_footer')){
			unlink($fn.'_footer');
		}
	}
	
	/**
	 * Returns true if given blogID exists
	 * @param $id
	 * @param $sectionID
	 * @return boolean
	 */
	function  exists($id,$sectionID){
		$fn = BLOG_PATH.$this->blogID.BLOG_DATA.$sectionID.'/'.$id;
		if (file_exists($fn))
			return true;
		else
			if (file_exists($fn.'_hide'))
				return true;
			else
				return false;
	}


	/**
	 * Is post visible?
	 * @param $id
	 * @param $sectionID
	 * @return boolean
	 */
	function isPostVisible($id,$sectionID){
		$fn = BLOG_PATH.$this->blogID.BLOG_DATA.$sectionID.'/'.$id;
		if (file_exists($fn))
			return true;
		else
			if (file_exists($fn.'_hide'))
				return false;
			else
				return true;
	}


} // end class
?>