Project

General

Profile

1
<?php
2

    
3
defined('_JEXEC') or die('Access denied');
4

    
5
jimport('joomla.cache.cache');
6
jimport('joomla.environment.uri');
7
jimport('joomla.log.log');
8
jimport('joomla.plugin.plugin');
9

    
10
class plgSystemOpenAireRouter extends JPlugin {
11
	const BUILD_CACHE_ID = 'build';
12
	const PARSE_CACHE_ID = 'parse';
13
	const CACHE_GROUP = 'openaire.router';
14
	const LOG = 'router';
15
	
16
	private static $REWRITE_COMPONENTS = array('com_openaire', 'com_content', 'com_users', 'com_improved_ajax_login', 'com_widgetkit', 'com_jevents', 'com_hoduma');
17
	
18
	private $cache;
19
	
20
	public function __construct(&$subject, $config) {
21
		parent :: __construct($subject, $config);
22
		$this -> cache = JCache :: getInstance();
23
		JLog :: addLogger(array('text_file' => 'router.log'), JLog :: ALL, array(self :: LOG));
24
	}
25
	
26
	public function onAfterInitialise() {
27
		$router = JFactory :: getApplication() -> getRouter();
28
		$router -> attachBuildRule(array($this, 'build'));
29
		$router -> attachParseRule(array($this, 'parse'));
30
	}
31
	
32
	public function build($router, &$uri) {
33
		if ($this -> cache -> getCaching()) {
34
			$cacheId = self :: BUILD_CACHE_ID . ':' . $this -> uriToString($uri);
35
			$newUri = $this -> cache -> get($cacheId, self :: CACHE_GROUP);
36
			if ($newUri === FALSE) {
37
				$newUri = $this -> _build($router, $uri);
38
				if ($newUri !== NULL)
39
					$this -> cache -> store($newUri, $cacheId, self :: CACHE_GROUP);
40
			}
41
		} else
42
			$newUri = $this -> _build($router, $uri);
43
		$uri = $newUri;
44
	}
45
	
46
	public function parse($router, $uri) {
47
		if ($this -> cache -> getCaching()) {
48
			$cacheId = self :: PARSE_CACHE_ID . ':' . $this -> uriToString($uri);
49
			$parameters = $this -> cache -> get($cacheId, self :: CACHE_GROUP);
50
			if ($parameters === FALSE) {
51
				$parameters = $this -> _parse($router, $uri);
52
				if ($parameters !== NULL)
53
					$this -> cache -> store($parameters, $cacheId, self :: CACHE_GROUP);
54
			}
55
		} else
56
			$parameters = $this -> _parse($router, $uri);
57
		return $parameters;
58
	}
59
	
60
	private function _build($router, $uri) {
61
		$parameters = array();		
62
		foreach (explode('&', $uri -> getQuery()) as $parameter) { // get initial parameters
63
			if (($valueIndex = strpos($parameter, '=')) === FALSE) {
64
				$name = $parameter;
65
				$value = NULL;
66
			} else {
67
				$name = substr($parameter, 0, $valueIndex);
68
				$value = substr($parameter, $valueIndex + strlen('='));
69
			}
70
			$parameters[$name] = $value;
71
		}
72
		$newUri = new JURI();
73
		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
74
			$option = substr($parameters['option'], strlen('com_'));
75
			$view = array_key_exists('view', $parameters) ? $parameters['view'] : 'index';
76
			$format = array_key_exists('format', $parameters) ? $parameters['format'] : 'html';
77
			$newUri -> setPath('openaire/' . $option . '/' . $view . '.' . $format);
78
			foreach ($parameters as $name => $value) {
79
				if (($name != 'option') && ($name != 'view') && ($name != 'format'))
80
					$newUri -> setVar(urlencode($name), urlencode($value));
81
			}
82
		} else { // no Joomla! URL; just clean it up
83
			$newUri -> setScheme($uri -> getScheme());
84
			$newUri -> setHost($uri -> getHost());
85
			$newUri -> setPort($uri -> getPort());
86
			$newUri -> setPath($uri -> getPath());
87
			foreach ($parameters as $name => $value)
88
				$newUri -> setVar(urlencode($name), urlencode($value));
89
		}
90
		$newUri -> setFragment(urlencode($uri -> getFragment()));
91
		JLog :: add('Built ' . $this -> uriToString($uri) . ' to ' . $this -> uriToString($newUri), JLog :: INFO, self :: LOG);
92
		return $newUri;
93
	}
94
	
95
	private function _parse($router, &$uri) {
96
		$parameters = array();		
97
		foreach (explode('&', $uri -> getQuery()) as $parameter) { // get initial parameters
98
			if (($valueIndex = strpos($parameter, '=')) === FALSE) {
99
				$name = $parameter;
100
				$value = NULL;
101
			} else {
102
				$name = substr($parameter, 0, $valueIndex);
103
				$value = substr($parameter, $valueIndex + strlen('='));
104
			}
105
			$parameters[$name] = $value;
106
		}
107
		$newUri = new JURI();
108
		$matches = array();
109
		if (preg_match('/^openaire\/([^\/]*)\/([^\.]*)\.(.*)$/', $uri -> getPath(), $matches)) { // Rewritten URL; parse it
110
			$parameters['option'] = 'com_' . $matches[1];
111
			$parameters['view'] = $matches[2];				
112
			$parameters['format'] = $matches[3];
113
			
114
			$newUri -> setPath('index.php');
115
			foreach ($parameters as $name => $value)
116
				$newUri -> setVar(urlencode($name), urlencode($value));
117
		} else {
118
			$newUri -> setScheme($uri -> getScheme());
119
			$newUri -> setHost($uri -> getHost());
120
			$newUri -> setPort($uri -> getPort());
121
			$newUri -> setPath($uri -> getPath());
122
		}
123
		$newUri -> setFragment(urlencode($uri -> getFragment()));		
124
		JLog :: add('Parsed ' . $this -> uriToString($uri) . ' to ' . $this -> uriToString($newUri), JLog :: INFO, self :: LOG);
125
		$uri = $newUri;
126
		return $parameters;
127
	}
128
	
129
	private function uriToString($uri) {
130
		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()));
131
	}
132
}
133

    
(2-2/3)