<?php
$sortorder =  $_REQUEST["sortorder"];
$indir = "../galleries";
$files = opendir($indir);
$outxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n\n";
$outxml .= "<galleries>\n\n";	
while ($file = readdir($files)) {
  if(is_dir($indir.'/'.$file))
  if ($file != '.' && $file != '..') {
    {
    	$tmp_outxml[] = $file;
    }
	}
  }
if ($sortorder == "date")
{
	foreach ($tmp_outxml as $k => $v)
	{
		$modified = filemtime ($indir.'/'.$v);
		$moddate[$k] = $modified;
	}
	array_multisort ($moddate, $tmp_outxml);
}
else if ($sortorder == "random")
{
	shuffle ($tmp_outxml);
}
else
{
	sort ($tmp_outxml);
}
foreach ($tmp_outxml as $v)
{
	$outxml .= get_folder_xml($indir.'/'.$v,$v);
}
$outxml .= "</galleries>";

closedir($files);  
$fp = fopen("../xml/gallery/gallery.xml", "w");
fwrite($fp, $outxml);
fclose($fp);


function get_folder_xml($imageDir,$title)
  {

  $extensions = array(".jpg", ".jpeg", ".JPG",".JPEG",".gif",".GIF",".swf",".SWF");
  $output = "";
  
  if($folder = opendir($imageDir))
    {
    $filenames=array();
        
    while (false !== ($file = readdir($folder)))
      {
      $dot = strrchr($file, '.');
      if(in_array($dot, $extensions))
        {
        array_push($filenames, $file);
        }         
      }
     $spaceTitle = str_replace("_"," ",$title);
     $newPath = str_replace("../","",$imageDir);

    $output .= "<gallery>\n";
     $output .= "<title>$spaceTitle</title>\n";
	$output .= " <images>\n";
    foreach ($filenames as $source)
      {
	  $imageName = str_replace("mainThumb_","",$source);
      $finalName = str_replace("_"," ",$imageName);
	
      $imageName = str_replace($extensions,"",$finalName);
      $output .= "\t <image src=\"$newPath/$source\" thumb=\"$newPath/thumbs/$source\">$imageName</image>\n";
      }
   $output .= " </images>\n";
   $output .= "</gallery>\n\n";
return $output;
     
    closedir($folder);   
    }
  }
?>
