<?php
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//																										//
//	iStrip web comic publishing system																	//
//	buildindex - Strip index creation utility															//
//																										//
//	Copyright © 2002, 2003 Gordon McVey. Released under the GNU General Public License V2.0				//
//	http://www.gnu.org/licenses/gpl.html																//
//																										//
//////////////////////////////////////////////////////////////////////////////////////////////////////////

// This file is part of iStrip.
//
// iStrip 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.
//
// iStrip 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 iStrip; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

define ("VER_ISTRIP_ADMIN_CP_BUILDINDEX",'1.6.3');

include ('../include/istrip_ver.php');

if (VER_ISTRIP != VER_ISTRIP_ADMIN_CP_BUILDINDEX)
	die ('admin/buildindex.php: Version incompatibility');

include ('../include/istrip_constants.php');
include ('../include/istrip_lang.php');
include ('../include/istrip_lib.php');
include ('../include/lib_utils.php');

if (!@include ('../'.CFG_DIR_FILES.CFG_FILE_CONFIG))
	die (L_ISTRIP_ERR_CFG.L_BR);

// User auth
require ('../'.CFG_DIR_FILES.CFG_FILE_USERS);
require ('../include/istrip_auth.php');

function scanforstrips ($gf_stripdir)
// Scans the strip directory and returns an array of files that are valid for use with iStrip and their
// properties
{
	// Get directory listing for stripdir
	if ($gf_files = dirlist ($gf_stripdir))
	{
		sort ($gf_files);
		// Make sure filename is properly formatted and isn't a future date and that associated file is an image
		foreach ($gf_files as $gf_thisfile)
		{
			if ((preg_match (CFG_REGEX_FILENAME_STRIP,$gf_thisfile)) && ((strtotime ($gf_date = substr ($gf_thisfile,0,8))) !== -1) && ((int) $gf_date <= (int) date_today (CFG_DATE_IN)) && ($gf_image = @getimagesize ($gf_stripdir.$gf_thisfile)))
			{
				// Check that the computed date hasn't already been assigned
				$gf_date_used = false;
				if ($gf_valid)
					foreach ($gf_valid as $gf_thisrec)
						if ($gf_date == $gf_thisrec['strip_date'])
							$gf_date_used = true;
				if (!$gf_date_used)
				{
					// Add a new strip index entry
					$gf_valid [] = array 
					(
						'strip_id'		=> sizeof ($gf_valid) + 1,
						'strip_date'	=> $gf_date,
						'strip_date_s'	=> reformatdatestr (CFG_DATE_S,$gf_date),
						'strip_width'	=> $gf_image[IMG_WIDTH],
						'strip_height'	=> $gf_image[IMG_HEIGHT],
						'strip_file'	=> $gf_thisfile,
						'strip_url'		=> CFG_DIR_FILES.CFG_DIR_STRIP.$gf_thisfile
					);
				}
			}
		}
	}
	if (sizeof ($gf_valid) > 0)
		return ($gf_valid);
	else
		return (false);
}

function generatestripindex (&$gi_formdata,$gi_verbose)
// Create a stripindex array from form data
{
	if ($gi_formdata)
	{
		for ($gi_thisrec = 1; $gi_thisrec <= sizeof ($gi_formdata); $gi_thisrec++)
		{
			// Generate entry
			$gi_index[$gi_thisrec] = array
			(
				'strip_date'	=> $gi_formdata[$gi_thisrec]['date'],
				'strip_width'	=> $gi_formdata[$gi_thisrec]['width'],
				'strip_height'	=> $gi_formdata[$gi_thisrec]['height'],
				'strip_file'	=> $gi_formdata[$gi_thisrec]['file'],
				'news_file'		=> '',
				'strip_title'	=> strip_tags (stripslashes (trim ($gi_formdata[$gi_thisrec]['title_strip']))),
				'news_title'	=> ''
			);
			if ($gi_verbose)
			{
				// Generate stats for var subbing
				$gi_stats ['strip_id']		= $gi_thisrec;
				$gi_stats ['strip_width']	= $gi_index[$gi_thisrec]['strip_width'];
				$gi_stats ['strip_height']	= $gi_index[$gi_thisrec]['strip_height'];
				$gi_stats ['strip_file']	= $gi_index[$gi_thisrec]['strip_file'];
				$gi_stats ['strip_title']	= $gi_index[$gi_thisrec]['strip_title'];
				// Output status report
				echo (L_OK.varsubstring (L_BUILDINDEX_GENREC.L_BR.L_OK.L_STRIP_INFO,$gi_stats).L_BR);
				if ($gi_stats ['strip_title'] != '')
					echo (L_OK.varsubstring (L_STRIP_SET_TITLE_OK,$gi_stats).L_BR);
				else
					echo (L_OK.varsubstring (L_STRIP_SET_TITLE_NOOP,$gi_stats).L_BR);
			}
		}
		return ($gi_index);
	}
	else
		return (false);

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Strip index builder</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="smarty_templates/stylesheets/admin.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
if (sizeof ($_POST['strip']) == 0)
// Data gathering stage
{
	if ($temp = loadindexfile ('../'.CFG_DIR_FILES.CFG_FILE_INDEX))
	{
		?>
		<table width="100%"  border="0" cellspacing="2" cellpadding="2">
		<tr>
		<th class="warn">Stripindex file detected</th>
		</tr>
		<tr>
		<td class="warncolour2">A pre-existing .stripindex file was detected
          by this script. This indicates that you have already uploaded strips
          via the iStrip system or have already run this script at least once.
		  If you continue then the pre-existing strip index will be overwritten
		  by the one created by this script. This will cause all previously set
		  strip titles to be reset. In addition, any news items that you have
		  created will become orphaned from the strip index. It is recommended
		  that you do not proceed in using this script.</td>
		</tr>
		</table>
		<br />
		<?php
		unset ($temp);
	}
    if ($strips = scanforstrips ('../'.CFG_DIR_FILES.CFG_DIR_STRIP))
	{
		?>
		<table width="100%"  border="0" cellspacing="2" cellpadding="2">
		<tr>
		<th>Strip index builder </th>
		</tr>
		<tr>
		<td class="colour2">This script scans the strip directory for image files
		  with filenames in the appropriate format. Any that it finds are listed
		  below and will be compiled into a new strip index file when the form is
		  submitted. If you wish, you can specify titles for the images that were
		  found in the fields provided. </td>
		</tr>
		</table>
		<br />
		<form name="striplist" id="striplist" action="buildindex.php" method="post">
		<table width="100%" cellpadding="2" cellspacing="2" border="0">
		<tr><th width="40"> ID</th>
		<th width="80">Date</th>
		<th>Title</th><th width="100">View</th>
		</tr>
		<?php
		for ($count = 0; $count < sizeof ($strips); $count++)
		{
			echo (varsubstring (CFG_HTML_TR_BUILDINDEX,$strips[$count]));
		}
		?>
		<tr align="center">
		  <td colspan="4" class="colour2">Generate verbose status report 
		    <input name="verbose" type="checkbox" id="verbose" value="true" checked="checked" />
		    &nbsp;&nbsp;
	      <input type="submit" value="Generate strip index" /></td>
		</tr>
		</table>
		</form>
		<?php
	}
	else
	{
		?>
		<table width="100%"  border="0" cellspacing="2" cellpadding="2">
		<tr>
		<th scope="col">No valid strips </th>
		</tr>
		<tr>
		<td class="colour2">No valid strip files could be found in the strip directory.
		  Make sure you have uploaded your strips to the correct location and
		  that they are named properly. For more details, consult the documentation. If you are not running this script from the admin/ subdirectory of your iStrip installation, move it there and try again. </td>
		</tr>
		</table>
		<?php
	}
}
else
// Data processing stage
{
	?>
	<table width="100%"  border="0" cellspacing="2" cellpadding="2">
	<tr>
	<th>Generate strip index</th>
	</tr>
	<tr>
	<td class="colour2">
	<?php
	// Generate strip index data
	if ($stripindex = generatestripindex ($_POST['strip'],$_POST['verbose']))
	{
		// Write strip index to disk
		$filestats ['file_name'] = CFG_DIR_FILES.CFG_FILE_INDEX;
		if ($filestats ['bytes_written'] = writeindexfile ($stripindex,'../'.CFG_DIR_FILES.CFG_FILE_INDEX))
		{
			$filestats ['number_of_strips'] = sizeof ($stripindex);
			$filestats ['bytes_written'] = number_format ($filestats ['bytes_written']);
			echo (L_OK.varsubstring (L_INDEX_COUNT.L_BR.L_OK.L_FILE_WRITE,$filestats).L_BR);
		}
		else
		{
			echo (L_ERR.varsubstring (L_FILE_WRITE_ERR,$filestats).L_BR);
			$_POST ['strip_del_confirm'] = true;
		}
	}
	?>
	<hr /> 
	The strip index generation is now complete. If no errors were reported above
	then you should check the integrity of the generated strip index. First look
	at your <a href="index.php">admin index page</a> to ensure all the strip dates and titles are correct.
	You should also check that the strip images are correct as well. If this check
	reveals no problems then you should next check that there are no orphaned files
	in the strip and news directories. To do this, run the <a href="checkfs.php">filesystem
	check</a> from
	the admin index page. If this check reveals no problems then you should delete
	this script from your server as it is no longer needed. If, however, you find
	a problem then you may need to run this script again after taking the appropriate
	corrective action. See the iStrip documentation for more details on troubleshooting
	this script for more details. </td>
	</tr>
	</table>
	<?php
}
?>
<p align="center">[ <a href="index.php">admin</a> ] [ <a href="../index.php">home</a> ]</p>
<p align="center">
  <?php
echo (L_COPYRIGHT);
?>
</p>
</body>
</html>
