<?php
/*
Gallery Index (subdir version)

Searches each subdirectory one level beneath the current directory. If "autoindex.xml"
is found in that subdirectory, it parses the file and adds to the list of galleries.
Finally, it outputs an index of those galleries.

Some debug information can be obtained by typing this after the url:

      ?php        lots of information about your server's configuration of php
      ?array      the data obtained by this script after parsing the various xml files
*/

$reverseit = false;  // change to true if you'd like the album displayed in reverse order

function write_top() {
	include "./resources/html/head.html";
	echo '
	<body id="galleryindex">';
	include "./resources/html/header.html";
	include "./resources/html/galleryindex_description.html";
}

function write_bottom() {
	include "./resources/html/footer.html";
	echo '
	</body>
</html>';
}

error_reporting(E_ALL ^ E_NOTICE);
$show = trim($_SERVER["QUERY_STRING"]);
if ($show == 'php') phpinfo();

// for debugging; displays $w in human-readable format
function debug($w) {
   write_top();
   echo '<div style="text-align: left"><pre>';
   if (is_string($w)) echo htmlentities($w);
     else print_r ($w);
   echo "</pre></div>";
   write_bottom();
   exit;
}

// returns an array if autoindex.xml exists in the given dir; otherwise returns false
function gallery_from_dir($dir) {
  // load external xml data
   if (!file_exists($dir.'/autoindex.xml')) return false;
   $data = file_get_contents($dir.'/autoindex.xml');
   if ($data === false) return false;
  // parse xml into usable structure
   $parser = xml_parser_create();
   xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
   xml_parse_into_struct($parser, $data, $vals);
   xml_parser_free($parser);
   $al = array();
   $i = 0;
  // restructure into simple record-type array
   for($i=0; $i < count($vals); $i++)
     if ($vals[$i]['type'] == 'complete')
        $al[strtolower($vals[$i]['tag'])] = $vals[$i]['value'];
  // if no url is specified, link to the dir
   if ((!isset($al['url'])) or ($al['url'] == '')) $al['url'] = $dir.'/';
  // if invalid thumbnail specified, supply random image from thumbnails dir, or default
   $al['thumbnail'] = $dir.'/'.$al['thumbnail'];
   if (($al['thumbnail'] == $dir.'/') or (!file_exists($al['thumbnail']))) {
      $chosenimg = 'resources/images/nothumb.jpg';
      $images=glob("$dir/thumbnails/*.jpg");
      if (count($images) > 0) do {
         $chosenimg = $images[array_rand($images)];
      } while (!getimagesize($chosenimg));
      $al['thumbnail'] = $chosenimg;
   }
   if ((!isset($al['title'])) or ($al['title'] == '')) $al['title'] = '';
   if ((!isset($al['description'])) or ($al['description'] == '')) $al['description'] = '';
   return $al;
}

// populate array of galleries
$albums = array();
foreach (glob("galleries/*", GLOB_ONLYDIR) as $fn) {
   if (is_dir($fn)) $thisal = gallery_from_dir($fn);
   if (is_array($thisal)) $albums[] = $thisal;
}
if ($reverseit) $albums = array_reverse($albums);
if ($show == 'array') debug($albums);

// output page
write_top();
echo '
		<div id="albumIndex">
			';
for($i=0; $i<count($albums); $i++) {
?>
            <div class="albumBox" onclick="location.href='<?php print $albums[$i]['url']?>';">
                <div class="albumBoxContent">
                    <div class="albumThumb">
                        <img src="<?php print $albums[$i]['thumbnail']?>" class="thumbImg" alt="<?=$albums[$i]['title']?>" />
                    </div>
                    <div class="albumCopy">
                        <p class="albumTitle"><?php print $albums[$i]['title']?></p>
                        <p class="albumDescription"><?php print $albums[$i]['description']?></p>
                    </div>
                </div>
            </div>
<?php
}
echo '
			
		</div> <!-- /albumIndex -->

		<div class="clear"></div>

	</div> <!-- /gallery -->
		';
write_bottom();
?>