<?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/File.class.php");
require_once(dirname(__FILE__)."/../common/utils.func.php");
require_once(dirname(__FILE__)."/../cms/Errors.inc.php");
require_once(dirname(__FILE__)."/../cms/Constants.inc.php");
	
/**
 * @title			: BlogEntry
 * @description		: Manages blog's entries   
 * @date			: 11-Oct-2005   
 * @author			: Alberto Sfolcini  <a.sfolcini@gmail.com>
 */
class BlogEntry {
	
	var $system;
	var $blogID;
	var $sectionID;
	
	/**
	 * Constructor
	 * @param $blogID string
	 */
	function BlogEntry($blogID,$sectionID){
		error_reporting(E_USER_ERROR);
		$this->blogID = $blogID;
		$this->sectionID = $sectionID;
	}

	/**
	 * Show a single post (permalink option)
	 * @param $entryID 
	 */
	function showEntry($entryID){
		$file = new File();
		if (strcasecmp(trim($this->sectionID),"")==0)
			$JournalContentDir = "./blogs/".$this->blogID."/data";
		else
			$JournalContentDir = "./blogs/".$this->blogID."/data/".$this->sectionID;
		// check if blog's dir exists
		if (!file_exists($JournalContentDir)){
			print(ERROR_FYLESYSTEM_MISSING);
		} else {
			if ((stripos($entryID,'_')==false)||(stripos($entryID,'_hide')!=false)){						
						if (is_dir($JournalContentDir."/".$entryID)==false){
					  		$tmp = stripos($entryID,'_hide');
							if ($tmp!=false){
					  			$auth = new Auth();
					  			if ($auth->isAuth()){
					  				$JournalContent = $JournalContentDir."/".substr($entryID,0,$tmp);
		  							include_once $JournalContent."_header";
		  							include_once $JournalContent."_title";
		  							include_once $JournalContent."_hide";
		  							include_once $JournalContent."_footer";
					  			}								
							}else{		  		
					  			$JournalContent = $JournalContentDir."/".$entryID;
		  						include_once $JournalContent."_header";
		  						include_once $JournalContent."_title";
		  						include_once $JournalContent;
		  						include_once $JournalContent."_footer";
							}
						}
			}
		}
	}

	/**
	 * show all entries
	 */	
	function showAllEntries(){
		$file = new File();
		if (strcasecmp(trim($this->sectionID),"")==0)
			$JournalContentDir = "./blogs/".$this->blogID."/data";
		else
			$JournalContentDir = "./blogs/".$this->blogID."/data/".$this->sectionID;
		// check if blog's dir exists
		if (!file_exists($JournalContentDir)){
			print(ERROR_FYLESYSTEM_MISSING);
		} else {
			// scans blog's dir
			$files=$file->scandir($JournalContentDir,1);
			foreach($files as $key=>$val) {
				if ($val[0]!=".") {
					if ((stripos($val,'_')==false)||(stripos($val,'_hide')!=false)){						
						if (is_dir($JournalContentDir."/".$val)==false){
					  		$tmp = stripos($val,'_hide');
							if ($tmp!=false){
					  			$auth = new Auth();
					  			if ($auth->isAuth()){
					  				$JournalContent = $JournalContentDir."/".substr($val,0,$tmp);
		  							include_once $JournalContent."_header";
		  							include_once $JournalContent."_title";
		  							include_once $JournalContent."_hide";
		  							include_once $JournalContent."_footer";
					  			}								
							}else{		  		
					  			$JournalContent = $JournalContentDir."/".$val;
		  						include_once $JournalContent."_header";
		  						include_once $JournalContent."_title";
		  						include_once $JournalContent;
		  						include_once $JournalContent."_footer";
							}
						}
					}
				} // end if
	 		} // end for
	 	}
	} // end showEntries


	/**
	 * show last N entries
	 * @param $n number of entries for page
	 * @param $url
	 * @param $from index of entry to show
	 */	
	function showLastEntries($n,$url,$from){
		$file = new File();
		if (strcasecmp(trim($this->sectionID),"")==0)
			$JournalContentDir = "./blogs/".$this->blogID."/data";
		else
			$JournalContentDir = "./blogs/".$this->blogID."/data/".$this->sectionID;
		// check if blog's dir exists
		if (!file_exists($JournalContentDir)){
			print(ERROR_FYLESYSTEM_MISSING);
		} else {
			// scans blog's dir
			$files=$file->scandir($JournalContentDir,1);
			// sorting
			arsort($files);
			$shown = 0;
			/**
			 * Why are you multiplies for 4?
			 * Because each entries has 4 files:
			 * 1. 20060101 			the content
			 * 2. 20060101_footer  	the footer
			 * 3. 20060101_header	the header
			 * 4. 20060101_title	the title
			 */
			for($i=($from*4);$i<sizeof($files);$i++){								
				$val = $files[$i];

				// Pagining system
				if ($shown>=$n){
					print('<div class="pager">');
					if ($from>0){
						print('&nbsp;<a href="'.$url.'&from='.($from-$n).'"><< '.PREV_PAGE.'</a>&nbsp;|&nbsp;');
					}								
					print('<a href="'.$url.'&from='.($from+$n).'"> '.NEXT_PAGE.' >></a>');					
					print('</div>');					
					break;
				}
				if (((($from+$n)*4)>=sizeof($files))&&($i>=sizeof($files)-1)&&($from>0)){
					print('<div class="pager">');
					print('&nbsp;<a href="'.$url.'&from='.($from-$n).'"><< '.PREV_PAGE.'</a>');
					print('</div>');
					break;
				}
				// ----
				
				if ($val[0]!=".") {
					if ((stripos($val,'_')==false)||(stripos($val,'_hide')!=false)){												
						if (is_dir($JournalContentDir."/".$val)==false){
					  		$tmp = stripos($val,'_hide');
							if ($tmp!=false){
					  			$auth = new Auth();
					  			if ($auth->isAuth()){
					  				$JournalContent = $JournalContentDir."/".substr($val,0,$tmp);
		  							include_once $JournalContent."_header";
		  							include_once $JournalContent."_title";
		  							include_once $JournalContent."_hide";
		  							include_once $JournalContent."_footer";
					  			}								
							}else{		  		
					  			$JournalContent = $JournalContentDir."/".$val;
		  						include_once $JournalContent."_header";
		  						include_once $JournalContent."_title";
		  						include_once $JournalContent;
		  						include_once $JournalContent."_footer";
		  						$shown++;
							}
						}
					}
				} // end if
	 		} // end for
	 	}
	} // end showEntries


	/**
	 * show month entries
	 * @param $month
	 * @param $year
	 */	
	function showMonthEntries($month,$year){
		$file = new File();
		if (strcasecmp(trim($this->sectionID),"")==0)
			$JournalContentDir = "./blogs/".$this->blogID."/data";
		else
			$JournalContentDir = "./blogs/".$this->blogID."/data/".$this->sectionID;
		// check if blog's dir exists
		if (!file_exists($JournalContentDir)){
			print(ERROR_FYLESYSTEM_MISSING);
		} else {
			// scans blog's dir
			$files=$file->scandir($JournalContentDir,1);
			foreach($files as $key=>$val) {
				if ($val[0]!=".") {
					if ((stripos($val,'_')==false)||(stripos($val,'_hide')!=false)){						
						if (is_dir($JournalContentDir."/".$val)==false){
					  		$tmp = stripos($val,'_hide');
							$ym = substr($val,0,6);
					  		if (strcmp(trim($ym),trim($year.$month))==0){							
								if ($tmp!=false){					  			
					  				$auth = new Auth();
						  			if ($auth->isAuth()){
						  				$JournalContent = $JournalContentDir."/".substr($val,0,$tmp);
		  								include_once $JournalContent."_header";
		  								include_once $JournalContent."_title";
		  								include_once $JournalContent."_hide";
		  								include_once $JournalContent."_footer";
					  				}								
								}else{		  		
						  			$JournalContent = $JournalContentDir."/".$val;
		  							include_once $JournalContent."_header";
		  							include_once $JournalContent."_title";
		  							include_once $JournalContent;
		  							include_once $JournalContent."_footer";
								}
					  		}
						}
					}
				} // end if
	 		} // end for
	 	}
	} // end showEntries


	/**
	 * count entries
	 * @param $month
	 * @param $year
	 */	
	function countEntries($month,$year){
		$count = 0;
		$file = new File();
		if (strcasecmp(trim($this->sectionID),"")==0)
			$JournalContentDir = "./blogs/".$this->blogID."/data";
		else
			$JournalContentDir = "./blogs/".$this->blogID."/data/".$this->sectionID;
		// check if blog's dir exists
		if (!file_exists($JournalContentDir)){
			print(ERROR_FYLESYSTEM_MISSING);
		} else {
			// scans blog's dir
			$files=$file->scandir($JournalContentDir,1);
			foreach($files as $key=>$val) {
				if ($val[0]!=".") {
					if ((stripos($val,'_')==false)||(stripos($val,'_hide')!=false)){						
						if (is_dir($JournalContentDir."/".$val)==false){
					  		$tmp = stripos($val,'_hide');
							if ($tmp!=false){
							}else{		  		
					  			$ym = substr($val,0,6);
					  			if (strcmp(trim($ym),trim($year.$month))==0)
					  				$count++;
							}
						}
					}
				} // end if
	 		} // end for
	 		return $count;
	 	}
	} // end showEntries

		
} // end class
?>
