<?php
include '../config.inc.php';

$sitemap  = parse_ini_file('../sitemap.ini', true);
$sitemapconfig = $config['sitemapoptions'];
$date_template = $sitemapconfig['date_template'];

if (strtolower($_SERVER['SCRIPT_NAME']) == '/sitemap.xml' or strtolower($_SERVER['SCRIPT_NAME']) == '/sitemap.gz') {
    header('Content-type: application/xml; charset="UTF-8"');
}

header('Pragma: no-cache');

$database = get_object('DataBase');
$images = get_object('Images');
$template = get_object('Template');

$filename = pathinfo($_SERVER['SCRIPT_NAME']);
if (strtolower($filename['extension']) == 'gz') {
    ob_start("ob_gzhandler");
} else {
    ob_start();
}

if (strtolower($filename['extension']) != 'php') {
    echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
    // echo '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">'."\n";
    echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">'."\n";
}

if (is_array($sitemap) and count($sitemap) > 0) {
    foreach ($sitemap AS $riga) {
        if (empty($riga['loc'])) continue;
        if ($riga['dynamic']) {
            $lastmod = strftime($date_template, $now);
            $location = htmlentities($riga['loc']);
        } else {
            $location = $riga['loc'];
            if (file_exists($basepath.'/'.$riga['loc'])) {
                $temparray = stat($basepath.'/'.$riga['loc']);
                $lastmod = strftime($date_template, $temparray[9]);
            } else {
                $errors[] = $riga['loc'];
                continue;
            }
        }

        stampamelo($baseurl.$location,$riga['changefreq'],$lastmod,$riga['priority']);
    }
}


if ($sitemapconfig['do_news'] == true) {
    $news = get_object('News');
    $news->type = '';

    $risultato = $news->select('all');

    if (is_array($risultato) and count($risultato) > 0) {
        $changefreq = 'monthly';
        $lastmod = strftime($date_template, $now);
        $priority = '0.5';
        foreach ($risultato AS $riga) {
            if (is_array($GLOBALS['config']['supported_locales']) and count($GLOBALS['config']['supported_locales']) > 1 ) {
                stampamelo($baseurl."/n/{$riga['locale']}/{$riga['codice']}/".urlencode(urlencode($riga['news_'.$riga['locale']])).".htm",$changefreq,$lastmod,$priority,$riga);
            } else {
                stampamelo($baseurl."/n/{$riga['codice']}/".urlencode(urlencode($riga['news'])).".htm",$changefreq,$lastmod,$priority,$riga);
            }
        }
    }
}

if ($sitemapconfig['do_cms'] == true) {
    $cms = get_object('CMS');

    $risultato = $cms->select_pagine($activelocale);

    if (is_array($risultato) and count($risultato) > 0) {
        $changefreq = 'weekly';
        $lastmod = strftime($date_template, $now);
        $priority = '0.5';
        foreach ($risultato AS $riga) {
            if (is_array($GLOBALS['config']['supported_locales']) and count($GLOBALS['config']['supported_locales']) > 1 ) {
                stampamelo($baseurl."/p/{$riga['locale']}/".urlencode(urlencode($riga['pagina'])).'.htm',$changefreq,$lastmod,$priority);
            } else {
                stampamelo($baseurl."/p/".urlencode(urlencode($riga['pagina'])).'.htm',$changefreq,$lastmod,$priority);
            }
        }
    }
}

if ($sitemapconfig['do_catalogo'] == true) {
    $catalogo = get_object('Catalogo');
    $changefreq = 'weekly';
    $lastmod = strftime($date_template, $now);
    $priority = '0.5';

    if (is_array($GLOBALS['config']['supported_locales'])) {
        $tempsezioni = array();
        foreach ($GLOBALS['config']['supported_locales'] AS $thislocale) {
            $risultato = $catalogo->select_sezioni_plain($thislocale);
            if (okArray($risultato)) {
                $tempsezioni = array_merge($tempsezioni, $risultato);
            }
        }
    } else {
        $tempsezioni = $catalogo->select_sezioni_plain($activelocale);
    }

    if (okArray($tempsezioni)) {
        foreach ($tempsezioni AS $riga) {
            $sezioni[$riga['codice']][$riga['locale']] = $riga;
            if (!empty($riga['prettyurl'])) {
                if (count($GLOBALS['config']['supported_locales']) > 1) {
                    stampamelo($baseurl."/s/{$riga['locale']}/{$riga['codice']}/".urlencode(urlencode($riga['prettyurl'])).".htm",$changefreq,$lastmod,$priority);
                } else {
                    stampamelo($baseurl."/s/{$riga['codice']}/".urlencode(urlencode($riga['prettyurl'])).".htm",$changefreq,$lastmod,$priority);
                }
            } else {
                stampamelo($baseurl."/catalogo.php?lang={$riga['locale']}&amp;action=sezione&amp;value={$riga['codice']}&amp;nomesezione=".urlencode(urlencode($riga['sezione'])),$changefreq,$lastmod,$priority);
            }
        }
    }

    if ($catalogo->schede_prodotto == true) {

        if (is_array($GLOBALS['config']['supported_locales'])) {
            $prodotti = array();
            foreach ($GLOBALS['config']['supported_locales'] AS $thislocale) {
                $risultato = $catalogo->select_prodotti($thislocale);
                if (okArray($risultato)) {
                    $prodotti = array_merge($prodotti, $risultato);
                }
            }
        } else {
            $prodotti = $catalogo->select_prodotti($activelocale);
        }

        if (is_array($prodotti) and count($prodotti) > 0) {
            foreach ($prodotti AS $riga) {
                if (!empty($riga['prettyurl'])) {
                    if (count($GLOBALS['config']['supported_locales']) > 1) {
                        stampamelo($baseurl."/{$riga[locale]}/{$riga['codice']}/".urlencode(urlencode($riga['prettyurl'])).".htm",$changefreq,$lastmod,$priority,$riga);
                    } else {
                        stampamelo($baseurl."/{$riga['codice']}/".urlencode(urlencode($riga['prettyurl'])).".htm",$changefreq,$lastmod,$priority,$riga);
                    }
                } else {
                    stampamelo($baseurl."/catalogo.php?lang={$riga['locale']}&amp;action=prodotto&amp;prodotto={$riga['codice']}&amp;sezione={$riga['sezione']}&amp;nomeprodotto=".urlencode(urlencode($riga['prodotto']))."&amp;nomesezione=".urlencode(urlencode($sezioni[$riga['sezione']][$riga['locale']]['sezione'])),$changefreq,$lastmod,$priority,$riga);
                }

            }
        }
    }
}

if ($sitemapconfig['do_custom'] && function_exists($sitemapconfig['do_custom'])) {
    $myfunc = $sitemapconfig['do_custom'];
    $myfunc();
}

if (is_array($errors) and count($errors) > 0) {
    echo "<errors>\n";
    foreach ($errors AS $riga) {
        echo "<loc>$riga</loc>\n";
    }
    echo "</errors>\n";
}

echo "</urlset>\n";

if (strtolower($_SERVER['SCRIPT_NAME']) != '/sitemap.gz') {
    $size=ob_get_length();

    $gmt_mtime = date('D, d M Y H:i:s', $now) . ' GMT';
    header("Last-Modified: ".$gmt_mtime);
    header("Content-Length: $size");

    ob_end_flush();
}


exit;


function stampamelo($url, $changefreq='always', $lastmod=0, $priority='0.5',$dati=array()) {
    if ($lastmod == 0) $lastmod = strftime($GLOBALS['date_template'], $GLOBALS['now']);
    if (strtolower($_SERVER['SCRIPT_NAME']) == '/sitemap.xml' or strtolower($_SERVER['SCRIPT_NAME']) == '/sitemap.gz') {
        echo "<url>\n";
        echo "<loc>$url</loc>\n";
        echo "<lastmod>$lastmod</lastmod>\n";
        echo "<changefreq>$changefreq</changefreq>\n";
        echo "<priority>$priority</priority>\n";

        if ($GLOBALS['sitemapconfig']['do_immagini'] == true && okArray($dati)) {
            $dati['img'] = $GLOBALS['database']->decode_pgarray($dati['img']);
            if (okArray($dati['img'])) {
                $dati['imgprettyurl'] = $GLOBALS['database']->decode_pgarray($dati['imgprettyurl']);
                $dati['imgmime'] = $GLOBALS['database']->decode_pgarray($dati['imgmime']);
                foreach ($dati['img'] AS $k => $img) {
                    $prettyurl = $dati['imgprettyurl'][$k][0];
                    foreach ($GLOBALS['images']->images AS $bah => $mah) {
                        echo "<image:image>\n";
                        echo "<image:loc>{$GLOBALS['baseurl']}";
                        if ($prettyurl) {
                            echo "/img/{$bah}/{$img}/{$prettyurl}.".$GLOBALS['template']->estensione_da_mimetype($dati['imgmime'][$k][$bah]);
                        } else {
                            echo "/img.php?value={$bah}-{$img}";
                        }
                        echo "</image:loc>\n";
                        if ($prettyurl) echo "<image:caption>".urlencode(urlencode($prettyurl))."</image:caption>\n";
                        echo "</image:image>\n";
                    }
                }
            }
        }

        echo "</url>\n";
    } else {
        /*
        debugga($url);
        if ($GLOBALS['sitemapconfig']['do_immagini'] == true && okArray($dati)) debugga($dati);
        */
    }
}
?>