Source for file RequestEngine.class.php
Documentation is available at RequestEngine.class.php
* @copyright 2009-2010 NADEO
* The Request Engine helps handling GET variables and referers as well as
* creating links and redirection. It also handles session ID propagation when
* the client doesn't accept cookies.
static private $instance;
protected $requestParams =
array();
protected $params =
array();
protected $protectedParams =
array();
protected $globalParams =
array();
protected $registerRefererAtDestruct;
* Use this methode to retrieve a reference on the request object from anywhere in the code
self::$instance =
new $class;
protected function __construct()
if(get_magic_quotes_gpc())
$this->params =
array_map('stripslashes', $this->params);
$this->requestParams =
$this->params;
* Retrieves a GET parameter, or the default value if not found
function get($name, $default=
null)
return $this->params[$name];
* Retrieves a GET parameter, or throws an exception if not found or null
* @throws RequestParameterNotFoundException
* @param string Optional human readable name for error dialog
return $this->params[$name];
throw
new UserException($message);
throw
new InvalidArgumentException($name);
function set($name, $value)
$this->params[$name] =
$value;
* Deletes a GET parameter
unset
($this->params[$name]);
* Restores a GET parameter to the value it had when the page was loaded
$this->params[$name] =
$this->requestParams[$name];
* Returns an URL containing all the currently defined GET parameters
* // Current page: http://url/index.php?toto=a&foo=bar
* $request = RequestEngine::getInstance();
* $request->createLink('page.php'); // Returns http://url/page.php?toto=a&foo=bar
* @param string The filename
* @param boolean Whether the first parameter is a relative URL (default:
* true). Set this parameter to false if you want to create and external
function createLink($file=
null, $relativePath=
true)
return $this->createLinkString($file, $relativePath, $arr);
* Returns an URL with the request parameters specified as method arguments
* // Current page: http://url/index.php?toto=a&foo=bar&bla=bla
* $request = RequestEngine::getInstance();
* $request->createLinkArgList("page.php", "toto", "bla"); // Returns http://url/page.php?toto=a&bla=bla
* @param string The filename (eg: "index.php" or "admin/login.php")
$args[$elt] =
$this->params[$elt];
return $this->createLinkString($file, true, $args);
* Returns an URL with the request parameters specified as method arguments
* @param string The absolute URL
$args[$elt] =
$this->params[$elt];
* Creates a Manialink redirection to the specified file with GET
* parameters specified as method arguments.
* $request->redirectManialink("index.php", "param1", "param2");
* @param string The filename of the link target
* Creates a Manialink redirection to the specified absolute URI
* Creates a Manialink redirection to the previously registered referer, or
* the index if no referer was previously registered
* Registers the '$name' parameter as protected parameters. Protected
* parameters are always removed from the parameter array when the page is
$this->protectedParams[] =
$name;
unset
($this->params[$name]);
* Registers the "$name" parameter as protected parameters. Global
* parameters atr always removed from the parameter array and saved as a
* session parameter when the page is loaded.
$value =
$this->requestParams[$name];
$session->set($name, $value);
* Registers the current page as referer
$this->registerRefererAtDestruct =
$link;
* Returns the referer, or the specified default page, or index.php
$referer =
$session->get('referer');
if($this->registerRefererAtDestruct)
$session->set('referer', rawurlencode($this->registerRefererAtDestruct));
protected function createLinkString($file=
null, $relativePath=
true, $params=
array())
// If absolute path, there's nothing to do
// If relative path, we have to compute the link
if($this->URLBase ===
null)
if($this->URLPath ===
null)
explode('/', $this->URLPath))).
'/';
if($this->URLPath ==
'.' ||
$this->URLPath ==
'/')
$this->URLFile =
$file ?
$file :
basename($_SERVER['SCRIPT_FILENAME']);
$params['rp'] =
$this->URLPath.
$this->URLFile;
$link =
$this->URLBase.
$this->URLPath.
$this->URLFile;
// Create parameter string
class RequestParameterNotFoundException extends Exception {}