Ejemplo n.º 1
0
 static function _16($url, $xhtml, $ssl)
 {
     global $FSFRoute_debug;
     global $FSFRoute_menus;
     // get any menu items for fsf
     FSF_Helper::GetRouteMenus();
     //$FSFRoute_debug[] = "<h1>Start URL : $url</h1>";
     // Get the router
     $app = JFactory::getApplication();
     $router = $app->getRouter();
     // Make sure that we have our router
     if (!$router) {
         return null;
     }
     if (strpos($url, '&') !== 0 && strpos($url, 'index.php') !== 0) {
         return $url;
     }
     /* Into JRouter::build */
     //Create the URI object
     $uri = FSFRoute::_createURI($router, $url);
     //Process the uri information based on custom defined rules
     //$router->_processBuildRules($uri);
     // Build RAW URL
     /*if($router->getMode() == JROUTER_MODE_RAW) {
     			$router->_buildRawRoute($uri);
     		}*/
     /* Custom part of JRouter::build */
     // split out parts of the url for
     //$parts = FSFRoute::SplitURL($menu->link);
     //$FSFRoute_debug[] = "URI : <pre>" . print_r($uri,true). "</pre>";
     // work out is we are in an Itemid already, if so, set it as the best match
     if ($uri->hasVar('Itemid')) {
         $bestmatch = $uri->getVar('Itemid');
     } else {
         $bestmatch = '';
     }
     $bestcount = 0;
     $uriquery = $uri->toString(array('query'));
     $urivars = FSFRoute::SplitURL($uriquery);
     // find all the vars in the new url
     $sourcevars = FSFRoute::SplitURL($url);
     //$addedvars = array();
     //$FSFRoute_debug[] = "Source Vars from calling url, to save and emptied fields : <pre>" . print_r($sourcevars,true). "</pre>";
     //$FSFRoute_debug[] = "Initial URI Vars : <pre>" . print_r($urivars,true). "</pre>";
     // check through the menu item for the current url, and add any items to the new url that are missing
     if ($bestmatch && array_key_exists($bestmatch, $FSFRoute_menus)) {
         foreach ($FSFRoute_menus[$bestmatch] as $key => $value) {
             if (!array_key_exists($key, $urivars) && !array_key_exists($key, $sourcevars)) {
                 //$FSFRoute_debug[] = "<span style='color:red; font-size:150%'>Adding source var $key => $value</span><br>";
                 $urivars[$key] = $value;
                 //$addedvars[$key] = $value;
             }
         }
     }
     //$FSFRoute_debug[] = "Source Vars Added : <pre>" . print_r($addedvars,true). "</pre>";
     //$FSFRoute_debug[] = "URI Vars after adding any missing bits : <pre>" . print_r($urivars,true). "</pre>";
     foreach ($FSFRoute_menus as $id => $vars) {
         $count = FSFRoute::MatchVars($urivars, $vars);
         //$FSFRoute_debug[] = "Match against $id => $count<br>";
         if ($count > $bestcount) {
             $bestcount = $count;
             $bestmatch = $id;
         }
     }
     // if no match found, and we are in groups, try to link to admin
     if ($bestcount == 0 && array_key_exists('view', $sourcevars) && $sourcevars['view'] == "groups") {
         // no match found, try to fallback on the main support menu id
         foreach ($FSFRoute_menus as $id => $item) {
             if ($item['view'] == "admin" && (!array_key_exists('layout', $item) || $item['layout'] == "default")) {
                 $bestcount = 1;
                 $bestmatch = $id;
             }
         }
     }
     if ($bestcount == 0) {
         // no match found, try to fallback on the main support menu id
         foreach ($FSFRoute_menus as $id => $item) {
             if ($item['view'] == "main") {
                 $bestcount = 1;
                 $bestmatch = $id;
             }
         }
     }
     if ($bestcount == 0) {
         // still no match found, use any fsf menu
         if (count($FSFRoute_menus) > 0) {
             foreach ($FSFRoute_menus as $id => $item) {
                 $bestcount = 1;
                 $bestmatch = $id;
                 break;
             }
         }
     }
     if ($bestcount > 0) {
         //$FSFRoute_debug[] = "Best Match $bestmatch => $bestcount<br>";
         $uri->setVar('Itemid', $bestmatch);
         /*foreach($addedvars as $key => $value)
         		unset($uri->_vars[$key]);*/
         if ($bestmatch && array_key_exists($bestmatch, $FSFRoute_menus)) {
             foreach ($FSFRoute_menus[$bestmatch] as $key => $value) {
                 if ($uri->hasVar($key) && $uri->getVar($key) == $value) {
                     if ($router->getMode() == JROUTER_MODE_SEF) {
                         //$FSFRoute_debug[] = "<span style='color:red; font-size:150%'>Removing var $key, its part of the menu definition</span><br>";
                         $uri->delVar($key);
                     }
                 }
             }
         }
     } else {
         //echo "No Match found, leaving as is - $url<br>";
     }
     /* End custom part */
     // Build SEF URL : mysite/route/index.php?var=x
     //$FSFRoute_debug[] = "Pre SEF URL : {$uri->toString(array('path', 'query', 'fragment'))}<Br>";
     if ($router->getMode() == JROUTER_MODE_SEF) {
         FSFRoute::_buildSefRoute($router, $uri);
     }
     //$FSFRoute_debug[] = "Post SEF URL : {$uri->toString(array('path', 'query', 'fragment'))}<Br>";
     /* End JRoute::build */
     /* Stuff From JRouterSite */
     // Get the path data
     $route = $uri->getPath();
     //Add the suffix to the uri
     if ($router->getMode() == JROUTER_MODE_SEF && $route) {
         $app = JFactory::getApplication();
         if ($app->getCfg('sef_suffix') && !(substr($route, -9) == 'index.php' || substr($route, -1) == '/')) {
             if ($format = $uri->getVar('format', 'html')) {
                 $route .= '.' . $format;
                 $uri->delVar('format');
             }
         }
         if ($app->getCfg('sef_rewrite')) {
             //Transform the route
             $route = str_replace('index.php/', '', $route);
         }
     }
     //Add basepath to the uri
     $uri->setPath(JURI::base(true) . '/' . $route);
     /* End Stuff From JRouterSite */
     /* Back into FSFRoute::x */
     $url = $uri->toString(array('path', 'query', 'fragment'));
     // Replace spaces
     $url = preg_replace('/\\s/u', '%20', $url);
     //$FSFRoute_debug[] = "pre ssl $url</br>";
     /*
      * Get the secure/unsecure URLs.
      * If the first 5 characters of the BASE are 'https', then we are on an ssl connection over
      * https and need to set our secure URL to the current request URL, if not, and the scheme is
      * 'http', then we need to do a quick string manipulation to switch schemes.
      */
     $ssl = (int) $ssl;
     if ($ssl) {
         $uri = JURI::getInstance();
         // Get additional parts
         static $prefix;
         if (!$prefix) {
             $prefix = $uri->toString(array('host', 'port'));
             //$prefix .= JURI::base(true);
         }
         // Determine which scheme we want
         $scheme = $ssl === 1 ? 'https' : 'http';
         // Make sure our url path begins with a slash
         if (!preg_match('#^/#', $url)) {
             $url = '/' . $url;
         }
         // Build the URL
         $url = $scheme . '://' . $prefix . $url;
     }
     if ($xhtml) {
         $url = str_replace('&', '&amp;', $url);
     }
     /* End FSFRoute::x */
     //$FSFRoute_debug[] = "returning $url<Br>";
     return $url;
 }