(file) Return to Request.class.php CVS log (file) (dir) Up to [Development] / advokit-installer

File: [Development] / advokit-installer / Request.class.php (download) / (as text)
Revision: 1.5, Wed Aug 11 14:38:23 2004 UTC (6 years ago) by travislow
Branch: MAIN
CVS Tags: r1-x-dev, r0-9-9, r0-9-8, r0-9-5, footag0, HEAD
Changes since 1.4: +9 -0 lines
- Fixed stupid-ass bug I introduced in Request.class.php
- CSV generation now iterative.

<?
# ======================================================================
# AdvoKit -- a campaign managment tool
# Copyright (C) 2004  OrchidSuites, Inc. (info@orchidsuites.net)
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the AFFERO GENERAL PUBLIC LICENSE
# as published by Affero, Inc.; either version 1
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# AFFERO GENERAL PUBLIC LICENSE for more details.
# 
# You should have received a copy of the AFFERO GENERAL PUBLIC LICENSE
# along with this program; if not, write to Affero, Inc. at
# 510 Third Street - Suite 225, San Francisco, CA 94107, USA
# or visit <http://www.affero.org>.
# ======================================================================

# ======================================================================
# A request object.
# ======================================================================
class Request
{
    # Logger for writing messages to log files
    var $logger;

    # Database object
    var $db;

    # Combined get and post information.
    var $forminfo;

    # Unique identifier for this request.
    var $id;

    # TRUE => automatically trim whitespace from all request parameters.
    var $auto_trim;

    # Look in $_POST or $_GET first? 
    var $parm_precedence = "get";

    #.................................................. Request
    #
    # Constructor.  Pass in the configuration and logger.
    #
    function Request( &$config, &$logger, &$db )
    {
        $this->auto_trim = $config->get( "req_auto_trim" );
        $this->db =& $db;
        $this->logger =& $logger;
        $this->logger->debug( __FILE__, "Creating request object from POST and GET information..." );
        $this->logger->debug( __FILE__, "...precedence is '".$config->get( "req_parm_precedence" )."'..." );
        $this->id = md5( uniqid( rand(), 1 ));
        if( "get" == $config->get( "req_parm_precedence" ) )
        {
            $this->forminfo = array_merge( $_POST, $_GET );
        }
        else
        {
            $this->forminfo = array_merge( $_GET, $_POST );
        }
        if( isset( $_SERVER["REQUEST_URI"] ) )
        {
            $this->logger->debug( __FILE__, "...request created. URI is '".$_SERVER["REQUEST_URI"]."'" );
        }
        else
        {
            $this->logger->debug( __FILE__, "...request created." );
        }
    }

    #.................................................. hasNonEmpty
    #
    # Return true if the form contains a value associated
    # with the specified parameter name, and the value
    # is non-empty.
    #
    function hasNonEmpty( $parameter )
    {
        return ( isset( $this->forminfo[$parameter] )
               && $this->forminfo[$parameter] != ""
               );
    }

    #.................................................. has
    #
    # Return true if the specified parameter name has 
    # been set in the form.
    #
    function has( $parameter )
    {
        return isset( $this->forminfo[$parameter] );
    }

    #.................................................. getIp
    #
    # Return the IP from which the request originated.
    #
    function getIp()
    {
        return $_SERVER["REMOTE_ADDR"];
    }

    #.................................................. getPort
    #
    # Return the port from which the request originated.
    #
    function getPort()
    {
        return $_SERVER["REMOTE_PORT"];
    }

    #.................................................. get
    #
    # Return the value associated with the specified
    # form parameter name.
    #
    function get( $parameter )
    {
        $value = $this->forminfo[$parameter];
        if( ! is_array( $value ) )
        {
            if( $this->auto_trim )
            {
                $value = trim( $value );
            }
            $value = $this->db->auto_sql_escape( $value );
        }
        else
        {
            for( $i = 0; $i < count( $value ); $i++ )
            {
                if( $this->auto_trim )
                {
                    $value[$i] = trim( $value[$i] );
                }
                $value[$i] = $this->db->auto_sql_escape( $value[$i] );
            }
        }
        return $value;
    }

    #.................................................. getDate
    #
    # Like "get", but return a SQL-quoted date value.
    # Assumes input of the form "mm/dd/yyyy" or "mm/yyyy".
    # In the case of "mm/yyyy", dd is assumed to be 1.
    # If date was empty, return SQL "null".
    #
    function getDate( $parameter )
    {
        $date = $this->get( $parameter );
        if( !$date )
        {
            return "null";
        }
        list( $m, $d, $y ) = explode( "/", $date );
        $m = intval($m);
        $d = intval($d);
        $y = intval($y);
        if( !$y )
        {
            $y = $d;
            $d = 1;
        }
        return "'$y-$m-$d'";
    }

    #.................................................. getEndOfDate
    #
    # Like "get", but return a SQL-quoted date value 
    # representing the end of the day on the specified
    # date.
    #
    function getEndOfDate( $parameter )
    {
        $date = $this->get( $parameter );
        if( !$date )
        {
            return "null";
        }
        list( $m, $d, $y ) = explode( "/", $date );
        $m = intval($m);
        $d = intval($d);
        $y = intval($y);
        if( !$y )
        {
            $y = $d;
            $d = 1;
        }
        return "'$y-$m-$d 23:59:59'";
    }

    #.................................................. getStartOfDate
    #
    # Like "get", but return a SQL-quoted date value 
    # representing the start of the day on the specified
    # date.
    #
    function getStartOfDate( $parameter )
    {
        $date = $this->get( $parameter );
        if( !$date )
        {
            return "null";
        }
        list( $m, $d, $y ) = explode( "/", $date );
        $m = intval($m);
        $d = intval($d);
        $y = intval($y);
        if( !$y )
        {
            $y = $d;
            $d = 1;
        }
        return "'$y-$m-$d 00:00:00'";
    }

    #.................................................. getInteger
    #
    # Like "get", but return an integer value.
    #
    function getInteger( $parameter )
    {
        return intval( $this->get( $parameter ) );
    }

    #.................................................. getYesNo
    #
    # Like "get", but return a SQL-quoted Y or N.
    #
    function getYesNo( $parameter )
    {
        return 'Y' == strtoupper( $this->get( $parameter ) ) ? "'Y'" : "'N'";
    }

    #.................................................. set
    #
    # Unconditionally set the value within the request.
    # Use sparingly, as the request object should reflect
    # what the user has requested.  Don't muck with it
    # unless you really have to.
    #
    function set( $key, $value )
    {
        return $this->forminfo[$key] = $value;
    }

    #.................................................. getParms
    #
    # Return the forminfo array of request parameters
    # and their values.
    #
    function getParms()
    {
        return $this->forminfo;
    }

    #.................................................. setParms
    #
    # Set the forminfo array to $parms
    #
    function setParms( $parms )
    {
        $this->forminfo = $parms;
    }
} 
?>

cvsadmin@voter2voter.org
CVS Snapshots (updated daily)