app->albumDir . "/block-random.dat"); define('CACHE_EXPIRED', $gallery->app->blockRandomCache); /* * Declare some globals, in case this source is being include()'ed, which * would then make the scope of these vars the same as the point of inclusion. * */ global $global_album_name; global $everybody; global $skipSubAlbums; // Check the cache file to see if it's up to date $rebuild = 1; if (fs_file_exists(CACHE_FILE)) { $stat = fs_stat(CACHE_FILE); $mtime = $stat[9]; if ((time() - $mtime) < CACHE_EXPIRED) { $rebuild = 0; } } // Only choose albums that Everybody can access $everybody = $gallery->userDB->getEverybody(); if ($rebuild) { scanAlbums(); saveCache(); } else { readCache(); } /** * Parameter: album * Limit the photo block to one album (plus its sub-albums). * If not specified, an album will be randomly selected from the cache file. * * Example: album=album01 * Default: none. */ if (!empty($HTTP_GET_VARS["album"])) { $rand_album = new Album(); $rand_album->load($HTTP_GET_VARS["album"]); $skipSubAlbums = false; // The album specified is the top of a "tree" of eligible albums } else { $rand_album = chooseAlbum(); $skipSubAlbums = true; // Only pick a real photo from the randomly-chosen album, not any sub-albums } if (!empty($rand_album)) { $global_album_name = $rand_album->fields['name']; $save_album_name = $rand_album_name; // Recursion can cause album object to have been changed $index = choosePhoto($rand_album); // And if it has been changed, then load the new album if (strcmp($global_album_name, $save_album_name) <> 0 ) { $rand_album->load($global_album_name); } } /** * Parameter: size * Specify that the thumbnail image should be resized when displayed, * to fit a specific dimension in both height and width. Aspect ratio is maintained. * Intended to reduce the size of images, but if the size specified is larger than * either thumbnail dimension, the image will be enlarged, with a corresponding loss of quality. * Note: This does not create an additional thumbnail, nor does it alter the existing thumbnail; * the image is dynamically scaled by the browser when displayed. * * Example: size=75 * Default: 0, meaning no resize. */ if (!empty($HTTP_GET_VARS["size"])) { $size = $HTTP_GET_VARS["size"]; } else { $size = 0; } /** * Parameter: textclass * Specifies the name an existing CSS class to be used to style the caption text. * * Example: textclass=name_of_css_class * Default: caption */ if (!empty($HTTP_GET_VARS["textclass"])) { $captionclassname = $HTTP_GET_VARS["textclass"]; } else { $captionclassname = "caption"; } /** * The following parameters are intended for use when * using an IFRAME to display your random block(s). */ /** * Parameter: domain * Specify a value to which document.domain should be set. * Only needed if the random block is in an IFRAME, and that page and your Gallery are on * different servers (but the two servers must be share at least a second-level domain, though, * e.g. "www.foo.com" and "gallery.foo.com"). * The calling page must use set its document.domain to this value. * * Example: domain=foo.com * Default: none. If not specified, no document.domain statement will be executed. */ if (!empty($HTTP_GET_VARS["domain"])) { $domain = $HTTP_GET_VARS["domain"]; } /** * Parameter: target * Specify the value to be used in the target= attribute of the A tag wrapping the random image. * Used when the random block is in an IFRAME, to "break out" of the IFRAME when clicking on the image. * * Example: target=_top * Default: none. If not specified, no target will be specified for the A tags. */ if (!empty($HTTP_GET_VARS["target"])) { $targetAttr = "target=\"".$HTTP_GET_VARS["target"] ."\""; } else { $targetAttr = ""; } /** * Parameter: body * Specifies with a value of 1 that the random block should be wrapped in a complete * HTML page of its own, with a BODY tag. This page will also include a HEAD section with * a reference to the style sheet specified in Gallery for the specific album from * which the random image is chosen. Normally only used within an IFRAME. * * Example: body=1 * Default: 0 */ if (!empty($HTTP_GET_VARS["body"])) { $doBody = ($HTTP_GET_VARS["body"] == "1"); } else { $doBody = false; } if ($doBody) { echo "" .getStyleSheetLink(); echo ""; if ($domain) { echo ""; } echo ""; } echo "
"; if (isset($index)) { $id = $rand_album->getPhotoId($index); echo "fields["name"], $id) ."\" $targetAttr>" .$rand_album->getThumbnailTag($index,$size) . ""; $caption = $rand_album->getCaption($index); echo ""; if ($caption) { echo "
$caption"; } echo "
From: " ."fields["name"]) ."\" $targetAttr>" .$rand_album->fields["title"] ."
"; } else { // No photo chosen. Provide link to album itself, otherwise to top-level of gallery. if ($rand_album) { echo "fields["name"]) ."\" $targetAttr>" .$rand_album->getHighlightAsThumbnailTag($size) ."
" ."fields["name"]) ."\" $targetAttr>" .$rand_album->fields["title"] ."
"; } else { print "" . $gallery->app->galleryTitle . ""; } } echo "
"; if ($doBody) { echo ""; } /* * -------------------------------------------------- * Support functions * -------------------------------------------------- */ function saveCache() { global $cache; safe_serialize($cache, CACHE_FILE); } function readCache() { global $cache; $sCache = getFile(CACHE_FILE); $cache = unserialize($sCache); } function choosePhoto($rand_album) { global $global_album_name; // echo "choosePhoto(".$rand_album->fields["name"].")
"; $count = $rand_album->numPhotos(1); // echo "count = ".$count."
"; if ($count == 0) { // Shouldn't happen return null; } else { $choose = mt_rand(1, $count); $wrap = 0; while ( !isEligibleItem($rand_album, $choose) ) { $choose++; if ($choose > $count) { $choose = 1; $wrap++; if ($wrap == 2) { return null; } } } } /** * If we've picked a sub-album, then * make it the chosen album, and * recursively choose a photo from *it* */ if ( $rand_album->isAlbum($choose) ) { $global_album_name = $rand_album->getAlbumName($choose); $rand_album->load($global_album_name); // echo "recurse
"; return choosePhoto($rand_album); } // echo "chose".$choose.")
"; return $choose; } function chooseAlbum() { global $cache; /* * The odds that an album will be selected is proportional * to the number of (visible) items in the album. */ $total = 0; foreach ($cache as $name => $count) { if (empty($choose)) { $choose = $name; } $total += $count; if ($total != 0 && ($total == 1 || mt_rand(1, $total) <= $count)) { $choose = $name; } } if ($choose) { $alb = new Album(); $alb->load($choose); return $alb; } else { return null; } } function scanAlbums() { global $cache; global $gallery; global $everybody; $cache = array(); $albumDB = new AlbumDB(); foreach ($albumDB->albumList as $tmpAlbum) { if ($tmpAlbum->canReadRecurse($everybody->getUid()) && !$tmpAlbum->isHiddenRecurse()) { $seeHidden = $everybody->canWriteToAlbum($tmpAlbum); $numPhotos = $tmpAlbum->numPhotos($seeHidden); $name = $tmpAlbum->fields["name"]; if ($numPhotos > 0) { $cache[$name] = $numPhotos; } } } } /** * Is an item eligible to be returned as a random photo? * Not if it's a hidden item. Nor if it's a sub-album, unless we're being * asked to limit our selection to a single album and its sub-albums. * In that case, only albums that everyone can read are eligible. */ function isEligibleItem($alb, $idx) { global $everybody; global $skipSubAlbums; $isSubAlbum = $alb->isAlbum($idx); // echo "isEligibleItem(".$alb.",".$idx.")
"; if ( $isSubAlbum ) { // echo $alb."[".$idx."].isSubAlbum=TRUE
"; if ($skipSubAlbums) { return false; } else { $tmpAlbum = new Album(); $tmpAlbum->load($alb->getAlbumName($idx)); // echo "subAlbum:".$tmpAlbum." isEligible=".($tmpAlbum->canReadRecurse($everybody->getUid()) && !$tmpAlbum->isHiddenRecurse())."
"; return ($tmpAlbum->canReadRecurse($everybody->getUid()) && !$tmpAlbum->isHiddenRecurse()); } } // echo "Not subAlbum. isEligible=".!($alb->isHidden($idx))."
"; return !($alb->isHidden($idx)); } ?>