useDefaultColumns(false);
$page->addStyle("albumViewer.css");
$page->addScript("albumViewer.js");
$page->requireLogin();
$username = getVal($_GET, PARAM_USERNAME);
$owner = new User(); // the user who is the owner of the album
if($username != ""){
$owner->loadByUsername($username);
}
if($owner->exists()){
$user = getUser();
$ownerId = $owner->getId();
$ownerName = $owner->getUsername();
$ownerVisible = $ownerName;
if(albumAccess($user->getId(), $owner->getId())){
// Re-set the title if the user was found.
$title = $owner->getUsername()."'s pictures";
$page->setTitle($title);
$done = false;
$db = dil_connect();
define('DEFAULT_ALBUM_DESC', defaultAlbumDesc($ownerVisible)); // STRINGTABLES
// If a picture is set, display the picture with navigation.
$picId = getVal($_GET, PARAM_PIC_ID);
$albumId = getVal($_GET, PARAM_ALBUM_ID);
$offset = getVal($_GET, PARAM_OFFSET);
$limit = 50; //$limit = getVal($_GET, PARAM_LIMIT); // don't let the user mess with it for now - maybe give them a select-box later.
$editId = getVal($_GET, PARAM_EDIT);
$deleteId = getVal($_GET, PARAM_DELETE);
if("$picId$editId$deleteId" != ""){
$done = albumViewer_pic($user, $owner, $picId, $albumId, $offset, $limit, $editId, $deleteId);
}
// If not done and an album is set, display thumbnails (paginated) for the album
if((!$done) && ($albumId != "")){
$done = albumViewer_album($user, $owner, $picId, $albumId, $offset, $limit);
}
// If not done, display each album with a couple of thumbnails.
if(!$done){
$done = albumViewer_user($user, $owner, $picId, $albumId, $offset, $limit);
}
// Something went wrong, let the user know, and log the problem.
if(!$done){
$visibleName = $owner->getUsername();
$username = $owner->getUsername();
$msg = "There was an error processing your request. We have logged this problem and will fix as soon as we figure out what's wrong.";
$msg .= "
";
$msg .= "You may have more luck at \n";
$msg .= "$visibleName's profile or \n";
$PHP_SELF = $_SERVER['PHP_SELF'];
$msg .= "$visibleName's Photo Albums";
dispError($msg);
$msg = "Something went wrong during the loading of $visibleName ($username)'s Album Viewer page.";
$viewer = $user->getUsername();
$msg.= "The request was: ".$_SERVER['REQUEST_URI']." and was requested by $viewer";
logEvent($msg, 3);
}
} else {
include_once "mod/errorMod.php";
$err = albumAccessError($user->getId(), $owner->getId());
$err.= "
Back to $ownerVisible's Profile\n";
$errMod = new ErrorModule($err);
$page->setColumns(array(50,50));
$page->addModule($errMod, 0);
$page->addModule(new AdModule(), 1);
}
$page->display();
} else {
// STRINGTABLES
ob_start();
dispError("User not found. Please try the search page to locate what you were looking for.");
$content = ob_get_clean();
simplePage($content);
}
////
// Displays the page for a single picture in the Album Viewer.
////
function albumViewer_pic($user, $owner, $picId, $albumId, $offset, $limit, $editId, $deleteId){
GLOBAL $page;
$userId = $user->getId();
$ownerId = $owner->getId();
$ownerName = $owner->getUsername();
$ownerVisible = $owner->getUsername();
$db = dil_connect();
if(($picId == "") && ($editId != "")){
$picId = $editId;
} else if(($picId == "") && ($deleteId != "")){
$picId = $deleteId;
}
$wasEdited = false;
$formName = getPost('formName');
if(($formName == "albumMaker_edit") && ($userId == $ownerId)){
include_once "sanitizer.php";
$editAlbum = getPost('whichAlbum');
$editCaption = sanitize(getPost('caption'), ALLOW_HTML);
// Check to make sure you own the album.
$isMyAlbum = (($editAlbum == -1) || (0 < simpleQuery("SELECT COUNT(*) FROM albums WHERE id='$editAlbum' AND user_id='$userId'")));
if($isMyAlbum){
$currAlbum = simpleQuery("SELECT album_id FROM album_pics WHERE id='$picId' AND user_id='$userId'");
$queryString = "UPDATE album_pics SET album_id='$editAlbum', caption='$editCaption' WHERE id='$picId' AND user_id='$userId'";
if(sendQuery($queryString)){
// Change the number of pictures in each album.
if($currAlbum != $editAlbum){
if($currAlbum != -1){
sendQuery("UPDATE albums SET numPics=numPics-1 WHERE id='$currAlbum'");
}
if($editAlbum != -1){
sendQuery("UPDATE albums SET numPics=numPics+1 WHERE id='$editAlbum'");
}
}
// If this image was just uploaded, it should no longer show up in the list of recently uploaded photos.
$uploads = getVal($_SESSION, 'albumMaker_uploads', array());
$index = array_search($picId, $uploads);
if($index !== false){
unset($uploads[$index]);
$uploads = array_values($uploads); // re-keys the array.
$_SESSION['albumMaker_uploads'] = $uploads;
}
$wasEdited = true;
} else {
logQueryError($queryString);
}
}
}
$queryString = "SELECT * FROM album_pics WHERE id='$picId' AND user_id='$ownerId'";
if($result = mysql_query($queryString,$db)){
$done = true;
if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){
$cnt = 0;
$id = mysql_result($result, $cnt, "id");
//$fileName_thumb = mysql_result($result, $cnt, "fileName_thumb");
//$fileName_scaled = mysql_result($result, $cnt, "fileName_scaled");
$caption = mysql_result($result, $cnt, "caption");
$dateTaken = mysql_result($result, $cnt, "dateTaken");
$dateTaken = shortDate($dateTaken);
$albumId = mysql_result($result, $cnt, "album_id");
$createdOn = mysql_result($result, $cnt, "createdOn");
$updatedAt = mysql_result($result, $cnt, "updatedAt");
$fileName_fs = mysql_result($result, $cnt, "fileName_fs");
$fileName_scaled = mysql_result($result, $cnt, "fileName_scaled");
$fileName_thumb = mysql_result($result, $cnt, "fileName_thumb");
if($albumId == DEFAULT_ALBUM_ID){
$albumName = DEFAULT_ALBUM_NAME;
} else {
$albumName = simpleQuery("SELECT name FROM albums WHERE id='$albumId' AND user_id='$ownerId'");
}
ob_start();
//$albumViewer = $_SERVER['PHP_SELF'];
$albumViewer = "/albumViewer.php";
$src = albumImg($ownerId, $picId, ALBUM_SIZE_SCALED);
print "
\n";
//print " \n";
if($deleteId != ""){
$hash = getVal($_GET, "hash");
if($hash == getItemHash()){
include_once 'lib/motive_fs.php';
// Delete files from the file-system and Motive FS.
motive_fs_deleteFile($fileName_fs);
motive_fs_deleteFile($fileName_scaled);
motive_fs_deleteFile($fileName_thumb);
sendQuery("DELETE FROM album_pics WHERE id='$picId'"); // Delete entry in the album pics table.
sendQuery("UPDATE albums SET numPics=numPics-1 WHERE id='$albumId'"); // Update the album's stats.
// If this image was just uploaded, it should no longer show up in the list of recently uploaded photos.
$uploads = getVal($_SESSION, 'albumMaker_uploads', array());
$index = array_search($picId, $uploads);
if($index !== false){
unset($uploads[$index]);
$uploads = array_values($uploads); // re-keys the array.
$_SESSION['albumMaker_uploads'] = $uploads;
}
dispSuccess("Your picture has been removed from ".STR_PEDLR);
} else {
$deleteId = "";
dispError("The link that sent you here appears to be old, please try the 'delete' link again. This is a security feature that makes sure you really intended to delete your picture."); // STRINGTABLES
}
}
if($deleteId == ""){
// Display the nav to the previous and next image(s) - EVENTUALLY THIS SHOULD BECOME FLICKRISH WITH AJAX W/SLIDE animate().
$numNear = 1;
$tableStart = "
\n"; // don't want to display it unless needed
//$toTheLeft = 0;
//$currCode = "[THIS PIC]";
$picsShown = 0;
$queryString = "SELECT id FROM album_pics WHERE album_id='$albumId' AND user_id='$ownerId' AND createdOn < '$createdOn' ORDER BY createdOn DESC LIMIT $numNear";
if($result = mysql_query($queryString,$db)){
if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){
if($picsShown == 0){
print $tableStart;
}
$picsShown += $numRows;
//$toTheLeft = $numRows;
$link = "/albumViewer.php?".PARAM_USERNAME."=$ownerName&";
for($cnt=$numRows-1; $cnt>=0; $cnt--){
$id = mysql_result($result, $cnt, "id");
$src = albumImg($ownerId, $id, ALBUM_SIZE_THUMB);
print "
\n";
} else {
$albumViewer = "/albumViewer.php";
print "\n";
}
} else {
print "This photo no longer exists\n"; // STRINGTABLES
}
$content = ob_get_clean();
$infoMod = new TextModule($content, 'text', $title);
$infoMod->setImage("pics.png");
// If this is the current user's photo, they will have the option to edit or delete it.
if(($ownerId == $userId) && ($deleteId == "")){
$link = "/albumViewer.php?".PARAM_USERNAME."=$ownerName&";
$code = "delete";
$mainMod->setTitleBarSuffix($code);
if($editId == ""){
$code = "edit";
} else {
$code = "cancel";
}
$infoMod->setTitleBarSuffix($code);
}
$page->setColumns(array(25,75));
$page->addModule($infoMod, 0);
// If the user has more images awaiting captions, link them back to the pictures.
$uploads = getVal($_SESSION, 'albumMaker_uploads', array());
if(count($uploads) > 0){
// STRINGTABLES
$title = "Captions pending...";
$content = "You have more images still waiting for captions...
";
$content.= "When you are done here, you can go back and see the updated list of ";
$content.= "pictures awaiting captions";
$alertMod = new TextModule($content, 'text', $title);
$alertMod->setImage("attention.png");
$page->addModule($alertMod, 0);
}
$page->addModule(new UserLinksModule(), 0);
$page->addModule(new AdModule(), 0);
$page->addModule($mainMod, 1);
} else {
ob_start();
$msg = "Picture not found. It was probably deleted by its creator."; // STRINGTABLES
$msg .= " ";
if($albumId != ""){
$msg .= " Back to album";
}
$msg .= " Back to $ownerVisible's pictures";
dispError($msg);
$content = ob_get_clean();
include_once 'homeTools.php';
$page = home_getHomePage();
$page->addMessage($content);
}
}
return $done;
} // end albumViewer_pic()
////
// Displays a specific album (if that is what was requested).
////
function albumViewer_album($user, $owner, $picId, $albumId, $offset, $limit){
GLOBAL $page;
$userId = $user->getId();
$ownerId = $owner->getId();
$ownerName = $owner->getUsername();
$ownerVisible = $owner->getUsername();
$editAlbumId = ((getVal($_GET, PARAM_EDIT_ALBUM) == "true")?$albumId:"");
$deleteAlbumId = ((getVal($_GET, PARAM_DELETE_ALBUM) == "true")?$albumId:"");
$db = dil_connect();
$found = false;
$albumName = "";
if($albumId == DEFAULT_ALBUM_ID){
$found = true;
$done = true;
$albumName = DEFAULT_ALBUM_NAME;
$albumDesc = DEFAULT_ALBUM_DESC;
$createdOn = "";
$updateAt = "";
} else {
$queryString = "SELECT * FROM albums WHERE id='$albumId' AND user_id='$ownerId'";
if($result = mysql_query($queryString,$db)){
$done = true;
if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){
$found = true;
$cnt = 0;
$albumName = mysql_result($result, $cnt, "name");
$albumDesc = mysql_result($result, $cnt, "description");
$createdOn = mysql_result($result, $cnt, "createdOn");
$updatedAt = mysql_result($result, $cnt, "updatedAt");
}
}
}
$title = $albumName;
ob_start();
if($found){
$albumDeleted = false;
$userPage = "/albumViewer.php?".PARAM_USERNAME."=$ownerName";
$action = $userPage."&".PARAM_ALBUM_ID."=$albumId";
if($editAlbumId != ""){
$nameText = str_replace("'", "'", $albumName);
$descText = prepareText($albumDesc);
print "
\n";
print "Editing \"".$nameText."\"";
print "
\n";
print "\n";
} else if($deleteAlbumId != ""){
$hash = getVal($_GET, 'hash');
if($hash == getItemHash()){
// Delete the album.
if(sendQuery("DELETE FROM albums WHERE id='$albumId' AND user_id='$userId'")){
// Move all photos to default album.
sendQuery("UPDATE album_pics SET album_id='".DEFAULT_ALBUM_ID."' WHERE album_id='$albumId' AND user_id='$userId'");
$msg = "This album has been deleted. Any pictures in the album were moved to the default album.";
$msg.= "
<< Back to all albums.";
dispSuccess($msg);
$albumDeleted = true; // to prevent the title-bar suffix from being set.
}
} else {
dispError("It appears the link that sent you here was old. Please click the 'delete' link again to delete this album."); // STRINGTABLES
}
} else {
// If the user edited the album, apply the changes
if(getPost('formName') == "albumMaker_editAlbum"){
include_once "sanitizer.php";
$editedName = sanitize(getPost('albumMaker_albumName'));
$editedDesc = sanitize(getPost('albumMaker_albumDesc'), ALLOW_HTML);
if($editedName != ""){
$queryString = "UPDATE albums SET name='$editedName', description='$editedDesc' WHERE id='$albumId' AND user_id='$userId'";
if(sendQuery($queryString)){
$albumName = str_replace("\'", "'", $editedName); // from mySQL-safe to displayable version
$albumDesc = str_replace("\'", "'", $editedDesc);
dispSuccess("Album updated."); // STRINGTABLES
}
} else {
dispError("Album names cannot be left blank");
}
}
$albumViewer = "/albumViewer.php";
print "
\n";
}
$queryString = "SELECT * FROM album_pics WHERE album_id='$albumId' AND user_id='$ownerId' ORDER BY createdOn LIMIT $limit";
if($result = mysql_query($queryString,$db)){
if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){
// We only need the extra query if there are at least enough pics to fill the page.
if($numRows >= $limit){
$numTotal = simpleQuery("SELECT COUNT(*) FROM album_pics WHERE album_id='$albumId'");
} else {
$numTotal = $numRows;
}
$baseUrl = $_SERVER['PHP_SELF']."?".PARAM_USERNAME."=$ownerName&".PARAM_ALBUM_ID."=$albumId";
albumNav($offset, $limit, $numRows, $numTotal, $baseUrl);
$picData = array();
for($cnt=0; $cnt<$numRows; $cnt++){
$picId = mysql_result($result, $cnt, "id");
$caption = mysql_result($result, $cnt, "caption");
$picData[] = array(
INDEX_PIC_ID => $picId,
INDEX_TT_DESC => $caption
);
}
$numCols = 5;
$toolTips = albumTable($picData, $numCols, $ownerId, $ownerName);
$page->addToolTips($toolTips);
albumNav($offset, $limit, $numRows, $numTotal, $baseUrl);
} else {
print "There are no photos in this album yet\n";
}
}
}
} else {
dispError("Album not found."); // STRINGTABLES - TODO: MAKE A BETTER MESSAGE
}
$content = ob_get_clean();
$mainMod = new TextModule($content, 'text', $title);
if(($userId == $ownerId) && ($albumId != DEFAULT_ALBUM_ID) && (!$albumDeleted)){
$link = "/albumViewer.php?".PARAM_USERNAME."=$ownerName&".PARAM_ALBUM_ID."=$albumId&";
$code = "";
if($editAlbumId == ""){
$code .= "edit";
$code .= " / ";
$code .= "delete";
} else {
$code .= "cancel";
}
$mainMod->setTitleBarSuffix($code);
}
$page->setColumns(array(25,75));
// If the user has more images awaiting captions, link them back to the pictures.
$uploads = getVal($_SESSION, 'albumMaker_uploads', array());
if(count($uploads) > 0){
// STRINGTABLES
$title = "Captions pending...";
$content = "You have more images still waiting for captions...
";
$content.= "When you are done here, you can go back and see the updated list of ";
$content.= "pictures awaiting captions";
$alertMod = new TextModule($content, 'text', $title);
$alertMod->setImage("attention.png");
$page->addModule($alertMod, 0);
}
$page->addModule(new UserLinksModule(), 0);
$page->addModule(new AdModule(), 0);
$page->addModule($mainMod, 1);
return $done;
} // end albumViewer_album
////
// Displays all of the albums for the given user.
////
function albumViewer_user($user, $owner, $picId, $albumId, $offset, $limit){
GLOBAL $page;
$ownerId = $owner->getId();
$ownerName = $owner->getUsername();
$ownerVisible = $owner->getUsername();
$db = dil_connect();
$title = "$ownerVisible's ".STR_PEDLR." Albums";
//print "
$title
\n";
ob_start(); // for the entire contents of the module
print "
\n";
// So that users don't get lost/confused.
if($owner->isSelf()){
print "
\n";
print "HINT: You can upload more photos using the Album Maker";
print "
\n";
}
// Show the default album if there are pictures in it.
$hasDefault = albumSlug($ownerName, $ownerId, DEFAULT_ALBUM_ID, DEFAULT_ALBUM_NAME, DEFAULT_ALBUM_DESC);
$queryString = "SELECT * FROM albums WHERE user_id='$ownerId'";
if($result = mysql_query($queryString,$db)){
$done = true;
if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){
for($cnt=0; $cnt<$numRows; $cnt++){
$albumId = mysql_result($result, $cnt, "id");
$albumName = mysql_result($result, $cnt, "name");
$albumDesc = mysql_result($result, $cnt, "description");
$createdOn = mysql_result($result, $cnt, "createdOn");
$updatedAt = mysql_result($result, $cnt, "updatedAt");
albumSlug($ownerName, $ownerId, $albumId, $albumName, $albumDesc);
}
} else {
// STRINGTABLES - TODO: MAKE A BETTER MESSAGE
if(!$hasDefault){
dispError($owner->getUsername()." doesn't have any photo albums yet. They really should upload some pictures, don't you think?");
}
}
}
$content = ob_get_clean();
$mainMod = new TextModule($content, 'text', $title);
$page->setColumns(array(25,75));
// If the user has more images awaiting captions, link them back to the pictures.
$uploads = getVal($_SESSION, 'albumMaker_uploads', array());
if(count($uploads) > 0){
// STRINGTABLES
$title = "Captions pending...";
$content = "You have more images still waiting for captions...
";
$content.= "When you are done here, you can go back and see the updated list of ";
$content.= "pictures awaiting captions";
$alertMod = new TextModule($content, 'text', $title);
$alertMod->setImage("attention.png");
$page->addModule($alertMod, 0);
}
$page->addModule(new UserLinksModule(), 0);
$page->addModule(new AdModule(), 0);
$page->addModule($mainMod, 1);
return $done;
} // end albumViewer_user()
////
// Displays a slugline about the album.
// Returns true if something was displayed and false otherwise (will only refrain from displaying if this is the default
// album AND there are no pictures in the default album for this user).
////
function albumSlug($ownerName, $ownerId, $albumId, $albumName, $albumDesc){
$retVal = false;
$top = "
\n";
if($albumId == DEFAULT_ALBUM_ID){
// Only show the default album if there are images in it.
ob_start();
$numResults = albumPreview($ownerName, $ownerId, $albumId);
$content = ob_get_clean();
if($numResults > 0){
print $top.$content.$bottom;
$retVal = true;
}
} else {
$retVal = true;
print $top;
albumPreview($ownerName, $ownerId, $albumId);
print $bottom;
}
return $retVal;
} // end albumSlug()
////
// Displays a couple of thumbnails for an album given its id.
// If the albumId is -1 (DEFAULT_ALBUM_ID), then the user_id should also be set, and that will
// return the preview of the default album for the user.
//
// Returns the number of thumbnails shown (this will be capped at 'NUM_TO_SHOW'... about 4).
////
function albumPreview($ownerName, $ownerId, $albumId){
$retVal = 0;
$NUM_TO_SHOW = 4;
// Don't need to check the ownership since the album check was already made.
$db = dil_connect();
if(($albumId == DEFAULT_ALBUM_ID) && ($ownerId != "")){
$queryString = "SELECT * FROM album_pics WHERE album_id='".DEFAULT_ALBUM_ID."' AND user_id=$ownerId ORDER BY createdOn LIMIT $NUM_TO_SHOW";
} else {
$queryString = "SELECT * FROM album_pics WHERE album_id='$albumId' ORDER BY createdOn LIMIT $NUM_TO_SHOW";
}
if($result = mysql_query($queryString,$db)){
if(($numRows = mysql_num_rows($result)) && ($numRows > 0)){
for($cnt=0; $cnt<$numRows; $cnt++){
$picId = mysql_result($result, $cnt, "id");
$caption = mysql_result($result, $cnt, "caption");
$picData[] = array(
INDEX_PIC_ID => $picId,
INDEX_TT_DESC => $caption
);
}
$numCols = 4;
$toolTips = albumTable($picData, $numCols, $ownerId, $ownerName);
if(count($toolTips) > 0){
GLOBAL $page;
$page->addToolTips($toolTips);
}
$retVal = $numRows;
} else {
print "There are no photos in this album yet\n";
}
}
return $retVal;
} // end albumPreview()
?>