<?
$page = "album.php";
include "header.php";

if(isset($_GET['task'])) { $task = $_GET['task']; } elseif(isset($_POST['task'])) { $task = $_POST['task']; } else { $task = "main"; }

// GET USER
$u = $_GET['u'];
if($u == "") { exit; }
$user_info = mysql_query("SELECT u_id, username FROM bhost_users WHERE username='$u'");
if(mysql_num_rows($user_info) == 0) { exit; } elseif(mysql_num_rows($user_info) == 1) { $user_info = mysql_fetch_assoc($user_info); }

// GET PATH TO FILES
$path = url("/images/", "uploads/");
$path .= strtolower($user_info[username]{0});
$path .= "/$user_info[username]/";













// SHOW FILE
if($task == "view") {
$up_id = $_GET['up_id'];

if($up_id == "") { exit; }
$file = mysql_query("SELECT * FROM bhost_uploads WHERE up_id='$up_id' LIMIT 1");

// FILE DOES NOT EXIST IN DATABASE
if(mysql_num_rows($file) != 1) { exit; }

// FILE DOES NOT BELONG TO THIS ALBUM
$file = mysql_fetch_assoc($file);
if($file[u_id] != $user_info[u_id]) { exit; }

// GET IMAGE PATH
$filepath = "$path$file[up_id].$file[ext]";

// IF LOCATION IS FOLDER, BE SURE FOLDER BELONGS TO THIS ALBUM
if($file[location] == "folder" AND $file[f_id] != 0) {
$folder_info = mysql_fetch_assoc(mysql_query("SELECT f_id, in_album FROM bhost_folders WHERE f_id='$file[f_id]' LIMIT 1"));
if($folder_info[in_album] != 1) { exit; }
}



// CREATE START ARROW
$start = mysql_query("SELECT up_id FROM bhost_uploads WHERE u_id='$user_info[u_id]' AND location='$file[location]' AND f_id='$file[f_id]' ORDER BY up_id DESC LIMIT 1");
$start = mysql_fetch_assoc($start);
if($start[up_id] != $up_id) {
$start_string = "<a href='".url("album", "", "$user_info[username]", "", "", "", "&task=view&up_id=$start[up_id]")."'><img src='".url("/images/", "/images/album_start.gif")."' border='0'></a>";
} else {
$start_string = "<img src='".url("/images/", "/images/album_start_d.gif")."' border='0'>";
}

// CREATE LAST ARROW
$last = mysql_query("SELECT up_id FROM bhost_uploads WHERE u_id='$user_info[u_id]' AND location='$file[location]' AND f_id='$file[f_id]' AND up_id>'$file[up_id]' ORDER BY up_id ASC LIMIT 1");
if(mysql_num_rows($last) == 1) {
$last = mysql_fetch_assoc($last);
$last_string = "<a href='".url("album", "", "$user_info[username]", "", "", "", "&task=view&up_id=$last[up_id]")."'><img src='".url("/images/", "/images/album_back.gif")."' border='0'></a>";
} else {
$last_string = "<img src='".url("/images/", "/images/album_back_d.gif")."' border='0'>";
}

// CREATE NEXT ARROW
$next = mysql_query("SELECT up_id FROM bhost_uploads WHERE u_id='$user_info[u_id]' AND location='$file[location]' AND f_id='$file[f_id]' AND up_id<'$file[up_id]' ORDER BY up_id DESC LIMIT 1");
if(mysql_num_rows($next) == 1) {
$next = mysql_fetch_assoc($next);
$next_string = "<a href='".url("album", "", "$user_info[username]", "", "", "", "&task=view&up_id=$next[up_id]")."'><img src='".url("/images/", "/images/album_forward.gif")."' border='0'></a>";
} else {
$next_string = "<img src='".url("/images/", "/images/album_forward_d.gif")."' border='0'>";
}

// CREATE END ARROW
$end = mysql_query("SELECT up_id FROM bhost_uploads WHERE u_id='$user_info[u_id]' AND location='$file[location]' AND f_id='$file[f_id]' ORDER BY up_id ASC LIMIT 1");
$end = mysql_fetch_assoc($end);
if($end[up_id] != $up_id) {
$end_string = "<a href='".url("album", "", "$user_info[username]", "", "", "", "&task=view&up_id=$end[up_id]")."'><img src='".url("/images/", "/images/album_end.gif")."' border='0'></a>";
} else {
$end_string = "<img src='".url("/images/", "/images/album_end_d.gif")."' border='0'>";
}

// CREATE GO BACK URL
switch($file[location]) {
	case "folder":
		$back_url = url("album", "", "$user_info[username]", "", "", "", "&task=viewfolder&f_id=$file[f_id]");
	break;
	case "album":
		$back_url = url("album", "", "$user_info[username]");
	break;
}


echo $head;
echo "
<div class='album_viewpic1'>
<table cellpadding='0' cellspacing='0'>
<tr>
<td>$start_string</td>
<td>$last_string</td>
<td NOWRAP><font class='gray'>[</font> <a href='$back_url'>$album1</a> <font class='gray'>]</font></td>
<td>$next_string</td>
<td>$end_string</td>
</tr>
</table>
<br>
<h2>$file[name]</h2>
$file[description]
<br>
";
if($file[description] != "") { echo "<br>"; }
echo "
<table cellpadding='0' cellspacing='0'>
<tr><td class='album_viewpic2'><img src='$filepath' border='0'></td></tr>
</table>
</div>
";


echo $foot;
exit;
}












// SHOW FOLDER
if($task == "viewfolder") {
$f_id = $_GET['f_id'];

$folder = mysql_query("SELECT * FROM bhost_folders WHERE f_id='$f_id' AND u_id='$user_info[u_id]'");
if(mysql_num_rows($folder) == 0) { exit; }

$folder_info = mysql_fetch_assoc($folder);
$files = mysql_query("SELECT up_id, f_id, location, name, dimensions, ext FROM bhost_uploads WHERE u_id='$user_info[u_id]' AND location='folder' AND f_id='$folder_info[f_id]' ORDER BY up_id DESC");

echo $head;

echo "
<h2><a href='".url("album", "", $user_info[username])."'>$user_info[username]$album2</a> &#187; $folder_info[folder]</h2>
$folder_info[description]
<br><br>
";

// SHOW IMAGES
while($file = mysql_fetch_assoc($files)) {

	// GET FILE PATH
	$filepath = "$path$file[up_id].$file[ext]";

	// GET FILE DIMENSIONS
	$dimension = explode("x", $file[dimensions]);
	$width = $dimension[0];
	$height = $dimension[1];

	// MAKE THUMBNAILS
	while($width > 150 OR $height > 150) {
		$width = $width * .95;
		$height = $height * .95;
	}

	if(file_exists("$path$file[up_id]_thumb.$file[ext]")) {
	 $thumbpath = "$path$file[up_id]_thumb.$file[ext]";
	} else {
	 $thumbpath = $filepath;
	}

	if(($c % 3) == 0 AND $c != 0) { echo "</tr>"; }
	if(($c % 3) == 0) { echo "<tr>"; }

	echo "
	<div class='albumpic'>
		<table cellpadding='0' cellspacing='0' height='100%' width='100%'>
		<tr><td align='center'>$file[name]</td></tr>
		<tr><td align='center'>
		<a href='".url("album", "", "$user_info[username]", "", "", "", "&task=view&up_id=$file[up_id]")."'><img src='$thumbpath' width='$width' height='$height' border='0' class='thumb'></a>
		</td></tr>
		</table>
	</div>
	";

	$c++;
}

echo $foot;
exit;
}
















// SHOW ALBUM
if($task == "main") {
echo $head;
echo "
<h2>$user_info[username]$album2</h2>
<br>
";


$folders = mysql_query("SELECT f_id, folder, description FROM bhost_folders WHERE u_id='$user_info[u_id]' AND in_album='1'");
$files = mysql_query("SELECT up_id, f_id, location, name, dimensions, ext FROM bhost_uploads WHERE u_id='$user_info[u_id]' AND location='album' ORDER BY up_id DESC");
$files_total = mysql_num_rows($files);

// SHOW FOLDERS
$c = 0;
while($folder = mysql_fetch_assoc($folders)) {

	echo "
	<div class='albumpic'>
		<table cellpadding='0' cellspacing='0' height='100%' width='100%'>
		<tr><td align='center'>$folder[folder]</td></tr>
		<tr><td align='center'>
		<a href='".url("album", "", "$user_info[username]", "", "", "", "&task=viewfolder&f_id=$folder[f_id]")."'><img src='".url("/images/", "/images/album_bigfolder.gif")."' border='0'></a>
		</td></tr>
		</table>
	</div>
	";

	$c++;
}


// SHOW IMAGES
while($file = mysql_fetch_assoc($files)) {

	// GET FILE PATH
	$filepath = "$path$file[up_id].$file[ext]";

	// GET FILE DIMENSIONS
	$dimension = explode("x", $file[dimensions]);
	$width = $dimension[0];
	$height = $dimension[1];

	// MAKE THUMBNAILS
	while($width > 150 OR $height > 150) {
		$width = $width * .95;
		$height = $height * .95;
	}

	if(file_exists("$path$file[up_id]_thumb.$file[ext]")) {
	 $thumbpath = "$path$file[up_id]_thumb.$file[ext]";
	} else {
	 $thumbpath = $filepath;
	}

	echo "
	<div class='albumpic'>
		<table cellpadding='0' cellspacing='0' height='100%' width='100%'>
		<tr><td align='center'>$file[name]</td></tr>
		<tr><td align='center'>
		<a href='".url("album", "", "$user_info[username]", "", "", "", "&task=view&up_id=$file[up_id]")."'><img src='$thumbpath' width='$width' height='$height' border='0' class='thumb'></a>
		</td></tr>
		</table>
	</div>
	";

	$c++;
}


if($c == "0") {
echo "$album3";
}


echo $foot;
exit;
}
?>