<?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__)."/../../cms/Validator.class.php");
  require_once(dirname(__FILE__)."/../../cms/Post.class.php");
  require_once(dirname(__FILE__)."/../../cms/SectionBlog.class.php");
  require_once(dirname(__FILE__)."/../../cms/Auth.class.php");
  require_once(dirname(__FILE__)."/../../cms/Errors.inc.php");

  // Check for authorizations
  $auth = new Auth();
  if (!$auth->isAuth()){
  	header("Location: login.php");
  	die();
  }
  // ------ end auth's check
  	
  $postID 	  = date("Ymd");
  $postHeader = date ("l dS of F Y");
  $postTitle = "";
  $postContent = "";
  $postSection = "";
  $postVisible = false;
  $footVisible = true;

  if (isset($_GET['blogID']))
  	$blogID = $_GET['blogID'];
  else
  	$blogID = "";
  if (isset($_GET['postSection']))
  	$postSection = stripslashes($_GET['postSection']);
  else
  	$postSection = "";
 
   /**
   * Delete action
   */
  if (isset($_GET['delete'])){
  	$pID 	= $_GET['delete'];
  	$post = new Post($blogID);
  	$post->deletePost($pID,$postSection);
  	print('DELETED!');
  	die();
  }

  /**
   * Modify action
   */
  if (isset($_GET['modify'])){
  	$pID 	= $_GET['modify'];
  	$postID = $pID;
  	$footVisible = $_GET['footVisible'];
	$postHeader = stripslashes(strip_tags(file_get_contents(BLOG_PATH.$blogID.BLOG_DATA.$postSection.'/'.$pID.'_header'),''));
  	$postTitle = stripslashes(strip_tags(file_get_contents(BLOG_PATH.$blogID.BLOG_DATA.$postSection.'/'.$pID.'_title'),''));
  	$post = new Post($blogID);
  	$postVisible = $post->isPostVisible($pID,$postSection);
	if ($postVisible)
  		$postContent = file_get_contents(BLOG_PATH.$blogID.BLOG_DATA.$postSection.'/'.$pID);
  	else
  		$postContent = file_get_contents(BLOG_PATH.$blogID.BLOG_DATA.$postSection.'/'.$pID.'_hide');
  }
  	
  	
  if (isset($_POST['blogID'])){
  	if (!($_POST['blogID']=='')){
	  	$blogID = $_POST['blogID'];
	   if (isset($_POST['postID']))
		  	$postID = $_POST['postID'];
	   else
	   		$postID = "";
	   if (isset($_POST['postHeader']))
		  	$postHeader = stripslashes($_POST['postHeader']);
		else
			$postHeader = "";
 	 	if (isset($_POST['postTitle']))
	  		$postTitle 	= stripslashes($_POST['postTitle']);
	  	else
	  		$postTitle = "";
 	 	if (isset($_POST['postContent']))
	  		$postContent 	= stripslashes($_POST['postContent']);
	  	else
	  		$postContent = "";		
 	 	if (isset($_POST['postSection']))
	  		$postSection = stripslashes($_POST['postSection']);
	  	else
	  		$postSection = "";		
 		if (isset($_POST['postVisible'])){
		  	if (strcasecmp($_POST['postVisible'],"on")==0)
		  		$postVisible = true;
		}else
			$postVisible = false; 		
 		if (isset($_POST['footVisible'])){
		  	if (strcasecmp($_POST['footVisible'],"on")==0)
		  		$footVisible = true;
		}else
			$footVisible = false;
		if (strcasecmp(trim($footVisible),'')==0)
			$footVisible = false;
  	  	  	  	
		/** 
		 * Check values
		 */
		 $validator = new Validator();
		 $validator->validateEmptyGeneral($postHeader,"Header contains invalid chars");
		 $validator->validateEmptyGeneral($postTitle,"Title contains invalid chars");
		 $validator->validateGeneral($postContent,"Content is empty or contains invalid chars");
		 // prints errorsEmpty
		 if ($validator->foundErrors()){
		 	print("Errors found:<BR>");
		 	print("<font color=\"red\">");	 	
		 	print($validator->listDotErrors());
		 	print("</font>");
		 }else { 	
			 /**
			  * Init CMS system
		  	  */
	  		 $post = new Post($blogID);
			 if (!($post->addPost($postID,$postHeader,$postTitle,$postContent,$postSection,$postVisible,$footVisible))){
			  		print(ERROR_FILESYSTEM_PERMISSION);
			  		die();
			  }else{
			  	$postID = "";
			  	$postHeader = "";
			  	$postTitle = "";
			  	$postContent = "";
			  	$postSection = "";
			  	$postVisible = false;
			  	$footVisible = true;
			  	print("ADDED!");
			  	die();
			  }			  	
		 }

		 
  	}
  }
  
 
?>
<script>
function win(str) {
   Win = window.open(str,'browseContent','scrollbars=yes,resizable=no,width=800,height=600,status=no,location=no,toolbar=no');
}
function winsmall(str) {
   Win = window.open(str,'browseHelp','scrollbars=yes,resizable=no,width=600,height=400,status=no,location=no,toolbar=no');
}
</script>
	
<FORM action="<?=$_SERVER['PHP_SELF']?>" method="POST">
	<input type="hidden" name="blogID" value="<?=$blogID?>">
	<table cellspacing="0" cellpaddgin="0" border="0">
		<tr>
			<td><b>Section</b></td>
			<td>
				<SELECT name="postSection">
				<?
					$sec = new SectionBlog($blogID);
					$sections = $sec->getBlogSectionsID();
					foreach($sections as $val){
						if (strcasecmp(trim($postSection),$val)==0)
							$sel = "selected";
						else
							$sel = "";
					?>
						<OPTION <?=$sel?>><?=$val?></OPTION>
					<?
					}
				?>
				</SELECT>
			</td>
		</tr>	
		<tr>
			<td><b>PostID</b></td>
			<td><input type="TEXT" name="postID" size="20" maxlength="8" value="<?=$postID?>">
			<?
				$tmpID = $postID;
				if (file_exists(BLOG_PATH.$blogID.BLOG_DATA.$postSection.'/'.$tmpID)){
					$overwrite = true;
				}else{
					if (file_exists(BLOG_PATH.$blogID.BLOG_DATA.$postSection.'/'.$tmpID."_hide")){
						$overwrite = true;
					}else
						$overwrite = false;
				}
				
				if ($overwrite){
					print('<font color="red">You are going to overwrite the current entry.</font>');
				}
			
			?>
			</td>
		</tr>
		<tr>
			<td><b>Header</b></td>
			<td><input type="TEXT" name="postHeader" size="60" value="<?=$postHeader?>"></td>
		</tr>
		<tr>
			<td><b>Title</b></td>
			<td><input type="TEXT" name="postTitle" size="60" value="<?=$postTitle?>">
			 &nbsp; 
			 <a href="javascript:win('../admin/manage.php?blogID=<?=$blogID?>&contentType=<?=_CONTENT?>');">browse contents</a>
			 <a href="javascript:winsmall('../admin/helpPost.html');"><img src="./images/help.png" border="0" width="16" height="16"></a>
			 </td>
		</tr>
		<tr>
			<td><b>Content</b>&nbsp;</td>
			<td><TEXTAREA name="postContent" cols="60" rows="14"><?=$postContent?></TEXTAREA></td>
		</tr>
		<tr>
			<td><b>Visible</b></td>
			<td><input type="CHECKBOX" name="postVisible" <?if($postVisible) echo('checked')?>></td>
		</tr>
		<tr>
			<td><b>Footer</b></td>
			<td><input type="CHECKBOX" name="footVisible" <?if($footVisible) echo('checked')?>></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>
				<input type="SUBMIT" value="post" size="1">
			</td>
		</tr>
	</table>
</FORM>