<?php if(!defined('master_script')) die();

/**
 * Copyright (C) Nico Wenig
 * All rights reserved
 *
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @filesource
 */



/**
 * Functions
 *
 * @category   Extension (xml-sitemap)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-08
 */
	
	// Get priority by file date (last modified)
	function sitemap_property($file, $mode = 'priority') {
		global $system_data, $extensions;
		
		$file = $system_data['folder']['pages'] . strval($file) . '/' . $system_data['file']['page info file'];
		
		if(is_file($file)) {
			$filetime = filemtime($file);
			$current_time = time();
			
			$priority = '0.1';
			$changefreq = 'never';
			
			if($filetime >= $current_time - $extensions['xml-sitemap']['0.2']) { $priority = '0.2'; $changefreq = 'yearly'; }
			if($filetime >= $current_time - $extensions['xml-sitemap']['0.3']) { $priority = '0.3'; $changefreq = 'yearly'; }
			if($filetime >= $current_time - $extensions['xml-sitemap']['0.4']) { $priority = '0.4'; $changefreq = 'monthly'; }
			if($filetime >= $current_time - $extensions['xml-sitemap']['0.5']) { $priority = '0.5'; $changefreq = 'monthly'; }
			if($filetime >= $current_time - $extensions['xml-sitemap']['0.6']) { $priority = '0.6'; $changefreq = 'monthly'; }
			if($filetime >= $current_time - $extensions['xml-sitemap']['0.7']) { $priority = '0.7'; $changefreq = 'weekly'; }
			if($filetime >= $current_time - $extensions['xml-sitemap']['0.8']) { $priority = '0.8'; $changefreq = 'weekly'; }
			if($filetime >= $current_time - $extensions['xml-sitemap']['0.9']) { $priority = '0.9'; $changefreq = 'weekly'; }
			if($filetime >= $current_time - $extensions['xml-sitemap']['1.0']) { $priority = '1.0'; $changefreq = 'daily'; }
		}
		
		return $mode == 'priority' ? $priority : $changefreq;
	}

/**
 * Settings
 *
 * @category   Extension (xml-sitemap)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-08
 */
	
	$extensions['xml-sitemap']['file'] = 'sitemap.xml';
	
	$extensions['xml-sitemap']['lastmod active'] = 1;
	$extensions['xml-sitemap']['priority active'] = 0;
	$extensions['xml-sitemap']['changefreq active'] = 0;

	$extensions['xml-sitemap']['1.0'] = 60 * 60 * 24 * 1;
	$extensions['xml-sitemap']['0.9'] = 60 * 60 * 24 * 3;
	$extensions['xml-sitemap']['0.8'] = 60 * 60 * 24 * 7;
	$extensions['xml-sitemap']['0.7'] = 60 * 60 * 24 * 14;
	$extensions['xml-sitemap']['0.6'] = 60 * 60 * 24 * 31;
	$extensions['xml-sitemap']['0.5'] = 60 * 60 * 24 * 62;
	$extensions['xml-sitemap']['0.6'] = 60 * 60 * 24 * 93;
	$extensions['xml-sitemap']['0.5'] = 60 * 60 * 24 * 124;
	$extensions['xml-sitemap']['0.4'] = 60 * 60 * 24 * 155;
	$extensions['xml-sitemap']['0.3'] = 60 * 60 * 24 * 186;
	$extensions['xml-sitemap']['0.2'] = 60 * 60 * 24 * 217;
	$extensions['xml-sitemap']['0.1'] = 60 * 60 * 24 * 248;

/**
 * Get info about existing sitemap
 *
 * @category   Extension (xml-sitemap)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-08
 */
	
	if(is_file($extensions['xml-sitemap']['file'])) {
		$extensions['xml-sitemap']['last changed'] = date('d.m.Y / H:i:s', filemtime($extensions['xml-sitemap']['file']));
		$extensions['xml-sitemap']['file size'] = format_filesize(filesize($extensions['xml-sitemap']['file']));
		
		$sitemap_xml = simplexml_load_file($extensions['xml-sitemap']['file']);
		$extensions['xml-sitemap']['found pages'] = count($sitemap_xml->url);
		
		$extensions['xml-sitemap']['page urls'] = array();
		foreach($sitemap_xml->url as $sitemap_url) $extensions['xml-sitemap']['page urls'][] = $sitemap_url;
	}

/**
 * Save sitemap
 *
 * @category   Extension (xml-sitemap)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-17
 */
	
	if(isset($_POST['save_xml_sitemap'])) {
		// Open new XML file
    	$sitemap_xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>');
		$sitemap_urls = 0;
		
		// Standard pages
		if(isset($_POST['standard_pages'])) {
			foreach($database_categories->cfile as $category_entry) {
				$database_category_file = $system_data['folder']['database'] . 'standard/' . $category_entry->id . '.cat';
		    	$database_category = is_file($database_category_file) ? file($database_category_file) : array();
				
				// Search in catalogue file
				foreach($database_category as $page_entry) {
					$page_entry = trim($page_entry);
				    $page_entry_info = get_page_info($page_entry);
				    
				    if($page_entry_info->searchable == 1) {
					    $sitemap_xml->url[$sitemap_urls]->loc = xml_string($user_data["base url"] . $system_data['frontend']['href attribute'] . $page_entry_info->alias);
					    if(isset($_POST['last_modified'])) $sitemap_xml->url[$sitemap_urls]->lastmod = date('Y-m-d', intval($page_entry_info->timestamp));
					    if(isset($_POST['priority'])) $sitemap_xml->url[$sitemap_urls]->priority = sitemap_property($page_entry_info->alias, 'priority');
					    if(isset($_POST['changefreq'])) $sitemap_xml->url[$sitemap_urls]->changefreq = sitemap_property($page_entry_info->alias, 'changefreq');
				    	$sitemap_urls++;
					}
				}
			}
		}

		// News pages
		if(isset($_POST['news_pages'])) {
			$news_database_filelist = filelist($system_data['folder']['database'] . 'news/', 'xml', array(), array('topics.xml'), true);
			
			foreach($news_database_filelist as $news_database_file) {
				$news_database_array = is_file($news_database_file) ? simplexml_load_file($news_database_file) : simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><pages></pages>');
				
				foreach($news_database_array->page as $page_entry_info) {
	    		    if(strval($page_entry_info->searchable) == 1) {
	    			    $sitemap_xml->url[$sitemap_urls]->loc = xml_string($user_data["base url"] . $system_data['frontend']['href attribute'] . $page_entry_info->alias);
	    			    if(isset($_POST['last_modified'])) $sitemap_xml->url[$sitemap_urls]->lastmod = date('Y-m-d', intval($page_entry_info->timestamp));
	    			    if(isset($_POST['priority'])) $sitemap_xml->url[$sitemap_urls]->priority = sitemap_property($page_entry_info->alias, 'priority');
	    			    if(isset($_POST['changefreq'])) $sitemap_xml->url[$sitemap_urls]->changefreq = 'never';
	    		    	$sitemap_urls++;
	    			}
				}
			}
		}
		
		// Save XML sitemap to file
		$sitemap_xml->saveXML($extensions['xml-sitemap']['file']);

		// Reload backend page
		header('location: index.php?backend=xml-sitemap');
	}

/**
 * Get last user settings
 *
 * @category   Extension (xml-sitemap)
 * @author     Nico Wenig <nico.wenig@yellowgreen.de>
 * @version	   2009-12-08
 */

	if(is_file($extensions['xml-sitemap']['file']) && count($extensions['xml-sitemap']['page urls']) > 0) {
		$extensions['xml-sitemap']['lastmod active'] = $extensions['xml-sitemap']['page urls'][0]->lastmod == '' ? 0 : 1;
		$extensions['xml-sitemap']['priority active'] = $extensions['xml-sitemap']['page urls'][0]->priority == '' ? 0 : 1;
		$extensions['xml-sitemap']['changefreq active'] = $extensions['xml-sitemap']['page urls'][0]->changefreq == '' ? 0 : 1;
	}

?>