• What we have here, is a failure to communicate…

Capturing Form Submissions – Part 1

Something that as frustrated me for some time is the fact that to capture form posts in PHP, we have to rely on the the POST or GET superglobals and how much a black hole those variables seem to be. Well, black hole isn’t really the best description, as we know where the data is coming from (hopefully) and what it’s going to be (should), but it just seems like I just can’t get a handle on a way that is easy to manage. Really, the longer I develop, the more I hate dislike arrays in general (strange statement for someone that uses a TON of them and HATES the fact that C# can’t do associative arrays.)

However, yesterday I stumbled upon an idea that might make like much easier and, although, it’s not a perfect solution, I think I found a good start:  

class DATA_PostData{

	/** @var int */
	public $id;

	/** @var string */
	public $deadline;

	/** @var string */
	public $hdnTicketsAdminDataHash;

	/** @var string */
	public $cmdSubmit;

	/** @var string */
	public $txtNote;

	/** @var bool */
	public $chkTechnicalUpdate;

	/** @var bool */
	public $chkResendLatestResponse;

	/** @var float */
	public $hours_spent;

	/** @var string */
	public $hdnPendingResponseUsers;

	/** @var int */
	public $hdnMaxID;

	/** @var string */
	public $sReferrer;

	/** @var bool */
	private $bDataLoaded = false;

	/** @var int */
	public $hdnFileID;

	/** @var string */
	public $hdnName;

	/** @var int */
	public $hdnPriorStatusID;

	/**
	 */
	public function __construct(){

		$oSQL = (object) $_POST;

		if(!_PHPUNITTEST)
			unset($_POST);

		if($oSQL){

			foreach($oSQL as $sColumn => $sValue)
				$this->$sColumn = $sValue;

			$this->bDataLoaded = true;

		}
		elseif($this->id)
			$this->bDataLoaded = true;

		Common::checkForUndefinedPropertiesAndDisplayError($this, __CLASS__);

	}

	/**
	 * @return string
	 */
	public static function getClassName(){

		return __CLASS__;

	}

	/**
	 * @return bool
	 */
	public function isDataLoaded(){

		return $this->bDataLoaded;

	}

	/**
	 * @static
	 * @param object $oObject
	 * @param string $sClass
	 * @throws Exception_MissingDataObjectProperty
	 */
	public static function checkForUndefinedPropertiesAndDisplayError($oObject, $sClass){

		try{

			if((bool) $aUndefinedProperties = Common::getPropertiesUsedButNotDeclared($oObject, $sClass))
				throw new Exception_MissingDataObjectProperty(
					"Properties (".Common::CommaSpace($aUndefinedProperties).
						") does not exist in class ($sClass.) ");

		}
		catch(Exception_MissingDataObjectProperty $oException){

			Exceptions::addToOnShutdown($oException);

		}

	}

}

So the idea is to create a data object that has all the possible post submission variables and then have a function designed to throw an exception if something is received that is not expected.

Comments List

dustinmoormanJuly 19, 2012 9:25 am /

A good touch on form data input security!