Location: PHPKode > projects > Standardized Test Practice Engine > stpe/admin/editimgsubmit.php
<?php
/*
Copyright 2004 by Jonathan Bell and Daniel Perelman
This file is part of STPE - the Standardized Test Practice Engine.

    STPE 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.

    STPE 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 Foobar; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
include_once("../globals.inc.php");
?>
<?php
include("login.inc.php");

$wantedtopic = (int) $_POST['top'];
$wantedsection = (int) $_POST['sub'];

require_once("../dbinit.inc.php");

function done()
{
	global $wantedtopic, $wantedsection;
	header("Location: editimg.php?top=$wantedtopic&sub=$wantedsection");
	exit;
}

//print_r($_POST);
$mime_types = array("image/gif" => "gif", "image/jpeg" => "jpg",
        "image/png" => "png", "image/x-png" => "png", 
        "image/bmp" => "bmp", "image/tiff" => "tiff");
if(isset($_POST['uploadnewsubmit']))
{
	$newimgnum = $_POST['highimgnum'] + 1;
	move_uploaded_file($_FILES['uploadnew']['tmp_name'],
		"../reviewpix/$wantedtopic-$wantedsection-$newimgnum." .
        $mime_types[mime_content_type($_FILES['uploadnew']['tmp_name'])]);
	done();
}
foreach($_POST as $key => $value)
{
	if($value == "Upload Replacement Image")
	{
		//TODO upload replacement image (done?)
		$imgnum = substr($key, 12);
		//echo("WARNING: Upload Replacement Image #$imgnum not yet implemented.");
		if(move_uploaded_file($_FILES["upload$imgnum"]['tmp_name'],
			"../reviewpix/$wantedtopic-$wantedsection-$imgnum." .
        	($imgtype = $mime_types[mime_content_type(
        		$_FILES["upload$imgnum"]['tmp_name'])]))
           !== false)
        {
        	// Check if extension is the same as the old file.
        	// If it is different, we need to delete the old
        	// file and change all references.
        	if(substr(strrchr($_POST["filename$imgnum"], '.'), 1) != $imgtype)
        	{
				unlink($_POST["filename$imgnum"]);
				//TODO change references to image
				$reviewres = mysql_query("SELECT id, review FROM reviews " . 
					"WHERE topic_id = $wantedtopic AND section_id = $wantedsection;")
					 OR die(mysql_error());
				function fix_img($str)
				{
					global $wantedtopic, $wantedsection, $imgnum, $imgtype;
					return str_replace(
						"reviewpix/$wantedtopic-$wantedsection-$imgnum" . 
							strrchr($_POST["filename$imgnum"], '.'), 
						"reviewpix/$wantedtopic-$wantedsection-$imgnum." .
							$imgtype,
						$str);					
				}
				while(($reviewrow = mysql_fetch_assoc($reviewres)) !== false)
				{
					$rev = fix_img($reviewrow['review']);
					mysql_query("UPDATE reviews SET review = '$rev' WHERE id = {$reviewrow['id']};")
						OR die(mysql_error());
				}
				$questionres = mysql_query("SELECT id, question, answers FROM questions " . 
					"WHERE topic_id = $wantedtopic AND section_id = $wantedsection;")
					 OR die(mysql_error());
				while(($questionrow = mysql_fetch_assoc($questionres)) !== false)
				{
					$ques = fix_img($questionrow['question']);
					$answ = fix_img($questionrow['answers']);
					mysql_query("UPDATE questions SET question = '$ques', answers = '$answ' " . 
						"WHERE id = {$questionrow['id']};") OR die(mysql_error());
				}
			}
        }
		done();
	}
	else if($value == "Delete This Image")
	{
		//TODO delete image (done? permissions problem)
		$imgnum = substr($key, 6);
		//echo("WARNING: Delete not yet implemented.");
		unlink($_POST["filename$imgnum"]);
		done();
	}
}
?>
Return current item: Standardized Test Practice Engine