<?php 
require_once('scripts/sb_functions.php');
include_once "scripts/sb_gallery_config.php";
include_once "scripts/sb_gallery.php";


if (!signed_in()){
  global $logged_in;
  $logged_in = logged_in( true, true );
}
else{
    global $blog_permissions;
    if (! ($blog_permissions['add']==1 || $blog_permissions['edit']==1) ){ 
	redirect_to_url('index.php');
    }
}

setupStickyURLOptions();
setGlobal("album");
setGlobal("page");
setGlobal("pic");

read_config();

sb_language( 'image_list' );	
sb_language( 'index' );
sb_language( 'gallery' );

html_header('',false);


function page_content() {


    global $page;
    global $pic;
    global $album;
    global $album_directory;
    global $lang_string;


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

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

    // set the logged_in and signed_in variables to false
    // to disable special functions in the gallery pages
    // ... again ... this is very ugly

    global $logged_in,$signed_in;
    $tmp_logged_in = $logged_in;
    $tmp_signed_in = $signed_in;
    $logged_in = false;
    $signed_in = false;

    $entry_array['subject']=$lang_string[ 'title' ];

    // instructions to copy the image links
    $entry_array['entry'] = $lang_string[ 'instructions' ].'<p />';

    // album navigation
    $entry_array['entry'] .= '<table><tr><td valign="top">';
    $url=makeURL("",array("album"=>"","page"=>"1"));
    $entry_array['entry'] .= "<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['entry'] .=
		" / <a class='locationlink' href='$url'>$value</a>";
	    $album_path .= '/';
	}
    }
    $entry_array['entry'] .= '</td></tr></table><p />';


    // 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)){
      $link='?album='.$album;
      if ($album == ""){$link.=".";}
      $entry_array['entry'] .= '<a href="'.$link.'"><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 />';
    }


    ///// now - picture and thumbnails


    if (isset($pic)){
      // picture
      $entry_array['entry'] .= '<div align="center">';
      $entry_array['entry'] .= picture_page($album_directory,$album,$pic);
      $entry_array['entry'] .= '</div>';
    }
    else{
      // thumbnails
      $entry_array['entry'] .= '<div align="center">';
      $entry_array['entry'] .= thumbnails($album_directory,$album,$page);
      $entry_array['entry'] .= '</div>';
    }

    // restore the logged_in/signed_in variables
    $logged_in = $tmp_logged_in;
    $signed_in = $tmp_signed_in;

    echo( theme_staticentry( $entry_array ) );
}

theme_popuplayout();
html_footer();

?>
