<?php
/*
script: galleries.php
purpose: list the user's created image galleries and allows them to create, edit or delete them.
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 = '';
$action = $ace->getstr('action', $_REQUEST);
$errors = array();

// include the site header

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

// display the intro text for the galleries page

$pagecontent->display('Galleries');

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.

	require(dirname(__FILE__).'/includes/notloggedin.inc.php');
}elseif( $user->max_galleries == 0 ){
	$pagecontent->display('Galleries Not Allowed');
	require_once(dirname(__FILE__).'/footer.php');
	exit();
}

$gid = $ace->getint('g', $_REQUEST);
$gallery = 0;
if( $gid != 0 ) $gallery = $images->getgallery($gid);
if( $gallery == 0 ){
	$gid = 0;
	if( $action == 'edit' ) $action = '';
}

$ids = isset($_POST['ids']) ? $_POST['ids'] : array();
$emails = array();
for( $i = 0 ; $i < $user->email_friends; $i++) $emails[$i] = '';
$msg = $ace->getstr('msg', $_POST);

if( $gallery != 0 ){
	if( $ace->getstr('updategallery', $_POST) != '' ){
		$gallery->gallery_name = $ace->getstr('name', $_POST);
		$gallery->gallery_intro = $ace->getstr('intro', $_POST);
		if( $images->updategallery($gid, $gallery->gallery_name, $gallery->gallery_intro) ){
			$gallery = $images->getgallery($gid);
			$message = 'Gallery "'.$gallery->gallery_name.'" Updated.';
			$user->galleries[$gid] = $gallery;
		}else{
			$action = 'edit';
			$errors = $images->errors;
		}
	}elseif( $ace->getstr('delete', $_REQUEST) != '' ){
		if( $images->deletegallery($gid) ){
			$message = 'Gallery "'.$gallery->gallery_name.'" Deleted.';
			unset($user->galleries[$gid]);
			$gid = $gallery = 0;
		}
	}
}elseif($ace->getstr('addgallery', $_POST) != '' && $user->max_galleries > count($user->galleries)){
	$name = $ace->getstr('name', $_POST);
	$intro = $ace->getstr('intro', $_POST);
	$gid = $images->addgallery($name, $intro);
	if( $gid ){
		$gallery = $images->getgallery($gid);
		$message = 'Gallery "'.$gallery->gallery_name.'" Created.';
		$user->galleries[$gid] = $gallery;
	}else{
		$action = 'add';
		$errors = $images->errors;
	}
}elseif( $ace->getstr('send', $_POST) != '' ){

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

	$gals = array();
	foreach( $ids as $i ){
		if( isset($user->galleries[$i]) ){
			$gals[] =& $user->galleries[$i];
		}
	}

	if( count($gals) ){
		$emails = array();
		$sentto = array();
		for( $i = 0; $i < $user->email_friends; $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{
						$errors[] = 'The address "'.htmlspecialchars($email).'" is not a valid email address.';
					}
				}
			}
		}
		if( count($sentto) > 0 ){
			$msg = $ace->getstr('msg', $_POST);
			if( strlen($msg) > 255 ){
				$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 ){
				$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;
				$gallerylinks = array();
				foreach( $gals as $g ){
					$gallerylinks[] = $ace->config->siteurl.'gallery.php?g='.$g->gallery_name.'&u='.$user->username;
				}
				$s = array('{sitename}', '{siteurl}','{message}', '{imagelinks}');
				$r = array($config->sitename, $config->siteurl, $msg,join("\n\n", $gallerylinks));
				$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 galleries has been sent to '.join(" and ", $sentto).'.';
			}
		}else{
			$errors[] = 'You must enter an email address to send the your galleries to.';
		}
	}else{
		$errors[] = 'You need to check the checkboxes next to the galleries you want to send.';
	}
	if( count($errors) > 0 ) $errors[] = '<br />Please correct these errors and resubmit the <a href="#email">email form</a>.';
}

if( count($errors) > 0 ){
?>
<div class="errors">The following errors occurred:<br /><br /><?php echo join("<br />\n", $errors);?></div>
<?php
}

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

if( count($user->galleries) >= $user->max_galleries ){
	if( $action == 'add' ) $action = '';
}else{
?>
<div style="text-align: center; margin: 15px; font-weight: bold;"><a href="<?php echo $_SERVER['SCRIPT_NAME'];?>?action=add#addg">Create A New Gallery</a></div>
<?php
}

switch( $action ){
	case 'add':
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<div style="text-align: center;"><a name="addg"></a>
<div class="galleryform">
	<div class="galleryheading">Create A New Gallery</div>
	<div class="gallerylabel">Gallery Name:</div>
	<input class="gallerynameinput" type="text" name="name" />
	<div class="gallerylabel">Gallery Introduction:</div>
	<textarea class="galleryintrotext" name="intro"></textarea>
	<br />(optionally) enter a short introduction to your gallery.
	<input class="gallerysubmit" type="submit" name="addgallery" value="Create New Gallery" />
</div>
</div>
</form>
<?php
		break;
	case 'edit':
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<input type="hidden" name="g" value="<?php echo $gallery->gallery_id;?>" />
<div style="text-align: center;"><a name="editg"></a>
<div class="galleryform">
	<div class="galleryheading">Edit Gallery "<?php echo htmlspecialchars($gallery->gallery_name);?>"</div>
	<div class="gallerylabel"b>Gallery Name:</div>
	<input class="gallerynameinput" type="text" name="name" value="<?php echo htmlspecialchars($gallery->gallery_name);?>" />
	<div class="gallerylabel">Gallery Introduction:</div>
	<textarea class="galleryintrotext" name="intro" ><?php echo htmlspecialchars($gallery->gallery_intro);?></textarea>
	<br />(optionally) enter a short introduction to your gallery.
	<input class="gallerysubmit" type="submit" name="updategallery" value="Update Gallery Details" />
</div>
</div>
</form>
<?php
		break;
}

if( count($user->galleries) > 0 ){
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<table class="imagetable" cellspacing="1" cellpadding="0" border="0" align="center" style="width: 95%;">
<tr>
	<td class="imagetitle" colspan="2" style="text-align: center;">Manage Your Image Galleries</td>
</tr>
<?php
	$tr = 0;
	foreach( $user->galleries as $id=>$g ){
		$gurl = $ace->config->siteurl;
		switch( $ace->config->gallery_url_mode){
			case 'rewrite':
				$gurl.='viewgallery/'.$user->username.'/'.$g->gallery_name.'.htm';
				break;
			default:
				$gurl.='gallery.php?u='.$user->username.'&g='.$g->gallery_name;
				break;
		}
?>
<tr class="tr<?php echo ($tr+1);?>">
	<td class="tdcheck"><input type="checkbox" name="ids[]" value="<?php echo $g->gallery_id;?>" <?php if( in_array($g->gallery_id, $ids) ) echo ' CHECKED ';?>/></td>
	<td class="tdlabel">
		<b><a target="_blank" href="<?php echo $gurl;?>"><?php echo $g->gallery_name;?> (<?php echo $g->images;?> images)</a></b>
		<br /><i><?php echo nl2br(htmlspecialchars($g->gallery_intro));?></i>
		<br />
		[<a href="<?php echo $_SERVER['SCRIPT_NAME'].'?g='.$id;?>&action=edit#editg">Edit</a>]
		[<a href="<?php echo $_SERVER['SCRIPT_NAME'].'?g='.$id;?>&delete=1" onClick="return confirm('Are you sure you want to delete this gallery?');">Delete</a>]
		[<a href="images.php?g=<?php echo $g->gallery_id;?>">Manage Images</a>]
		<br /><br /><b>Gallery URL:</b>
		<br /><input onFocus="this.select();" type="text" class="galleryurlinput" value="<?php echo htmlspecialchars($gurl);?>" />
		<br /><br /><b>HTML Link:</b>
		<br /><input onFocus="this.select();" type="text" class="galleryurlinput" value="<?php echo htmlspecialchars('<a href="'.$gurl.'">'.$g->gallery_name.'</a>');?>" />
	</td>
</tr>
<?php
		$tr = 1-$tr;
	}
	if( $user->email_friends > 0 ){
?>
<tr>
	<td colspan="2" class="imagetitle"><a name="email"></a>&nbsp;</td>
</tr>
<tr>
	<td class="imagetitle">&nbsp;</td>
	<td class="imagetitle" style="text-align: center;">
		<b>Email The Selected Galleries To Your Friends!</b>
	</td>
</tr>
<tr class="tr1">
	<td class="tdcheck">&nbsp;</td>
	<td class="tdinfo" style="text-align: left;">
		<b>Enter Your Friend's Email Address<?php if( $user->email_friends > 1 ) echo 'es';?>:</b><br />
<?php
	for( $i = 0; $i < $user->email_friends; $i++){
?>
		<input type="text" name="to[]" value="<?php echo htmlspecialchars($emails[$i]);?>" class="urlinput" style="width: 90%;" />
		<br />
<?php
	}
?>
	<br />
<?php if( $user->email_friends > 1 ){ ?>
Enter a single email address to send your selected galleries to in each
of the boxes above.
You can send the galleries 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 galleries to in the box above.
<?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: left;">
		<b>Short Message (max 255 characters)</b><br />
		<br /><textarea name="msg" class="urlinput" style="width: 90%; height: 60px;"><?php echo htmlspecialchars($msg);?></textarea>
		<br />This message will be included in the email.
	</td>
</tr>
<tr class="tr1">
	<td class="tdcheck">&nbsp;</td>
	<td class="tdinfo"><input type="submit" name="send" value="Email The Selected Galleries" style="width: 90%;" /></td>
</tr>
<?php 
}
?>
</table>
</form>
<?php
}


// include the site footer.

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

?>
