<?php

/*
	Google sitemap generator for PHP5/xml and CPG 1.4.x
*/

// use time of last comment on a pic as lastmod ? (if false then we just use pic's upload time (ie. db, not EXIF) which is cheaper)
define('INC_COMMENT_TIMES', true);

// dump the sitemap to a local file (specified here), or false to output straight to user agent. 
// Use a .gz ending if you are making a compressed file.
// Use a .gz or .xml.gz ending if you are making a compressed file.
//define('FILEDUMP', 'c:\sitemap.xml');
define('FILEDUMP', false);

// use gzip compression ? disable if you have zlib output compression enabled
// Zlib support in PHP is not enabled by default. You will need to configure PHP --with-zlib[=DIR]
// The windows version of PHP has built in support for this extension. You do not need to load any additional extension in order to use these functions.
define('GZ_COMPRESS', true);

// [0.0 <= priority <= 1.0]
define('P_DISPLAYIMAGE', 1.0);
define('P_ALBUM', 1.0);
define('P_CATEGORY', 1.0);

// [changefreq = always || hourly || daily || weekly || monthly || yearly || never]
define('CF_DISPLAYIMAGE', 'always');
define('CF_ALBUM', 'always');
define('CF_CATEGORY', 'always');

// No user servicable parts below here

define('IN_COPPERMINE', true);
require('include/init.inc.php');
define('BASEURL', rtrim($CONFIG['site_url'], '/') . '/');

class sitemap extends DOMDocument {
	
	private $urlset, $starttime;
	
	function sitemap()
	{
		$this->starttime = microtime(true);
		parent::__construct();
		$this->urlset = $this->createElement("urlset");
		$this->urlset->setAttribute('xmlns', 'http://www.google.com/schemas/sitemap/0.84');
		$this->AddURL('', false, '1.0', '');
	}
	
	function AddURL($link, $lastmodtime = false, $priority="0.5", $frequency = 'unspecified')
	{
		$url = $this->createElement("url");
		$loc = $this->createElement("loc");
		$url->appendChild($loc);
		$loc->appendChild($this->createTextNode(BASEURL . $link));
		
		if ($lastmodtime){
			$lastmod = $this->createElement("lastmod");
			$url->appendChild($lastmod);
			$lastmod->appendChild($this->createTextNode(date('c', $lastmodtime)));
		}
		
		if ($priority != 0.5 && $priority <= 1 && $priority >= 0){
			$pnode = $this->createElement("priority");
			$url->appendChild($pnode);
			$pnode->appendChild($this->createTextNode($priority));
		}
		
		if (in_array($frequency, array('always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never'))){
			$changefreq = $this->createElement("changefreq");
			$url->appendChild($changefreq);
			$changefreq->appendChild($this->createTextNode($frequency));
		}
		
		$this->urlset->appendChild($url);
	}
	
	function complete()
	{
		// this doesn't work for some reason, answers on a postcard....
		//$not = $this->schemaValidate('http://www.google.com/schemas/sitemap/0.84/sitemap.xsd') ? '' : ' NOT';
		
		$time = round(microtime(true) - $this->starttime, 2);
		$date = date('r');
		$result = $this->createComment("Generated $date in $time seconds.");
		$this->appendChild($result);
		$this->appendChild($this->urlset);
		
		if (FILEDUMP) {
			if (GZ_COMPRESS){
				if ($zp = gzopen(FILEDUMP, "w9")){
					gzwrite($zp, $this->saveXML());
					gzclose($zp);
					die("Generated $date in $time seconds, output file was compressed and dumped to " . FILEDUMP);
				} else {
					die('Failed to create compressed output file [' . FILEDUMP . '] check dir permissions and verify gzip capability');
				}
			} else {
				if ($this->save(FILEDUMP)){
					die("Generated $date in $time seconds, output file was dumped to " . FILEDUMP);
				} else {
					die('Failed to create output file [' . FILEDUMP . '] check dir permissions or create and chmod the file for me');	
				}
			}
		} else {
			header('Content-type: text/xml');
			if (!GZ_COMPRESS) die($this->saveXML());
			header('Content-encoding: gzip');
			header('Vary: Accept-Encoding');
			die(gzencode($this->saveXML()));
		}
	}
}

$sitemap = new sitemap;

$cats = cpg_db_query("SELECT cid FROM {$CONFIG['TABLE_CATEGORIES']}");

while(list($cid) = mysql_fetch_row($cats)){
	$sitemap->AddURL($CONFIG['home_target'] . "?cat=$cid", false, P_CATEGORY, CF_CATEGORY);
	$albs = cpg_db_query("SELECT a.aid, MAX(ctime) FROM {$CONFIG['TABLE_ALBUMS']} AS a INNER JOIN {$CONFIG['TABLE_PICTURES']} AS p ON p.aid = a.aid WHERE category = '$cid' AND visibility = 0 AND ISNULL(alb_password) GROUP BY a.aid");

	while(list($aid, $lastmodtime) = mysql_fetch_row($albs)){
		$sitemap->AddURL("thumbnails.php?album=$aid", $lastmodtime, P_ALBUM, CF_ALBUM);
		$pics = cpg_db_query("SELECT pid, ctime FROM {$CONFIG['TABLE_ALBUMS']} AS a INNER JOIN {$CONFIG['TABLE_PICTURES']} AS p ON p.aid = a.aid WHERE p.aid = '$aid'");

		while(list($pid, $lastmodtime) = mysql_fetch_row($pics)){			
			if (INC_COMMENT_TIMES){
				$com = cpg_db_query("SELECT UNIX_TIMESTAMP(MAX(msg_date)) FROM {$CONFIG['TABLE_COMMENTS']} AS c INNER JOIN {$CONFIG['TABLE_PICTURES']} AS p ON p.pid = c.pid WHERE p.pid = '$pid' GROUP BY p.pid");
				if (mysql_num_rows($com)) list($lastmodtime) = mysql_fetch_row($com);
				mysql_free_result($com);
			}
			$sitemap->AddURL("displayimage.php?pos=-$pid", $lastmodtime, P_DISPLAYIMAGE, CF_DISPLAYIMAGE);
		}
	}
}

// uncomment me to enable listing of user profile urls, this is largely bridge safe but url may just redirect to your bb anywayz
/*
$peeps = cpg_db_query("SELECT {$cpg_udb->field['user_id']} FROM {$cpg_udb->usertable}");

while(list($uid) = mysql_fetch_row($peeps)){			
	$sitemap->AddURL("profile.php?uid=$uid");
}
mysql_free_result($peeps);
*/

$sitemap->complete();