<?php
/*
script: images.php
purpose: list the user's uploaded images with thumbnails, urls to the images, and the option to delete images.
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 general site config / initialization stuff

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

// include and initialize an instance of the images utility class

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

// 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 ){

	// the user must be logged in to access this page.
	// if not logged in, tell them so, display the login form and exit.

	$pagecontent->display('Images - Not Logged In');
	require(dirname(__FILE__).'/includes/notloggedin.inc.php');
}

// check if a gallery has been selected...

$gallery = 0;
$g = $ace->getint('g', $_REQUEST, -1);
if( !isset($user->galleries[$g]) ){
	if( $g > 0 ) $g = 0;
}else{
	$gallery = $user->galleries[$g];
}

// display the intro text for the managing your images page

$pagecontent->display('Images');

// initialize variables used to determine which images to list / order to list them in, which page of images to display, etc.

$orderby = $ace->getstr('o', $_REQUEST, 'date');
$orderdir = $ace->getstr('od', $_REQUEST, 'desc');

if( !in_array($orderby, array('name', 'uploaded') ) ) $orderby = 'uploaded';
if( !in_array($orderdir, array('asc', 'desc') ) ) $orderdir = 'desc';


$ids = array();
$msg = '';
$emails = array();
for( $i = 0; $i < $user->email_friends; $i++) $emails[$i] = '';

$modified = false;

if( $ace->getstr('delete', $_POST) != '' ){

	// user wants to delete some of their images

	$ids = isset($_POST['ids']) ? $_POST['ids'] : array();
	$deleted = $images->deleteimages($ids, $userid);
	if( $deleted ){
		$message = $deleted.' image(s) deleted.';
		$user->images -= $deleted;
		$modified = true;
		if( $user->images < 0 ) $user->images = 0;
	}
}elseif( $ace->getstr('send', $_POST) != '' ){

	// user wants to email links to their images to their friends..

	$ids = isset($_POST['ids']) ? $_POST['ids'] : array();
	$imgs = $images->getimages(array('ids'=>$ids, 'user_id'=>$userid));
	if( count($imgs) ){
		$emails = array();
		$sentto = array();
		for( $i = 0; $i < $user->email_friends; $i++){
//			$emails[] = $i;
			if( isset($_POST['to'][$i]) ){
				$email = trim(get_magic_quotes_gpc() == 1 ? stripslashes($_POST['to'][$i]) : $_POST['to'][$i]);
				$emails[$i] = $email;
				if( $email != '' ){
					if( $ace->validateemail($email) ){
						$sentto[] = $email;
					}else{
						$images->errors[] = 'The address "'.htmlspecialchars($email).'" is not a valid email address.';
					}
				}
			}
		}
		if( count($sentto) > 0 ){
			$msg = $ace->getstr('message', $_POST);
			if( strlen($msg) > 255 ){
				$images->errors[] = 'Your message can only contain a maximum of 255 characters. It currently contains '.strlen($msg).'. Please reduce your message length.';
			}elseif( strlen($msg) == 0 ){
				$images->errors[] = 'You must enter a message to send.';
			}else{
				$headers = "From: {$user->email}\r\nReply-To: {$user->email}\r\nErrors-To: {$config->admin_email}\r\nReturn-Path: {$config->admin_email}";
				$tpl = $config->email_images_template;
				$imagelinks = array();
				foreach( $imgs as $i ){
					$imagelinks[] = $i->image_url;
				}
				$s = array('{sitename}', '{siteurl}','{message}', '{imagelinks}');
				$r = array($config->sitename, $config->siteurl, $msg,join("\n\n", $imagelinks));
				$tpl = str_replace($s, $r, $tpl);
				foreach($sentto as $email ){
					@mail($email, $config->email_images_subject, $tpl, $headers);
				}
				$msg = '';
				for( $i = 0; $i < $user->email_friends; $i++) $emails[$i] = '';
				$message = 'An email containing your message and links to your images has been sent to '.join(" and ", $sentto).'.';
			}
		}else{
			$images->errors[] = 'You must enter an email address to send the images to.';
		}
	}else{
		$images->errors[] = 'You need to check the checkboxes next to the images you want to send.';
	}
	if( count($images->errors) > 0 ) $images->errors[] = '<br />Please correct these errors and resubmit the <a href="#email">email form</a>.';
}elseif( $ace->getstr('addtogallery', $_POST) != '' ){
	$ids = isset($_POST['ids']) ? $_POST['ids'] : array();
	$added = $images->addtogallery($ids, $ace->getint('gallery_id', $_POST));
	if( count($images->errors) > 0 ){
		$errors= $images->errors;
	}else{
		$message = "$added image(s) have had their gallery changed.";
	}
}

if( $modified ) $user = $users->getuser(array('id'=>$user->user_id));

$criteria = array('user_id'=>$userid);
if( $gallery != 0 ){
	$criteria['galleryid'] = $gallery->gallery_id;
	$criteria['count'] = true;
	$user->images = $images->getimages($criteria);
	$criteria['count'] = false;
}
$page = $ace->getint('p', $_REQUEST, 1);
if( $page < 1 ) $page = 1;
$perpage = $user->images_per_page;
$totalpages = ceil($user->images / $perpage);
if( $page > $totalpages ) $page = $totalpages;
$first = ($page -1 ) * $perpage;
$last = min($first+$perpage,$user->images);
$imgs = $images->getimages($criteria, $orderby, $orderdir, $first, $perpage);

$purl = $_SERVER['SCRIPT_NAME'].'?o='.$orderby.'&od='.$orderdir.'&p={page}&g='.$g;

if( $message != '' ){
?>
<div class="message"><?php echo $message;?></div>
<?php
}

// any error messages? display them

if( count($images->errors) > 0 ){
?>
<div class="errors">Please correct the following errors:<br /><br />
<?php echo join("<br />\n", $images->errors);?></div>
<?php
}

if( count($imgs) > 0 ){

?>
<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<div class="imagelist">
<?php

	// if the user has uploaded more images than their account allows (only possible if they previously
	// had a paid account which expired, or the admin has reduced the number of images allowed), then display a message

	if( $user->images > $user->max_images && $user->max_images > 0 ){
?>
<div class="errors">You are currently exceeding the uploaded image allowance for your account.
<br />Your <?php echo htmlspecialchars($user->type_name);?> account allows you to have
<?php echo $user->max_images;?> images at a time.
<br />Please delete at least <?php echo ($user->images-$user->max_images);?> of your images or
<a href="upgrade.php">upgrade your account</a>.
</div>
<?php
	}

	// now list all their images, x to a page, with options to order by name or date uploaded.
	// images are displayed with a thumbnail (click for full size), and urls to use to link to the image.

?>
<table class="imagetable" cellspacing="1" cellpadding="0" border="0" align="center">
<tr>
	<td class="imagetitle" colspan="3" style="text-align: center;">
	Images in order of
	<a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?o=name&od=asc&g=<?php echo $g;?>">Name (a-z)</a>
	| <a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?o=name&od=desc&g=<?php echo $g;?>">Name (z-a)</a>
	| <a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?o=uploaded&od=desc&g=<?php echo $g;?>">Newest first</a>
	| <a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?o=uploaded&od=asc&g=<?php echo $g;?>">Oldest first</a>
<?php
	if( count($user->galleries) > 0 ){
?>
<br />
Galleries:
<?php
		$ng = 0;
		foreach( $user->galleries as $id=>$gal ){
//			if(  $ng < count($user->galleries)) echo ', ';
			$ng++;
?><a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?g=<?php echo $id;?>"><?php echo $gal->gallery_name;?></a><?php
			echo ', ';
		}
?><a href="<?php echo $_SERVER['SCRIPT_NAME'];?>">All Images</a><?php
	}
?>
	</td>
</tr>
<tr>
	<td class="imagetitle">
	<script language="JavaScript" type="text/javascript">
<!--
document.writeln('<input type="checkbox" onClick="check(this.form, this.checked);" />');
//-->
</script>
<noscript>&nbsp;</noscript>
	</td>
	<td class="imagetitle" colspan="2" style="text-align: center;">
		Images <?php echo $first+1;?> to <?php echo $last;?> of <?php echo $user->images;?><?php if( $gallery ) echo ' in gallery "'.$gallery->gallery_name.'"';?>.
		Page <?php echo $page;?>
		of <?php echo $totalpages;?>
		<br />
<?php 
	if( $page > 1 ){
?>
		<div style="float: left;">
			<a style="text-decoration: none;"  href="<?php echo str_replace('{page}', ($page-1), $purl);?>">&lt; Previous</a>
		</div>
<?php
	}
	if( $page < $totalpages ){
?>
		<div style="float: right;">
			<a style="text-decoration: none;" href="<?php echo str_replace('{page}',($page+1), $purl);?>">Next &gt;</a>
		</div>
<?php
	}
?>
	</td>
</tr>
<?php
$tr = 1;
foreach( $imgs as $i ){
?>
<tr class="tr<?php echo ($tr+1);?>">
	<td class="tdcheck"><input type="checkbox" name="ids[]" value="<?php echo $i->image_id;?>" <?php if( in_array($i->image_id, $ids) ) echo ' CHECKED ';?>/></td>
	<td class="tdthumb">
	<a onClick="return si('<?php echo $i->image_url;?>', <?php echo $i->width;?>, <?php echo $i->height;?>);"  target="_blank" href="<?php echo $i->image_url;?>"><img border="0" class="thumb" src="<?php echo $i->thumb_url;?>" alt="" /></a>
	<br />
	(<?php echo $i->width.'x'.$i->height;?>)
	<?php echo number_format($i->filesize/1024, 1).'k';?>
	<br />
	Bandwidth:
<?php 
	if( $i->bandwidthr > (1024*1024) ){
		echo $i->bandwidth.'mb';
	}elseif( $i->bandwidthk > (1024*1024) ){
		echo number_format($i->bandwidthr/(1024*1024*1024),2).'gb';
	}else{
		echo $i->bandwidthk.'k';
	}
?>
<?php if( $user->resize_images || $user->rename_images ){ ?>
		<div class="infolabel" style="text-align: center;">
<?php if( $user->rename_images ){ ?>
			[<a href="rename.php?i=<?php echo $i->image_id;?>">Rename</a>]
<?php } ?>
<?php if( $user->resize_images ){ ?>
			[<a href="resize.php?i=<?php echo $i->image_id;?>">Resize</a>]
<?php } ?>
		</div>
<?php } ?>
	</td>
	<td class="tdinfo">
	<b><a onClick="return si('<?php echo $i->image_url;?>', <?php echo $i->width;?>, <?php echo $i->height;?>);" target="_blank" href="<?php echo $i->image_url;?>"><?php echo $i->name.'.'.$i->type;?></a></b>
	<br />
		<div class="infolabel">URL</div>
		<div class="infotext"><input onFocus="this.select();" class="urlinput" type="text" value="<?php echo $i->image_url;?>" /></div>
		<div class="infolabel">HTML &lt;img&gt;</div>
		<div class="infotext"><input onFocus="this.select();" class="urlinput" type="text" value="<?php echo htmlentities('<img src="'.$i->image_url.'" alt="image hosting by '.$ace->config->siteurl.'" />');?>" /></div>
		<div class="infolabel">BB Code</div>
		<div class="infotext"><input onFocus="this.select();" class="urlinput" type="text" value="[img]<?php echo $i->image_url;?>[/img]" /></div>
		<div class="infolabel">Uploaded on the <?php echo date('jS F Y', strtotime($i->uploaded));?></div>
<?php if( $i->gallery_id != 0 ){?>
	<b>In Gallery: <a href="<?php echo $_SERVER['SCRIPT_NAME'].'?g='.$i->gallery_id;?>"><?php echo $i->gallery_name;?></a></b>
	[<a target="_blank" href="<?php echo $ace->config->siteurl.'gallery.php?g='.$i->gallery_name.'&u='.$user->username;?>">View</a>]
<?php } ?>
	</td>
</tr>
<?php
	$tr = 1 - $tr;
}
?>
<tr>
	<td class="imagetitle">
	<script language="JavaScript" type="text/javascript">
<!--
document.writeln('<input type="checkbox" onClick="check(this.form, this.checked);" />');
//-->
</script>
<noscript>&nbsp;</noscript>
	</td>
	<td class="imagetitle" colspan="2" style="text-align: center;">
<?php
	if( $page > 1 ){
?>
		<div style="float: left;">
			<a style="text-decoration: none;"  href="<?php echo str_replace('{page}', ($page-1), $purl);?>">&lt; Previous</a>
		</div>
<?php
	}
	if( $page < $totalpages ){
?>
		<div style="float: right;">
			<a style="text-decoration: none;" href="<?php echo str_replace('{page}',($page+1), $purl);?>">Next &gt;</a>
		</div>
<?php
	}
?>

	<br />
		Images <?php echo $first+1;?> to <?php echo $last;?> of <?php echo $user->images;?><?php if( $gallery ) echo ' in gallery "'.$gallery->gallery_name.'"';?>.
		Page <?php echo $page;?>
		of <?php echo $totalpages;?>
	</td>
</tr>
<?php if( $user->email_friends > 0 ){ ?>
<tr>
	<td colspan="3" class="imagetitle"><a name="email"></a>&nbsp;</td>
</tr>
<tr>
	<td class="imagetitle">&nbsp;</td>
	<td class="imagetitle" colspan="2">
		<b>Email The Selected Images To Your Friends!</b>
	</td>
</tr>
<tr class="tr1">
	<td class="tdcheck">&nbsp;</td>
	<td class="tdinfo" style="text-align: right;">Friend's Email:</td>
	<td class="tdinfo">
<?php
	for( $i = 0; $i < $user->email_friends; $i++){
?>
		<input type="text" name="to[]" value="<?php echo htmlspecialchars($emails[$i]);?>" class="urlinput" /><br />
<?php
	}
?>
	<br />
<?php if( $user->email_friends > 1 ){ ?>
Enter the email address of each friend you want to send these images to in one of the boxes above.
You can send the images to up to <?php echo $user->email_friends;?> email addresses at once.
<?php }else{ ?>
Enter the email address of the friend you want to send these images to.
<?php } ?>
<br />We respect your privacy. We do not store the email addresses submitted via this form.
	</td>
</tr>
<tr class="tr1">
	<td class="tdcheck">&nbsp;</td>
	<td class="tdthumb" style="text-align: right;">Short Message<br />(max 255 characters)</td>
	<td class="tdinfo"><textarea name="message" class="urlinput" style="height: 60px;"><?php echo htmlspecialchars($msg);?></textarea></td>
</tr>
<tr class="tr1">
	<td class="tdcheck">&nbsp;</td>
	<td class="tdthumb">&nbsp;</td>
	<td class="tdinfo"><input type="submit" name="send" value="Email The Selected Images" /></td>
</tr>
<?php 
}

if( count($user->galleries) > 0 ){
?>
<tr>
	<td class="imagetitle" colspan="3">&nbsp;</td>
</tr>
<tr>
	<td class="imagetitle" colspan="3">Add The Selected Images To One Of Your Galleries</td>
</tr>
<tr class="tr1">
	<td class="tdcheck">&nbsp;</td>
	<td class="tdthumb">&nbsp;</td>
	<td class="tdinfo">
		<select name="gallery_id">
			<option value="0">No Gallery</option>
<?php
	foreach( $user->galleries as $id=>$g ){
		echo "<option value=\"$id\">{$g->gallery_name}</option>\n";
	}
?>
		</select>
		[<a href="galleries.php">Manage Galleries</a>]<br />
		<input type="submit" name="addtogallery" value="Add Selected Images To This Gallery" onClick="return confirm('Are you sure you want to add the selected images to the selected gallery?');" />
	</td>
</tr>
<?php
}
?>
<tr>
	<td class="imagetitle" colspan="3">&nbsp;</td>
</tr>
<tr>
	<td class="imagetitle" colspan="3">Remove The Selected Images From Your Account</td>
</tr>
<tr class="tr1">
	<td class="tdcheck">&nbsp;</td>
	<td class="tdthumb">&nbsp;</td>
	<td class="tdinfo">
		<input type="submit" name="delete" value="Delete The Selected Images" onClick="return confirm('Are you sure you want to delete the selected images?');" />
	</td>
</tr>
</table>
</div>
</form>
<?php

}else{

	// if the user hasn't uploaded any images ,display a message telling them this.

	$pagecontent->display('No Images');

}

// include the site footer.

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

?>
