The Easiest Way to Save and Share Code Snippets on the web

Virtemart improved url method

php-brief | by: roland_soos

posted: Nov, 4th 2011 | jump to bottom

	function url($text, $createAbsoluteURI=false, $encodeAmpersands=true, $ignoreSEF=false ) {
		global $mm_action_url, $page, $mainframe;
 
		if(!defined('_VM_IS_BACKEND')) {
 
			// Strip the parameters from the $text variable and parse to a temporary array
			$tmp_text=str_replace('amp;','',substr($text,strpos($text,'?')));
			if(substr($tmp_text,0,1)=='?') $tmp_text=substr($tmp_text,1);
 
			parse_str($tmp_text,$ii_arr);
 
			// Init the temp. Itemid
			$tmp_Itemid='';
 
			$db = new ps_DB;
      static $u = null;
      if($u == null){
        $u = array();
        $db->setQuery( "SELECT id, params FROM #__menu WHERE link='index.php?option=com_virtuemart' AND published=1");
        $rows = $db->loadRowList();
 
        foreach($rows AS $row){
          $es = explode("\n", $row[1]);
          foreach($es AS $e){
            $c = explode("=", $e);
            switch($c[0]){
              case 'product_id':
              case 'category_id':
              case 'flypage':
              case 'page':
                if($c[1] != '') $u[$c[0]][$c[1]] = $row[0];
                break;
            }
          }
        }
      }
 
			// Check if there is a menuitem for a product_id (highest priority)
			if (!empty($ii_arr['product_id'])) {
				if ($ii_product_id=intval($ii_arr['product_id'])) {
          if(isset($u['product_id']) && isset($u['product_id'][$ii_product_id])) $tmp_Itemid = $u['product_id'][$ii_product_id];
				} 
			}
			// Check if there is a menuitem for a category_id
			// This only checks for the exact category ID, it might be good to check for parents also. But at the moment, this would produce a lot of queries
			if (!empty($ii_arr['category_id'])) {
				$ii_cat_id=intval($ii_arr['category_id']);
				if ( $ii_cat_id && $tmp_Itemid=='') {
          if(isset($u['category_id']) && isset($u['category_id'][$ii_product_id])) $tmp_Itemid = $u['category_id'][$ii_cat_id];
				}
			}
			// Check if there is a menuitem for a flypage
			if (!empty($ii_arr['flypage'])) {
				$ii_flypage=$db->getEscaped(vmget($ii_arr,'flypage'));
				if ($ii_flypage && $tmp_Itemid=='') {
          if(isset($u['flypage']) && isset($u['flypage'][$ii_product_id])) $tmp_Itemid = $u['flypage'][$ii_flypage];
				}
			}
			// Check if there is a menuitem for a page
			if (!empty($ii_arr['page'])) {
				$ii_page=$db->getEscaped(vmget($ii_arr,'page' ));
				if ($ii_page && $tmp_Itemid=='') {
          if(isset($u['page']) && isset($u['page'][$ii_product_id])) $tmp_Itemid = $u['page'][$ii_page];
				}
			}
			// If we haven't found an Itemid, use the standard VM-Itemid
			$Itemid = "&Itemid=" . ($tmp_Itemid ? $tmp_Itemid : $this->getShopItemid()); 
		} else {
			$Itemid = NULL;
		}
 
		// split url into base ? path
		$limiter = strpos($text, '?');
		if ($limiter === false) {
			if (!strstr($text, "=")) { // $text recognized to be parameter-list (bug?)
				$base = NULL;
				$params = $text;
			}
			else { // text recognized to be url without parameters
				$base = $mm_action_url;
				$params = $text;
			}
		}
		else { // base?params
			$base = substr($text, 0, $limiter);
			$params = substr($text, $limiter+1);
		}
 
		// normalize base (cut off multislashes)
		$base = str_replace("//", "/", $base);
		$base = str_replace(":/", "://", $base);
 
		// add script name to naked base url
		// TODO: Improve
		if ($base == URL || $base == SECUREURL)
			$base .= basename($_SERVER['SCRIPT_NAME']);
		if (!basename($base))
			$base .= basename($_SERVER['SCRIPT_NAME']);
 
		// append "&option=com_virtuemart&Itemid=XX"
		$params .= (!strstr($params, $this->component_name)) ? ($params ? "&" : NULL) . $this->component_name : NULL;
		$params .= $Itemid;
 
		if (vmIsAdminMode() && strstr($text, 'func') !== false)
			$params .= ($params ? "&" : NULL) . 'vmtoken=' . vmSpoofValue($this->getSessionId());
 
		if (!defined( '_VM_IS_BACKEND' )) {
			// index3.php is not available in the frontend!
			$base = str_replace("index3.php", "index2.php", $base);
 
			$url = basename($base) . "?" . $params;
 
			// make url absolute
			if ($createAbsoluteURI && !substr($url, 0, 4 ) != "http") {
				$url = (stristr($text, SECUREURL) ? SECUREURL : URL) . substr($url, $url[0] == '/' ? 1 : 0);
			}
 
			if( class_exists('JRoute') && !$ignoreSEF && $mainframe->getCfg('sef') )
				$url = JRoute::_($url);
			else if( function_exists('sefRelToAbs') && !$ignoreSEF && !defined( '_JLEGACY' ) )
				$url = sefRelToAbs($url);
 
		}
		else // backend
			$url = ($_SERVER['SERVER_PORT'] == 443 ? SECUREURL : URL) . "administrator/" . basename($base) . "?" . $params;
 
		$url = $encodeAmpersands ? vmAmpReplace($url) : str_replace('&', '&', $url);
 
		return $url;
	}
49 views