Location: PHPKode > projects > PHPFreeNews > phpfn/Inc/Images.php
<?php

/*	+--------------------------------------------------------------
	| PHPFreeNews - News Headlines on your website                |
	| Developed by Jim Willsher.                                  |
	| http://www.phpfreenews.co.uk                                |
	+-------------------------------------------------------------+
*/

if (! defined('IN_PHPFN'))
	die('Illegal attempt to access script directly!');

function DisplayData($ID, $ImageName, $ImageFilename)
{
	global $ErrorText, $AdminScript, $MaxImageFileSize, $NewsDir, $ImageDir;

	DisplayGroupHeading(  ($ID != -1 ? 'Rename' : 'Upload' ) . ' Image');
	?>
	<table class="Admin">
		<?php
		if ($ID != -1)
		{
			?>
			<form name="ImageMaint" action="<?=$AdminScript?>?action=ImageList" method="post">
			<?php
		}
		else
		{
			?>
			<form name="ImageMaint" enctype="multipart/form-data" action="<?=$AdminScript?>?action=ImageList" method="post">
				<input type="hidden" name="MAX_FILE_SIZE" value="<?=$MaxImageFileSize ?>" />
			<?php
		}
		?>
	   		<input type="hidden" name="id" value="<?= $ID ?>" />
	   		<input type="hidden" name="ImageFilename" value="<?= $ImageFilename ?>" />

			<?php
			if ($ErrorText != '')
			{
				?>
				<tr>
					<td colspan="3" class="ErrorText">
						<?= $ErrorText ?>
					</td>
				</tr>
				<?php
			}
			?>

			<tr>
		 		<td rowspan="4" width="10%">
			 		<img src="<?= ($ID != -1 ? $NewsDir . $ImageDir . "/" . $ImageFilename : 'Inc/Images/Images.gif') ?>">
		 		</td>
			</tr>
			<tr>
				<td class="FieldPrompt">
					Image Common Name:
				</td>
				<td align="left">
					<input type="text" name="ImageName" value="<?= $ImageName ?>" size="20" maxlength="255" />
				</td>
			</tr>

			<?php
			if ($ID == -1)
			{
				?>
				<tr>
					<td class="FieldPrompt">
						File Path:
					</td>
					<td>
						<input name="ImageFile" size="30" type="file" />
					</td>
				</tr>
				<?php
			}
			?>
					
  			<tr>
  				<td colspan="3">
  					<hr width="100%" size="2">
				</td>
			</tr>
			<tr>
				<td colspan="3" class="C">
					<input class="but" type="reset" name="submit" value="Reset" />
					<input class="but" type="submit" name="submit" value="Save Changes" />
				</td>
			</tr>
		</form>
	</table>
	<script language="javascript" type="text/javascript">
		ImageMaint.ImageName.focus();
	</script>
	<?php
}


$Action = isset($_GET['action']) ? $_GET['action'] : '';
$Mode = isset($_GET['mode']) ? $_GET['mode'] : '';
$Confirm = isset($_GET['confirm']) ? $_GET['confirm'] : '';
$GetId = isset($_GET['ID']) ? $_GET['ID'] : '';
$ReturnText = ' Click <A href="' . $AdminScript . '?action=ImageList">here</A> to return to image maintenance';

// Attempt to delete an image
if ($Action == 'ImageList' AND $Mode == 'delete' AND $Confirm == 'yes')
{
	$result = mysql_query("SELECT * FROM news_images WHERE ID='$GetId'");
	$row = mysql_fetch_array($result);
	$ImageName = $row['ImageName'];
	$ImageFileName = $row['ImageFilename'];

	// First, remove the image from the database
	$result = mysql_query("DELETE FROM news_images WHERE ID=$GetId");
	if ($result)
	{
		// Write audit, if required
		if ($EnableAudit == 1)
			WriteAuditEvent(AUDIT_TYPE_IMAGE, 'D', $GetId, "Image deleted: " .$ImageName);

		$updatenews = mysql_query("UPDATE news_posts SET ImageID = '' WHERE ImageID = $GetId");
		if ($updatenews)
		{
			// Now remove the file from the server
			$filelink =  $_SERVER['DOCUMENT_ROOT'] . $NewsDir . $ImageDir . "/" . $ImageFileName;
			$delete = @unlink($filelink);
			if ($delete)
			{
				$_SESSION['Info'] = 'The image file has been removed from the server, and the database has been updated.';
				header('location:' . $AdminScript . '?action=ImageList');
				exit;
			}
			else
			{
				$_SESSION['Info'] = 'There was a problem removing the image file from the system.';
				header('location:' . $AdminScript . '?action=ImageList');
				exit;
			}
		}
		else
		{
			$_SESSION['Info'] = 'There was an error whilst detaching the image from news articles.';
			header('location:' . $AdminScript . '?action=ImageList');
			exit;
		}
	}
	else
	{
		$_SESSION['Info'] = 'There was an error whilst removing the image from the Images table.';
		header('location:' . $AdminScript . '?action=ImageList');
		exit;
	}
}

// Request confirmation for the delete
elseif ($Action == 'ImageList' AND $Mode == 'delete' AND $Confirm == '')
{
	$results = mysql_query("SELECT * FROM news_images WHERE ID='$GetId'");
	$result = mysql_fetch_array($results);

	DisplayGroupHeading('Delete Image');
	?>
	<table class="Admin">
		<tr>
			<td width="100">
				<center><img src="<?=$NewsDir.$ImageDir?>/<?=$result['ImageFilename'] ?>"></center>
			</td>
			<td>
				<center>You have requested to delete the following image.<br />
				Are you sure you want to remove this image?<br /><br />
				<a href="<?=$AdminScript?>?action=ImageList&amp;mode=delete&amp;confirm=yes&amp;ID=<?= $result['ID'] ?>">Yes</a> | 
				<a href="<?=$AdminScript?>?action=ImageList">No</a>
			</td>
		</tr>
	</table>
	<?php
}

// Rename an image?
elseif ($Action == 'ImageList' AND $Mode == 'rename')
{
	$resultrow = mysql_query("SELECT * FROM news_images WHERE ID='$GetId'"); 
	$result = mysql_fetch_array($resultrow);

	// Display the image information in the form for editing
	$ImageName = $result['ImageName'];
	$ImageFilename = $result['ImageFilename'];
	DisplayData($GetId, $ImageName, $ImageFilename);
}

// Upload an image
elseif ($Action == 'ImageList' AND $Mode == 'upload')
{
	DisplayData(-1, '', '');
}

elseif (isset($_POST['submit']))
{
	$ID = $_POST['id'];
	$ImageName = $_POST['ImageName'];
	$ImageFilename = $_POST['ImageFilename'];

	// Verify that all fields have been completed
	if ($ImageName == '')
	{
		$ErrorText = 'You must enter an image description.';
		DisplayData($ID, $ImageName, $ImageFilename);
	}
	else
	{
		// If the ID is <> -1 then this is just a RENAME only
		if ($ID != -1)
		{
			$sql = "UPDATE news_images SET ImageName='$ImageName' WHERE ID='$ID'";
			if (mysql_query($sql))
			{
				// Write audit, if required
				if ($EnableAudit == 1)
					WriteAuditEvent(AUDIT_TYPE_IMAGE, 'C', $ID, "Image renamed: " . $ImageName);

				$_SESSION['Info'] = "The image's 'common name' has been updated.";
				header('location:' . $AdminScript . '?action=ImageList');
				exit;
			}
			else
			{
				$errormsg = 'An error was found trying to update the image name.';
				DisplayError($errormsg, 1);
			}
		}
		else			// This must be an upload of a new image
		{
			// First we check to see if the file already exists on the server by checking it against the database
			$ImageFilename = $_FILES['ImageFile']['name'];

			$filethere = mysql_query("SELECT * FROM news_images WHERE ImageFilename = '$ImageFilename'");
	       	$filecount = mysql_num_rows($filethere);
			if ($filecount > 0)
			{
				$ErrorText = 'A file already exists with this file name.  Change the file name and try uploading it again.';
				DisplayData($ID, $ImageName, $ImageFilename);
			}
			elseif (move_uploaded_file($_FILES['ImageFile']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . $NewsDir . $ImageDir . "/" . $_FILES['ImageFile']['name']))
			{
				// It worked so lets move the data into the database now...
				$ImageFilename = $_FILES['ImageFile']['name'];
				$sql = "INSERT INTO news_images SET ImageName='$ImageName', ImageFilename='$ImageFilename'";
	            if (mysql_query($sql))
    	        {
					// Write audit, if required
					if ($EnableAudit == 1)
						WriteAuditEvent(AUDIT_TYPE_IMAGE, 'A', mysql_insert_id(), "Image created: " . $ImageName);

					$_SESSION['Info'] = 'Image successfully uploaded and added to database.';
					header('location:' . $AdminScript . '?action=ImageList');
					exit;
				}
				else
				{
					$errormsg = 'There was an error adding the new image.';
					DisplayError($errormsg, 1);
				}
    	    }
        	else
	        {
				// The upload failed.
				$errormsg = 'An error occured while trying to upload the image to the server.<br />Make sure the correct image directory is specified in the config<br />file and that this directory has been CHMOD to 777.';
				DisplayError($errormsg, 0);
			}
		}
	}
}

elseif ($Action == 'ImageList')
{
	DisplayGroupHeading('News Images');
	?>
	<table class="Admin" >
		<tr>
			<td width="110">
				<div align="center">
					<a href="<?=$AdminScript?>?action=ImageList&amp;mode=upload"><img src="Inc/Images/Images.gif" border="0" alt="Images"><br />Upload Images</a>
				</div>
			</td>
			<td>
				Each post can have one image associated with it. It's a good idea to keep these images fairly small in size, otherwise they will become too dominant on the page. Think of them as a "logo" style of image. 
			</td>
		</tr>
	</table>
	<br />
	<br />

	<?php
	DisplayGroupHeading('Image Maintenance');
	DisplayInfoMessage();

	// Grab all the current images...
	$ResultSet = mysql_query("SELECT * FROM news_images ORDER BY ImageName ASC");
	$NumRecords = mysql_num_rows($ResultSet);

	// If there are any images in the database, display them
	if ($NumRecords > 0)
	{
		$buildpass=0;
		?>
		<table class="Admin">
			<tr>
				<?php
				// Build the table, 3 images per row
				while ($Row = mysql_fetch_array($ResultSet))
				{
					$buildpass++;
					?>
					<td class="ImageListing">
						<img src="<?=$NewsDir.$ImageDir?>/<?=$Row['ImageFilename'] ?>" border="0" alt="<?=$Row['ImageName'] ?>">
						<br /><strong><?= $Row['ImageName'] ?></strong>
						<br />
						<?php echo $Row['ImageFilename']; ?><br />
						<a href="<?=$AdminScript?>?action=ImageList&amp;mode=rename&amp;ID=<?= $Row['ID'] ?>">Rename</a> | 
						<a href="<?=$AdminScript?>?action=ImageList&amp;mode=delete&amp;ID=<?= $Row['ID'] ?>">Delete</a>
					</td>
					<?php
					// Time to start a new row?
					if ($buildpass == 3)
					{
						$buildpass = 0;
						echo ('</tr><tr>');
					}
				}
				?>
			</tr>
		</table>
		<?php
	}
}
?>
Return current item: PHPFreeNews