plugins:magicalurl2:magicalurl2_code

<?php
 
/**
  *
  * .htaccess
  * RewriteEngine on
  * RewriteCond %{REQUEST_FILENAME} !-f
  * RewriteCond %{REQUEST_FILENAME} !-d
  * RewriteRule ^(.*)$ index.php?virtualpath=$1 [L,QSA]
  *
  **/
 
class NP_MagicalURL2 extends NucleusPlugin
{
	function getName()
	{
		return 'MagicalURL2';
	}
 
	function getAuthor()
	{
		return 'nakahara21 + fujisaki + shizuki';
	}
 
	function getURL()
	{
		return 'http://japan.nucleuscms.org/wiki/plugins:magicalurl2';
	}
 
	function getVersion()
	{
		return '1.0.1.2_080602';
	}
 
	function getMinNucleusVersion()
	{
		return 322;
	}
 
	function getMinNucleusPatchLevel()
	{
		return 0;
	}
 
	function getDescription()
	{
		return 'Replaces the /item/1234 URLs by /item_1234.html.';
	}
 
	function supportsFeature($what) 
	{
		switch ($what)
		{
			case 'SqlTablePrefix':
				return 1;
			default:
				return 0;
		}
	}
 
	function init()
	{
		$this->ext = '.html';
		global $manager, $CONF;
// Keys initialize
		$CONF['ArchiveKey']  = 'archive';
		$CONF['ArchivesKey'] = 'archives';
		$CONF['MemberKey']   = 'member';
		$CONF['ItemKey']     = 'item';
		$CONF['CategoryKey'] = 'catid';
		$CONF['BlogKey']     = 'blogid';
	}
 
	function install()
	{
		if (getNucleusVersion() < '324') {
// Can't install when faster requier Nucleus Core Version
			$ver_min = (getNucleusVersion() < $this->getMinNucleusVersion());
			$pat_min = ((getNucleusVersion() == $this->getMinNucleusVersion()) &&
						(getNucleusPatchLevel() < $this->getMinNucleusPatchLevel()));
			if ($ver_min || $pat_min) {
				global $DIR_LIBS;
				// uninstall plugin again...
				include_once($DIR_LIBS . 'ADMIN.php');
				$admin = new ADMIN();
				$admin->deleteOnePlugin($this->getID());
 
				// ...and show error
				$admin->error(_ERROR_NUCLEUSVERSIONREQ .
				$this->getMinNucleusVersion() . ' patch ' .
				$this->getMinNucleusPatchLevel());
			}
		}
//Plugins sort
		$plugTable = sql_table('plugin');
		$myid      = intval($this->getID());
		$res       = sql_query('SELECT pid, porder FROM ' . $plugTable);
		while ($p = mysql_fetch_array($res)) {
			$updateQuery = 'UPDATE %s '
						 . 'SET    porder = %d '
						 . 'WHERE  pid    = %d';
			if (($pid = intval($p['pid'])) == $myid) {
				$q      = sprintf($updateQuery, $plugTable, 1, $myid);
				sql_query($q);
			} else {
				$porder = intval($p['porder']);
				$q      = sprintf($updateQuery, $plugTable, $porder + 1, $pid);
				sql_query($q);
			}
		}
 
	}
 
	function getEventList() 
	{
		return  array(
			'ParseURL',
			'GenerateURL',
			'PreItem',
			'PostItem',
			'PreSkinParse',
		);
	}
 
	function pluginCheck($pluginName)
	{
		global $manager;
		if (!$manager->pluginInstalled('NP_' . $pluginName)) {
			return;
		}
		$plugin =& $manager->getPlugin('NP_' . $pluginName);
		return $plugin;
	}
 
	function event_ParseURL($data)
	{
		global $CONF, $manager, $blogid, $itemid, $catid;
		global $memberid, $archivelist, $archive, $query;
// initialize
		$info  =  $data['info'];
		$exlen = strlen($this->ext);
		$complete =& $data['complete'];
		if ($complete) {
			return;
		}
 
		// debug_01
//		$CONF['alertOnHeadersSent'] = 0;
 
// Use NP_MultipleCategories ?
		$mcategories  = $this->pluginCheck('MultipleCategories');
		if ($mcategories) {
			$mcategories->event_PreSkinParse(array());
			global $subcatid;
			if (method_exists($mcategories, 'getRequestName')) {
				$subrequest = $mcategories->getRequestName();
			} else {
				$subrequest = 'subcatid';
			}
		}
 
// initialize and sanitize '$blogid'
		if (!$blogid) {
			if ( getVar('blogid') ) {
				if ( is_numeric(getVar('blogid')) ) {
					$blogid = intval(getVar('blogid'));
				} else {
					$blogid = intval(getBlogIDFromName(getVar('blogid')));
				}
			} else {
				$blogid = $CONF['DefaultBlog'];
			}
		} else {
			if (is_numeric($blogid)) {
				$blogid = intval($blogid);
			} else {
				$blogid = intval(getBlogIDFromName($blogid));
			}
		}
 
		if (!$info) {
			if (serverVar('PATH_INFO')) {
				$info = serverVar('PATH_INFO');
			} elseif (getNucleusVersion() < 330) {
				if (getVar('virtualpath')) {
					$info = getVar('virtualpath');
				}
			} else {
				return;
			}
		}
		if ($exlen) {
			$exlen = 0 - $exlen;
			$info  = substr($info, 0, $exlen);
		}
 
		// debug_02
//		echo $info;
//		echo "\n";
 
// Sanitize 'PATH_INFO'
		$info  = trim($info, '/');
		$vPath = explode("_", $info);
		foreach($vPath as $key => $value) {
			$value       = urlencode($value);
			$value       = preg_replace('|[^a-zA-Z0-9-~+_.?#=&;,/:@%]|i', '', $value);
			$vPath[$key] = $value;
		}
		if (phpversion() >= '4.1.0') {
			$_SERVER['PATH_INFO'] = implode('_', $vPath) . '.html';
		}
		global $HTTP_SERVER_VARS;
		$HTTP_SERVER_VARS['PATH_INFO'] = implode('_', $vPath) . '.html';
 
		// debug_03
//		print_r($vPath);
//		echo "\n";
 
		$i = 1;
		foreach($vPath as $pathName) {
			switch ($pathName) {
// decode URL
				// for blogsgetAllBlogOptions($name)
				case 'blogid':
				case $CONF['BlogKey']:
					if (isset($vPath[$i]) && is_numeric($vPath[$i])) {
						$blogid = intval($vPath[$i]);
					}
 
		// debug_04
//		echo "HIT BlogKey!\nblogid='$blogid'\n";
 
					break;
				// for items
				case 'item':
				case $CONF['ItemKey']:
					if (isset($vPath[$i]) && is_numeric($vPath[$i])) {
						$itemid = intval($vPath[$i]);
						$blogid = intval(getBlogIDFromItemID($itemid));
					}
 
		// debug_05
//		echo "HIT ItemKey!\nblogid='$blogid'\nitemid='$itemid'\n";
 
					break;
				// for categories
				case 'catid':
				case $CONF['CategoryKey']:
					if (isset($vPath[$i]) && is_numeric($vPath[$i])) {
						$catid  = intval($vPath[$i]);
						$blogid = intval(getBlogIDFromCatID($catid));
					}
 
		// debug_06
//		echo "HIT CategoryKey!\nblogid='$blogid'\ncatid='$catid'\n";
 
					break;
				// for subcategories
				case $subrequest:
					$c = $i - 2;
					$subCat = (isset($vPath[$i]) && is_numeric($vPath[$i]));
					if ($mcategories && $subCat && $i >= 3 && is_numeric($vPath[$c])) {
						$subcatid = intval($vPath[$i]);
						$catid    = intval($vPath[$c]);
						$blogid   = intval(getBlogIDFromCatID($catid));
					}
 
		// debug_07
//		echo "HIT SubRequest!\nblogid='$blogid'\ncatid='$catid'\nsubcatid='$subcatid'\n";
 
					break;
				// for archives
				case 'archivelist':
				case $CONF['ArchivesKey']:
					if (isset($vPath[$i]) && is_numeric($vPath[$i])) {
						$archivelist = intval($vPath[$i]);
						$blogid      = $archivelist;
					}
 
		// debug_08
//		echo "HIT ArchiveListKey!\nblogid='$blogid'\n";
 
					break;
				// for archive
				case 'archive':
				case $CONF['ArchiveKey']:
					$ar = $i + 1;
					if (isset($vPath[$i])) {
						$darc  = (ereg('([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})', $vPath[$i]));
						$marc  = (ereg('([0-9]{4})-([0-9]{1,2})', $vPath[$i]));
						$yarc  = (ereg('([0-9]{4})', $vPath[$i]));
						$adarc = (ereg('([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})', $vPath[$ar]));
						$amarc = (ereg('([0-9]{4})-([0-9]{1,2})', $vPath[$ar]));
						$ayarc = (ereg('([0-9]{4})', $vPath[$ar]));
						$arc   = (!$darc && !$marc && !$yarc);
						$aarc  = ($adarc || $amarc || $ayarc);
						$carc  = ($darc || $marc || $yarc);
						if (is_numeric($vPath[$i]) && $arc && isset($vPath[$ar]) && $aarc) {
							list($y, $m, $d) = sscanf($vPath[$ar], '%d-%d-%d');
							$blogid = intval($vPath[$i]);
							if (!empty($d)) {
								$archive = sprintf('%04d-%02d-%02d', $y, $m, $d);
							} elseif (!empty($m)) {
								$archive = sprintf('%04d-%02d',      $y, $m);
							} else {
								$archive = sprintf('%04d',           $y);
							}
						} elseif (!is_numeric($vPath[$i]) && $carc) {
							list($y, $m, $d) = sscanf($vPath[$i], '%d-%d-%d');
							if (!empty($d)) {
								$archive = sprintf('%04d-%02d-%02d', $y, $m, $d);
							} elseif (!empty($m)) {
								$archive = sprintf('%04d-%02d',      $y, $m);
							} else {
								$archive = sprintf('%04d',           $y);
							}
						}
					}
 
		// debug_09
//		echo "HIT ArchiveKey!\nblogid='$blogid'\narchive='$archive'\n";
 
					break;
				// for member
				case 'member':
				case $CONF['MemberKey']:
					if (isset($vPath[$i]) && is_numeric($vPath[$i])) {
						$memberid = intval($vPath[$i]);
					}
 
		// debug_10
//		echo "HIT MemberKey!\nmemberid='$memberid'\n";
 
					break;
				// for special
				case 'special':
					if (isset($vPath[$i]) && !is_numeric($vPath[$i])) {
						$_REQUEST['special'] = strtolower($vPath[$i]);
					}
 
		// debug_11
//		$tmpspkey = strtolower($vPath[$i]);
//		echo "HIT SpecialSkin!\nspecial='$tmpspkey'\n";
 
					break;
				// for TagEX
				case 'tag':
					break;
				// for PageSwitch
				case 'page':
					if (isset($vPath[$i]) && is_numeric($vPath[$i])) {
						$_GET['page'] = intval($vPath[$i]);
					}
 
		// debug_12
//		$pagekey = intval($vPath[$i]);
//		echo "HIT Page!\npage='$pagekey'\n";
 
					break;
				default:
					break;
			}
 
		// debug_13
//		echo '$i=' . $i . "\n";
 
			$i++;
		}
 
		$uri          = createBlogidLink($blogid);
		$CONF['Self'] = rtrim($uri, '/');
		$complete     = TRUE;
		return ;
	}
 
	function event_GenerateURL(&$data)
	{
 
		// if another plugin already generated the URL
		if ($data['completed']) {
			return;
		}
 
		global $CONF, $manager;
		$params = $data['params'];
 
		if (isset($params['extra']['blogid'])) {
			$bid = $params['extra']['blogid'];
			unset($params['extra']['blogid']);
		} elseif ($params['blogid']) {
			$bid = intval($params['blogid']);
			unset($params['blogid']);
		} else {
			global $blogid;
			if (!$blogid) {
				$bid = $CONF['DefaultBlog'];
			} else {
				$bid = $blogid;
			}
		}
 
		$ext = $this->ext;
		switch ($data['type']) {
			case 'item':
				$iid  = intval($params['itemid']);
				$bid  = intval(getBlogIDFromItemID($iid));
				$burl = $this->getBaseURL($bid);
				$url  = $burl . $CONF['ItemKey'] . '_' . $iid;
				break;
 
			case 'archive':
				list($y, $m, $d) = sscanf($params['archive'], '%d-%d-%d');
				if (!empty($d)) {
					$achv = sprintf('%4d-%02d-%02d', $y, $m, $d);
				} elseif (!empty($m)) {
					$achv = sprintf('%4d-%02d', $y, $m);
				} else {
					$achv = sprintf('%4d', $y);
				}
				$burl = $this->getBaseURL($bid);
				$url  = $burl . $CONF['ArchiveKey'] . '_' . $achv;
				break;
 
			case 'archivelist':
				$burl = $this->getBaseURL($bid);
				$url  = $burl . $CONF['ArchivesKey'] . '_' . $bid;
				break;
 
			case 'category':
				$cid  = intval($params['catid']);
				$bid  = intval(getBlogIDFromCatID($cid));
				$burl = $this->getBaseURL($bid);
				$url  = $burl . $CONF['CategoryKey'] . '_' . $cid;
				break;
 
			case 'blog':
				$burl = $this->getBaseURL($bid);
				if (empty($params['extra']) && substr($burl, -1) == '_') {
					$burl = substr($burl, 0, -1);
				} elseif (empty($params['extra']) && substr($burl, -1) == '/') {
					$ext = '';
				}
				$url = $burl;
				break;
 
			case 'member':
				$mid  = intval($params['memberid']);
				$burl = $this->getBaseURL($bid);
				$url  = $burl . $CONF['MemberKey'] . '_' . $mid;
				break;
			default:
				break;
 
		}
 
		if ($url) {
				$data['url'] = $this->_addLinkParams($url, $params['extra']) . $ext;
				$data['completed'] = true;
		}
 
	}
 
	function _addLinkParams($link, $params)
	{
		global $CONF;
		if (is_array($params)) {
			foreach ($params as $param => $value) {
				$link .= '_' . $param . '_' . urlencode($value);
			}
		}
		if (strstr($link, '//')) {
			$link = preg_replace("/([^:])\/\//", "$1/", $link);
		}
		return $link;
	}
 
// getBaseURL
	function getBaseURL($bid)
	{
		global $CONF, $manager;
		$exlen = strlen($this->ext);
		if ($exlen) {
			$exlen = 0 - $exlen;
		}
		if ($bid == $CONF['DefaultBlog']) {
			$uri = $CONF['IndexURL'];
 
		// debug_18
//		addToLog(INFO, "BLOG is Default");
 
		} else {
 
		// debug_19
//		addToLog(INFO, "BLOG isnot Default");
 
			$b   =& $manager->getBlog($bid);
			$uri =  $b->getURL();
		}
		$exstr = substr($uri, $exlen);
		if ($bid != $CONF['DefaultBlog'] && $uri == $CONF['IndexURL']) {
			if ($exstr == $this->ext) {
				$pos = 0 - strrpos($uri, '/');
				$uri = substr($uri, 0, $pos) . '/';
			} elseif (substr($uri, -1) != '/') {
				$uri .= '/';
			}
			$uri .= $CONF['BlogKey'] . '_' . $bid . '_';
		} elseif (substr($uri, -1) != '/') {
			if ($exstr == $this->ext) {
				$uri = substr($uri, 0, $exlen) . '_';
			} elseif (substr($uri, -1) != '/') {
				$uri .= '/';
			}
		}
		return $uri;
	}
 
 
 
// merge NP_RightURL
	function event_PreSkinParse($data)
	{
		global $CONF, $manager, $blog, $catid, $itemid, $subcatid;
		global $memberid;
		if (!$blog) {
			$b =& $manager->getBlog($CONF['DefaultBlog']);
		} else {
			$b =& $blog;
		}
		$blogurl = $b->getURL();
 
		if (!$blogurl) {
			if($blog) {
				$tmpBlog =& $manager->getBlog($CONF['DefaultBlog']);
				$blogurl =  $tmpBlog->getURL();
			}
			if (!$blogurl) {
				$blogurl = $CONF['IndexURL'];
				if ($CONF['URLMode'] != 'pathinfo') {
					if ($data['type'] == 'pageparser') {
						$blogurl .= 'index.php';
					} else {
						$blogurl  = $CONF['Self'];
					}
				}
			}
		}
		if ($CONF['URLMode'] == 'pathinfo') {
			if (substr($blogurl, -1) == '/') {
				$blogurl = substr($blogurl, 0, -1);
			}
		}
		$CONF['BlogURL']        = $blogurl;
		$CONF['ItemURL']        = $blogurl;
		$CONF['CategoryURL']    = $blogurl;
		$CONF['ArchiveURL']     = $blogurl;
		$CONF['ArchiveListURL'] = $blogurl;
		$CONF['SearchURL']      = $blogurl;
//      $CONF['MemberURL']      = $blogurl;
	}
 
	function event_PreItem($data)
	{
		global $CONF, $manager;
 
		$itemid   =  intval($data['item']->itemid);
		$itemblog =& $manager->getBlog(getBlogIDFromItemID($itemid));
		$blogurl  =  $itemblog->getURL();
		if (!$blogurl) {
			$b =& $manager->getBlog($CONF['DefaultBlog']);
			if (!($blogurl = $b->getURL())) {
				$blogurl = $CONF['IndexURL'];
				if ($CONF['URLMode'] != 'pathinfo') {
					if ($data['type'] == 'pageparser') {
						$blogurl .= 'index.php';
					} else {
						$blogurl  = $CONF['Self'];
					}
				}
			}
		}
		if ($CONF['URLMode'] == 'pathinfo') {
			if (substr($blogurl, -1) == '/') {
				$blogurl = substr($blogurl, 0, -1);
			}
		}
		$CONF['BlogURL']        = $blogurl;
		$CONF['ItemURL']        = $blogurl;
		$CONF['CategoryURL']    = $blogurl;
		$CONF['ArchiveURL']     = $blogurl;
		$CONF['ArchiveListURL'] = $blogurl;
//      $CONF['MemberURL']      = $blogurl;
	}
 
	function event_PostItem($data)
	{
		global $CONF, $manager, $blog;
		if (!$blog) {
			$b =& $manager->getBlog($CONF['DefaultBlog']);
		} else {
			$b =& $blog;
		}
		$blogurl = $b->getURL();
		if (!$blogurl) {
			if($blog) {
				$b_tmp   =& $manager->getBlog($CONF['DefaultBlog']);
				$blogurl =  $b_tmp->getURL();
			}
			if (!$blogurl) {
				$blogurl = $CONF['IndexURL'];
				if ($CONF['URLMode'] != 'pathinfo') {
					if ($data['type'] == 'pageparser') {
						$blogurl .= 'index.php';
					} else {
						$blogurl  = $CONF['Self'];
					}
				}
			}
		}
		if ($CONF['URLMode'] == 'pathinfo') {
			if (substr($blogurl, -1) == '/') {
				$blogurl = substr($blogurl, 0, -1);
			}
		}
		$CONF['BlogURL']        = $blogurl;
		$CONF['ItemURL']        = $blogurl;
		$CONF['CategoryURL']    = $blogurl;
		$CONF['ArchiveURL']     = $blogurl;
		$CONF['ArchiveListURL'] = $blogurl;
//      $CONF['MemberURL']      = $CONF['Self'];
	}
// merge NP_RightURL end
 
}
 
plugins/magicalurl2/magicalurl2_code.txt · 最終更新: 2011/03/23 21:28 (外部編集)