cache = JCache :: getInstance(); JLog :: addLogger(array('text_file' => 'router.log'), JLog :: ALL, array(self :: LOG)); } public function onAfterInitialise() { $router = JFactory :: getApplication() -> getRouter(); $router -> attachBuildRule(array($this, 'build')); $router -> attachParseRule(array($this, 'parse')); } public function build($router, &$uri) { if ($this -> cache -> getCaching()) { $cacheId = self :: BUILD_CACHE_ID . ':' . $this -> uriToString($uri); $newUri = $this -> cache -> get($cacheId, self :: CACHE_GROUP); if ($newUri === FALSE) { $newUri = $this -> _build($router, $uri); if ($newUri !== NULL) $this -> cache -> store($newUri, $cacheId, self :: CACHE_GROUP); } } else $newUri = $this -> _build($router, $uri); $uri = $newUri; } public function parse($router, $uri) { if ($this -> cache -> getCaching()) { $cacheId = self :: PARSE_CACHE_ID . ':' . $this -> uriToString($uri); $parameters = $this -> cache -> get($cacheId, self :: CACHE_GROUP); if ($parameters === FALSE) { $parameters = $this -> _parse($router, $uri); if ($parameters !== NULL) $this -> cache -> store($parameters, $cacheId, self :: CACHE_GROUP); } } else $parameters = $this -> _parse($router, $uri); return $parameters; } private function _build($router, $uri) { $parameters = array(); foreach (explode('&', $uri -> getQuery()) as $parameter) { // get initial parameters if (($valueIndex = strpos($parameter, '=')) === FALSE) { $name = $parameter; $value = NULL; } else { $name = substr($parameter, 0, $valueIndex); $value = substr($parameter, $valueIndex + strlen('=')); } $parameters[$name] = $value; } $newUri = new JURI(); if (($uri -> getScheme() == NULL) && ($uri -> getHost() == NULL) && ($uri -> getPort() == NULL) && ($uri -> getPath() == 'index.php') && array_key_exists('option', $parameters) && in_array($parameters['option'], self :: $REWRITE_COMPONENTS)) { // Joomla! component URL; rewrite it $option = substr($parameters['option'], strlen('com_')); $view = array_key_exists('view', $parameters) ? $parameters['view'] : 'index'; $format = array_key_exists('format', $parameters) ? $parameters['format'] : 'html'; $newUri -> setPath('openaire/' . $option . '/' . $view . '.' . $format); foreach ($parameters as $name => $value) { if (($name != 'option') && ($name != 'view') && ($name != 'format')) $newUri -> setVar(urlencode($name), urlencode($value)); } } else { // no Joomla! URL; just clean it up $newUri -> setScheme($uri -> getScheme()); $newUri -> setHost($uri -> getHost()); $newUri -> setPort($uri -> getPort()); $newUri -> setPath($uri -> getPath()); foreach ($parameters as $name => $value) $newUri -> setVar(urlencode($name), urlencode($value)); } $newUri -> setFragment(urlencode($uri -> getFragment())); JLog :: add('Built ' . $this -> uriToString($uri) . ' to ' . $this -> uriToString($newUri), JLog :: INFO, self :: LOG); return $newUri; } private function _parse($router, &$uri) { $parameters = array(); foreach (explode('&', $uri -> getQuery()) as $parameter) { // get initial parameters if (($valueIndex = strpos($parameter, '=')) === FALSE) { $name = $parameter; $value = NULL; } else { $name = substr($parameter, 0, $valueIndex); $value = substr($parameter, $valueIndex + strlen('=')); } $parameters[$name] = $value; } $newUri = new JURI(); $matches = array(); if (preg_match('/^openaire\/([^\/]*)\/([^\.]*)\.(.*)$/', $uri -> getPath(), $matches)) { // Rewritten URL; parse it $parameters['option'] = 'com_' . $matches[1]; $parameters['view'] = $matches[2]; $parameters['format'] = $matches[3]; $newUri -> setPath('index.php'); foreach ($parameters as $name => $value) $newUri -> setVar(urlencode($name), urlencode($value)); } else { $newUri -> setScheme($uri -> getScheme()); $newUri -> setHost($uri -> getHost()); $newUri -> setPort($uri -> getPort()); $newUri -> setPath($uri -> getPath()); } $newUri -> setFragment(urlencode($uri -> getFragment())); JLog :: add('Parsed ' . $this -> uriToString($uri) . ' to ' . $this -> uriToString($newUri), JLog :: INFO, self :: LOG); $uri = $newUri; return $parameters; } private function uriToString($uri) { return (($uri -> getScheme() == NULL) ? '' : ($uri -> getScheme() . ':')) . (($uri -> getHost() == NULL) ? '' : ('//' . $uri -> getHost())) . (($uri -> getPort() == NULL) ? '' : (':' . $uri -> getPort())) . (($uri -> getPath() == NULL) ? '' : ('/' . $uri -> getPath())) . (($uri -> getQuery() == NULL) ? '' : ('?' . $uri -> getQuery())) . (($uri -> getFragment() == NULL) ? '' : ('#' . $uri -> getFragment())); } }