<?
require_once 'intro.php';

//параметры компонента//
$root=0;if(isset($doc['settings']['sitemap_root']) && is_numeric($doc['settings']['sitemap_root'])) $root = $doc['settings']['sitemap_root'];if(isset($_GET['root']) && is_numeric($_GET['root'])) $root = $_GET['root'];
$max_val=1.0;if(isset($doc['settings']['sitemap_max_value']) && is_numeric($doc['settings']['sitemap_max_value'])) $max_val = $doc['settings']['sitemap_max_value'];if(isset($_GET['max_val']) && is_numeric($_GET['max_val'])) $max_val = $_GET['max_val'];
$second_val=0.8;if(isset($doc['settings']['sitemap_second_value']) && is_numeric($doc['settings']['sitemap_second_value'])) $second_val = $doc['settings']['sitemap_second_value'];if(isset($_GET['second_val']) && is_numeric($_GET['second_val'])) $second_val = $_GET['second_val'];
$step1=0.1;if(isset($doc['settings']['sitemap_step1']) && is_numeric($doc['settings']['sitemap_step1'])) $step1 = $doc['settings']['sitemap_step1'];if(isset($_GET['step1']) && is_numeric($_GET['step1'])) $step1 = $_GET['step1'];
$step2=0.01;if(isset($doc['settings']['sitemap_step2']) && is_numeric($doc['settings']['sitemap_step2'])) $step2 = $doc['settings']['sitemap_step2'];if(isset($_GET['step2']) && is_numeric($_GET['step2'])) $step2 = $_GET['step2'];
$third_val=0.6;if(isset($doc['settings']['sitemap_third_value']) && is_numeric($doc['settings']['sitemap_third_value'])) $third_val = $doc['settings']['sitemap_third_value'];if(isset($_GET['third_val']) && is_numeric($_GET['third_val'])) $third_val = $_GET['third_val'];
$include_root=0;if(isset($doc['settings']['sitemap_include_root']) && is_numeric($doc['settings']['sitemap_include_root'])) $include_root = $doc['settings']['sitemap_include_root'];if(isset($_GET['include_root']) && is_numeric($_GET['include_root'])) $include_root = $_GET['include_root'];
$changefreq_val='weekly';if(isset($doc['settings']['sitemap_changefreq'])) $changefreq_val = $doc['settings']['sitemap_changefreq'];if(isset($_GET['changefreq'])) $changefreq_val = $_GET['changefreq'];

//параметры компонента//

header('Content-Type: text/xml');
$dom = new DOMDocument('1.0');
$urlset = $dom->appendChild($dom->createElement('urlset'));
$xmlns=$urlset->appendChild($dom->createAttribute("xmlns"));
$xmlns->appendChild($dom->createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9"));
//сформировали шапку

$param['tree']['childs'] = get_child_pages($root,true,true);
if($include_root==1){
    $root_element = getVal("SELECT `id`,`modified`,`changefreq`,`priority` FROM `pages` WHERE `id`='".adds($root)."' LIMIT 1",-1);
    array_unshift($param['tree']['childs'], $root_element);
}
//log_add(addslashes(print_r($param['tree'],true)));
function show_sitemap_tree($p,$pr) {
    global $dom,$urlset,$doc;
    global $root,$max_val,$second_val,$third_val,$step1,$step2,$include_root,$changefreq_val;
    if(!empty($p['childs']))
    {
        foreach($p['childs'] as $c) {
            $url = $urlset->appendChild($dom->createElement('url'));
                $loc = $url->appendChild($dom->createElement("loc"));
                    $loc->appendChild($dom->createTextNode($doc['host'].$c['path']));
                $lastmod = $url->appendChild($dom->createElement("lastmod"));
                    $lastmod->appendChild($dom->createTextNode(date("c", $c['modified'])));
                $changefreq = $url->appendChild($dom->createElement("changefreq"));
                    $changefreq->appendChild($dom->createTextNode($changefreq_val));
                $priority = $url->appendChild($dom->createElement("priority"));
                if($c['priority']!='-1') $priority_value = $c['priority'];
                else $priority_value = $pr;
                $priority->appendChild($dom->createTextNode($priority_value));
                //$id = $url->appendChild($dom->createElement("id"));
                    //$id->appendChild($dom->createTextNode($c['id']));
             if($pr>$second_val) $npr=$second_val;
             elseif($pr>$third_val) $npr=$pr-$step1;
             else $npr=$pr-$step2;
             show_sitemap_tree($c,$npr);
        }
    }
}

show_sitemap_tree($param['tree'],$max_val);
//вывод xml документа
echo $dom->saveXML();
?>
