<?php
/*
script: upload.php
purpose: allows registered users to upload images to their account.
copyright: copyright 2004 Php Image Host. All Rights Reserved. You may modify this file for use on your
					site running a licenced copy of the Php Image Host scripts.
					You must not distribute this file or derivations of it.
Nullified By CrAcKlOvE

*/

// include site config / database initialization, etc stuff

require_once(dirname(__FILE__).'/includes/config.inc.php');

// include and initialize the images class which handles adding images, etc.

require_once(dirname(__FILE__).'/classes/images.class.php');
$images =& new images($ace, $user);
$images->setuser($user);

$uploaderrors = '';
$uploaded = array();
$ids = array();

// include the site header

require_once(dirname(__FILE__).'/header.php');

if( $banned ){
	$pagecontent->display('IP Banned');
	require_once(dirname(__FILE__).'/footer.php');
	exit();
}elseif( !$loggedin ){

	// login is required to upload, so if the user is not logged in we display some content,
	// include the default "not logged in page", and exit.
	$pagecontent->display('Upload - Not Logged In');
	require(dirname(__FILE__).'/includes/notloggedin.inc.php');
}


// display the introduction / instructions for the upload page.

$pagecontent->display('Upload');

// process any uploaded images.
$uploaderrors = array();
$gallery = $ace->getint('gallery_id', $_POST);
if( $ace->getstr('upload', $_POST) != ''  && $config->allow_uploads){ 
	for( $i = 0; $i < $user->simultaneous_uploads; $i++){
		$id = 'images'.$i;
		if( isset($_FILES[$id]['tmp_name']) && is_uploaded_file($_FILES[$id]['tmp_name']) ){
			$fname = $_FILES[$id]['tmp_name'];
			$name = $ace->getstr('names'.$i, $_POST);
			if( !preg_match('/[a-z0-9]{2,}/i', $name) ) $name = $_FILES[$id]['name'];
			$iid = $images->addimage($name, $userid, $fname, $gallery);
			if( $iid ){
				$user = $users->getuser(array('id'=>$user->user_id));
				$ids[] = $iid;
			}else{
//				if( !isset($_FILES[$id]['name']) ) echo "$id is not set...";
				$uploaderrors[$_FILES[$id]['name']] = join("<br />\n",$images->errors);
				$images->errors = array();
			}
		}
	}
	if( count($ids) ){
		$uploaded = $images->getimages(array('ids'=>$ids));
	}
}

// display message(s) if any images successfully uploaded

if( count($uploaded) > 0 ){
?>
<br /><br /><b>The following image(s) were successfully uploaded:</b><br /><br />
<?php

	foreach( $uploaded as $img ){
?>
<div style="float: left; text-align: center; width: <?php echo $config->thumbnail_width+20;?>; height: <?php echo $config->thumbnail_height+20;?>; vertical-align: middle;">
<a target="_blank" href="<?php echo $img->image_url;?>" onClick="return si('<?php echo $img->image_url;?>', <?php echo $img->width;?>, <?php echo $img->height;?>);"><img src="<?php echo $img->thumb_url;?>" alt="click for full size" border="0" style="border: 1px solid black;" /></a> 
</div>
<?php
	}
?>
<br clear="all" />
<?php

}

// display any image upload error messages...

if( count($uploaderrors) > 0 ){
?>
<br /><br /><b>Errors occurred uploading the following images:</b><br /><br />
<?php foreach( $uploaderrors as $i=>$errs){ ?>
<?php echo htmlspecialchars($i);?><br /><?php echo $errs;?><br /><br />
<?php
	}
}

if( ($user->max_images == 0 || $user->images < $user->max_images ) &&
		($user->bandwidth == 0 || $user->bandwidth_used < $user->bandwidth) && 
		($user->storage == 0 || $user->storage_used < $user->storage) && 
		$config->allow_uploads){

	// if the user can upload any more images, display the upload form.
	// more than one file upload input will be displayed if the user's account
	// type settings specify it.

?>
<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post" enctype="multipart/form-data">
<div class="uploadform">
	<div class="uploadheading">Upload Images From Your PC</div>
	<div class="uploadinstructions">Click the "Browse" button to select an image to upload from your PC.
<?php
	if( $user->rename_images == 1 ){
?>
		<br />You can (optionally) enter a new name for your image. You will also be able to rename your images
		after you have uploaded them.
<?php
	}
?>
	</div>
<?php
	for( $i = 0; $i < $user->simultaneous_uploads; $i++){
?>
	<div class="uploadfield">
		<input class="uploadinput" type="file" name="images<?php echo $i;?>" <?php if( $user->rename_images == 1 ){ ?>onChange="this.form.names<?php echo $i;?>.value = exfname(this.value);" <?php } ?>/>
<?php if( $user->rename_images == 1){ ?>
		<b>Name:</b> <input class="uploadname" type="text" name="names<?php echo $i;?>" />
<?php } ?>
	</div>
<?php 
	}
	if( count($user->galleries) > 0 ){
?>
	<div class="uploadheading">Add Uploaded Images To Gallery:</div>
	<div class="uploadinstructions">You can (optionally) select one of your galleries to add these images to.</div>
	<div class="uploadfield">
		<select class="uploadgallery" name="gallery_id">
			<option value="0">No Gallery</option>
<?php
		foreach( $user->galleries as $id=>$g ){
			echo "<option value=\"$id\">".htmlspecialchars($g->gallery_name)."</option>\n";
		}
?>
		</select>
	</div>
<?php
	
	}
?>
	<div class="uploadfield"><input class="uploadsubmit"  type="submit" name="upload" value="Upload Images" /></div>
</div>
</form>
<?php

}else{

	// display a message informing the user that they cannot upload any more images, unless they
	// either upgrade their account or delete some existing ones.
	if( $config->allow_uploads ){
		if( $user->bandwidth != 0 && $user->bandwidth_used > $user->bandwidth){
			$pagecontent->display('Monthly Bandwidth Limit Reached');
		}else{
			$pagecontent->display('Image Limit Reached');
		}
	}else{
		$pagecontent->display('Uploads Suspended');
	}
}

// include the site footer.

require_once(dirname(__FILE__).'/footer.php');

?>
