Location: PHPKode > scripts > googleSiteMap > googlesitemap/class.googlesitemap.php
<?

/**
 * include Snoopy class file
 */
//include_once(dirname(__FILE__)."/includes/lib/Snoopy.class.php");
include_once(dirname(__FILE__)."/Snoopy.class.php");

/**
 * google sitemape create
 * 
 * @package googleSiteMap
 * @author www.phptw.idv.tw
 * @copyright www.phptw.idv.tw
 * @version 2.0
 * @uses snoopy class
 * @link www.phptw.idv.tw
 * @example $URL="http://www.phptw.idv.tw";
 * @example $cacheDIR=dirname(__FILE__)."/cache";
 * @example $siteMap=new googleSiteMap($URL,$cacheDIR);
 * @example $siteMap->printSiteMap();
 * 
 */
class googleSiteMap{
	
	/**
	 * url 
	 *
	 * @access private
	 * @var string
	 */
	var $_url;
	
	/**
	 * sitemap cache
	 *
	 * @access public
	 * @var boolean
	 */
	var $cache=false;
	
	/**
	 * cache dir
	 *
	 * @access private
	 * @var string
	 */
	var $_cacheDir;
	
	/**
	 * chace file name
	 *
	 * @access private
	 * @var string
	 */
	var $_cacheName="gogolemap.tmp";
	
	/**
	 * cache file
	 *
	 * @access private
	 * @var string
	 */
	var $_chaceFile;
	
	/**
	 * out put google site map xml string
	 *
	 * @access private
	 * @var string|boolean
	 */
	var $_string=false;
	
	/**
	 * site url level
	 *
	 * @access public
	 * @var int
	 */
	var $level=0;

	/**
	 * google sitemap aryMap
	 *
	 * @access private
	 * @var array
	 */
	var $_aryMap=array();

	/**
	 * google site map link url Max nums
	 *
	 * @access public
	 * @var int
	 */
	var $maxNums=9999;
	
	/**
	 * google site map cache allow days
	 *
	 * @access public
	 * @var ing
	 */
	var $cacheDay=7;
	
	/**
	 * google site map construct
	 *
	 * @access public
	 * @param string $url
	 * @param steing $dir
	 * @param boolean $cache
	 * @return void
	 */
	function googleSiteMap($url,$dir=false){
		$this->_url=$url;
		if ($dir) {
			$this->setCache($dir);
			$this->cache=true;
		}
		//$this->_compile();
	}
	
	/**
	 * get google site map content
	 *
	 * @access public
	 * @return string
	 */
	function getSiteMapContent(){
		if ($this->_string==false) $this->_compile();
		return $this->_string;
	}
	
	/**
	 * print google site map
	 *
	 * @access public
	 * @return void
	 */
	function printSiteMap(){
		$this->getSiteMapContent();
		ob_start();
		ob_end_clean();
		header('Content-Type: text/xml; charset=utf-8');
		print $this->_string;
		return ;
	}
	
	/**
	 * compile google site map xml
	 *
	 * @access public
	 * @return string
	 */
	function _compile(){
		$aryMap=array();
		
		if ($this->cache) $aryMap=$this->_getCache();
		
		$this->_string="<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
		$this->_string.="<urlset xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.google.com/schemas/sitemap/0.84\" xsi:schemaLocation=\"http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd\">\r\n";
		$this->_string.="\t<url>\r\n";
		$this->_string.="\t\t<loc>".$this->_url."</loc>\r\n";
		$this->_string.="\t\t<lastmod>".date("Y-m-d\TH:i:s\Z",time())."</lastmod>\r\n";
		$this->_string.="\t\t<changefreq>always</changefreq>\r\n";
		$this->_string.="\t\t<priority>1.0</priority>\r\n";
		$this->_string.="\t</url>\r\n";
		
		$this->snoopy=new Snoopy();
		$this->snoopy->fetchlinks($this->_url);
		$url=parse_url($this->_url);
		
		if (is_array($this->snoopy->results)) {
			foreach ($this->snoopy->results as $aryKey => $aryValue){
				if (eregi($url[host],$aryValue)){
					$aryMap[$aryValue][loc]=$this->_replaceURL($aryValue);
					$aryMap[$aryValue][lastmod]=date("Y-m-d\TH:i:s\Z",time());
					$aryMap[$aryValue][changefreq]="daily";
					$aryMap[$aryValue][priority]=0.8;
					$aryCache[$aryValue][loc]=$this->_replaceURL($aryValue);
					$aryCache[$aryValue][lastmod]=date("Y-m-d\TH:i:s\Z",time());
					$aryCache[$aryValue][changefreq]="daily";
					$aryCache[$aryValue][priority]=0.8;
				}
			}
		}
		
		foreach ($aryMap as $aryKey => $aryValue){
			$this->_string.="\t<url>\r\n";
			$this->_string.="\t\t<loc>".$aryValue[loc]."</loc>\r\n";
			$this->_string.="\t\t<lastmod>".$aryValue[lastmod]."</lastmod>\r\n";
			$this->_string.="\t\t<changefreq>".$aryValue[changefreq]."</changefreq>\r\n";
			$this->_string.="\t\t<priority>".$aryValue[priority]."</priority>\r\n";
			$this->_string.="\t</url>\r\n";
		}
		$this->_string.="</urlset>";
		//$this->_string.="";
		
		if ($this->cache) $this->_saveCache($aryCache);
		
		unset($aryMap);
		return $this->_string;
	}

	/**
	* fetch url links
	* 
	* @access private
	* @param string $URL
	* @reurn array
	*/
	function _getURL($URL){
		$snoopy=new Snoopy();
		$snoopy->fetchlinks($URL);
		return $snoopy->results;
	}
	
	/**
	 * set google sitemap cache file
	 *
	 * @access public
	 * @param string $dir
	 * @return void
	 */
	function setCache($dir){
		if (!is_dir($dir)) @mkdir($dir,0700);
		$this->_cacheDir=$dir;
	}
	
	/**
	 * get google site map cache file content
	 *
	 * @access private
	 * @return array
	 */
	function _getCache(){
		if (!is_array($aryMap)) $aryMap=array();
		
		for ($i=0;$i<$this->cacheDay;$i++){
			$this->_getCacheFile("googlemap.".date("Ymd",mktime()-86400*$i).".tmp");
			$fp=fopen($this->_chaceFile,"r");
			$aryTmp=unserialize(trim(fgets($fp)));
			if (!is_array($aryTmp)) $aryTmp=array();
			$aryMap=array_merge($aryMap,$aryTmp);
			fclose($fp);
		}
		
		$this->_deleteCacheFile();
		unset($aryTmp);
		if (is_array($aryMap)) $aryMap=array();
		return $aryMap;
	}
	
	/**
	 * get cache file name
	 *
	 * @access private
	 * @param string $filename
	 */
	function _getCacheFile($filename){
		$this->_chaceFile=$this->_cacheDir."/".$filename;
		if (!is_file($this->_chaceFile)) {
			@touch($this->_chaceFile);
			@chmod($this->_chaceFile,0600);
		}
	}
	
	/**
	 * delete cache
	 *
	 * @access private
	 */
	function _deleteCacheFile(){
		for ($i=$this->cacheDay;$i<=($this->cacheDay+7);$i++){
			$file=$this->_cacheDir."/googlemap.".date("Ymd",mktime()-86400*$i).".tmp";
			@unlink($file);
		}
	}

	/**
	 * save google site map cache file
	 *
	 * @access private
	 * @param array $aryMap
	 */
	function _saveCache($aryMap){
		$fp=fopen($this->_cacheDir."/googlemap.".date("Ymd").".tmp","w");
		fputs($fp,serialize($aryMap));
		fclose($fp);
	}
	
	/**
	 * replace url parameter 
	 *
	 * @param string $url
	 * @return string
	 */
	function _replaceURL($url){
		if (!ereg("&",$url)) return $url;
		return eregi_replace("&","&amp;",$url);
	}
	
	
}
?>
Return current item: googleSiteMap