plugins:googlesitemap:googlesitemap_code

<?php
 
class NP_GoogleSitemap extends NucleusPlugin
{
 
   /* ================================================================
	* GoogleSitemap for Nucleus
	*
	* Copyright 2005 by Niels Leenheer
	* ================================================================
	* This program is free software and open source software; you can redistribute
	* it and/or modify it under the terms of the GNU General Public License as
	* published by the Free Software Foundation; either version 2 of the License,
	* or (at your option) any later version.
	*
	* This program is distributed in the hope that it will be useful, but WITHOUT
	* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
	* more details.
	*
	* You should have received a copy of the GNU General Public License along
	* with this program; if not, write to the Free Software Foundation, Inc.,
	* 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  or visit
	* http://www.gnu.org/licenses/gpl.html
	* ================================================================
	*/
 
 
	function getName()
	{
		return 'GoogleSitemap';
	}
 
	function getAuthor()
	{
		return 'Niels Leenheer + shizuki';
	}
 
	function getURL()
	{
		return 'http://japan.nucleuscms.org/wiki/plugins:googlesitemap';
	}
 
	function getVersion()
	{
		return '0.9';
	}
 
	function getDescription()
	{
		return _G_SITEMAP_DESC;
	}
 
	function getEventList()
	{
		return array(
					 'PostAddItem',
					 'PreSendContentType'
					);
	}
 
	function supportsFeature($feature)
	{
    	switch($feature) {
	        case 'SqlTablePrefix':
	        	return 1;
	        default:
	    		return 0;
		}
	}
 
	function event_PreSendContentType($data)
	{
		global $CONF, $manager, $blogid;
 
		$mcategories = $this->pluginCheck('MultipleCategories');
		if ($mcategories) {
			if (method_exists($mcategories, 'getRequestName')) {
				$subReq = $mcategories->getRequestName();
			} else {
				$subReq = 'subcatid';
			}
		}
 
		if (!$blogid) {
			$blogid = $CONF['DefaultBlog'];
		} else {
			if (is_numeric($blogid)) {
				$blogid = intval($blogid);
			} else {
				$blogid = intval(getBlogIDFromName($blogid));
			}
		}
 
		$b =& $manager->getBlog($blogid);
		$BlogURL = $b->getURL();
 
		if ( substr($BlogURL, -1) != '/'
		  && substr($BlogURL, -4) != '.php') {
			$BlogURL .= '/';
		}
 
		if (getVar('virtualpath')) {
			$info = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', getVar('virtualpath'));
		} elseif (serverVar('PATH_INFO')) {
			$info = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', serverVar('PATH_INFO'));
		} else {
			return;
		}
 
		$path_arr  = explode('/', $info);
		$PcMap     = $this->getBlogOption($blogid, 'PcSitemap');
		$MobileMap = $this->getBlogOption($blogid, 'MobileSitemap');
		if ( end($path_arr) == $PcMap
		  || (!empty($MobileMap) && end($path_arr) == $MobileMap) ) {
			$sitemap = array();
			if ( $this->getOption('AllBlogMap') == 'yes'
			  && $blogid == $CONF['DefaultBlog']) {
				$blogQuery  = 'SELECT * '
							. 'FROM %s '
							. 'ORDER BY bnumber';
				$blogQuery  = sprintf($blogQuery, sql_table('blog'));
				$blogResult = sql_query($blogQuery);
			} else {
				$blogQuery   = 'SELECT * '
							 . 'FROM %s '
							 . 'WHERE bnumber = %d';
				$blogQuery   = sprintf($blogQuery, sql_table('blog'), $blogid);
				$blogResult  = sql_query($blogQuery);
				$currentBlog = TRUE;
			}
			while ($blogs = mysql_fetch_array($blogResult)) {
				$blog_id = intval($blogs['bnumber']);
				if (  $this->getBlogOption($blog_id, 'IncludeSitemap') == 'yes'
				   || !empty($currentBlog)) {
					$temp_b  =& $manager->getBlog($blog_id);
					$TempURL =  $temp_b->getURL();
					$SelfURL =  $TempURL;
 
					$URLMode = $CONF['URLMode'];
					if (substr($TempURL, -4) == '.php') {
						$CONF['URLMode'] = 'normal';
					}
 
					$usePathInfo = ($CONF['URLMode'] == 'pathinfo');
 
					if (substr($SelfURL, -1) == '/') {
 
						if ($usePathInfo) {
							$SelfURL = substr($SelfURL, 0, -1);
						} else {
							$SelfURL = $SelfURL . 'index.php';
						}
 
					} elseif (substr($SelfURL, -4) != '.php') {
 
						if ($usePathInfo) {
							$SelfURL = $SelfURL;
						} else {
							$SelfURL = $SelfURL . '/index.php';
						}
 
					}
 
					$CONF['ItemURL']     = $SelfURL;
					$CONF['CategoryURL'] = $SelfURL;
 
					if ( substr($TempURL, -1) != '/'
					  && substr($TempURL, -4) != '.php') {
						$TempURL .= '/';
					}
 
					$patternURL = '/^' . preg_replace('/\//', '\/', $BlogURL) . '/';
 
					if (preg_match($patternURL, $TempURL)) {
 
						$sitemap[] = array(
							'loc'        => $this->_prepareLink($SelfURL, $TempURL),
							'priority'   => '1.0',
							'changefreq' => 'daily'
						);
 
						$catQuery  = 'SELECT * '
								   . 'FROM %s '
								   . 'WHERE cblog = %d '
								   . 'ORDER BY catid';
						$catTable  = sql_table('category');
						$catQuery  = sprintf($catQuery, $catTable, $blog_id);
						$catResult = sql_query($catQuery);
 
						while ($cat = mysql_fetch_array($catResult)) {
 
							$cat_id = intval($cat['catid']);
							$Link   = createCategoryLink($cat_id);
							$catLoc =$this->_prepareLink($SelfURL, $Link);
 
							$sitemap[] = array(
								'loc'        => $catLoc,
								'priority'   => '1.0',
								'changefreq' => 'daily'
							);
 
							if ($mcategories) {
								$scatQuery  = 'SELECT * '
											. 'FROM %s '
											. 'WHERE catid = %d '
//											. 'ORDER BY scatid';
											. 'ORDER BY ordid';
								$scatTable  = sql_table('plug_multiple_categories_sub');
								$scatQuery  = sprintf($scatQuery, $scatTable, $cat_id);
								$scatResult = sql_query($scatQuery);
 
								while ($scat = mysql_fetch_array($scatResult)) {
 
									$scat_id = intval($scat['scatid']);
									$params  = array($subReq => $scat_id);
									$Link    = createCategoryLink($cat_id, $params);
									$scatLoc = $this->_prepareLink($SelfURL, $Link);
 
									$sitemap[] = array(
										'loc'        => $scatLoc,
										'priority'   => '1.0',
										'changefreq' => 'daily'
									);
 
								}
 
							}
 
						}
 
						$itemQuery  = 'SELECT *, '
									. '       UNIX_TIMESTAMP(itime) AS timestamp '
									. 'FROM %s '
									. 'WHERE iblog  = %d '
									. 'AND   idraft = 0 '
									. 'ORDER BY itime DESC';
						$itemTable  = sql_table('item');
						$itemQuery  = sprintf($itemQuery, $itemTable, $blog_id);
						$itemResult = sql_query($itemQuery);
 
						while ($item = mysql_fetch_array($itemResult)) {
 
							$item_id = intval($item['inumber']);
							$Link    = createItemLink($item_id);
							$tz      = date('O', $item['timestamp']);
							$tz      = substr($tz, 0, 3) . ':' . substr($tz, 3, 2);
							$itemLoc = $this->_prepareLink($SelfURL, $Link);
 
							if (time() - $item['timestamp'] < 86400 * 2) {
								$fq = 'hourly';
							} elseif (time() - $item['timestamp'] < 86400 * 14) {
								$fq = 'daily'; 
							} elseif (time() - $item['timestamp'] < 86400 * 62) {
								$fq = 'weekly';
							} else {
								$fq = 'monthly';
							}
							$lastmod = gmdate('Y-m-d\TH:i:s', $item['timestamp'])
									 . $tz;
 
							$sitemap[] = array(
								'loc'        => $itemLoc,
								'lastmod'    => $lastmod,
								'priority'   => '1.0',
								'changefreq' => $fq
							);
 
						}
 
					}
 
				}
 
				if ($CONF['URLMode'] != $URLMode) {
					$CONF['URLMode'] = $URLMode;
				}
 
			}
 
			$manager->notify('SiteMap', array ('sitemap' => & $sitemap));
 
			header ("Content-type: application/xml");
 
	// old Google sitemap protocol ver.0.84
//	$sitemapHeader  = "<" . "?xml version='1.0' encoding='UTF-8'?" . ">\n\n";
//	. "\t<urlset" . ' xmlns="http://www.google.com/schemas/sitemap/0.84"' . "\n";
//	. "\t" . 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n";
//	. "\t" . 'xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84' . "\n";
//	. "\t" . '        http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">' . "\n";
 
	// new sitemap common protocol ver 0.9
	$sitemapHeader  = "<" . "?xml version='1.0' encoding='UTF-8'?" . ">\n\n"
	. '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n"
	. '         xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' . "\n"
	. '         http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"' . "\n"
	. '         xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"';
	// uncomment and edit next line when you need "example_schema"
//	$sitemapHeader .= '         xmlns:example="http://www.example.com/schemas/example_schema"';
	$sitemapHeader .= '>';
 
			echo $sitemapHeader;
			while (list(, $url) = each($sitemap)) {
 
				echo "\t<url>\n";
 
				while (list($key, $value) = each($url)) {
					if ($key == 'loc') {
						$value = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', $value);
						$data  = "\t\t<" . $key . ">"
							   . htmlspecialchars($value, ENT_QUOTES, _CHARSET)
							   . "</" . $key . ">\n";
					} else {
						$data  = "\t\t<" . $key . ">"
							   . htmlspecialchars($value, ENT_QUOTES, _CHARSET)
							   . "</" . $key . ">\n";
					}
					echo $data;
				}
 
				echo "\t</url>\n";
 
			}
 
			echo "</urlset>\n";
			exit;
 
		}
	}
 
	function pluginCheck($pluginName)
	{
		global $manager;
		if (!$manager->pluginInstalled('NP_' . $pluginName)) {
			return;
		} else {
			$plugin =& $manager->getPlugin('NP_' . $pluginName);
			return $plugin;
		}
	}
 
	function _prepareLink($base, $url) {
		if (substr($url, 0, 7) == 'http://') {
			return $url;
		} else {
			return $base . $url;
		}
	}
 
	function event_PostAddItem(&$data)
	{
		global $manager, $CONF;
 
		$item_id = intval($data['itemid']);
		$blog_id = intval(getBlogIDFromItemID($item_id));
 
		if ($this->getBlogOption($blog_id, 'PingGoogle') == 'yes') {
 
			$b     =& $manager->getBlog($blog_id);
			$b_url =  $b->getURL();
 
			if (substr($b_url, -4) == '.php') $CONF['URLMode'] = 'normal';
			$usePathInfo = ($CONF['URLMode'] == 'pathinfo');
 
			if (substr($b_url, -1) == '/') {
				if (!$usePathInfo) {
					$b_url .= 'index.php?virtualpath=';
				}
			} elseif (substr($b_url, -4) == '.php') {
				$b_url .= '?virtualpath=';
			} else {
				if ($usePathInfo) {
					$b_url = $b_url . '/';
				} else {
					$b_url = $b_url . '/index.php?virtualpath=';
				}
			}
 
			$siteMap = $this->getBlogOption($blog_id, 'PcSitemap');
			$url     = 'http://www.google.com/webmasters/sitemaps/ping?sitemap='
				     . urlencode($b_url . $siteMap);
			$url     = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', $url);
			$fp      = @fopen($url, 'r');
			@fclose($fp);
			$MobileMap = $this->getBlogOption($blog_id, 'MobileSitemap');
			if (!empty($MobileMap)) {
				$url = 'http://www.google.com/webmasters/sitemaps/ping?sitemap='
					 . urlencode($b_url . $MobileMap);
				$url = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', $url);
				$fp  = @fopen($url, 'r');
				@fclose($fp);
			}
		}
	}
 
	function init()
	{
		global $admin;
		$language = ereg_replace( '[\\|/]', '', getLanguageName());
		if (file_exists($this->getDirectory() . $language.'.php')) {
			include_once($this->getDirectory() . $language.'.php');
		}else {
			include_once($this->getDirectory() . 'english.php');
		}
	}
 
	function install()
	{
		$this->createOption('AllBlogMap',         _G_SITEMAP_ALLB, 'yesno', 'yes');
		$this->createBlogOption('PingGoogle',     _G_SITEMAP_PING, 'yesno', 'yes');
		$this->createBlogOption('IncludeSitemap', _G_SITEMAP_INC,  'yesno', 'yes');
		$this->createBlogOption('PcSitemap',      _G_SITEMAP_PCSM, 'text',  'sitemap.xml');
		$this->createBlogOption('MobileSitemap',  _G_SITEMAP_MBSM, 'text',  '');
	}
}
 
plugins/googlesitemap/googlesitemap_code.txt · 最終更新: 2011/03/23 21:28 (外部編集)