示例#1
0
文件: router.php 项目: rich20/Kunena
/**
 * Build SEF URL
 *
 * All SEF URLs are formatted like this:
 *
 * http://site.com/menuitem/1-category-name/10-subject/[view]/[layout]/[param1]-value1/[param2]-value2?param3=value3&param4=value4
 *
 * - If catid exists, category will always be in the first segment
 * - If there is no catid, second segment for message will not be used (param-value: id-10)
 * - [view] and [layout] are the only parameters without value
 * - all other segments (task, id, userid, page, sel) are using param-value format
 *
 * NOTE! Only major variables are using SEF segments
 *
 * @param $query
 * @return segments
 */
function KunenaBuildRoute(&$query) {
	KUNENA_PROFILER ? KunenaProfiler::instance()->start('function '.__FUNCTION__.'()') : null;

	$segments = array ();

	// If Kunena SEF is not enabled, do nothing
	if (! KunenaRoute::$config->sef) {
		KUNENA_PROFILER ? KunenaProfiler::instance()->start('function '.__FUNCTION__.'()') : null;
		return $segments;
	}

	// Get menu item
	if (isset ( $query ['Itemid'] )) {
		static $menuitems = array();
		$Itemid = $query ['Itemid'] = (int) $query ['Itemid'];
		if (!isset($menuitems[$Itemid])) {
			$menuitems[$Itemid] = JFactory::getApplication()->getMenu ()->getItem ( $Itemid );
			if (!$menuitems[$Itemid]) {
				// Itemid doesn't exist or is invalid
				unset ($query ['Itemid']);
			}
		}
		$menuitem = $menuitems[$Itemid];
	}

	// Safety check: we need view in order to create SEF URLs
	if (!isset ( $menuitem->query ['view'] ) && empty ( $query ['view'] )) {
		KUNENA_PROFILER ? KunenaProfiler::instance()->start('function '.__FUNCTION__.'()') : null;
		return $segments;
	}

	// Get view for later use (query wins menu item)
	$view = isset ( $query ['view'] ) ? (string) preg_replace( '/[^a-z]/', '', $query ['view'] ) : $menuitem->query ['view'];

	// Get default values for URI variables
	if (isset(KunenaRouter::$views[$view])) {
		$defaults = KunenaRouter::$views[$view];
	}
	// Check all URI variables and remove those which aren't needed
	foreach ( $query as $var => $value ) {
		if (isset ( $defaults [$var] ) && !isset ( $menuitem->query [$var] ) && $value == $defaults [$var] ) {
			// Remove URI variable which has default value
			unset ( $query [$var] );
		} elseif ( isset ( $menuitem->query [$var] ) && $value == $menuitem->query [$var] && $var != 'Itemid' && $var != 'option' ) {
			// Remove URI variable which has the same value as menu item
			unset ( $query [$var] );
		}
	}

	// We may have catid also in the menu item (it will not be in URI)
	$numeric = !empty ( $menuitem->query ['catid'] );

	// Support URIs like: /forum/12-my_category
	if (!empty ( $query ['catid'] ) && ($view == 'category' || $view == 'topic' || $view == 'home')) {
		// TODO: ensure that we have view=categories/category/topic
		$catid = ( int ) $query ['catid'];
		if ($catid) {
			$numeric = true;

			if (KunenaRouter::$catidcache === null) {
				KunenaRouter::loadCategories ();
			}
			if (isset ( KunenaRouter::$catidcache [$catid] )) {
				$catname = KunenaRouter::$catidcache [$catid];
			}
			if (empty ( $catname )) {
				// If category name is empty (or doesn't exist), use numeric catid
				$segments [] = $catid;
			} elseif (KunenaRoute::$config->sefcats && isset(KunenaRouter::$sefviews[$view]) && !KunenaRouter::isCategoryConflict($menuitem, $catid, $catname)) {
				// If there's no naming conflict, we can use category name
				$segments [] = $catname;
			} else {
				// By default use 123-category_name
				$segments [] = "{$catid}-{$catname}";
			}
			// This segment fully defines category view so the variable is no longer needed
			if ($view == 'category') {
				unset ( $query ['view'] );
			}
		}
		unset ( $query ['catid'] );
	}

	// Support URIs like: /forum/12-category/123-topic
	if (!empty ( $query ['id'] ) && $numeric) {
		$id = (int) $query ['id'];
		if ($id) {
			$subject = KunenaRouter::stringURLSafe ( KunenaForumTopicHelper::get($id)->subject );
			if (empty ( $subject )) {
				$segments [] = $id;
			} else {
				$segments [] = "{$id}-{$subject}";
			}
			// This segment fully defines topic view so the variable is no longer needed
			if ($view == 'topic') {
				unset ( $query ['view'] );
			}
		}
		unset ( $query ['id'] );
	} else {
		// No id available, do not use numeric variable for mesid
		$numeric = false;
	}

	// View gets added only when we do not use short URI for category/topic
	if (!empty ( $query ['view'] )) {
		// Use filtered value
		$segments [] = $view;
	}

	// Support URIs like: /forum/12-category/123-topic/reply
	if (!empty ( $query ['layout'] )) {
		// Use filtered value
		$segments [] = (string) preg_replace( '/[^a-z]/', '', $query ['layout'] );
	}

	// Support URIs like: /forum/12-category/123-topic/reply/124
	if (isset ( $query ['mesid'] ) && $numeric) {
		$segments [] = (int) $query ['mesid'];
		unset ( $query ['mesid'] );
	}

	// Support URIs like: /forum/user/128-matias
	if (isset ( $query ['userid'] ) && $view == 'user') {
		$segments [] = (int) $query ['userid'] .'-'.KunenaRouter::stringURLSafe ( KunenaUserHelper::get((int)$query ['userid'])->getName() );
		unset ( $query ['userid'] );
	}

	unset ( $query ['view'], $query ['layout'] );

	// Rest of the known parameters are in var-value form
	foreach ( KunenaRouter::$parsevars as $var=>$dummy ) {
		if (isset ( $query [$var] )) {
			$segments [] = "{$var}-{$query[$var]}";
			unset ( $query [$var] );
		}
	}

	KUNENA_PROFILER ? KunenaProfiler::instance()->stop('function '.__FUNCTION__.'()') : null;
	return $segments;
}