<?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/Errors.inc.php");
  require_once(dirname(__FILE__)."/../../cms/Constants.inc.php");
  require_once(dirname(__FILE__)."/../../cms/Auth.class.php");
  require_once(dirname(__FILE__)."/../../cms/Theme.class.php");
  require_once(dirname(__FILE__)."/../../cms/themeConstants.inc.php");
  require_once(dirname(__FILE__)."/../../common/File.class.php");
  require_once(dirname(__FILE__)."/../../common/utils.func.php");
  require_once(dirname(__FILE__)."/../../common/Ini.class.php");
  
    // Check for authorizations
  $auth = new Auth();
  if (!$auth->isAuth()){
  	header("Location: login.php");
  	die();
  }
  // ------ end auth's check
  	
  $maxFileSize = 0; // Unlimited	  
  unset($fileID);

  /**
   * Delete action
   */
  if (isset($_GET['delete'])){
  	$id = $_GET['delete'];
  	
  	$fn = THEME_DIR.$id;
  	if (file_exists($fn)){
  		/*
  		 * This forces to run under unix/linux enviroment, this is not a big deal,
  		 * but rm_dir() does not work here and I have no time to check it out.
  		 */
  		shell_exec("rm -fr '".$fn."'");
  		//print('DELETE '.$fn);
  	}  	
  }

  /**
   * Install
   */
  if (isset($_GET['install'])){
  	$id = $_GET['install'];
  	$fn = THEME_DIR.$id;
  	if (file_exists($fn)){
		//print('INSTALL '.$id);
		// CALL here the Theme.class.php and do the right thing! ;)
		$t = new Theme($id);			  	
		if ($t->install($fn)){
			// ok
			printf('<font color="green"><b>Theme '.$id.' INSTALLED.</b></font>');
		}else{
			// ko	
			printf('<font color="red">Cannot find '.$fn.'</font>');
		}
		
  	}
  }

  if (trim($_FILES['fileID']['name'])!=''){	
  	$uploaddir = THEME_DIR.'/';
  	$uploadfile = $uploaddir . basename($_FILES['fileID']['name']);
	echo '<pre>';
  	if (move_uploaded_file($_FILES['fileID']['tmp_name'], $uploadfile)) {  
     		shell_exec("cd '".$uploaddir."'; tar xzf '".$uploadfile."'");
     		shell_exec("rm -f '".$uploadfile."'");
     		print(UPLOAD_SUCCESS);
  	} else {
    	print('<font color="red">'.UPLOAD_FAILED.'</font>');
  	}
  	print "</pre>";
  }
?>	
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST" enctype="multipart/form-data">
	<input type="hidden" name="MAX_FILE_SIZE" value="<?=$maxFileSize?>">
	File&nbsp;<input type="file" name="fileID">
	<input type="submit" value="upload">&nbsp;&nbsp;&nbsp;
	max upload filesize is set to:&nbsp;<?=ini_get('upload_max_filesize')?>
</form> 

<hr size=1>

<TABLE width="99%" cellpadding="4" cellspacing="1" border="0" background="grey">
	<TR bgcolor="#C7D3E0">
		<TD width="1%">&nbsp;</TD>
		<TD width="15%"><b>id<b></TD>
		<TD width="30%"><b>description<b></TD>		
		<TD width="20%"><b>author<b></TD>
		<TD width="5%"><b>version<b></TD>
		<TD width="10%"><b>preview<b></TD>
		<TD width="5%"><b>status<b></TD>
		<TD width="1%"><b>install<b></TD>
	</TR>
<?php
	$file = new File();
	$dir = THEME_DIR;
	$files=$file->scandir($dir,1);
	foreach($files as $key=>$val) {
		if ($val[0]!=".") {
			// theme dir, that is also a theme ID
			$theme_dir 		= $val;	
			// index file
			$theme_index 	= $dir.$theme_dir.'/'.'template.php';
			// theme INI
			$theme_ini 		= $dir.$theme_dir.'/'.$theme_dir.'.ini';
			// theme screenshot
			$theme_shot		= '../themes/'.$theme_dir.'/'.$theme_dir.'.png';
			// theme CSS
			$theme_css		= $dir.$theme_dir.'/'.$theme_dir.'.css';

			// check if ok
			$ok = true;
			if (!file_exists($theme_index))
				$ok = $ok&&false;
			if (!file_exists($theme_ini))
				$ok = $ok&&false;
			if (!file_exists($theme_css))
				$ok = $ok&&false;
			// ---------
			
			// read the INI file
			if (file_exists($theme_ini)){
				$ini = new Ini($theme_ini);
				// Description
				$theme_desc = $ini->getValue('Description');
				// Author
				$theme_author = $ini->getValue('Author');
				// Email
				$theme_email = $ini->getValue('Email');
				// Web
				$theme_web = $ini->getValue('Web');
				// Version
				$theme_version = $ini->getValue('Version');
			}else{
				$theme_desc = '';
				$theme_author = '';
				$theme_email = '';
				$theme_web = '';
				$theme_version = '';
			}
			
?>
	<TR bgcolor="white">
		<TD><a href="<?=$_SERVER['PHP_SELF']."?delete=".$theme_dir?>"><img src="./images/delete.png" width="16px" height="16px" border="0" alt="delete"></a></TD>
		<TD><?=$theme_dir?></TD>
		<TD><font size=1><?=$theme_desc?></font></TD>
		<TD><font size=1><?=$theme_author.'<br>'.$theme_email.'<br>'.$theme_web?></font></TD>
		<TD><?=$theme_version?></TD>
		<TD><a href="<?=$theme_shot?>" target="_new"><img src="<?=$theme_shot?>" border="1" width="120px" alt="Theme Screenshot"></a></TD>
		<TD>
			<?
				if ($ok) 
					print('ok!');
				else
					print('<font color="red">not oK!</font>'); 
			?>
		</TD>
		<TD>
			<?php 
				if ($ok)
					print('<a href="'.$_SERVER['PHP_SELF'].'?install='.$theme_dir.'"><b>install</b></a>');
				else 
			    	print('n/a'); 
			?>
		</TD>		
	</TR>
<?php
	}
} // end for
?>
	<TR bgcolor="white">
		<TD colspan="9"><hr size="-1"></TD>
	</TR>
</TABLE>