<?php

/* * * * * * * * * * * * * * * * * * * * * * * * * 

This script combines a phpMyEdit v5.4 form with an Image Upload form.

Uploaded to platon.sk on March 23, 2004.
by Doug Hockinson
http://hockinson.com
http://phpvs.com

You may want to rename the image table defined below as vs_images1.
You may want to edit the HTML DOCTYPE and META tags appearing below.
Configuration ends around line 133 below.

--------------------------------------- 

# Database schema for new image table:

CREATE TABLE `vs_images1` (
  `id` int(11) NOT NULL auto_increment,
  `image_caption` varchar(255) NOT NULL default '',
  `image` longblob NOT NULL,
  `image_width` smallint(6) NOT NULL default '0',
  `image_height` smallint(6) NOT NULL default '0',
  `image_type1` tinyint(2) NOT NULL default '0',
  `image_type2` varchar(20) NOT NULL default '',
  `img_attrib` varchar(30) NOT NULL default '',
  `hidden` tinyint(1) NOT NULL default '0',
  `updated` timestamp(14) NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id`(`id`)
) TYPE=MyISAM;

---------------------------------------

# Schema to retrofit images into existing `vs_images1` 
# This assumes the `id` field already exists.

# ALTER TABLE `vs_images1` ADD `image_caption` varchar(255) NOT NULL default ''; 
# ALTER TABLE `vs_images1` ADD `image` longblob NOT NULL; 
# ALTER TABLE `vs_images1` ADD `image_width` smallint(6) NOT NULL default '0'; 
# ALTER TABLE `vs_images1` ADD `image_height` smallint(6) NOT NULL default '0'; 
# ALTER TABLE `vs_images1` ADD `image_type1` tinyint(2) NOT NULL default '0'; 
# ALTER TABLE `vs_images1` ADD `image_type2` varchar(20) NOT NULL default ''; 
# ALTER TABLE `vs_images1` ADD `img_attrib` varchar(30) NOT NULL default ''; 
# ALTER TABLE `vs_images1` ADD `hidden` tinyint(1) NOT NULL default '0'; 
# ALTER TABLE `vs_images1` ADD `updated` timestamp(14) NULL; 

--------------------------------------- 

Copy/paste the following into a new file: getimage.php

<?php
// This part is based on getimage.php - by Florian Dittmer <dittmer@gmx.net>
// Implemenation: pass image id and tb (table) using tags displayed in phpMyEdit form.
// Usage outside this phpMyEdit form: <img src="getimage.php?id=21&tb=vs_images1">
$id         = array_key_exists('id',$_GET) ? $_GET['id'] : '';
$opts['tb'] = array_key_exists('tb',$_GET) ? $_GET['tb'] : '';
if(is_numeric($id) && !empty($opts['tb'])){
	$opts['hn'] = $_SERVER['DOCUMENT_ROOT'] == '/apache/htdocs' ? '127.0.0.1' : 'localhost';
	$opts['db'] = 'domain_com';
	$opts['un'] = 'username';
	$opts['pw'] = 'passwrd';
	@mysql_pconnect($opts['hn'],$opts['un'],$opts['pw']) or die ('Unable to connect');
	@mysql_select_db($opts['db']) or die('Database not selected');
	$res  = @mysql_query('SELECT image, image_type2 FROM '.$opts['tb'].' WHERE id = '.$id);
	$data = @mysql_result($res,0,'image');
	$type = @mysql_result($res,0,'image_type2');
	Header("Content-type: $type");
	echo $data;
}
?>

* * * * * * * * * * * * * * * * * * * * * * * * */

ob_start('ob_gzhandler'); 


### Begin configuration ###

$opts['tb'] = 'vs_images1';
$opts['hn'] = $_SERVER['DOCUMENT_ROOT'] == '/apache/htdocs' ? '127.0.0.1' : 'localhost';
$opts['db'] = 'domain_com';
$opts['un'] = 'username';
$opts['pw'] = 'passwrd';

// You probably do not need this switch():

switch($_SERVER["DOCUMENT_ROOT"])
{
   Case '/apache/htdocs':
      error_reporting(E_ALL & ~E_NOTICE);
      break;
   default:
      error_reporting(0);
      break;
};

// $opts['logtable']   = 'vs_change_log';

/**
 * To display "thumbs" instead of full-size images 
 * in List mode, set: $resize_images = 1;
 */

$resize_images =  0;
$resize_height = 32; // max. height for "thumbs" (pixels)

/**
 * Prevent users from uploading images having a
 * width greater than $max_image_width pixels, or
 * height greater than $max_image_height pixels.
 */

$max_image_width  = 640;

$max_image_height = 480;

/**
 * $form_title in H3 tag below the phpMyEdit form:
 */

$form_title = 'upload images';

/**
 * Path/filename of stylesheet
 */

$stylesheet        = 'style/blue.php';
$cfg['bdr_bottom'] = 1; // See /style/blue.php

### End configuration ###


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-language" content="en-US">
<meta http-equiv="content-style-type" content="text/css">
<meta http-equiv="content-type" content="text/html; charset=us-ascii">
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Pragma" content="no-cache">
<meta name="expires" content="Fri, 01 Jan 1990 00:00:00 GMT">
<?php 

/**
 * Style names ending in -right-align relate to forms created 
 * using Multi Form Generator at http://www.phpvs.com/mfg/
 * Styles specific to phpMyEdit begin with: pme-
 * Minimal style is applied below (font size, a border, etc.)
 *
 * If /style/blue.php does not meet your needs, the following 
 * style should be a starting point for customization.
 * Remove references to text-align:right; for Netscape 4 compatibility.
 */

$cfg['size'] = 'x-small';

?>
<style type="text/css">
<!--
body{background-color:#ffffff; color:#000000; font-family:verdana,helvetica,arial,sans-serif; font-size:<?php echo $cfg['size']; ?>; font-weight:normal; }
a, a:active, a:focus, a:link, a:visited{ background-color:transparent; color:#000000; }
a:hover{ background-color:transparent; color:#ff0000; }
blockquote, caption, code, div, li, ol, p, pre, span, ul{ font-family:verdana,helvetica,arial,sans-serif; font-size:<?php echo $cfg['size']; ?>; font-weight:normal; }
i{ font-family:verdana,helvetica,arial,sans-serif; font-size:<?php echo $cfg['size']; ?>; font-weight:normal; font-style:oblique; }
h1, h2, h3, h4, h5, h6{ font-family:arial,verdana,helvetica,sans-serif; }
/* Form tags */
fieldset, form, input, label, legend, option, select, textarea{ font-family:verdana,helvetica,arial,sans-serif; font-size:<?php echo $cfg['size']; ?>; font-weight:normal; }
b{ font-family:verdana,helvetica,arial,sans-serif; font-size:<?php echo $cfg['size']; ?>; font-weight:bold; }
table{ border-collapse:collapse; }
td{ font-family:verdana,helvetica,arial,sans-serif; font-size:<?php echo $cfg['size']; ?>; font-weight:normal; padding:3px; }
th{ border-top:1px solid #cccccc; border-bottom:1px solid #cccccc; font-family:verdana,helvetica,arial,sans-serif; font-size:<?php echo $cfg['size']; ?>; font-weight:bold; padding:3px; }
hr,hr.pme-hr{ background-color:transparent; color:#e7e7e7; height:1px; }
table.container{ border:none; }
.ftr{ font-size:x-small; }
.pme-header-right-align,.pme-cell-0-right-align,.pme-cell-1-right-align{ font-family:verdana,helvetica,arial,sans-serif; font-size:<?php echo $cfg['size']; ?>; font-weight:normal; text-align:right; }
.pme-add,.pme-buttons,.pme-cancel,.pme-cell-0,.pme-cell-1,.pme-change,.pme-clear,.pme-copy,.pme-delete,.pme-filter-right-align,.pme-filter,.pme-first,.pme-form,.pme-goto,.pme-header-right-align,.pme-header,.pme-help-0,.pme-help-1,.pme-hide,.pme-input-0-right-align,.pme-input-0,.pme-input-1-right-align,.pme-input-1,.pme-key-0-right-align,.pme-key-0,.pme-key-1-right-align,.pme-key-1,.pme-last,.pme-main,.pme-message,.pme-more,.pme-navigation-0,.pme-navigation-1,.pme-navigation,.pme-next,.pme-prev,.pme-query,.pme-queryinfo,.pme-row-0-right-align,.pme-row-0,.pme-row-1-right-align,.pme-row-1,.pme-save,.pme-search,.pme-sortinfo,.pme-stats,.pme-value-0-right-align,.pme-value-0,.pme-value-1-right-align,.pme-value-1,.pme-view{ font-family:verdana,helvetica,arial,sans-serif; font-size:<?php echo $cfg['size']; ?>; font-weight:normal; }
td.pme-cell-0-right-align, td.pme-cell-0, td.pme-cell-1-right-align, td.pme-cell-1, td.pme-filter-right-align, td.pme-filter, td.pme-navigation-0, td.pme-navigation-1, td.pme-queryinfo, td.pme-sortinfo, th.pme-header-right-align, th.pme-header{ border-bottom:1px solid #cccccc; }
a.pme-tab-selected{background-color:transparent;color:#000000;} a.pme-tab{background-color:transparent;color:#cccccc;} /*table.pme-tab{border-collapse:collapse;background-color:#6699cc;color:#000000;}*/tr.pme-tab{background-color:transparent;color:#000000;} td.pme-tab{background-color:#e7e7e7;color:#000000;} td.pme-tab-end{background-color:transparent;color:#000000;} td.pme-tab-selected{background-color:#ffff00;color:#000000;border:1px solid #cccccc}
-->
</style>
<title><?php echo isset($_SERVER['SCRIPT_URI']) ? $_SERVER['SCRIPT_URI'] : $_SERVER["SCRIPT_FILENAME"]; ?></title>
</head>
<body>

<?php 

/**
 * Check for hidden form value passed from the image upload form:
 */

$insert_image = (int) array_key_exists('insert_image',$_POST) ? $_POST['insert_image'] : 0;

/**
 * This if() loop is nearly 300 lines long (contents not indented):
 */

if($insert_image == 0){

?>

<table class="container" cellspacing="0" cellpadding="5"> <!-- Container table for phpMyEdit form  -->

<?php 

$oper_label = empty($operation) ? $opts['db'].'.'.$opts['tb'] : $operation;

echo "\n".'<tr><td><h3>'.$oper_label.'</h3></td></tr>';

?>

<tr><td> <!-- Container table cell for phpMyEdit form -->

<?php

/**
 *Tabs are set to false by default (set true to activate):
 */

$opts['display']    = array('form' => true, 'query' => true, 'sort' => true, 'tabs' => false, 'time' => false);

$opts['inc']        = 5;
$opts['key']        = 'id';
$opts['key_type']   = 'int';
$opts['language']   = $HTTP_SERVER_VARS['HTTP_ACCEPT_LANGUAGE'];
$opts['multiple']   = '4';
$opts['navigation'] = 'TUD';
$opts['options']    = 'CVDFL'; // ACPVDFL
$opts['sort_field'] = array('-id');


$opts['fdd']['id'] = array(
  'css'        => array('postfix' => 'right-align'),
  'default'    => '0',
  'input'      => 'R',
  'maxlen'     => 11,
  'name'       => 'ID',
  'options'    => 'VDFL',
  'required'   => true,
  'select'     => 'T',
  'tab'        => 'Caption',
  'sort'       => true
);


$opts['fdd']['image_caption'] = array(
  'help|ACP'   => 'Limit 255 chars.',
  'input'      => '',
  'maxlen'     => 255,
  'name'       => 'Caption',
  'options'    => 'CVDFL',
  'required'   => true,
  'select'     => 'T',
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => true
);

$opts['fdd']['image_caption']['sql|LF'] = 'if(image_caption = "", concat("-"), image_caption)';


/* 
	### DO NOT DISPLAY BINARY DATA ###

	$opts['fdd']['image'] = array(
	  'help|ACP'   => 'Limit 16,777,215 chars.',
	  'input'      => 'R',
	  'maxlen'     => 16777215,
	  'name'       => 'Image',
	  'options'    => '',
	  'required'   => true,
	  'select'     => 'T',
	  'sqlw'       => 'TRIM("$val_as")',
	  'strip_tags|LF' => true,
	  'tab'        => 'Image',
	  'textarea'   => array('rows' => '5', 'cols' => '110'),
	  'trimlen|LF' => 30,
	  'sort'       => false
	);
*/


$opts['fdd']['image_type1'] = array(
  'default'    => '0',
  'help|ACP'   => 'Limit 2 digits',
  'input'      => 'R',
  'maxlen'     => 2,
  'options'    => 'VDFL',
  'required'   => true,
  'select'     => 'D',
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => true
);

$opts['fdd']['image_type1']['values2'] = array( '0' => 'None', '1' => 'GIF', '2' => 'JPG', '3' => 'PNG', '4' => 'SWF', '5' => 'PSD', '6' => 'BMP', '7' => 'TIFF (Intel)', '8' => 'TIFF (Motorola)', '9' => 'JPC', '10' => 'JP2', '11' => 'JPX', '12' => 'JB2', '13' => 'SWC', '14' => 'IFF', '15' => 'WBMP', '16' => 'XBM' );

$opts['fdd']['image_type1']['escape'] = false;

if($resize_images > 0 && empty($operation)){
	$opts['fdd']['image_type1']['sql|L'] = 'if(image_type1 > 0, concat("<img src=\"getimage.php?id=",id, "&tb='.$opts['tb'].'\" height=\"'.$resize_height.'\""),"")';
}else{
	$opts['fdd']['image_type1']['sql|LVD'] = 'if(image_type1 > 0, concat("<img src=\"getimage.php?id=",id, "&tb='.$opts['tb'].'\" width=\"", image_width,"\" height=\"", image_height,"\""),"")';
}


$opts['fdd']['image_type2'] = array(
  'help|ACP'   => 'Limit 20 chars.',
  'input'      => 'R',
  'maxlen'     => 20,
  'name'       => 'Type',
  'options'    => 'VD',
  'required'   => true,
  'select'     => 'D',
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => true
);


$opts['fdd']['image_width'] = array(
  'css'        => array('postfix' => 'right-align'),
  'default'    => '0',
  'help|ACP'   => 'Limit 6 digits',
  'input'      => 'R',
  'maxlen'     => 6,
  'name'       => 'Width',
  'options'    => 'VD',
  'required'   => true,
  'select'     => 'T',
  'sqlw'       => 'TRIM("$val_as")',
  'tab'        => 'Details',
  'sort'       => true
);


$opts['fdd']['image_height'] = array(
  'css'        => array('postfix' => 'right-align'),
  'default'    => '0',
  'help|ACP'   => 'Limit 6 digits',
  'input'      => 'R',
  'maxlen'     => 6,
  'name'       => 'Height',
  'options'    => 'VD',
  'required'   => true,
  'select'     => 'T',
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => true
);


$opts['fdd']['img_attrib'] = array(
  'help|ACP'   => 'Limit 30 chars.',
  'input'      => 'R',
  'maxlen'     => 30,
  'name'       => '',
  'options'    => 'VDFL',
  'required'   => true,
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => false
);

// Possible IMG tag attributes: id, class, title, style, onclick, ondblclick, onmousedown, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeydown, onkeyup, ismap, usemap, 

// Usual IMG tag attributes: align, alt, border, hspace, vspace, width, height

$opts['fdd']['img_attrib']['sql|LVD'] = 'if(img_attrib > "", concat("<img src=\"getimage.php?id=",id, "&tb='.$opts['tb'].'\" align=\"left\" alt=\"",image_caption,"\" border=\"0\" hspace=\"0\" vspace=\"0\" width=\"", image_width,"\" height=\"", image_height,"\">"),"")';

// The 'hidden' field may not be relevant to your application:

$opts['fdd']['hidden'] = array(
  'default'    => '0',
  'help|ACP'   => 'Select',
  'input'      => '',
  'maxlen'     => 1,
  'name'       => 'Hidden',
  'options'    => 'CVDF',
  'required'   => true,
  'select'     => 'D',
  'sqlw'       => 'TRIM("$val_as")',
  'sort'       => true
);

$opts['fdd']['hidden']['values2'] = array('0' => 'No', '1' => 'Yes');


$opts['fdd']['updated'] = array(
  'datemask'   => 'Y-m-d H:i:s',
  'help|ACP'   => 'Timestamp',
  'input'      => 'R',
  'maxlen'     => 14,
  'name'       => 'Updated',
  'nowrap'     => true,
  'options'    => 'VD',
  'required'   => false,
  'select'     => 'N',
  'sqlw'       => 'TRIM("$val_as")',
  'tab'        => 'Updated',
  'sort'       => true
);

require('phpMyEdit.class.php');

new phpMyEdit($opts);

?>

</td></tr> <!-- Close container table cell (end phpMyEdit form above) -->

<tr>
	<td class="ftr">
		<?php echo $resize_images == 1 ? 'Thumbnails are currently configured to a height of '.$resize_height.' pixels.<br>' : 'To display "thumbs" instead of full-size images in List mode, set: $resize_images = 1;<br>'; ?>
		Click &quot;V&quot; to view an image, &quot;D&quot; to delete, or &quot;C&quot; to change captions.
	</td>
</tr>

</table>

<?php if($operation == '' && $moreadd != 'More' && $morechange != 'Apply'){ ?> <!-- Begin phpMyEdit Operation mode loop -->

<br>

<h3><?php echo $form_title; ?></h3>

<form class="pme-form" name="none" method="POST" action="<?php echo basename($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">

<table border="0" cellpadding="0" cellspacing="0" class="pme-table" width="450">
	<tr>
		<td class="normal" align="right">
			Caption
		</td>
		<td class="normal">
			<input type="text" name="image_caption" value="" size="44" maxlength="255"> 
		</td>
	</tr>
	<tr>
		<td class="normal" align="right">
			Image
		</td>
		<td class="normal">
			<input style="font-size:x-small" type="file" name="form_data" size="45" maxlength="255">
		</td>
	</tr>
	<tr>
		<td class="normal">&nbsp;</td>
		<td class="normal">
			<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
			<input type="hidden" name="tb" value="<?php echo $opts['tb']; ?>">
			<input type="hidden" name="insert_image" value="1">
			<input type="submit" name="submit" value="Submit" class="pme-view">
			<input type="reset"  name="submit" value="Reset" class="pme-view">
		</td>
	</tr>
	<tr>
		<td class="normal" colspan="2">
			Caption (recommended) becomes the alt=&quot;&quot; attribute for the &lt;IMG&gt; tag.
			<br><br>
			The Browse facilitates selecting an image on your computer. Click on Submit after an image has been selected. As currently configured, image files must not be wider than <?php echo $max_image_width; ?> pixels, nor taller than <?php echo $max_image_height; ?> pixels. 
			<br><br>Recommended file types: web-optimized GIF, JPG, or PNG.
			<br><br>Allowed file types: BMP, GIF, IFF, JB2, JP2, JPC, JPG, JPX, PNG, PSD, SWC, SWF, TIFF, WBMP, XBM.
		</td>
	</tr>
</table>
</form>

<?php }; ?> <!-- End phpMyEdit Operation mode loop -->

<!-- Begin $insert_image loop -->

<?php

}elseif($insert_image > 0){

	$success = 0;

	if(!$dbl = @mysql_pconnect($opts['hn'],$opts['un'],$opts['pw'])) { die ('<h3>Unable to connect</h3></body></html>'); }

	if(!@mysql_select_db($opts['db'])) { die('<h3>Database not selected</h3></body></html>'); }

	while(list($key,$val) = each($_POST)){
		$$key = stripslashes(trim(strip_tags($val)));
	}

	echo "\n".'<h3>Upload results for '.$opts['db'].'.'.$opts['tb'].' . . .</h3>';

	echo "\n".'<table class="container"><tr><td align="center" class="normal">';

	$image_caption = array_key_exists('image_caption',$_POST) ? htmlspecialchars(strip_tags(stripslashes(trim($_POST['image_caption'])))) : '';

	list($image_width, $image_height, $image_type1, $img_attrib) = getimagesize($form_data); 

	$errors = array();

	if($image_width > $max_image_width){
		$errors[] = 'The image width exceeds the allowed width of '.$max_image_width. ' pixels by '.($image_width-$max_image_width).' pixels';
	}
	if($image_height > $max_image_height){
		$errors[] = 'The image height exceeds the allowed height of '.$max_image_height. ' pixels by '.($image_height-$max_image_height).' pixels';
	}
	if(!$data = addslashes(@fread(@fopen($form_data, "r"), filesize($form_data)))){
		$errors[] = 'No file specified, or unable to read file';
	}

	if(count($errors) > 0){
		echo "\n".'<ol style="text-align:left">';
		for ($i = 0; $i < count($errors); $i++){
			echo "\n".'<li>'.$errors[$i].'</li>';
		}
		echo "\n".'</ol>';
		echo "\n".'</td></tr></table>';
	}elseif($data = addslashes(@fread(@fopen($form_data, "r"), filesize($form_data)))) {
		$qry = sprintf("INSERT INTO {$opts['tb']} (image, image_caption, image_width, image_height, image_type1, image_type2, img_attrib) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')", $data, addslashes($image_caption), $image_width, $image_height, $image_type1, $form_data_type, addslashes($img_attrib));
		$res = @mysql_query($qry);
		$id  = @mysql_insert_id();
		if(!$id || !$res) {
			echo '<h3>Upload failed.</h3><p class="ftr">No insert ID returned.<br>Query: '.$qry.'<br><br>'.mysql_error().'</p>';
		}else{
			echo "\n".'<p>Do not re-load this page.</p>';
			echo "\n".'<p>Image ID #'.$id.'</p>';
			echo $image_caption != '' ? '<p>'.stripslashes($image_caption).'</p>' : '<p>Caption omitted</p>';
			echo '</p>'."\n".'<p><img src ="getimage.php?id='.$id.'&tb='.$opts['tb'].'" alt="'.stripslashes($image_caption).'"></p>';
			$success = 1;
		}
	}else{
		echo '<h3>Query failed.</h3><p>Query: '.$qry.'<br><br>'.mysql_error().'</p>';
	}
	echo "\n".'</td></tr></table>';

	echo "\n".'<p class="ftr">';
	echo "\n".'<a class="ftr" href="javascript:history.go(-1)" onmouseover="self.status=document.referrer;return true" title="Go Back">Go Back</a>';
	echo "\n".' | ';
	echo "\n".'<a class="ftr" href="'.basename($_SERVER['PHP_SELF']).'" title="List images">List images</a>';
	echo $success > 0 ? "\n".' | <a class="ftr" href="'.basename($_SERVER['PHP_SELF']).'?operation=Change&rec='.$id.'" title="Edit caption'.$id.'">Edit caption</a>' : '';
	echo "\n".'</p>';
};

?> 

</body>
</html>


