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;
// 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'])) {
$tmp_Itemid=$this->checkMenuItems('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=='') {
$tmp_Itemid=$this->checkMenuItems('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=='') {
$tmp_Itemid=$this->checkMenuItems('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=='') {
$tmp_Itemid=$this->checkMenuItems('page', $ii_page);
}
}
if (!empty($ii_arr['Itemid'])) {
$ii_itemid=intval($ii_arr['Itemid']);
if ($ii_itemid && $tmp_Itemid=='') {
$tmp_Itemid=$ii_itemid;
}
}
// 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;
}