|
|
|
@ -9,72 +9,148 @@ class Multilang_Request extends Kohana_Request {
|
|
|
|
|
/**
|
|
|
|
|
* @var string request language code
|
|
|
|
|
*/
|
|
|
|
|
static public $lang = '';
|
|
|
|
|
static public $lang = NULL;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Extension of the request factory method. If none given, the URI will
|
|
|
|
|
* be automatically detected. If the URI contains no language segment, the user
|
|
|
|
|
* will be redirected to the same URI with the default language prepended.
|
|
|
|
|
* If the URI does contain a language segment, I18n and locale will be set.
|
|
|
|
|
* Also, a cookie with the current language will be set. Finally, the language
|
|
|
|
|
* segment is chopped off the URI and normal request processing continues.
|
|
|
|
|
* be automatically detected. If the URI contains no language segment and
|
|
|
|
|
* we don't hide the default language, the user will be redirected to the
|
|
|
|
|
* same URI with the default language prepended.
|
|
|
|
|
* If the URI does contain a language segment, I18n and locale will be set and
|
|
|
|
|
* a cookie with the current language aswell.
|
|
|
|
|
*
|
|
|
|
|
* @param string URI of the request
|
|
|
|
|
* @param Kohana_Cache cache object
|
|
|
|
|
* @param array $injected_routes an array of routes to use, for testing
|
|
|
|
|
* @return Request
|
|
|
|
|
*/
|
|
|
|
|
public static function factory($uri = TRUE, Cache $cache = NULL, $injected_routes = array())
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(!Kohana::$is_cli)
|
|
|
|
|
{
|
|
|
|
|
// Get the list of supported languages
|
|
|
|
|
$langs = (array) Kohana::config('multilang.languages');
|
|
|
|
|
// If we don't hide the default language, we must look for a language code for the root uri
|
|
|
|
|
if(!Kohana::config('multilang.hide_default') && $uri === TRUE && Request::detect_uri() === '')
|
|
|
|
|
{
|
|
|
|
|
$lang = Multilang::find_user_language();
|
|
|
|
|
|
|
|
|
|
// Use the default server protocol
|
|
|
|
|
$protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
|
|
|
|
|
|
|
|
|
|
if($uri === TRUE)
|
|
|
|
|
// Redirect to the root URI, but with language prepended
|
|
|
|
|
header($protocol.' 302 Found');
|
|
|
|
|
header('Location: '.URL::base(TRUE, TRUE).$lang.'/');
|
|
|
|
|
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$request = parent::factory($uri, $cache, $injected_routes);
|
|
|
|
|
Request::$lang = $request->param('lang');
|
|
|
|
|
Multilang::init();
|
|
|
|
|
return $request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ONLY REMOVE THE FRONT SLASHES FROM THE URI
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($uri, Cache $cache = NULL, $injected_routes = array())
|
|
|
|
|
{
|
|
|
|
|
// Remove the front slashes
|
|
|
|
|
$uri = ltrim($uri, '/');
|
|
|
|
|
|
|
|
|
|
// Initialise the header
|
|
|
|
|
$this->_header = new HTTP_Header(array());
|
|
|
|
|
|
|
|
|
|
// Assign injected routes
|
|
|
|
|
$this->_injected_routes = $injected_routes;
|
|
|
|
|
|
|
|
|
|
// Cleanse query parameters from URI (faster that parse_url())
|
|
|
|
|
$split_uri = explode('?', $uri);
|
|
|
|
|
$uri = array_shift($split_uri);
|
|
|
|
|
|
|
|
|
|
// Initial request has global $_GET already applied
|
|
|
|
|
if (Request::$initial !== NULL)
|
|
|
|
|
{
|
|
|
|
|
if ($split_uri)
|
|
|
|
|
{
|
|
|
|
|
// We need the current URI
|
|
|
|
|
$uri = Request::detect_uri();
|
|
|
|
|
parse_str($split_uri[0], $this->_get);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Normalize URI
|
|
|
|
|
$uri = ltrim($uri, '/');
|
|
|
|
|
// Detect protocol (if present)
|
|
|
|
|
// Always default to an internal request if we don't have an initial.
|
|
|
|
|
// This prevents the default index.php from being able to proxy
|
|
|
|
|
// external pages.
|
|
|
|
|
if (Request::$initial === NULL OR strpos($uri, '://') === FALSE)
|
|
|
|
|
{
|
|
|
|
|
$processed_uri = Request::process_uri($uri, $this->_injected_routes);
|
|
|
|
|
|
|
|
|
|
// Look for a supported language in the first URI segment
|
|
|
|
|
if(!preg_match('~^(?:'.implode('|', array_keys($langs)).')(?=/|$)~i', $uri, $matches))
|
|
|
|
|
if ($processed_uri === NULL)
|
|
|
|
|
{
|
|
|
|
|
// If we don't have any, we look whether it's normal (is it in the no lang routes ?) or we need to append it
|
|
|
|
|
if(Request::process_uri($uri, Route::nolang_routes()))
|
|
|
|
|
{
|
|
|
|
|
return parent::factory($uri, $cache);
|
|
|
|
|
}
|
|
|
|
|
throw new HTTP_Exception_404('Unable to find a route to match the URI: :uri', array(
|
|
|
|
|
':uri' => $uri,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We can't find a language, we're gonna need to look deeper
|
|
|
|
|
$lang = Multilang::find_default();
|
|
|
|
|
// Store the URI
|
|
|
|
|
$this->_uri = $uri;
|
|
|
|
|
|
|
|
|
|
// Use the default server protocol
|
|
|
|
|
$protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
|
|
|
|
|
// Store the matching route
|
|
|
|
|
$this->_route = $processed_uri['route'];
|
|
|
|
|
$params = $processed_uri['params'];
|
|
|
|
|
|
|
|
|
|
// Redirect to the same URI, but with language prepended
|
|
|
|
|
header($protocol.' 302 Found');
|
|
|
|
|
header('Location: '.URL::base(TRUE, TRUE).$lang.'/'.$uri);
|
|
|
|
|
// Is this route external?
|
|
|
|
|
$this->_external = $this->_route->is_external();
|
|
|
|
|
|
|
|
|
|
// Stop execution
|
|
|
|
|
exit;
|
|
|
|
|
if (isset($params['directory']))
|
|
|
|
|
{
|
|
|
|
|
// Controllers are in a sub-directory
|
|
|
|
|
$this->_directory = $params['directory'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store the controller
|
|
|
|
|
$this->_controller = $params['controller'];
|
|
|
|
|
|
|
|
|
|
if (isset($params['action']))
|
|
|
|
|
{
|
|
|
|
|
// Store the action
|
|
|
|
|
$this->_action = $params['action'];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Use the default action
|
|
|
|
|
$this->_action = Route::$default_action;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Language found in the URI
|
|
|
|
|
Request::$lang = strtolower($matches[0]);
|
|
|
|
|
// These are accessible as public vars and can be overloaded
|
|
|
|
|
unset($params['controller'], $params['action'], $params['directory']);
|
|
|
|
|
|
|
|
|
|
Multilang::init();
|
|
|
|
|
// Params cannot be changed once matched
|
|
|
|
|
$this->_params = $params;
|
|
|
|
|
|
|
|
|
|
// Remove language from URI
|
|
|
|
|
$uri = (string) substr($uri, strlen(Request::$lang));
|
|
|
|
|
// Apply the client
|
|
|
|
|
$this->_client = new Request_Client_Internal(array('cache' => $cache));
|
|
|
|
|
}
|
|
|
|
|
// Continue normal request processing with the URI without language*/
|
|
|
|
|
return parent::factory($uri, $cache, $injected_routes);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Create a route
|
|
|
|
|
$this->_route = new Route($uri);
|
|
|
|
|
|
|
|
|
|
// Store the URI
|
|
|
|
|
$this->_uri = $uri;
|
|
|
|
|
|
|
|
|
|
// Set external state
|
|
|
|
|
$this->_external = TRUE;
|
|
|
|
|
|
|
|
|
|
// Setup the client
|
|
|
|
|
$this->_client = new Request_Client_External(array('cache' => $cache));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|