Location: PHPKode > projects > photolibrary > standalone.php
<?PHP
/*
   [Start: Program Information Header]

    Name    : Photolibrary: Standalone
    Purpose : Generates XML files for standalone Flash viewer
    Syntax  :

   [End: Program Information Header]

   [Start: Author Information Header]

    Name    : James D. Forrester
    E-mail  : hide@address.com

    Name    : Ed Sanders
    E-mail  : hide@address.com

   [End: Author Information Header]
*/

$non_html = true;
require('include.initialise.php');

echo "Rebuilding offline database...\r\n";

echo " * Photos by categories...\r\n";
$ptc_xml = fopen('./bin/ptc.xml', 'w+');
fwrite($ptc_xml, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n<photosbycategory>\r\n");

$query_handle = mysql_query("
  SELECT hash, category
  FROM photostocategories
  ORDER BY category, hash") or trigger_error(mysql_error(), E_USER_ERROR);
$category_old = '';
$first = true;
while($array = mysql_fetch_assoc($query_handle))
{
  extract($array);
  if($category != $category_old)
  {
    if(!$first)
      fwrite($ptc_xml, "</category>\r\n");
    fwrite($ptc_xml, '<category category="'.$category.'">'.$hash);
  }
  else
  {
    fwrite($ptc_xml, ','.$hash);
  }
  $category_old = $category;
  $first = false;
}
fwrite($ptc_xml, "</category>\r\n");
fwrite($ptc_xml, "</photosbycategory>");
fclose($ptc_xml);

echo " * Categories by photos...\r\n";
$ctp_xml = fopen('./bin/ctp.xml', 'w+');
fwrite($ctp_xml, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n<categoriesbyphoto>\r\n");

$query_handle = mysql_query("
  SELECT hash, category
  FROM photostocategories
  ORDER BY hash, category") or trigger_error(mysql_error(), E_USER_ERROR);
$hash_old = '';
$first = true;
while($array = mysql_fetch_assoc($query_handle))
{
  extract($array);
  if($hash != $hash_old)
  {
    if(!$first)
      fwrite($ctp_xml, "</photo>\r\n");
    fwrite($ctp_xml, '<photo hash="'.$hash.'">'.$category);
  }
  else
  {
    fwrite($ctp_xml, ','.$category);
  }
  $hash_old = $hash;
  $first = false;
}
fwrite($ctp_xml, "</photo>\r\n");
fwrite($ctp_xml, "</categoriesbyphoto>");
fclose($ctp_xml);

echo " * Photos...\r\n";
$photos_xml = fopen('./bin/photos.xml', 'w+');
fwrite($photos_xml, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n<photos>\r\n");

$query_handle = mysql_query("
  SELECT hash, type, IF(taken, taken, '') AS taken, filesize, orientation AS rot, height, width
  FROM photos
  ORDER BY hash") or trigger_error(mysql_error(), E_USER_ERROR);
$i = 0;
while($array = mysql_fetch_assoc($query_handle))
{
  $i++;
  fwrite($photos_xml, '<photo ');
  foreach($array as $var => $value)
  {
    if($var == 'type') $value = type($value);
    fwrite($photos_xml, ''.$var.'="'.$value.'" ');
  }
  fwrite($photos_xml, "/>\r\n");
}
fwrite($photos_xml, '</photos>');

echo " * Category names...\r\n";
$categories_xml = fopen('./bin/categories.xml', 'w+');
fwrite($categories_xml, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n<categories>\r\n");

$query_handle = mysql_query("
  SELECT category, name, parent, top_parent
  FROM categories
  ORDER BY category") or trigger_error(mysql_error(), E_USER_ERROR);
while($array = mysql_fetch_assoc($query_handle))
{
  extract($array);
  fwrite($categories_xml, "<category category=\"$category\" name=\"$name\" parent=\"$parent\" top_parent=\"$top_parent\" />\r\n");
}

fwrite($categories_xml, '</categories>');
fclose($categories_xml);

echo " * Settings and globals...\r\n";
$globals_xml = fopen('./bin/globals.xml', 'w+');
fwrite($globals_xml, "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n<globals>\r\n");

foreach($settings as $var => $value)
{
  $var = preg_replace("/_([a-z])/e", "strtoupper('\\1')", $var);
  fwrite($globals_xml, "<var name=\"$var\" value=\"$value\" />\r\n");
}

fwrite($globals_xml, "<var name=\"publicCategory\" value=\"$public_category\" />\r\n");
fwrite($globals_xml, "<var name=\"photographersCategory\" value=\"$photographers_category\" />\r\n");
fwrite($globals_xml, '</globals>');

fclose($globals_xml);

// // Category tree (parents)
// $cattree_file = fopen('./bin/cattree.txt', 'w+');

// $query_handle = mysql_query("
//   SELECT category, parent
//   FROM categories
//   ORDER BY parent, name") or trigger_error(mysql_error(), E_USER_ERROR);
// while($array = mysql_fetch_assoc($query_handle))
// {
//   $parent_old = @$parent;
//   extract($array);
//   if($parent != $parent_old)
//   {
//     fwrite($cattree_file, '&parent'.$parent.'='.$category);
//   }
//   else
//   {
//     fwrite($cattree_file, ','.$category);
//   }
// }

// fwrite($cattree_file, '&cattree_file='.$category);
// fclose($cattree_file);

echo "Done.";
mysql_close($dbserverlink);

?>
Return current item: photolibrary