ManiaLib 1.0b1 API Reference

Source for file RequestEngine.class.php

Documentation is available at RequestEngine.class.php

  1. <?php
  2. /**
  3.  * @author Maxime Raoust
  4.  * @copyright 2009-2010 NADEO
  5.  * @package ManiaLib
  6.  */
  7.  
  8. /**
  9.  * Request engine
  10.  * 
  11.  * The Request Engine helps handling GET variables and referers as well as
  12.  * creating links and redirection. It also handles session ID propagation when
  13.  * the client doesn't accept cookies.
  14.  * 
  15.  * @package ManiaLib
  16.  */
  17. {
  18.     /**#@+
  19.      * @ignore
  20.      */
  21.     static private $instance;
  22.     protected $requestParams array();
  23.     protected $params array();
  24.     protected $protectedParams array();
  25.     protected $globalParams array();
  26.     protected $URLBase;
  27.     protected $URLPath;
  28.     protected $URLFile;
  29.     protected $registerRefererAtDestruct;
  30.     /**#@-*/
  31.     
  32.     /**
  33.      * Use this methode to retrieve a reference on the request object from anywhere in the code
  34.      * 
  35.      * @return RequestEngine 
  36.      */
  37.     public static function getInstance()
  38.     {
  39.         if (!self::$instance)
  40.         {
  41.             $class = __CLASS__;
  42.             self::$instance new $class;
  43.         }
  44.         return self::$instance;
  45.     }
  46.     
  47.     /**
  48.      * @ignore
  49.      */
  50.     protected function __construct()
  51.     {
  52.         $this->params $_GET;
  53.         if(get_magic_quotes_gpc())
  54.         {
  55.             $this->params array_map('stripslashes'$this->params);
  56.         }
  57.         $this->requestParams $this->params;
  58.     }
  59.     
  60.     /**
  61.      * Retrieves a GET parameter, or the default value if not found
  62.      * 
  63.      * @param string 
  64.      * @param mixed 
  65.      * @return mixed 
  66.      */
  67.     function get($name$default=null)
  68.     {
  69.         if(array_key_exists($name$this->params))
  70.         {
  71.             return $this->params[$name];
  72.         }    
  73.         else
  74.         {
  75.             return $default;
  76.         }    
  77.     }
  78.     
  79.     /**
  80.      * Retrieves a GET parameter, or throws an exception if not found or null
  81.      * 
  82.      * @throws RequestParameterNotFoundException
  83.      * @param string 
  84.      * @param string Optional human readable name for error dialog
  85.      * @return mixed 
  86.      */
  87.     function getStrict($name$message=null)
  88.     {
  89.         if(array_key_exists($name$this->params&& $this->params[$name])
  90.         {
  91.             return $this->params[$name];
  92.         }
  93.         elseif($message)
  94.         {
  95.             throw new UserException($message);
  96.         }
  97.         else
  98.         {
  99.             throw new InvalidArgumentException($name);    
  100.         }
  101.     }
  102.         
  103.     /**
  104.      * Sets a GET parameter
  105.      * 
  106.      * @param string 
  107.      * @param mixed 
  108.      */
  109.     function set($name$value)
  110.     {
  111.         $this->params[$name$value;
  112.     }
  113.     
  114.     /**
  115.      * Deletes a GET parameter
  116.      * 
  117.      * @param string 
  118.      */
  119.     function delete($name)
  120.     {
  121.         unset($this->params[$name]);
  122.     }
  123.     
  124.     /**
  125.      * Restores a GET parameter to the value it had when the page was loaded
  126.      * 
  127.      * @param string 
  128.      */
  129.     function restore($name)
  130.     {
  131.         if(array_key_exists($name$this->requestParams))
  132.         {
  133.             $this->params[$name$this->requestParams[$name];
  134.         }
  135.         else
  136.         {
  137.             $this->delete($name);
  138.         }
  139.     }
  140.     
  141.     /**
  142.      * Returns an URL containing all the currently defined GET parameters
  143.      * 
  144.      * Example:
  145.      * <code>
  146.      * // Current page: http://url/index.php?toto=a&foo=bar
  147.      * $request = RequestEngine::getInstance();
  148.      * $request->createLink('page.php'); // Returns http://url/page.php?toto=a&foo=bar
  149.      * </code>
  150.      * 
  151.      * @param string The filename
  152.      * @param boolean Whether the first parameter is a relative URL (default:
  153.      *  true). Set this parameter to false if you want to create and external
  154.      *  link.
  155.      * @return string 
  156.      */
  157.     function createLink($file=null$relativePath=true)
  158.     {
  159.         $arr $this->params;
  160.         return $this->createLinkString($file$relativePath$arr);
  161.     }
  162.     
  163.     /**
  164.      * Returns an URL with the request parameters specified as method arguments
  165.      * (eg.  )
  166.      * 
  167.      * Example:
  168.      * <code>
  169.      * // Current page: http://url/index.php?toto=a&foo=bar&bla=bla
  170.      * $request = RequestEngine::getInstance();
  171.      * $request->createLinkArgList("page.php", "toto", "bla"); // Returns http://url/page.php?toto=a&bla=bla
  172.      * </code>
  173.      * 
  174.      * @param string The filename (eg: "index.php" or "admin/login.php")
  175.      * @return string 
  176.      */
  177.     function createLinkArgList($file=null)
  178.     {
  179.         $arr func_get_args();
  180.         array_shift($arr);
  181.         $args array();
  182.         foreach($arr as $elt)
  183.         {
  184.             if(array_key_exists($elt$this->params))
  185.             {
  186.                 $args[$elt$this->params[$elt];
  187.             }    
  188.         }
  189.         return $this->createLinkString($filetrue$args);
  190.     }
  191.     /**
  192.      * Returns an URL with the request parameters specified as method arguments
  193.      * 
  194.      * @param string The absolute URL
  195.      * @return string 
  196.      */
  197.     function createAbsoluteLinkArgList($absoluteLink)
  198.     {
  199.         $arr func_get_args();
  200.         array_shift($arr);
  201.         $args array();
  202.         foreach($arr as $elt)
  203.         {
  204.             if(array_key_exists($elt$this->params))
  205.             {
  206.                 $args[$elt$this->params[$elt];
  207.             }    
  208.         }
  209.         return $absoluteLink.($args '?'.http_build_query($args'');
  210.     }
  211.     
  212.     /**
  213.      * Creates a Manialink redirection to the specified file with GET
  214.      * parameters specified as method arguments.
  215.      * 
  216.      * Example:
  217.      * <code>
  218.      * $request->redirectManialink("index.php", "param1", "param2");
  219.      * </code>
  220.      * 
  221.      * @param string The filename of the link target
  222.      */
  223.     function redirectManialink($file='index.php')
  224.     {
  225.         $arr func_get_args();
  226.         array_shift($arr);
  227.         array_unshift($arr$file);
  228.         $link call_user_func_array(array($this,  'createLinkArgList')$arr);
  229.         
  230.         Manialink::redirect($link);
  231.     }
  232.     
  233.     /**
  234.      * Creates a Manialink redirection to the specified absolute URI
  235.      * 
  236.      * @param string 
  237.      */
  238.     function redirectManialinkAbsolute($absoluteUri)
  239.     {
  240.         Manialink::redirect($absoluteUri);
  241.     }
  242.     
  243.     /**
  244.      * Creates a Manialink redirection to the previously registered referer, or
  245.      * the index if no referer was previously registered
  246.      */
  247.     function redirectToReferer()
  248.     {
  249.         Manialink::redirect($this->getReferer());
  250.     }
  251.     
  252.     /**
  253.      * Registers the '$name' parameter as protected parameters. Protected
  254.      * parameters are always removed from the parameter array when the page is
  255.      * loaded.
  256.      * @param string 
  257.      */
  258.     function registerProtectedParam($name)
  259.     {
  260.         $this->protectedParams[$name;
  261.         unset($this->params[$name]);
  262.     }
  263.     
  264.     /**
  265.      * Registers the "$name" parameter as protected parameters. Global
  266.      * parameters atr always removed from the parameter array and saved as a
  267.      * session parameter when the page is loaded.
  268.      * @param string 
  269.      */
  270.     function registerGlobalParam($name)
  271.     {
  272.         if(array_key_exists($name,$this->requestParams))
  273.         {
  274.             $value $this->requestParams[$name];
  275.             if($value !== null)
  276.             {
  277.                 $session SessionEngine::getInstance();
  278.                 $session->set($name$value);
  279.                 $this->registerProtectedParam($name);
  280.             }
  281.         }
  282.     }
  283.     
  284.     /**
  285.      * Registers the current page as referer
  286.      */
  287.     function registerReferer()
  288.     {
  289.         $session SessionEngine::getInstance();
  290.         $link $this->createLink();
  291.         $this->registerRefererAtDestruct $link;
  292.     }
  293.     
  294.     /**
  295.      * Returns the referer, or the specified default page, or index.php
  296.      * @param string 
  297.      */
  298.     function getReferer($default=null)
  299.     {
  300.         $session SessionEngine::getInstance();
  301.         $referer $session->get('referer');
  302.         if($referer)
  303.         {
  304.             return rawurldecode($referer);
  305.         }
  306.         elseif($default)
  307.         {
  308.             return $default;
  309.         }
  310.         else
  311.         {
  312.             return APP_URL.'index.php';
  313.         }
  314.     }
  315.     
  316.     /**
  317.      * @ignore
  318.      */
  319.     function __destruct()
  320.     {
  321.         if($this->registerRefererAtDestruct)
  322.         {
  323.             $session SessionEngine::getInstance();
  324.             $session->set('referer'rawurlencode($this->registerRefererAtDestruct));
  325.         }
  326.     }
  327.     
  328.     /**
  329.      * @ignore
  330.      */
  331.     protected function createLinkString($file=null$relativePath=true$params=array())
  332.     {
  333.         // Check for context
  334.         if(!isset($_SERVER))
  335.         {
  336.             return $file;
  337.         }
  338.         
  339.         // If absolute path, there's nothing to do
  340.         if(!$relativePath)
  341.         {
  342.             $link $file;
  343.         }
  344.         
  345.         // If relative path, we have to compute the link
  346.         else
  347.         {
  348.             // URL base
  349.             if($this->URLBase === null)
  350.             {
  351.                 $this->URLBase APP_USE_SHORT_MANIALINKS APP_MANIALINK APP_URL;
  352.             }
  353.             
  354.             // URL path
  355.             if($this->URLPath === null)
  356.             {
  357.                 $this->URLPath str_replace('\\''/',
  358.                     str_ireplace(realpath(APP_WWW_PATH)'',
  359.                         realpath(dirname($_SERVER['SCRIPT_FILENAME']))
  360.                     ));
  361.                 $this->URLPath implode('/'array_filter(
  362.                     explode('/'$this->URLPath))).'/';
  363.                 if($this->URLPath == '.' || $this->URLPath == '/')
  364.                 {
  365.                     $this->URLPath '';
  366.                 }
  367.             }
  368.             
  369.             // URL file
  370.             $this->URLFile $file $file basename($_SERVER['SCRIPT_FILENAME']);
  371.         
  372.             // Create the link
  373.             if(APP_USE_SHORT_MANIALINKS)
  374.             {
  375.                 $link $this->URLBase;
  376.                 $params['rp'$this->URLPath.$this->URLFile;
  377.             }
  378.             else
  379.             {
  380.                 $link $this->URLBase.$this->URLPath.$this->URLFile;
  381.             }
  382.         }
  383.         
  384.         
  385.         // Create parameter string
  386.         if(count($params))
  387.         {
  388.             $params '?'.http_build_query($params'''&')
  389.         }
  390.         else
  391.         {
  392.             $params '';
  393.         }
  394.                 
  395.         return $link.$params;
  396.     }
  397. }
  398.  
  399. /**
  400.  * @package ManiaLib
  401.  * @ignore
  402.  */
  403. class RequestParameterNotFoundException extends Exception  {}
  404.  
  405. ?>