<?php
/*************************
  Coppermine Photo Gallery
  ************************
  Copyright (c) 2003-2005 Coppermine Dev Team
  v1.1 originaly written by Gregory DEMAR

  This program 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.
**********************************************/

if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');

$thisplugin->add_action('page_start','replacer');
$thisplugin->add_filter('file_data','menu');

function menu($data)
{
	global $CONFIG, $CURRENT_ALBUM_DATA;
	
	if ((USER_ADMIN_MODE && $CURRENT_ALBUM_DATA['category'] == FIRST_USER_CAT + USER_ID) || ($CONFIG['users_can_edit_pics'] && $data['owner_id'] == USER_ID && USER_ID != 0) || GALLERY_ADMIN_MODE) {
		$data['menu'] .= ' <a href="?replacer_pid='.$data['pid'].'"  class="admin_menu">Replace file</a>';
	}
	return $data;
}

function replacer()
{
	global $CONFIG;
	
	if (isset($_GET['replacer_pid'])){
		
		$pid = (int) $_GET['replacer_pid'];
			
		$result = cpg_db_query("SELECT * FROM {$CONFIG['TABLE_PICTURES']} AS p INNER JOIN {$CONFIG['TABLE_ALBUMS']} AS a ON a.aid = p.aid WHERE p.pid = '$pid' LIMIT 1");
			
		$row = mysql_fetch_assoc($result);

		if (!((USER_ADMIN_MODE && $row['category'] == FIRST_USER_CAT + USER_ID) || ($CONFIG['users_can_edit_pics'] && $row['owner_id'] == USER_ID && USER_ID != 0) || GALLERY_ADMIN_MODE)) die('Access denied');

		if (count($_FILES) && $row){
	
			$image = $CONFIG['fullpath'] . $row['filepath'] . $row['filename'];
			$normal = $CONFIG['fullpath'] . $row['filepath'] . $CONFIG['normal_pfx'] . $row['filename'];
			$thumb = $CONFIG['fullpath'] . $row['filepath'] . $CONFIG['thumb_pfx'] . $row['filename'];
				
			if ($_FILES['fileupload']['error']) die('Upload errror ' . $_FILES['fileupload']['error']);

			move_uploaded_file($_FILES['fileupload']['tmp_name'], $image);
				
			require('include/picmgmt.inc.php');
			
			if (is_known_filetype($image)){
				
				if (is_image($image)){
				
					resize_image($image, $thumb, $CONFIG['thumb_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use']);
					resize_image($image, $normal, $CONFIG['picture_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use']);
					list($width, $height) = getimagesize($image);
					
				} else {
					$width = 0;
					$height = 0;
				}
				
				$image_filesize = filesize($image);
				$total_filesize = $image_filesize + (file_exists($normal) ? filesize($normal) : 0) + filesize($thumb);
	
				cpg_db_query("UPDATE {$CONFIG['TABLE_PICTURES']} SET filesize = '$image_filesize', total_filesize = '$total_filesize', pwidth = '$width', pheight = '$height' WHERE pid = '$pid' LIMIT 1");
	
				$CONFIG['site_url'] = rtrim($CONFIG['site_url'], '/');
			} else {
				@unlink($image);
			}
			header("Location: {$CONFIG['site_url']}/displayimage.php?pos=-$pid");
			die();
			
			
		} else {
	
			load_template();
			pageheader('File replacer');
			echo '<form method="post" enctype="multipart/form-data">';
			starttable('60%', 'Upload replacement file', 2);
		
			echo <<< EOT
<tr>
	<td class="tableb" valign="top">
		Browse: 
	</td>
	<td class="tableb" valign="top">
		<input type="file" name="fileupload" size="40" class="listbox" />
	</td>
</tr>
<tr>
	<td align="center" colspan="2" class="tablef">
		<input type="submit" name="commit" class="button" value="Upload"/>
	</td>
</tr>

EOT;
			endtable();
			echo '</form>';
			pagefooter();
			exit;
		}
	}
}



?>