<?php

session_start();

// include gallery configuration
//--------------------------------

include_once "scripts/sb_gallery_config.php";
include_once "scripts/sb_gallery.php";

//////////////////////////////////////////////////////////////////////////
// Load/Include Functions
include_once('scripts/sb_functions.php');
include_once "scripts/sb_events.php";


setupStickyURLOptions();
setGlobal("album");
setGlobal("page");
setGlobal("pic");
setGlobal("thumb");
setGlobal("rotate");
setGlobal("filter");


$PicYear = 'xx';       // this is a dummy date to store blog entries
$PicMonth = 'xx';      // when voting or commenting on photos
$PicDay = 'xx';        // from the gallery


if ( !file_exists( $album_directory ) ) {
    $oldumask = umask( 0 );
    $ok = my_mkdir( $album_directory, 0777 );
    umask( $oldumask );
}
if ( !file_exists( $data_directory ) ) {
    $oldumask = umask( 0 );
    $ok = my_mkdir( $data_directory, 0777 );
    umask( $oldumask );
}


if(!isset($album)){
    $album = "";
}
$album_path = $album=="" ? $album_directory : "$album_directory/$album"; 
// Set a standard album to start from, if none has been
// passed over as an URL-parameter






global $logged_in, $signed_in;
$logged_in = logged_in( false, true );

// if ($logged_in){
if ($signed_in){
  @ini_set(memory_limit, "100M");
  include_once "scripts/sb_gallery_admin.php";
}

read_config();
	
sb_language( 'index' );
sb_language( 'gallery' );


global $lang_string;

// display GD error, help folks with lame hosting companies 
// or misconfigured
// servers and prevents an ugly display of errors
if (!function_exists('imagejpeg') && $gd_available != 0) {
    echo $lang_string[ 'gd_warning' ];
    exit;
}



///////////////////////////////////////////////////////////////////////
//
//  create new albums
//
///////////////////////////////////////////////////////////////////////

if ($_REQUEST['vaclose']){
  close_virtual_album();
}
//if ($logged_in){
if ($signed_in){

    if ($logged_in || ($signed_in && $blog_permissions['edit_album'])){
	if (isset($_REQUEST["vaopen"])){
	    open_virtual_album($_REQUEST['vaopen']);
	}
    }
    if ($logged_in || ($signed_in && $blog_permissions['edit_album'])){
	if (isset($_REQUEST["varemove"])){
	    $file=virtual_album_file($album);
	    if (file_exists($file)){
		if (unlink($file)){
		    $album=dirname($album);
		    unset($_SESSION['valbum']);
		}
	    }
	}
    }

    if ($logged_in || ($signed_in && $blog_permissions['create_album'])){

	if (isset($_POST["newalb"])){
	    global $album_directory;
	    if (!file_exists($album_directory.'/'.$_POST['album'].'/'.$_POST['subalbum'])){
		open_virtual_album($_POST['album'].'/'.$_POST['subalbum']);
	    }
	}

	if (isset($_POST["newsub"])){
	    if (create_album($_POST['album'],$_POST['subalbum'])){
		$album=$_POST['album'].'/'.$_POST['subalbum'];
	    }
	}
    }


  /*
  if (isset($_POST["send"])){
    if (isset($_POST["email"])){
      if (email_pic($_POST["email"],
		    $_POST["album"],
		    $_POST["pic"],
		    $_POST["subject"])){
	echo "success!";
      }
      else{
	echo "failure!";
      }
    }
  }
  */
}

///////////////////////////////////////////////////////////////////////
//
//  upload picture
//
///////////////////////////////////////////////////////////////////////

if ($signed_in && $blog_permissions['upload'] && isset($_POST["upload"])){
    upload_picture();
}



/////////////////////////////////////////////////////////////////
// comment mode or vote mode: create blog entries if necessary!
//
// this is to allow user comments and user votes 
// for ALL pictures in the album
/////////////////////////////////////////////////////////////////

setGlobal("post");
global $post;
if (isset($post)){

  if (($post=='comment') && ($blog_config[ 'blog_enable_comments' ] == true) ||
      ($post=='rate') && ($blog_config[ 'blog_enable_voting' ] == true)){

    global $pic;
    global $album;


    $thispic=$pic;
    $thisalbum=$album;
    if (is_virtual_album($album)){
      $pics = getPictureList($album);
      $thispic = basename($pics[$pic]);
      $thisalbum = dirname($pics[$pic]);
    }

    // if there is already a pic-blog-entry: 
    //     rating-mode --> add a vote
    //     comment mode --> re-direct

    if ($entry = pic_entry($thisalbum,$thispic)){
      $file = array_pop($entry);
      $m = array_pop($entry);
      $y = array_pop($entry);
      if ($post == 'rate'){
	$rating = false;
	for ($star=1; $star <= 5; $star++){
	  if (array_key_exists('rating'.$star,$_POST)){
	    $rating = $star;
	    break;
	  }
	  if (array_key_exists('rating'.$star.'_x',$_POST)){
	    $rating = $star;
	    break;
	  }
	}
	// setGlobal("rating");
	// global $rating;
	vote($y,$m,$file,$rating);
      }
      else{
	$url = 'comments.php?y='.$y.'&m='.$m.'&entry='.$file;
	redirect_to_url( $url );
      }
    }

    // no blog entry exists --> create one

    else{

      global $album_directory;

      $img=$album_directory.'/'.$thisalbum.'/'.$thispic;
      $scaled=getScaledURL($img);
      $thumb=getThumbnailURL($thisalbum.'/'.$thispic);

      if (file_exists($scaled)){
	$img=$scaled;
      }
      
      $subject = ($pos=strrpos($thispic,"/")) ? substr($thispic,$pos+1) : $thispic;
      $text = '[html]<div align="center">[/html]';
      $src=str_replace(' ','%20',$img);
      $text .= '[img=' . $src . ' popup=false]';
      $text .= '[html]</div>[/html]';

      $category = array();

//      $ok = write_entry( stripslashes( $subject ), 
//			 stripslashes( $text ), 
//			 '', false, $category );

      $entry = write_event( stripslashes( $subject ),   // subject line
			    stripslashes( $text ) ,     // entry text
			    '',                         // no pings
			    $category,                  // categories (empty)
			    $PicYear,$PicMonth,$PicDay, // use a dummy date
			    '00','00','00',             // dummy time
			    '--');                      // do not repeat

      if ( $entry ){

	  $y = $PicYear;
	  $m = $PicMonth;
//      $y = date('y');
//      $m = date('m');
//      $stamp = date('ymd-His');
//      save_pic_entry($img,$y,$m,'entry'.$stamp);

	  save_pic_entry($thumb,$y,$m,$entry);


	  if ($post == 'rate'){
	    setGlobal("rating");
	    global $rating;
	    vote($y,$m,$entry,$rating);
	  }
	  else{
	    $url = 'comments.php?y='.$y.'&m='.$m.'&entry='.$entry;
	    redirect_to_url( $url );
	  }
#	  redirect_to_url( $url );
      }
    }
  }
}


function vote($y,$m,$entry,$vote){
  $rating = intval( $vote );
  if ( $rating >= 1 && $rating <= 5 ) {		
    if ( strpos( $y, array( "/", ".", "\\", "%" ) ) === false && 
	 strlen( $y ) == 2 &&
	 strpos( $m, array( "/", ".", "\\", "%" ) ) === false && 
	 strlen( $m ) == 2 &&
	 strpos( $entry, array( "/", ".", "\\", "%" ) ) === false && 
	 strlen( $entry ) == 18 ) {
      if ( entry_exists( $y, $m, $entry ) ) {
	write_rating( $y, $m, $entry, $rating );
      }
    }
  }
}


//////////////////////////////////////////////////////////////////////////
// Start HTML Page

global $gallery_style;
$extra_header = $gallery_style;

if (isset($_REQUEST['slideshow'])){
  $SLIDEREFRESH=$_REQUEST['slideshow'];
  $slideshow = true;
  $pics = getPictureList($album);
  if (count($pics)>0){
    $nr = 0;
    if (isset($_REQUEST['idx'])){
      $nr=$_REQUEST['idx'];
    }
    $next = $nr;
    $next++;
    if ($next>=count($pics)){
      $next = 0;
    }
    global $pic;
    if (is_virtual_album($album)){
      $pic = $nr;
    }
    else{
      $pic = $pics[$nr];
    }
    $extra_header .= '<meta http-equiv="refresh" content="';
    $extra_header .= $SLIDEREFRESH;
    $extra_header .= '; URL=?album='.$album;
    $extra_header .= '&amp;idx='.$next;
    if (isset($_REQUEST['big'])) $extra_header .= '&amp;big';
    $extra_header .= '&amp;slideshow='.$SLIDEREFRESH;
    $extra_header .= '">';
  }
}


html_header($extra_header,false);




function page_content() {


    global $page;
    global $pic;
    global $album;
    global $album_directory;
    global $rotate;
    global $filter;
    global $logged_in;

    global $blog_config;
    global $lang_string;
    global $signed_in,$blog_permissions;

    if (!isset($page)){$page=1;}


//    if ($logged_in){
    if (isset($_POST["send"])){
	if ($signed_in && $blog_permissions['send_album']){
	    if (isset($_POST["email"])){
		email_pic($_POST["email"],
			  $_POST["album"],
			  $_POST["pic"],
			  $_POST["subject"]);
	    }
	}
    }

    if (isset($_POST["protect"])){
	if ($signed_in && $blog_permissions['setup']){
	    if ($_POST['password'] === ''){
		if (release_album($_POST['album'])){
		    $string = $lang_string[ 'password_removed' ];
		    $string = str_replace('%s',$album,$string);
		    echo $string;
		    echo '<hr />';
		}
	    }
	    elseif (protect_album($_POST['album'],$_POST['password'])){
		$string = $lang_string[ 'password_set' ];
		$string = str_replace('%s',$album,$string);
		echo $string;
		echo '<hr />';
	    }
	}
    }
    if (isset($_POST["blog_album_style"])){
	if ($signed_in && $blog_permissions['setup']){
	  set_album_style($album,$_POST["blog_album_style"]);
	}
    }
    if ($_POST['change'] && $_POST['description']){
      if ($signed_in && $blog_permissions['setup']){
	if (isset($pic)){
	  set_pic_description($album,$pic,$_POST['description']);
	}
	else{
	  set_album_description($album,$_POST['description']);
	}
      }
    }

    if (album_is_protected($album)){

	// echo "album is protected! login!";

	if ($_POST['password']){
	  $_SESSION['album_password'] = $_POST['password'];
	}
	if (!check_album_password($album,$_SESSION['album_password'])){
	  echo album_password_form($album);
	  return true;
	}
    }

    $entry_array = array();


    ///////////////////////////////////////////////////////////////////////
    // path navigation

    $entry_array['subject'] .= '<table width="100%"><tr><td valign="top">';
    if ($logged_in){
	$entry_array['subject'] .= '<a href="filemanager.php?dir=';
	$entry_array['subject'] .= $album_directory.'/'.$album.'">';
	$entry_array['subject'] .= '<img src="filemanager.php?image=folder" alt="files" /></a> ';
    }

    $url=makeURL("",array('album' => $album,'albumtree' => 1));
    $entry_array['subject'] .= "<a href='$url'>[+] </a>";

    //    $url=makeURL("",array('show'=>'recent'));
    //    $entry_array['subject'] .= "<a href='$url'>[".$lang_string['new_pictures']."] </a>";
    //    $url=makeURL("",array('show'=>'popular'));
    //    $entry_array['subject'] .= "<a href='$url'>[".$lang_string['popular_pictures']."] </a>";

    $url=makeURL("",array("album"=>"","page"=>"1"));
    $entry_array['subject'] .= "<a href='$url'>".$lang_string['album']."</a>";

    $elements = explode("/", $album);
    $album_path = "";
    foreach($elements as $value){
	if($value != ""){
	    $album_path = "$album_path$value";
	    $url = makeURL("",array("album"=>"$album_path","page"=>"1"));
	    $entry_array['subject'] .=
		" / <a class='locationlink' href='$url'>$value</a>";
	    $album_path .= '/';
	}
    }

    if ($signed_in && $blog_permissions['create_album']){
      if (!isset($_SESSION['valbum'])){
 	if (is_virtual_album($album)){
	  $entry_array['subject'] .= ' [ <a href="?page=1&amp;vaopen='.$album.'&amp;album='.$album.'">edit</a> ]';
	}
	else{
	  $entry_array['subject'] .= ' <form style="display: inline" action="gallery.php" method="post">';
	  $entry_array['subject'] .= '<input type="hidden" name="album" value="'.$album.'">';
	  $entry_array['subject'] .= '<input name="subalbum" size="5" type="text">';
	  $entry_array['subject'] .= '<input name="newalb" type="submit" value="'.$lang_string['make_album_btn'].'">';
	  $entry_array['subject'] .= '<input name="newsub" type="submit" value="'.$lang_string['make_subdir_btn'].'">';
	  $entry_array['subject'] .= '</form>';
	}
      }
      else{
	$entry_array['subject'] .= ' [ <a href="?page=1&amp;vaclose='.$_SESSION['valbum'].'&amp;album='.$album;
	$entry_array['subject'] .= '">'.$lang_string['stop_editing'].'</a> ]';
      }
    }
    $entry_array['subject'] .= '</td>';

    // add slide-show buttons
    $entry_array['subject'] .= "<td align='right' valign='top'>";
    $entry_array['subject'] .= slideshow_buttons($album,$pic);
    $entry_array['subject'] .= "</td>";

    $entry_array['subject'] .= '</tr></table>';

    /*
      if ($_REQUEST['sth']){$_SESSION['THUMB_SIZE']='small';}
      if ($_REQUEST['mth']){$_SESSION['THUMB_SIZE']='medium';}
      if ($_REQUEST['bth']){$_SESSION['THUMB_SIZE']='big';}

      $imgdir = "/interface/gallery/";

      // small
      $entry_array['subject'] .= '<div class="right">';
      $url = makeURL("",array("album" => "$album_path",
			      "page"  => "1",
			      "sth"   => "1"));
      $entry_array['subject'] .="<a class='locationlink' href='$url'>";

      if ($_SESSION['THUMB_SIZE'] == 'small'){
	$img=$imgdir.'zoom1on.gif';
      }
      else{$img=$imgdir.'zoom1off.gif';}
      $entry_array['subject'] .= '<img src="'.$img.'" border="0" /></a>';

      // medium
      $url = makeURL("",array("album" => "$album_path",
			      "page"  => "1",
			      "mth"   => "1"));
      $entry_array['subject'] .="<a class='locationlink' href='$url'>";

      if ($_SESSION['THUMB_SIZE'] == 'medium'){
	$img=$imgdir.'zoom2on.gif';
      }
      else{$img=$imgdir.'zoom2off.gif';}
      $entry_array['subject'] .= '<img src="'.$img.'" border="0" /></a>';

      // big (default)
      $url = makeURL("",array("album" => "$album_path",
			      "page"  => "1",
			      "bth"   => "1"));
      $entry_array['subject'] .="<a class='locationlink' href='$url'>";

      if ($_SESSION['THUMB_SIZE'] == 'big'){
	$img=$imgdir.'zoom3on.gif';
      }
      else{$img=$imgdir.'zoom3off.gif';}
      $entry_array['subject'] .= '<img src="'.$img.'" border="0" /></a>';

      $entry_array['subject'] .= '</div>';

    }
    */


    // links for sub-directories

    $dir = "$album_directory/$album_path";

    $subdir = array();
    if (is_dir($dir)){
      if ($fd = opendir($dir)){
	$i = 0;
	while($file = readdir($fd)){
	  if($file != "." && $file != ".." && 
	     is_dir("$dir/$file") && ($file != 'CVS')) {
	    $subdir[$i] = $file;
	    $i = $i + 1;
	  }
	  elseif (($valbum = is_virtual_album_file($file)) != ""){
	    $subdir[$i] = $valbum;
	    $i = $i + 1;
	  }
	}
	closedir($fd);
      }
    }
    sort($subdir, SORT_STRING);
    reset($subdir);


    if (count($subdir)){

      $url=makeURL("",array("album"=>$album,"subalbums"=>"1"));

      $entry_array['entry'] .= '<a href="'.$url.'"><img border="0" src="interface/gallery/folder.png" alt="subdirs" /></a>';
      for($i = 0; $i < count($subdir); $i++){
	$url=makeURL("",array("album"=>"$album_path$subdir[$i]","page"=>"1"));
	$entry_array['entry'] .= " [<a href='$url'>$subdir[$i]</a>]";
      }
      $entry_array['entry'] .= '<br /><hr />';
    }

    echo "<div align='center'>";


    ///////////////////////////////////////////////////////////////////////
    //
    // add pictures to virtual albums
    //
    ///////////////////////////////////////////////////////////////////////

    if(isset($_SESSION['valbum']) && isset($_REQUEST['add'])){
	if ($signed_in && $blog_permissions['edit_album']){

	    if (add_to_virtual_album($_SESSION['valbum'],
				     $album.'/'.$_REQUEST['add'])){
		echo 'Picture '.$_REQUEST['add'];
		echo ' successfully added to '.$_SESSION['valbum'].'<br />';
	    }
	}
    }

    ///////////////////////////////////////////////////////////////////////
    //
    // delete pictures from virtual albums
    //
    ///////////////////////////////////////////////////////////////////////

    if(isset($_SESSION['valbum']) && isset($_REQUEST['del'])){
	if ($signed_in && $blog_permissions['edit_album']){
	    remove_from_virtual_album($_SESSION['valbum'],
				      $_REQUEST['idx'],
				      $_REQUEST['del']);
	}
    }


    ///////////////////////////////////////////////////////////////////////
    //
    // show
    //
    //  - most viewed pictures
    //  - recent albums
    //
    ///////////////////////////////////////////////////////////////////////


    if (isset($_REQUEST["show"])){
      if ($_REQUEST["show"] === "popular"){
	$entry_array['entry'] = '<h4>Most viewed pictures</h4><p>';
	$entry_array['entry'] .= mostViewed(20);
      }
      if ($_REQUEST["show"] === "recent"){
	$entry_array['entry'] = '<h4>Most recently modified albums</h4><p>';
      $entry_array['entry'] .= 
	listdir_recent($album_directory,'gallery.php?page=1&amp;album=',20,0);
      }
    }

    ///////////////////////////////////////////////////////////////////////
    //
    // show the album tree
    //
    ///////////////////////////////////////////////////////////////////////

    elseif(isset($_REQUEST['albumtree'])){
      //$entry_array['entry'] .= '<table width="100%"><tr><td>';
      $root_dir = $album_directory.'/'.$album;
      $link = 'gallery.php?page=1&amp;album=';
      if ($album != ''){
	$link .= $album.'/';
      }
      $entry_array['entry'] .= listdir($root_dir,$link,0);
      //	listdir($album_directory,'gallery.php?page=1&amp;album=',0);
      //$entry_array['entry'] .= '</td><td valign="top">';
      //$entry_array['entry'] .= mostViewed(10);
      //$entry_array['entry'] .= '</td></tr></table>';

    }

    ///////////////////////////////////////////////////////////////////////
    //
    // show sub-albums
    //
    ///////////////////////////////////////////////////////////////////////

    elseif(isset($_REQUEST['subalbums'])) {
	if ($signed_in && $blog_permissions['upload'] && 
	    !is_virtual_album($album)){
	  $entry_array['entry'] .= upload_pic_form($album);
	}
	$entry_array['entry'] .= '<div align="center">';
	$entry_array['entry'] .= subalbum($album_directory,$album,$page);
	$entry_array['entry'] .= '</div>';
    }


    ///////////////////////////////////////////////////////////////////////
    //
    // show pictures 
    //
    ///////////////////////////////////////////////////////////////////////

    elseif(isset($pic)) {
      // picture edit functions when logged in
      if ($logged_in){
	if (isset($rotate)){
	  include_once('scripts/sb_image_edit.php');
	  rotate($album_directory,$album,$pic,$rotate);
	}
	if (isset($filter)){
	  include_once('scripts/sb_image_edit.php');
	  filter($album_directory,$album,$pic,$filter);
	}
      }
      $entry_array['entry'] .= '<div align="center">';
      $entry_array['entry'] .= picture_page($album_directory,$album,$pic);
      $entry_array['entry'] .= '</div>';


      //----------------------------------------------------------------
      // voting and comments!
      //----------------------------------------------------------------

      if (( $blog_config[ 'blog_enable_comments' ] == true ) ||
	  ( $blog_config[ 'blog_enable_voting' ] == true )) {

	$thispic=$pic;
	$thisalbum=$album;
	if (is_virtual_album($album)){
	  $pics = getPictureList($album);
	  $thispic = basename($pics[$pic]);
	  $thisalbum = dirname($pics[$pic]);
	}

	$postexists = false;

	if ($blogentry = pic_entry($thisalbum,$thispic)){
	  $entry=array_pop($blogentry);
	  $m=array_pop($blogentry);
	  $y=array_pop($blogentry);
	  $postexists = true;
	}
      }

      //----------------------------------------------------------------
      // link to enable comments
      // 1) check if there's a file with the comments-URL (for posted pics)
      //    --> add the link

      if ( $blog_config[ 'blog_enable_comments' ] == true ) {

	// a picture post exists --> check entry comments
	// (this is a copy from scipts/sb_display.php

	if ($postexists){

	  if ( $logged_in || 
	       ($blog_config[ 'blog_enable_comments' ] == true) ||
	       ($signed_in && $blog_permissions['comment'])){
	    $comment_array = entry_comment_links($y,$m,$entry);
	  }
	  else{
	    $comment_array = entry_comment_nolinks($y,$m,$entry);
	  }
	  if ($comment_array != false){
	    $entry_array['comment']=$comment_array;
	  }
	  $entry_comments= read_comments( $y, $m, $entry, $logged_in );
	}

	// no picture post exists yet:
	// make a link to the gallery.php script to add posts

	else{
	  $url='?pic='.urlencode($pic).'&amp;album=';
	  $url.=urlencode($album).'&amp;post=comment';
	  $entry_array['comment']['name'] = $lang_string['sb_add_comment_btn'];
	  $entry_array['comment']['url'] = $url;
	}
      }


      //----------------------------------------------------------------
      // link to enable ratings

	if ( $blog_config[ 'blog_enable_voting' ] == true ) {

	  if ($postexists){
	    $rating_array = read_rating( $y, $m, $entry );
	  }
	  if ( $rating_array ) {
	    $points = $rating_array[ 'points' ];
	    $votes = $rating_array[ 'votes' ];
	    $rating = $points / $votes / 5;
	  } else {
	    $points = 0;
	    $votes = 0;
	    $rating = 0;
	  }
						
	  global $blog_theme;
	  $str = '';
	  // no line breaks in forms
	  // check http://weblogs.macromedia.com/cantrell/archives/2003/05/git_rid_of_anno.cfm
	  // css options:
	  // form {margin: 0px 0px 0px 0px; }
	  // form { margin: 0px; padding: 0px; }
	  // form {margin: 0px; padding: 0px; display: inline;}
	  // or the display style in the following: ...
	  $str.='<form style="display: inline" method="post" action="gallery.php">';
	  $str .= '<input type="hidden" name="post" value="rate" />';
	  $str .= '<input type="hidden" name="album" value="'.$album.'" />';
	  $str .= '<input type="hidden" name="pic" value="'.$pic.'" />';

	  for ( $star_number = 1; $star_number <= 5; $star_number++ ) {
	    $temp_ratio = ( $star_number / 5 );
	    if ( $rating >= ( $temp_ratio - .2 ) && 
		 $rating < ( $temp_ratio - .1 ) ) {
	      $star_image = 'no_star.png';
	    } else if ( $rating >= ( $temp_ratio - .1 ) && 
			$rating < $temp_ratio ) {
	      $star_image = 'half_star.png';
	    } else if ( $rating >= $temp_ratio ) {
	      $star_image = 'full_star.png';
	    } else {
	      $star_image = 'no_star.png';
	    }


	    // 2005-04-22: do voting always via gallery.php
	    //             (do not need to re-direct to rate_cgi.php!)
	    $str .= '<input type="image" src="';
	    $str .= 'themes/' . $blog_theme . '/images/stars/' . $star_image;
	    $str .= '" name="rating'.$star_number.'" value="1" onclick="submit;" />';

	    /*
	    $str .= 
	      '<a href="gallery.php?post=rate&amp;pic=' . $pic . 
	      '&amp;album=' . $album . '&amp;rating=' . $star_number . 
	      '" title="' . $lang_string['sb_rate_entry_btn'] . 
	      '"><img src="themes/' . $blog_theme . 
	      '/images/stars/' . $star_image . '" border="0" alt="' . 
	      $lang_string['sb_rate_entry_btn'] . '"/></a>';	
	    */

	  }
	  $str .= '</form>';
	  $str .= ' ( ' . round( $rating * 5, 1 ) . ' / ' . $votes . ' )';
	  $entry_array['stars'] = $str;
	}
    }

    ///////////////////////////////////////////////////////////////////////
    //
    // show thumbnails
    //
    ///////////////////////////////////////////////////////////////////////


    elseif(isset($page)) {

      // thumbnail zoom buttons
      if(!isset($pic)) {
	$list = getPictureList($album);
	if (count($list)>0){
	  $entry_array['entry'] .= '<div align="right">';
	  $entry_array['entry'] .= thumb_zoom_buttons($album_path,$page);
	  $entry_array['entry'] .= '</div>';
	}
      }


	if ($signed_in){
	    if (isset($_SESSION['valbum'])){
		if ($_SESSION['valbum'] === $valbum && $blog_permissions['edit_album']){
		    $pics = read_virtual_album($_SESSION['valbum']);
		    if (count($pics)){
			$entry_array['entry'] .= 'Click on images to <b>remove</b> pictures from ';
			$entry_array['entry'] .= '<i>'.$_SESSION['valbum'].'</i> ';
		    }
		    else{
			$entry_array['entry'] .= 'Click ';
			$entry_array['entry'] .= '<a href="?varemove=1&amp;album='.$_SESSION['valbum'].'&amp;page='.$page.'">here</a> ';;
			$entry_array['entry'] .= ' to remove this album<br />';
		    }
		}
		elseif ($blog_permissions['edit_album']){
		    $entry_array['entry'] .= 'Click on images to <b>add</b> pictures to ';
		    $entry_array['entry'] .= '<a href="?album='.$_SESSION['valbum'].'&amp;page='.$page.'">'.$_SESSION['valbum'].'</i></a> ';
		}
	    }
	    elseif (!is_virtual_album($album)){
		$entry_array['entry'] .= '<table width="90%"><tr><td>';
		if ($blog_permissions['upload']){
		    $entry_array['entry'] .= upload_pic_form($album);
		}
		$entry_array['entry'] .= '</td><td align="right">';
		if ($blog_permissions['setup']){
		    $entry_array['entry'] .= protect_album_form($album);
		}
		/*
		$entry_array['entry'] .= '</td></tr><tr><td></td><td align="right">';
		if ($blog_permissions['setup']){
		  $entry_array['entry'] .= set_album_style_form($album);
		}
		*/
		$entry_array['entry'] .= '</td></tr></table>';
		if ($blog_permissions['setup']){
		  $entry_array['entry'] .= set_album_style_form($album);
		}
	    }
	}
	
	$entry_array['entry'] .= '<div align="center">';
	$entry_array['entry'] .= thumbnails($album_directory,$album,$page);
	$entry_array['entry'] .= '</div>';
    }

    echo "</div>\n";

    echo( theme_staticentry( $entry_array ) );

    // add comments to picture pages + comment editor if necessary
    if ($entry_comments != ''){
      echo $entry_comments;
      /*
      sb_language('comments');
      if ( $logged_in || 
	   ($blog_config[ 'blog_enable_comments' ] == true) ||
	   ($signed_in && $blog_permissions['comment'])){
	require_once('scripts/sb_blogeditor.php');
	if (! $blog_config['disable_comment_capcha']){
	  @session_unregister( 'capcha_' . $_REQUEST[ 'entry' ] );
	  $_SESSION[ 'capcha_' . $_REQUEST[ 'entry' ] ]=sb_get_capcha();
	}
	editor_comments();
      }
      */
    }
}









function slideshow_buttons($album,$pic){
  global $logged_in, $blog_config, $lang_string;

  // theme image path
  global $blog_theme;
  $img_path = "themes/" . $blog_theme . "/images/";

  $html = '';
  if (!$logged_in){
    $pics = getPictureList($album);
    if (count($pics)>0){

      // slideshow is running
      if (isset($_REQUEST['slideshow'])){
	if (!isset($_REQUEST['big'])){
	  $params = array('album'=>$album,
			  'slideshow'=>$_REQUEST['slideshow'],
			  'big' => '1');
	  if (isset($_REQUEST['idx'])){
	    $params['idx'] = $_REQUEST['idx'];
	  }
	  $url=makeURL("",$params);
	  $html .= " <a href='$url'>";
	  if (file_exists($img_path.'zoom.png')){
	    $html .= '<img src="'.$img_path.'zoom.png" border="0" alt="';
	    $html .= $lang_string['bigger'].'"/></a> ';
	  } else {
	    $html .= '['.$lang_string['bigger']."</a> / ";
	  }

	}
	$speed=$_REQUEST['slideshow']+1;
	$params = array('album'=>$album,'slideshow'=>$speed);
	if (isset($_REQUEST['idx'])){
	  $params['idx'] = $_REQUEST['idx'];
	}
	$url=makeURL("",$params);

	$html .= "<a href='$url'>";

	if (file_exists($img_path.'slower.png')){
	  $html .= '<img src="'.$img_path.'slower.png" border="0" alt="';
	  $html .= $lang_string['slower'].'"/></a> ';
	}
	else{
	  $html .= $lang_string['slower']."</a> / ";
	}

	if ($_REQUEST['slideshow']>0){
	  $speed=$_REQUEST['slideshow']-1;
	  $params = array('album'=>$album,
			  'slideshow'=>$speed);
	  if (isset($_REQUEST['idx'])){
	    $params['idx'] = $_REQUEST['idx'];
	  }
	  $url=makeURL("",$params);
	  
	  $html .= "<a href='$url'>";
	  if (file_exists($img_path.'faster.png')){
	    $html .= '<img src="'.$img_path.'faster.png" border="0" alt="';
	    $html .= $lang_string['faster'].'" /></a> ';
	  } else {
	    $html .= $lang_string['faster']."</a> / ";
	  }
	}

	$url=makeURL("",array('album'=>$album,'pic'=>$pic));

	$html .= "<a href='$url'>";
	if (file_exists($img_path.'stop.png')){
	  $html .= '<img src="'.$img_path.'stop.png" border="0" alt="';
	  $html .= $lang_string['stop'].'" /></a> ';
	} else {
	  $html .= $lang_string['stop']."</a>] ";
	}
      }

      // slideshow is not running
      else{
	if (isset($pic)){
	  $idx = getPictureIndexNr($album,$pic);
	  $url=makeURL("",array('album'=>$album,
				'idx'=>$idx,
				'slideshow'=>'3'));
	}
	else{
	  $url=makeURL("",array('album'=>$album,
				'slideshow'=>'3'));
	}

	$html .= " <a href='$url'>";
	if (file_exists($img_path.'play.png')){
	  $html .= '<img src="'.$img_path.'play.png" border="0" alt="';
	  $html .=$lang_string['slideshow'].'" /></a> ';
	} else {
	  $html .= '['.$lang_string['slideshow'].'] </a>';
	}
      }
    }
  }
  return $html;
}


function thumb_zoom_buttons($album_path,$page){

  if ($_REQUEST['sth']){$_SESSION['THUMB_SIZE']='small';}
  if ($_REQUEST['mth']){$_SESSION['THUMB_SIZE']='medium';}
  if ($_REQUEST['bth']){$_SESSION['THUMB_SIZE']='big';}
  if (!isset($_SESSION['THUMB_SIZE'])){$_SESSION['THUMB_SIZE']='big';}

  $imgdir = "interface/gallery/";

  // $entry_array['subject'] .= '<div class="right">';

  // small
  $url = makeURL("",array("album" => "$album_path",
			  "page"  => "$page",
			  "sth"   => "1"));
  $html ="<a class='locationlink' href='$url'>";

  if ($_SESSION['THUMB_SIZE'] == 'small'){
    // $img=$imgdir.'zoom1on.gif';
    $img=$imgdir.'zoom1on.png';
  }
  else{$img=$imgdir.'zoom1off.png';}
  // else{$img=$imgdir.'zoom1off.gif';}
  $html .= '<img src="'.$img.'" border="0" /></a>';

  // medium
  $url = makeURL("",array("album" => "$album_path",
			  "page"  => "$page",
			  "mth"   => "1"));
  $html .="<a class='locationlink' href='$url'>";

  if ($_SESSION['THUMB_SIZE'] == 'medium'){
    //$img=$imgdir.'zoom2on.gif';
    $img=$imgdir.'zoom2on.png';
  }
  else{$img=$imgdir.'zoom2off.png';}
  // else{$img=$imgdir.'zoom2off.gif';}
  $html .= '<img src="'.$img.'" border="0" /></a>';

  // big (default)
  $url = makeURL("",array("album" => "$album_path",
			  "page"  => "$page",
			  "bth"   => "1"));
  $html .="<a class='locationlink' href='$url'>";

  if ($_SESSION['THUMB_SIZE'] == 'big'){
    //    $img=$imgdir.'zoom3on.gif';
    $img=$imgdir.'zoom3on.png';
  }  
  else{$img=$imgdir.'zoom3off.png';}
    //  else{$img=$imgdir.'zoom3off.gif';}
  $html .= '<img src="'.$img.'" border="0" /></a>';

  //$html .= '</div>';
  return $html;
}


		




if(isset($pic) && isset($_REQUEST['big'])) {
  echo '<div align="right">';
  echo slideshow_buttons($album,$pic);
  echo '</div>';
  echo '<div align="center">';
  $img = getPicture($album,$pic);
  $url=makeURL("",array("album"=>$album));
  if (isset($_REQUEST['slideshow'])){
    $url.="&slideshow=".$_REQUEST['slideshow'];
  }
  if (isset($_REQUEST['idx'])){
    $url.="&amp;idx=".$_REQUEST['idx'];
  }
  echo '<a href="'.$url.'"><img border="0" src="'.$img.'" alt="'.$pic;
  echo '" /></a>';
  echo '</div>';
}
else{
    theme_pagelayout();
}


html_footer();
