class_Secure.inc.php 4.01 KB
<?php
/**
 * @file	Secure.inc.php
 * @category freeSN
 * @mailto	code [at] netz.coop
 * @version 0.4.200901
 * @link http://netz.coop
 * 
 *  @copyright Copyright by netz.coop e.G. 2015
 *  
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 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
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
class Secure {
	/**
	 *	check the Insert SQL Data
	 *
	 *  * is required
	 *  * length
	 *	* is_numeric
	 *  * intval
	 *
	 * @param <type> $param_Value
	 * @param <type> $param_DescriptData
	 * @return <type>
	 */
	private static function checkDataForSql($param_Value, $param_DescriptData) {

		//		D::show($param_DescriptData, $param_Value);

		//$param_Value = trim($param_Value);

		if($param_DescriptData['Key']) {
			//			if(!ctype_alnum($param_Value)) {
			//				return Error::newError('UserError','NoSaveBE',$param_DescriptData['Field'].'('.$param_Value.') is wrong, it has to be Alpha or Number Char');
			//			}
			if(empty($param_Value)) {
				return Error::newError('UserError','WrongData',$param_DescriptData['Field'].'('.$param_Value.') is required');
			}
		}

		if($param_DescriptData['Null']==='NO' && empty($param_Value)) {
			return Error::newError('UserError','WrongData',$param_DescriptData['Field'].' is required');
		}

		if($param_DescriptData['Length'] && strlen($param_Value)>$param_DescriptData['Length']) {
			return Error::newError('UserError','WrongData',$param_Value.' is wrong, it is to long (max '.$param_DescriptData['Length'].')');
		}

		if($param_DescriptData['Type'] =='int') {
			if($param_Value && !is_numeric($param_Value) ) {
				return Error::newError('UserError','NoSaveBE',$param_DescriptData['Field'].'('.$param_Value.') is wrong, it has to be a integer');
			}
		}
		//D::ulli($param_DescriptData['Field'].' = '.$param_Value.' ### '.strlen($param_Value));
		return true;
	}

	private static function transformDataForSql($param_Value, $param_DescriptData) {
		if($param_DescriptData['Type'] =='int') {
			$param_Value = intval($param_Value);
		}
		return $param_Value;
	}

	public static function checkObjectAndTransformToSql(BasisElement &$param_Object) {
		//D::show(Form::varcharToArray(CONFIG::getSQL_Data(get_class($param_Object), 'SelectColumns')),'SelectColumns from '.$param_Object);

		foreach(Form::varcharToArray(CONFIG::getSQL_Data(get_class($param_Object), 'SelectColumns')) as $var_Column) {
			if($var_Column) {
				$var_FormData =  BasisElement::getFormDataFromDB($param_Object->getObjVar('DB'), $param_Object, $var_Column);
				if(Secure::checkDataForSql($param_Object->getObjVar($var_Column), $var_FormData)) {
					$param_Object->setObjVar($var_Column, Secure::transformDataForSql($param_Object->getObjVar($var_Column), $var_FormData));
				} else {
					return Error::newError('DevError','NoSaveBE',$param_Object.'->'.$var_Column.' is wrong');
				}
			}
		}

		foreach(array(/*'ID',*/ 'Author_ProfileID', 'Subject', 'Text', /*'CreateDate', 'TagListID', 'BECategoryChoiceBEID', 'BECategoryListID',*/ 'AuthReadListID', 'AuthWriteListID'/*, 'BasisElementDesignID'*/) as $var_Column) {
			$var_FormData =  BasisElement::getFormDataFromDB($param_Object->getObjVar('DB'), $param_Object, $var_Column);
			if(Secure::checkDataForSql($param_Object->getObjVar($var_Column), $var_FormData)) {
				$param_Object->setObjVar($var_Column, Secure::transformDataForSql($param_Object->getObjVar($var_Column), $var_FormData));
			} else {
				return Error::newError('DevError','NoSaveBE','MultimediaText->'.$var_Column.' is wrong');
			}
		}
		//D::li('alles toll');
		return true;
	}

}
?>