<?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.
 *
 */
 
  error_reporting(E_USER_ERROR);
 
  require_once(dirname(__FILE__)."/../../cms/Validator.class.php");
  require_once(dirname(__FILE__)."/../../cms/Blog.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
  
  $cms = new Blog();	
  $blogID = "";
  $blogName = "";
  $blogPosition = "";
  $blogVisible = false;
  $blogPostPerPage = "99";

  /**
   * Modify action
   */
  if (isset($_GET['modify'])){
  	$blogID 	= $_GET['modify'];
  	$blogName 	= $cms->getBlogFullName($blogID);
  	$blogPosition	= $cms->getBlogPosition($blogID);
  	$blogVisible	= $cms->isBlogVisible($blogID);
  	$blogPostPerPage = $cms->getPostPerPage($blogID);
  	$modify = true;
  } else{
  	$modify = false;
  }


  /**
   * Delete action
   */
  if (isset($_GET['delete'])){
  	$id = $_GET['delete'];
  	$cms->deleteBlog($id);	
  }


  if (isset($_POST['blogID'])){
  	if (!($_POST['blogID']=='')){

	  	$blogID = $_POST['blogID'];
	   if (isset($_POST['blogName']))
		  	$blogName = stripslashes($_POST['blogName']);
		else
			$blogName = "";
 	 	if (isset($_POST['blogPosition']))
	  		$blogPosition 	= stripslashes($_POST['blogPosition']);
	  	else
	  		$blogPosition = "";
		if (isset($_POST['blogVisible'])){
		  	if (strcasecmp($_POST['blogVisible'],"on")==0)
		  		$blogVisible = true;
		}else
			$blogVisible = false;
 	 	if (isset($_POST['blogPostPerPage']))
	  		$blogPostPerPage 	= stripslashes($_POST['blogPostPerPage']);	  	  	  	
  	  	
		/** 
		 * Check values
		 */
		 $validator = new Validator();
		 $validator->validateTextOnlyNoSpaces($blogID,"blogID is empty or contains blanks or invalid chars");
		 $validator->validateTextOnly($blogName,"blogName is empty or contains invalid chars");
		 $validator->validateNumber($blogPosition,"blogPosition is not numeric");
		 $validator->validateNumber($blogPostPerPage,"PostPerPage is not numeric");
		 if ($modify){
		 	if ($cms->exists($blogID))
			 	$validator->addError("BlogID must be unique, ".$blogID." already exists.");
		 }
		 // prints errors
		 if ($validator->foundErrors()){
		 	print("Errors found:<BR>");
		 	print("<font color=\"red\">");	 	
		 	print($validator->listDotErrors());
		 	print("</font>");
		 }else { 	
			 /**
			  * Init CMS system
		  	  */
			  if (!($cms->createBlog($blogID,$blogName,$blogPosition,$blogVisible,$blogPostPerPage))){
			  		print(ERROR_FILESYSTEM_PERMISSION);
			  		die();
			  }else{
			  	$blogID = "";
			  	$blogName = "";
			  	$blogPosition = "";
			  	$blogVisible = false;
			  	$blogPostPerPage = "99";
			  }			  	
		 }

		 
  	}
  }
?>	
<FORM action="<?=$_SERVER['PHP_SELF']?>" method="POST">
	<table cellspacing="0" cellpaddgin="0" border="0">
		<tr>
			<td><b>Blog ID</b></td>
			<td><input type="TEXT" name="blogID" size="10" value="<?=$blogID?>"></td>
		</tr>
		<tr>
			<td><b>Name</b>&nbsp;</td>
			<td><input type="TEXT" name="blogName" size="20" value="<?=$blogName?>"></td>
		</tr>
		<tr>
			<td><b>Position</b></td>
			<td>
				<SELECT name="blogPosition">
					<? for ($i=1;$i<16;$i++){
							if ($blogPosition==$i)
								$selected = 'selected';
							else
								$selected = '';
							print('<OPTION value='.$i.' '.$selected.'>'.$i.'</OPTION>');
						} // end for
					?>
				</SELECT>
			</td>
		</tr>
		<tr>
			<td><b>Posts per Page</b></td>
			<td>
				<SELECT name="blogPostPerPage">
					<? for ($i=1;$i<100;$i++){
							if ($blogPostPerPage==$i)
								$selected = 'selected';
							else
								$selected = '';
							print('<OPTION value='.$i.' '.$selected.'>'.$i.'</OPTION>');
						} // end for
					?>				
				</SELECT>			
			</td>
		</tr>		
		<tr>
			<td><b>Visible</b></td>
			<td><input type="CHECKBOX" name="blogVisible" <?if($blogVisible) echo('checked')?>></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>
				<input type="SUBMIT" value="update" size="1">
			</td>
		</tr>
	</table>
</FORM>
<?php
  
  
?>
<TABLE width="99%" cellpadding="4" cellspacing="1" border="0" background="grey">
	<TR bgcolor="#C7D3E0">
		<TD width="1%">&nbsp;</TD>
		<TD width="1%">&nbsp;</TD>
		<TD width="10%"><b>id<b></TD>
		<TD width="38%"><b>name<b></TD>
		<TD width="2%"><b>position<b></TD>
		<TD width="2%"><b>posts/page<b></TD>
		<TD width="2%"><b>visible</b></TD>
		<TD width="2%"><b>fs</b></TD>
		<TD width="40%" align="center"><b>note</b></TD>
	</TR>
<?php

	$blogs = $cms->getBlogsID();

	for($i=0;$i<$cms->getBlogsCount();$i++){
		$id = $blogs[$i];
		$name = $cms->getBlogFullName($id);
		$position = $cms->getBlogPosition($id);
		$isVisible = $cms->isBlogVisible($id);
		$blogPostPerPage = $cms->getPostPerPage($id); 		
		if ($isVisible)
			$visible='<img src="images/public.jpg" width="16" height="16" border="0">';
		else
			$visible='<img src="images/lock.jpg" width="16" height="16" border="0">';
		if (file_exists(BLOG_PATH.$id)){
			$fs = '<img src="images/folder.jpg" width="16" height="16" border="0">';
			$note = 'seems ok.';
		}else{
			$fs = '<img src="images/foldererror.jpg" width="16" height="16" border="0">';
			$note = '<font color="red">filesystem missing.</font>';
		}

?>
	<TR bgcolor="white">
		<TD><a href="<?=$_SERVER['PHP_SELF']."?delete=".$id?>"><img src="./images/delete.png" width="16px" height="16px" border="0" alt="delete"></a></TD>
		<TD><a href="<?=$_SERVER['PHP_SELF']."?modify=".$id?>"><img src="./images/edit.jpg" width="16px" height="16px" border="0" alt="edit"></a></TD>
		<TD><?=$id?></TD>
		<TD><?=$name?></TD>
		<TD align="right"><?=$position?></TD>
		<TD align="right"><?=$blogPostPerPage?></TD>
		<TD align="center"><?=$visible?></TD>
		<TD align="center"><?=$fs?></TD>
		<TD align="center"><?=$note?></TD>
	</TR>
<?php
	} // end for
?>
	<TR bgcolor="white">
		<TD colspan="9"><hr size="-1"></TD>
	</TR>
</TABLE>
