need help with a phpnuke block

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
msimonds
Forum Newbie
Posts: 3
Joined: Tue May 20, 2003 4:50 pm

need help with a phpnuke block

Post by msimonds »

this block pulls random pics from all galleries that I have running on my site for the Rant Girl of the month. I was wondering if there was any way to modify this block to pull just the newest/current gallery that is on the site. Here is the code:

I really need some help on this. The more galleries that I will be coming month to month, I will really need this to pull just the current gallery on to my home page/index.php

Code: Select all

<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2001 Bharat Mediratta
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/*
 * This block selects a random photo for display.  It will only display photos
 * from albums that are visible to the public.  It will not display hidden
 * photos.  
 *
 * Once a day (or whatever you set CACHE_EXPIRED to) we scan all albums and
 * create a cache file listing each public album and the number of photos it
 * contains.  For all subsequent attempts we use that cache file.  This means
 * that if you change your albums around it may take a day before this block
 * starts (or stops) displaying them.
 */

// Hack prevention.
if (!empty($HTTP_GET_VARS&#1111;"GALLERY_BASEDIR"]) ||
		!empty($HTTP_POST_VARS&#1111;"GALLERY_BASEDIR"]) ||
		!empty($HTTP_COOKIE_VARS&#1111;"GALLERY_BASEDIR"])) &#123;
	print "Security violation\n";
	exit;
&#125;

$GALLERY_BASEDIR = "/home/rantman/public_html/modules/gallery/";
require($GALLERY_BASEDIR . "init.php");

if ($profile) &#123;
    $timer = time();
&#125;

/* Initializing the seed */
srand ((double) microtime() * 1000000);

define(CACHE_FILE, $gallery->app->albumDir . "/block-random.cache");
define(CACHE_EXPIRED, 86400);

// Check the cache file to see if it's up to date
$rebuild = 1;
if (fs_file_exists(CACHE_FILE)) &#123;
    $stat = fs_stat(CACHE_FILE);
    $mtime = $stat&#1111;9];
    if (time() - $mtime < CACHE_EXPIRED) &#123;
	$rebuild = 1;
    &#125;
&#125;

if ($rebuild) &#123;
    scanAlbums();
    saveCache();
&#125; else &#123;
    readCache();
&#125;

$album = chooseAlbum();

if ($album) &#123;
    $index = choosePhoto($album);
&#125;

if (isset($index)) &#123;
    $id = $album->getPhotoId($index);
    $url = "http://www.sportsrant.com/";
    $url .= "modules.php?set_albumName=".$album->fields&#1111;"name"];
    $url .= "&id=" .$id;
    $url .= "&op=modload&name=gallery&file=index&include=view_photo.php";
//    echo"$url";

    echo "<center>"
	    ."<a href="$url">"
//	    ."<a href=" .makeAlbumUrl($album->fields&#1111;"name"], $id) .">"
	    .$album->getThumbnailTag($index)
	    ."</a></center>";
    
    $caption = $album->getCaption($index);
    if ($caption) &#123;
	echo "<br><center>$caption</center>";
    &#125;
    
    $albumURL = "http://www.sportsrant.com/";
    $albumURL .= "modules.php?set_albumName=".$album->fields&#1111;"name"];
    $albumURL .= "&op=modload&name=gallery&file=index&include=view_album.php";
    echo "<br><center>From: "
            ."<a href="$albumURL">"
//	    ."<a href=" .makeAlbumUrl(currentAlbum) .">"
	    .$album->fields&#1111;"title"]
	    ."</a></center>";
&#125; else &#123;
	print "No photo chosen.";
&#125;

if ($profile) &#123;
    $elapsed = time() - $timer;
    print "<br>Elapsed: $elapsed secs";
&#125;

/*
 * --------------------------------------------------
 * Support functions
 * --------------------------------------------------
 */

function saveCache() &#123;
    global $cache;
    if ($fd = fs_fopen(CACHE_FILE, "w")) &#123;
	foreach ($cache as $key => $val) &#123;
	    fwrite($fd, "$key/$val\n");
	&#125;
	fclose($fd);
    &#125;
&#125;

function readCache() &#123;
    global $cache;
    if ($fd = fs_fopen(CACHE_FILE, "r")) &#123;
	while ($line = fgets($fd, 4096)) &#123;
	    list($key, $val) = explode("/", $line);
	    $cache&#1111;$key] = $val;
	&#125;
	fclose($fd);
    &#125;
&#125;

function choosePhoto($album) &#123;
    global $cache;

    $count = $cache&#1111;$album->fields&#1111;"name"]];
    if ($count == 0) &#123;
	// Shouldn't happen
	return null;
    &#125; else if ($count == 1) &#123;
	$choose = 1;
    &#125; else &#123;
	$choose = rand(1, $count);
	$wrap = 0;
	if ($album->isHidden($choose)) &#123;
	    $choose++;
	    if ($choose > $album->numPhotos(1)) &#123;
		$choose = 1;
		$wrap++;

		if ($wrap = 2) &#123;
		    return null;
		&#125;
	    &#125;
	&#125;
    &#125;

    return $choose;
&#125;

function chooseAlbum() &#123;
    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) &#123;
	if (!$choose) &#123;
	    $choose = $name;
	&#125;
	
	$total += $count;
	if ($total != 0 && ($total == 1 || rand(1, $total) <= $count)) &#123;
	    $choose = $name;
	&#125;
    &#125;

    if ($choose) &#123;
	$album = new Album();
	$album->load($choose);
	return $album;
    &#125; else &#123;
	return null;
    &#125;
&#125;

function scanAlbums() &#123;
    global $cache;
    global $gallery;

    $cache = array();
    $everybody = $gallery->userDB->getEverybody();
    $albumDB = new AlbumDB();
    foreach ($albumDB->albumList as $tmpAlbum) &#123;
	if ($everybody->canReadAlbum($tmpAlbum)) &#123;
	    $seeHidden = $everybody->canWriteToAlbum($tmpAlbum);
	    $numPhotos = $tmpAlbum->numPhotos($seeHidden);
	    $name = $tmpAlbum->fields&#1111;"name"];
	    if ($numPhotos > 0) &#123;
		$cache&#1111;$name] = $numPhotos;
	    &#125;
	&#125;
    &#125;
&#125;
?>

can someone help me out to see if it is possible to just make this -1/or the current gallery instead of all galleries

Thanks
Mike
Post Reply