<?php
	
	lt_include( PLOG_CLASS_PATH."class/data/textfilter.class.php" );

    /**
     * \ingroup Net
     *
     * Represents a request in our system. Doing so we can in the future
     * change the format requests are recognized since all the dirty
     * stuff would be done here. After that, using an interface of the type
     * getValue( "param" ) would be enough to access those values, regardless
     * if the request was /index.php?op=Default&articleId=10 or
     * /index.php/op/Default/articleId/10.
     */
    class Request extends Properties 
	{

        var $_paramsRaw;
		var $_stripSlashes;
		
		const REQUEST_OUTPUT_JSON = "json";

        /**
         * Initializes the request object
         *
         * @param values An associative array that will be
         *               used to initialize the object
         */
    	function Request( $values = null )
        {
            if(get_magic_quotes_gpc()){
                $values = Textfilter::recursiveStripSlashes($values);
            }
            
            $this->Properties( $values );

        }

		function getRequest()
		{
			static $instance;
			if( $instance == null ) {
				$instance = new Request( HttpVars::getRequest());
			}
			
			return( $instance );
		}
		
		/**
		 * whether parameters should or should not be unescaped automatically
		 *
		 * @param escape
		 */
		function setStripSlashes( $enabled )
		{
			$this->_stripSlashes = $enabled;
		}
		
		/**
		 * reimplemented from Properties::getValue() so that we can correctly
		 * strip slashes from requests where magic_quotes_gpc was enabled
		 *
		 * @param key
		 * @param defaultValue
		 * @param filterClass
		 * @return the value
		 */
		function getValue( $key, $defaultValue = null, $filterClass = null )
		{
			// get the value from the parent
			$value = parent::getValue( $key, $defaultValue, $filterClass );
			
			// now if magic quotes are enabled and the input parameter is not an array
			// and the feature has not been disabled, then strip the slashes
			if( get_magic_quotes_gpc() && !is_array( $value ) && $this->_stripSlashes ) {
				$value = stripslashes( $value );
			}
			
			return( $value );
		}
		
		/**
		 * Returns the value of the "output" parameter, used by some actions and views
		 */
		function getOutput()
		{
			return( $this->getValue( "output" ));
		}
		
		/**
		 * Whether the code implementing the logic should load all available data or not
		 *
		 * @return True if all data should be loaded, false otherwise
		 */
		function loadAllData()
		{
			return( $this->getValue( "all", "") == "all" || $this->getValue( "all", "" ) == "1" );			
		}
		
		/**
		 * Returns true if the request was made via XmlHttpRequest
		 */
		function isXHR()
		{
			if( isset( $_SERVER["HTTP_X_REQUESTED_WITH"] )) {
				return( strtolower( $_SERVER["HTTP_X_REQUESTED_WITH"] ) == 'xmlhttprequest' ); 
			}
			else
				return( false );
		}
    }
?>
