<?php
# ==============================================================================
#                                                                              |
#  SITE-MAP FILE                                                               |
#                                                                              |
#  htp://WOODEX.UA                                                             |
#                                                                              |
#  (c) 2008 Igor Svist                                                         |
#                                                                              |
#                                                                              |
# ==============================================================================

  # define short Document_Root alias
  #
  define( 'DC', $_SERVER['DOCUMENT_ROOT'] );

  require_once DC.'/configs/config_index_face.php';

  #-----------------------------------------------------------------------------
  #                                                             select language
  #

  global $lng_from_domain;
         $lng_from_domain = '';

  global $lengv;
         $lengv           = '';

  global $lng;
         #$lng             = '';

  $lng = strtolower(substr(trim($lng),0,2));

  if (!empty($lng) and !in_array($lng, array('ua','ru','en'))) {
     $lng = 'ua';
  }

  if (defined('SUBDOMAINS_AS_LANGUAGE') and SUBDOMAINS_AS_LANGUAGE)
  {
     $lng_from_domain = 'ua';

         if (0 === strpos($_SERVER['HTTP_HOST'],'ru.') or 0 === strpos($_SERVER['HTTP_X_FORWARDED_HOST'],'ru.')) $lng_from_domain = 'ru';
     elseif (0 === strpos($_SERVER['HTTP_HOST'],'en.') or 0 === strpos($_SERVER['HTTP_X_FORWARDED_HOST'],'en.')) $lng_from_domain = 'en';

     $lengv = (!empty($lng) and $lng != $lng_from_domain) ? $lng : $lng_from_domain;
  }
  else
  {
     $lengv = empty($lng) ? 'ua' : $lng;
  }

  # check in the cache ---------------------------------------------------------
  #
  $ttl = 86400*5;
  $fn  = 'cache/sitemap/'.$lengv.'.xml';

  if ( file_exists($fn) and filesize($fn) and (filemtime($fn) > (time()-$ttl) ) )
  {
     echo file_get_contents( $fn );
     exit;
  }

  # -----------------------------------------------------------------------------
  #                                                                      INCLUDES
  #

  require_once DC.'/admin/conf.php';
  require_once DC.'/admin/func/db_fns.php';

  require_once DC.'/include/cache/cache_main_data.fns.php';

  require_once DC.'/include/menu_fns.php';
  require_once DC.'/include/text_fns.php';

  require_once DC.'/include/lang/str_conf.inc.php';

  # db-server connect ----------------------------------------------------------
  #
  global $conn;
         $conn = null;

         $conn = db_connect();

  # set templates
  #
  $tpl_header = '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  $tpl_footer = "\n" . '</urlset>';
  
  global $tpl_url;
         $tpl_url = "\n" . '<url><loc>%s</loc><lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%s</priority></url>';

  # init
  #  
  global $freq;
         $freq = array('a'=>'always', 'h'=>'hourly', 'd'=>'daily', 'w'=>'weekly', 'm'=>'monthly', 'y'=>'yearly', 'n'=>'never');

  define('URL', 'http://'.$_SERVER['HTTP_HOST']);
  
  # index page
  #
            $content.= sprintf($tpl_url,
                               URL,
                               date('Y-m-d'),
                               $freq['d'],
                               '1'
                           );

  # get list
  #
  
  global $output_type;
         $output_type = 'xml';

  $content.= include DC.'/plugin/site-map/index.php';  
 

  # save to the cache
  #
  
  $fo = fopen($fn, 'w');
  if ( $fo )
  {
  	 fwrite($fo, $tpl_header);
  	 fwrite($fo, $content);
  	 fwrite($fo, $tpl_footer);

  	 fclose($fo);
  }
  
  # show sitemap
  #

  echo $tpl_header;
  echo $content;
  echo $tpl_footer;

?>
