<?php



// CGI parameters
//    dir .... subdir with images
//    album .. photo album (use gallery extension)

$dir = 'images';
if (isset($_REQUEST['dir'])) $dir=$_REQUEST['dir'];

// album specified ... read picture list from album

if (isset($_REQUEST['album'])){
  $album = $_REQUEST['album'];
  if (file_exists('scripts/sb_gallery.php')){
    include_once('scripts/sb_gallery.php');
    include_once "scripts/sb_gallery_config.php";
    $files = getPictureList($album);
    $dir = makeAlbumPath($album);
  }
}


// otherwise: read files from dir

elseif (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (false !== ($filename = readdir($dh))) {
      $tempArray = explode(".", $filename);
      /* Add image file types in the code below */
      if($tempArray[1] == "jpg" || $tempArray[1] == "JPG" ||
	 $tempArray[1] == "jpeg" ||  $tempArray[1] == "JPEG" ||
	 $tempArray[1] == "gif" || $tempArray[1] == "GIF" ||
	 $tempArray[1] == "png" ||
	 $tempArray[1] == "tif" || 
	 $tempArray[1] == "bmp"){
	$files[] = $filename;
      }
    }
  }
}


/* Generate a random number */
$nooffildi = count($files);
$nooffiles = ($nooffildi-1);
srand((double)microtime()*1000000);
$randnum = rand(0,$nooffiles);

if (isset($_REQUEST['album'])){
  $pic = getPictureURL($album,$files[$randnum]);
  if (isset($_REQUEST['thumb'])){
    include_once "scripts/sb_thumbnails.php";
    if (validThumbnailExists($album.'/'.$files[$randnum])){
      $pic=getThumbnailURL($album.'/'.$files[$randnum]);
    }
  }
  elseif (validScaledExists($pic)){
    $pic=getScaledURL($pic);
  }
  $files[$randnum]=basename($pic);
  $dir=dirname($pic);
}

header("Content-Type: image/gif");
header("Content-Transfer-Encoding: binary");
if (is_array($files)){
    if (is_file($files[$randnum])){
	readfile("$dir/$files[$randnum]");
    }
}

?>
