00001 <?php
00002
00007 class oxInputValidator extends oxSuperCfg
00008 {
00009
00013 const INVALID_ACCOUNT_NUMBER = -5;
00014
00018 const INVALID_BANK_CODE = -4;
00019
00025 private static $_instance = null;
00026
00032 protected $_aRequiredCCFields = array( 'kktype',
00033 'kknumber',
00034 'kkmonth',
00035 'kkyear',
00036 'kkname',
00037 'kkpruef'
00038 );
00039
00045 protected $_aInputValidationErrors = array();
00046
00052 protected $_aPossibleCCType = array( 'mcd',
00053 'vis',
00054 'amx',
00055 'dsc',
00056 'dnc',
00057 'jcb',
00058 'swi',
00059 'dlt',
00060 'enr'
00061 );
00062
00068 protected $_aRequiredDCFields = array( 'lsbankname',
00069 'lsktonr',
00070 'lsktoinhaber'
00071 );
00072
00077 public function __construct()
00078 {
00079 }
00080
00088 static function getInstance()
00089 {
00090 return oxRegistry::get("oxInputValidator");
00091 }
00092
00102 public function validateBasketAmount( $dAmount )
00103 {
00104 $dAmount = str_replace( ',', '.', $dAmount );
00105
00106 if ( !is_numeric( $dAmount ) || $dAmount < 0) {
00110 $oEx = oxNew( 'oxArticleInputException' );
00111 $oEx->setMessage('ERROR_MESSAGE_INPUT_INVALIDAMOUNT');
00112 throw $oEx;
00113 }
00114
00115 if ( !oxRegistry::getConfig()->getConfigParam( 'blAllowUnevenAmounts' ) ) {
00116 $dAmount = round( ( string ) $dAmount );
00117 }
00118
00119
00120
00121
00122 return $dAmount;
00123 }
00124
00139 public function checkLogin( $oUser, $sLogin, $aInvAddress )
00140 {
00141
00142
00143 if ( $oUser->oxuser__oxpassword->value && $sLogin != $oUser->oxuser__oxusername->value ) {
00144
00145
00146 $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
00147 if ( !$sNewPass ) {
00148
00149
00150 $oEx = oxNew( 'oxInputException' );
00151 $oEx->setMessage('ERROR_MESSAGE_INPUT_NOTALLFIELDS');
00152
00153 return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00154 } else {
00155
00156
00157 if ( !$oUser->isSamePassword( $sNewPass ) ) {
00158 $oEx = oxNew( 'oxUserException' );
00159 $oEx->setMessage('ERROR_MESSAGE_USER_PWDDONTMATCH');
00160
00161 return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00162 }
00163 }
00164 }
00165
00166 if ( $oUser->checkIfEmailExists( $sLogin ) ) {
00167
00168 $oEx = oxNew( 'oxUserException' );
00169 $oLang = oxRegistry::getLang();
00170 $oEx->setMessage( sprintf( $oLang->translateString( 'ERROR_MESSAGE_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
00171
00172 return $this->_addValidationError( "oxuser__oxusername", $oEx );
00173 }
00174 }
00175
00185 public function checkEmail( $oUser, $sEmail )
00186 {
00187
00188 if ( !$sEmail ) {
00189 $oEx = oxNew( 'oxInputException' );
00190 $oEx->setMessage('ERROR_MESSAGE_INPUT_NOTALLFIELDS');
00191
00192 return $this->_addValidationError( "oxuser__oxusername", $oEx );
00193 }
00194
00195
00196 if ( !oxRegistry::getUtils()->isValidEmail( $sEmail ) ) {
00197 $oEx = oxNew( 'oxInputException' );
00198 $oEx->setMessage( 'ERROR_MESSAGE_INPUT_NOVALIDEMAIL' );
00199
00200 return $this->_addValidationError( "oxuser__oxusername", $oEx );
00201 }
00202 }
00203
00215 public function checkPassword( $oUser, $sNewPass, $sConfPass, $blCheckLength = false )
00216 {
00217
00218 if ( $blCheckLength && getStr()->strlen( $sNewPass ) == 0 ) {
00219 $oEx = oxNew( 'oxInputException' );
00220 $oEx->setMessage('ERROR_MESSAGE_INPUT_EMPTYPASS');
00221
00222 return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00223 }
00224
00225
00226 if ( $blCheckLength && getStr()->strlen( $sNewPass ) < 6 ) {
00227 $oEx = oxNew( 'oxInputException' );
00228 $oEx->setMessage('ERROR_MESSAGE_PASSWORD_TOO_SHORT');
00229
00230 return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00231 }
00232
00233
00234 if ( $sNewPass != $sConfPass ) {
00235 $oEx = oxNew( 'oxUserException' );
00236 $oEx->setMessage('ERROR_MESSAGE_USER_PWDDONTMATCH');
00237
00238 return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00239 }
00240 }
00241
00252 public function checkRequiredFields( $oUser, $aInvAddress, $aDelAddress )
00253 {
00254
00255 $aMustFields = array( 'oxuser__oxfname',
00256 'oxuser__oxlname',
00257 'oxuser__oxstreetnr',
00258 'oxuser__oxstreet',
00259 'oxuser__oxzip',
00260 'oxuser__oxcity' );
00261
00262
00263 $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
00264 if ( is_array( $aMustFillFields ) ) {
00265 $aMustFields = $aMustFillFields;
00266 }
00267
00268
00269 $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
00270 $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
00271
00272
00273 $aFields = array_merge( $aInvAddress, $aDelAddress );
00274
00275
00276
00277 $blCheckDel = false;
00278 if ( count( $aDelAddress ) ) {
00279 $blCheckDel = true;
00280 }
00281
00282
00283 foreach ( $aMustFields as $sMustField ) {
00284
00285
00286 if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
00287 continue;
00288 }
00289
00290 if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
00291 $this->checkRequiredArrayFields( $oUser, $sMustField, $aFields[$sMustField] );
00292 } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
00293 $oEx = oxNew( 'oxInputException' );
00294 $oEx->setMessage('ERROR_MESSAGE_INPUT_NOTALLFIELDS');
00295
00296 $this->_addValidationError( $sMustField, $oEx );
00297 }
00298 }
00299 }
00300
00310 public function checkRequiredArrayFields( $oUser, $sFieldName, $aFieldValues )
00311 {
00312 foreach ( $aFieldValues as $sValue ) {
00313 if ( !trim( $sValue ) ) {
00314 $oEx = oxNew( 'oxInputException' );
00315 $oEx->setMessage('ERROR_MESSAGE_INPUT_NOTALLFIELDS');
00316
00317 $this->_addValidationError( $sFieldName, $oEx );
00318 }
00319 }
00320 }
00321
00331 public function checkCountries( $oUser, $aInvAddress, $aDelAddress )
00332 {
00333 $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
00334 $sDelCtry = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
00335
00336 if ( $sBillCtry || $sDelCtry ) {
00337 $oDb = oxDb::getDb();
00338
00339 if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
00340 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
00341 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
00342 } else {
00343 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
00344 ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
00345 }
00346
00347 if ( !$oDb->getOne( $sQ ) ) {
00348 $oEx = oxNew( 'oxUserException' );
00349 $oEx->setMessage('ERROR_MESSAGE_INPUT_NOTALLFIELDS' );
00350
00351 $this->_addValidationError( "oxuser__oxpassword", $oEx );
00352 }
00353 }
00354 }
00355
00365 public function checkVatId( $oUser, $aInvAddress )
00366 {
00367 if ( $aInvAddress['oxuser__oxustid'] ) {
00368
00369 if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
00370
00371 return;
00372 }
00373 $oCountry = oxNew('oxcountry');
00374 if ( $oCountry->load( $sCountryId ) && $oCountry->isForeignCountry() && $oCountry->isInEU() ) {
00375
00376 if ( strncmp( $aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2 ) ) {
00377 $oEx = oxNew( 'oxInputException' );
00378 $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
00379
00380 return $this->_addValidationError( "oxuser__oxustid", $oEx );
00381 }
00382
00383 }
00384 }
00385 }
00386
00392 public function getFieldValidationErrors()
00393 {
00394 return $this->_aInputValidationErrors;
00395 }
00396
00402 public function getFirstValidationError()
00403 {
00404 $oErr = null;
00405 $aErr = reset( $this->_aInputValidationErrors );
00406 if ( is_array( $aErr ) ) {
00407 $oErr = reset( $aErr );
00408 }
00409 return $oErr;
00410 }
00411
00420 public function validatePaymentInputData( $sPaymentId, & $aDynValue )
00421 {
00422 $mxValidationResult = true;
00423
00424 switch( $sPaymentId ) {
00425 case 'oxidcreditcard':
00426 $mxValidationResult = false;
00427
00428 $blAllCreditCardInformationSet = $this->_isAllBankInformationSet( $this->_aRequiredCCFields, $aDynValue );
00429 $blCreditCardTypeExist = in_array( $aDynValue['kktype'], $this->_aPossibleCCType );
00430
00431 if ( $blAllCreditCardInformationSet && $blCreditCardTypeExist ) {
00432 $oCardValidator = oxNew( "oxccvalidator" );
00433 $mxValidationResult = $oCardValidator->isValidCard(
00434 $aDynValue['kknumber'],
00435 $aDynValue['kktype'],
00436 $aDynValue['kkmonth'].substr( $aDynValue['kkyear'], 2, 2 )
00437 );
00438 }
00439 break;
00440
00441 case "oxiddebitnote":
00442 $mxValidationResult = false;
00443
00444 if ( $this->_isAllBankInformationSet( $this->_aRequiredDCFields, $aDynValue ) ) {
00445 $mxValidationResult = $this->_validateDebitNote( $aDynValue );
00446 }
00447
00448 break;
00449 }
00450
00451 return $mxValidationResult;
00452 }
00453
00463 protected function _addValidationError( $sFieldName, $oErr )
00464 {
00465 return $this->_aInputValidationErrors[$sFieldName][] = $oErr;
00466 }
00467
00473 protected function _validateDebitNote( $aDebitInformation )
00474 {
00475 $aDebitInformation = $this->_cleanDebitInformation( $aDebitInformation );
00476 $sBankCode = $aDebitInformation['lsblz'];
00477 $sAccountNumber = $aDebitInformation['lsktonr'];
00478 $oSepaValidator = oxNew( "oxSepaValidator" );
00479
00480 if ( empty( $sBankCode ) || $oSepaValidator->isValidBIC( $sBankCode ) ) {
00481 $mxValidationResult = true;
00482 if ( !$oSepaValidator->isValidIBAN( $sAccountNumber ) ) {
00483 $mxValidationResult = self::INVALID_ACCOUNT_NUMBER;
00484 }
00485 } else {
00486 $mxValidationResult = self::INVALID_BANK_CODE;
00487 if ( !oxRegistry::getConfig()->getConfigParam( 'blSkipDebitOldBankInfo' ) ) {
00488 $mxValidationResult = $this->_validateOldDebitInfo( $aDebitInformation );
00489 }
00490 }
00491
00492 return $mxValidationResult;
00493 }
00494
00499 protected function _validateOldDebitInfo( $aDebitInfo )
00500 {
00501 $oStr = getStr();
00502 $aDebitInfo = $this->_fixAccountNumber( $aDebitInfo );
00503
00504 $mxValidationResult = true;
00505
00506 if ( !$oStr->preg_match( "/^\d{5,8}$/", $aDebitInfo['lsblz'] ) ) {
00507
00508 $mxValidationResult = self::INVALID_BANK_CODE;
00509 }
00510
00511 if ( true === $mxValidationResult && !$oStr->preg_match( "/^\d{10,12}$/", $aDebitInfo['lsktonr'] ) ) {
00512
00513 $mxValidationResult = self::INVALID_ACCOUNT_NUMBER;
00514 }
00515
00516
00517 return $mxValidationResult;
00518 }
00519
00525 protected function _fixAccountNumber( $aDebitInfo )
00526 {
00527 $oStr = getStr();
00528
00529 if ( $oStr->strlen( $aDebitInfo['lsktonr'] ) < 10 ) {
00530 $sNewNum = str_repeat(
00531 '0', 10 - $oStr->strlen( $aDebitInfo['lsktonr'] )
00532 ) . $aDebitInfo['lsktonr'];
00533 $aDebitInfo['lsktonr'] = $sNewNum;
00534 }
00535
00536 return $aDebitInfo;
00537 }
00538
00545 protected function _isAllBankInformationSet( $aRequiredFields, $aBankInformation )
00546 {
00547 $blResult = true;
00548 foreach ( $aRequiredFields as $sFieldName ) {
00549 if ( !isset( $aBankInformation[$sFieldName] ) || !trim( $aBankInformation[$sFieldName] ) ) {
00550 $blResult = false;
00551 break;
00552 }
00553 }
00554
00555 return $blResult;
00556 }
00557
00563 protected function _cleanDebitInformation( $aDebitInformation )
00564 {
00565 $aDebitInformation['lsblz'] = str_replace( ' ', '', $aDebitInformation['lsblz'] );
00566 $aDebitInformation['lsktonr'] = str_replace( ' ', '', $aDebitInformation['lsktonr'] );
00567
00568 return $aDebitInformation;
00569 }
00570 }