<?

/**
 * $Id: Request.class.php 395 2008-05-28 19:41:06Z dakl $
 * Obsluga requestow
 *
 */

class Request {
	
	/**
	 * Pobiera warto?� tablicy
	 *
	 * @param string $value
	 * @return unknown
	 */
	static function GetArray($value, $xssremover = true) {
		if (isset($_REQUEST[$value])) {

			if ($xssremover == true) {
				$func = create_function('$a', 'return Utils::RemoveXss($a);'); //FIXME: Gdy wejdzie php5.3 mo�na to troche przerobi� bo wprowadzaj? now? wersj� funkcji lambda

				return array_map($func, $_REQUEST[$value]);
			} else {
				return $_REQUEST[$value];
			}

		}
	}

    /**
     * Pobiera wartosc
     *
     * @param string $value
     * @return unknown
     */
    static function Get($value,$xssremover = true){
        if(isset($_REQUEST[$value]))
        {
            if($xssremover == true)
				return Utils::RemoveXss(self::QuotesRemover($_REQUEST[$value]));
            else
				return self::QuotesRemover($_REQUEST[$value]);
        }
    }

    /**
     * Sprawdza czy istnieje
     *
     * @param string $value
     * @return unknown
     */
    static function Check($value){
        if(isset($_REQUEST[$value]) || isset($_POST[$value])) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Sprawdza czy istnieje $_POST
     *
     * @return bool
     */
    static function IsPost() {
        if(isset($_POST) && !empty($_POST)) {
            return true;
        } else {
            return false;
        }
    }

	 /**
     * Sprawdza czy istnieje $_GET
     *
     * @return bool
     */
    static function IsGet() {
        if(isset($_GET) && !empty($_GET)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * pobiera cookie z wykorzystaniem xss remove'ra
     *
     * @param string $value
     * @param bool $xssremover = true - czy wykorzystywac xssremovera
     */
    static function GetCookie($value,$xssremover = true)
    {
        if(isset($_COOKIE[$value]))
        {
            if($xssremover == true)
            return Utils::RemoveXss(self::QuotesRemover($_COOKIE[$value]));
            else
            return self::QuotesRemover($_COOKIE[$value]);
        }
    }

    static function SetCookie($name,$value,$time = null,$path = '/')
    {
	if ($time == '') $time = null;
        setcookie($name, $value, $time, $path);
    }

    static function GetCookieArray($value,$xssremover = true, $delimiter = '_##_')
    {
        if(isset($_COOKIE[$value]))
        {
            if($xssremover == true)
            $raw = Utils::RemoveXss(self::QuotesRemover($_COOKIE[$value]));
            else
            $raw = self::QuotesRemover($_COOKIE[$value]);

	    return explode($delimiter, $raw);
        }
	else return array();
    }

    static function SetCookieArray($name,$array,$time = null, $glue = '_##_')
    {
	$value = implode($glue, $array);
	
	self::SetCookie($name, $value);
    }


    static function GetCookieAssocArray($value,$xssremover = false, $delimiter = '_#_', $delimiterKey = '_%_' )
    {
        if(isset($_COOKIE[$value]))
        {
            if($xssremover == true)
            $raw = Utils::RemoveXss(self::QuotesRemover($_COOKIE[$value]));
            else
            $raw = self::QuotesRemover($_COOKIE[$value]);

	    $gluedArray = explode($delimiter, $raw);
	    $array = array();
	    foreach ($gluedArray as $val) {
		$keyValue = explode($delimiterKey, $val) ;
		$array[$keyValue[0]] = $keyValue[1];
	    }
	    
	    return $array;

        }
	else return array();
    }

    static function SetCookieAssocArray($name,$array,$time = null, $glue = '_#_', $glueKey = '_%_')
    {

	$gluedArray = $array();
	foreach ($array as $key => $val) {
	    $gluedArray[] = $key . $glueKey . $val;
	}

	$value = implode($glue, $gluedArray);


	self::SetCookie($name, $value);
    }



    static function GetCookieArrayAssocArray($value,$xssremover = false, $delimiter = '_##_', $delimiterRow = '_#_', $delimiterKey = '_%_' )
    {
        if(isset($_COOKIE[$value]))
        {
            if($xssremover == true)
            $raw = Utils::RemoveXss(self::QuotesRemover($_COOKIE[$value]));
            else
            $raw = self::QuotesRemover($_COOKIE[$value]);

	    $gluedArray = explode($delimiter, $raw);
	    $finalArray = array();
	    foreach ($gluedArray as $stringAssoc) {
		$arrayAssoc = explode($delimiterRow, $stringAssoc);
		$array = array();
		foreach ($arrayAssoc as $val) {
		    $keyValue = explode($delimiterKey, $val) ;
		    $array[$keyValue[0]] = $keyValue[1];
		}
		$finalArray[] = $array;
	    }
	    return $finalArray;

        }
	else return array();
    }

    static function SetCookieArrayAssocArray($name,$array,$time = null, $glue = '_##_', $glueRow = '_#_', $glueKey = '_%_')
    {

	$gluedArray = array();
	foreach ($array as $arrayAssoc) {
	    $keyArray = array();
	    foreach ($arrayAssoc as $key => $val) {
		$keyArray[] = $key . $glueKey . $val;
	    }
	    $gluedArray[] = implode($glueRow,$keyArray);
	}
	$value = implode($glue, $gluedArray);


	self::SetCookie($name, $value);
    }

    /**
     * pobiera post z wykorzystaniem xss remove'ra
     *
     * @param string|array $value
     * @param bool $xssremover = true - czy wykorzystywac xssremovera
     *
     * @return string|array
     */
    static function GetPost($value,$xssremover = true, $striptags = true)
    {
        if(is_array($value)) {
            if(isset($_POST[$value['name']][$value['key']])) {

                if($striptags == true) {
                    $ret = strip_tags($_POST[$value['name']][$value['key']]);
                }
                else {
                    $ret = $_POST[$value['name']][$value['key']];
                }

                if($xssremover == true) {
                    return self::RemoveXss($ret);
                }
                else {
                    return self::QuotesRemover($ret);
                }
            }
        } else {
            if(isset($_POST[$value])) {

                if($striptags == true) {
                    if(!is_array($_POST[$value])) {
                        $ret = strip_tags($_POST[$value]);
                    } else {
                        $newarray = array();
                        foreach($_POST[$value] as $valueItem) {
                            $newarray[] = strip_tags($valueItem);
                        }
                        $ret = $newarray;
                    }
                }
                else {
                    $ret = $_POST[$value];
                }

                if($xssremover == true) {
                    return self::RemoveXss(self::QuotesRemover($ret));
                }
                else {
                    return self::QuotesRemover($ret);
                }
            }
        }
    }

    /**
     * pobranie calej tablicy POST z wykorzystaniem xss removera
     *
     * @param boolean $xssremover
     */
    static function GetAllPost($xssremover = true) {
        if($xssremover == true) {
            return self::RemoveXss(self::QuotesRemover($_POST));
        } else {
            return self::QuotesRemover($_POST);
        }
    }

	/**
     * pobranie calej tablicy POST z wykorzystaniem xss removera
     *
     * @param boolean $xssremover
     */
    static function GetAllGet($xssremover = true) {
        if($xssremover == true) {
            return self::RemoveXss(self::QuotesRemover($_GET));
        } else {
            return self::QuotesRemover($_GET);
        }
    }

    /**
     * pobiera get z wykorzystaniem xss remove'ra
     *
     * @param string|array $value
     * @param bool $xssremover = true - czy wykorzystywac xssremovera
     *
     * @return string|array
     */
    static function GetGet($value,$xssremover = true)
    {
        if(isset($_GET[$value]))
        {
            if($xssremover == true) {
                return self::RemoveXss(self::QuotesRemover($_GET[$value]));
            } else {
                return self::QuotesRemover($_GET[$value]);
            }
        }
    }

    /**
     * xss remover
     *
     * @param string|array $value
     * @return string|array
     */
    static function RemoveXss($value) {
        if(is_array($value)) {
            foreach($value as $k => $v) {
                $value[$k] = self::RemoveXss($v);
            }
            return $value;
        } else {
            return Utils::RemoveXss($value);
        }
    }

    static function SetPost($variable, $value) {
        $_POST[$variable] = $value;
    }

    
    /**
     * quotes remover
     *
     * @param string|array $value
     * @return string|array
     */
    static function QuotesRemover($value) {
//        if (get_magic_quotes_gpc()) {

            if (!is_array($value)) {
                $value = stripslashes($value);
            } else {
                $output = array();
                foreach($value as $key=>$val) {
                    //if(!is_array($val)) {
                        //TODO: fixme
                        $output[$key]=self::QuotesRemover($val);
                    //}
                }
                $value = $output;
            }
//        }

        return $value;

    }
}
?>