oxinputvalidator.php

Go to the documentation of this file.
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', // Master Card
00053                                          'vis', // Visa
00054                                          'amx', // American Express
00055                                          'dsc', // Discover
00056                                          'dnc', // Diners Club
00057                                          'jcb', // JCB
00058                                          'swi', // Switch
00059                                          'dlt', // Delta
00060                                          'enr'  // EnRoute
00061                                         );
00062 
00068     protected $_aRequiredDCFields = array( 'lsbankname',
00069                                            'lsktonr',
00070                                            'lsktoinhaber'
00071                                          );
00072 
00078     public function __construct()
00079     {
00080     }
00081 
00089     static function getInstance()
00090     {
00091         return oxRegistry::get("oxInputValidator");
00092     }
00093 
00103     public function validateBasketAmount( $dAmount )
00104     {
00105         $dAmount = str_replace( ',', '.', $dAmount );
00106 
00107         if ( !is_numeric( $dAmount ) || $dAmount < 0) {
00111             $oEx = oxNew( 'oxArticleInputException' );
00112             $oEx->setMessage('EXCEPTION_INPUT_INVALIDAMOUNT');
00113             throw $oEx;
00114         }
00115 
00116         if ( !oxRegistry::getConfig()->getConfigParam( 'blAllowUnevenAmounts' ) ) {
00117             $dAmount = round( ( string ) $dAmount );
00118         }
00119 
00120         //negative amounts are not allowed
00121         //$dAmount = abs($dAmount);
00122 
00123         return $dAmount;
00124     }
00125 
00140     public function checkLogin( $oUser, $sLogin, $aInvAddress )
00141     {
00142         // check only for users with password during registration
00143         // if user wants to change user name - we must check if passwords are ok before changing
00144         if ( $oUser->oxuser__oxpassword->value && $sLogin != $oUser->oxuser__oxusername->value ) {
00145 
00146             // on this case password must be taken directly from request
00147             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
00148             if ( !$sNewPass ) {
00149 
00150                 // 1. user forgot to enter password
00151                 $oEx = oxNew( 'oxInputException' );
00152                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00153 
00154                 return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00155             } else {
00156 
00157                 // 2. entered wrong password
00158                 if ( !$oUser->isSamePassword( $sNewPass ) ) {
00159                     $oEx = oxNew( 'oxUserException' );
00160                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
00161 
00162                     return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00163                 }
00164             }
00165         }
00166 
00167         if ( $oUser->checkIfEmailExists( $sLogin ) ) {
00168             //if exists then we do now allow to do that
00169             $oEx = oxNew( 'oxUserException' );
00170             $oLang = oxRegistry::getLang();
00171             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
00172 
00173             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00174         }
00175     }
00176 
00186     public function checkEmail(  $oUser, $sEmail )
00187     {
00188         // missing email address (user login name) ?
00189         if ( !$sEmail ) {
00190             $oEx = oxNew( 'oxInputException' );
00191             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00192 
00193             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00194         }
00195 
00196         // invalid email address ?
00197         if ( !oxRegistry::getUtils()->isValidEmail( $sEmail ) ) {
00198             $oEx = oxNew( 'oxInputException' );
00199             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
00200 
00201             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00202         }
00203     }
00204 
00216     public function checkPassword( $oUser, $sNewPass, $sConfPass, $blCheckLength = false )
00217     {
00218         //  no password at all
00219         if ( $blCheckLength && getStr()->strlen( $sNewPass ) == 0 ) {
00220             $oEx = oxNew( 'oxInputException' );
00221             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
00222 
00223             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00224         }
00225 
00226         //  password is too short ?
00227         if ( $blCheckLength &&  getStr()->strlen( $sNewPass ) < 6 ) {
00228             $oEx = oxNew( 'oxInputException' );
00229             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
00230 
00231             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00232         }
00233 
00234         //  passwords do not match ?
00235         if ( $sNewPass != $sConfPass ) {
00236             $oEx = oxNew( 'oxUserException' );
00237             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
00238 
00239             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00240         }
00241     }
00242 
00253     public function checkRequiredFields( $oUser, $aInvAddress, $aDelAddress )
00254     {
00255         // collecting info about required fields
00256         $aMustFields = array( 'oxuser__oxfname',
00257                               'oxuser__oxlname',
00258                               'oxuser__oxstreetnr',
00259                               'oxuser__oxstreet',
00260                               'oxuser__oxzip',
00261                               'oxuser__oxcity' );
00262 
00263         // config should override default fields
00264         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
00265         if ( is_array( $aMustFillFields ) ) {
00266             $aMustFields = $aMustFillFields;
00267         }
00268 
00269         // assuring data to check
00270         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
00271         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
00272 
00273         // collecting fields
00274         $aFields = array_merge( $aInvAddress, $aDelAddress );
00275 
00276 
00277         // check delivery address ?
00278         $blCheckDel = false;
00279         if ( count( $aDelAddress ) ) {
00280             $blCheckDel = true;
00281         }
00282 
00283         // checking
00284         foreach ( $aMustFields as $sMustField ) {
00285 
00286             // A. not nice, but we keep all fields info in one config array, and must support backward compatibility.
00287             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
00288                 continue;
00289             }
00290 
00291             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
00292                 $this->checkRequiredArrayFields( $oUser, $sMustField, $aFields[$sMustField] );
00293             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
00294                    $oEx = oxNew( 'oxInputException' );
00295                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00296 
00297                    $this->_addValidationError( $sMustField, $oEx );
00298             }
00299         }
00300     }
00301 
00311     public function checkRequiredArrayFields( $oUser, $sFieldName, $aFieldValues )
00312     {
00313         foreach ( $aFieldValues as $sValue ) {
00314             if ( !trim( $sValue ) ) {
00315                 $oEx = oxNew( 'oxInputException' );
00316                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00317 
00318                 $this->_addValidationError( $sFieldName, $oEx );
00319             }
00320         }
00321     }
00322 
00332     public function checkCountries( $oUser, $aInvAddress, $aDelAddress )
00333     {
00334         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
00335         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
00336 
00337         if ( $sBillCtry || $sDelCtry ) {
00338             $oDb = oxDb::getDb();
00339 
00340             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
00341                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
00342                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
00343             } else {
00344                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
00345                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
00346             }
00347 
00348             if ( !$oDb->getOne( $sQ ) ) {
00349                 $oEx = oxNew( 'oxUserException' );
00350                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
00351 
00352                 $this->_addValidationError( "oxuser__oxpassword", $oEx );
00353             }
00354         }
00355     }
00356 
00366     public function checkVatId( $oUser, $aInvAddress )
00367     {
00368         if ( $aInvAddress['oxuser__oxustid'] ) {
00369 
00370             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
00371                 // no country
00372                 return;
00373             }
00374             $oCountry = oxNew('oxcountry');
00375             if ( $oCountry->load( $sCountryId ) && $oCountry->isForeignCountry() && $oCountry->isInEU() ) {
00376 
00377                     if ( strncmp( $aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2 ) ) {
00378                         $oEx = oxNew( 'oxInputException' );
00379                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
00380 
00381                         return $this->_addValidationError( "oxuser__oxustid", $oEx );
00382                     }
00383 
00384             }
00385         }
00386     }
00387 
00393     public function getFieldValidationErrors()
00394     {
00395         return $this->_aInputValidationErrors;
00396     }
00397 
00403     public function getFirstValidationError()
00404     {
00405         $oErr = null;
00406         $aErr = reset( $this->_aInputValidationErrors );
00407         if ( is_array( $aErr ) ) {
00408             $oErr = reset( $aErr );
00409         }
00410         return $oErr;
00411     }
00412 
00421     public function validatePaymentInputData( $sPaymentId, & $aDynValue )
00422     {
00423         $mxValidationResult = true;
00424 
00425         switch( $sPaymentId ) {
00426             case 'oxidcreditcard':
00427                 $mxValidationResult = false;
00428 
00429                 $blAllCreditCardInformationSet = $this->_isAllBankInformationSet( $this->_aRequiredCCFields, $aDynValue );
00430                 $blCreditCardTypeExist = in_array( $aDynValue['kktype'], $this->_aPossibleCCType );
00431 
00432                 if ( $blAllCreditCardInformationSet && $blCreditCardTypeExist ) {
00433                     $oCardValidator = oxNew( "oxccvalidator" );
00434                     $mxValidationResult = $oCardValidator->isValidCard(
00435                                                     $aDynValue['kknumber'],
00436                                                     $aDynValue['kktype'],
00437                                                     $aDynValue['kkmonth'].substr( $aDynValue['kkyear'], 2, 2 )
00438                     );
00439                 }
00440                 break;
00441 
00442             case "oxiddebitnote":
00443                 $mxValidationResult = false;
00444 
00445                 if ( $this->_isAllBankInformationSet( $this->_aRequiredDCFields, $aDynValue ) ) {
00446                     $mxValidationResult = $this->_validateDebitNote( $aDynValue );
00447                 }
00448 
00449                 break;
00450         }
00451 
00452         return $mxValidationResult;
00453     }
00454 
00464     protected function _addValidationError( $sFieldName, $oErr )
00465     {
00466         return $this->_aInputValidationErrors[$sFieldName][] = $oErr;
00467     }
00468 
00474     protected function _validateDebitNote( $aDebitInformation )
00475     {
00476         $aDebitInformation = $this->_cleanDebitInformation( $aDebitInformation );
00477         $sBankCode = $aDebitInformation['lsblz'];
00478         $sAccountNumber = $aDebitInformation['lsktonr'];
00479         $oSepaValidator = oxNew( "oxSepaValidator" );
00480 
00481         if ( empty( $sBankCode ) || $oSepaValidator->isValidBIC( $sBankCode ) ) {
00482             $mxValidationResult = true;
00483             if ( !$oSepaValidator->isValidIBAN( $sAccountNumber ) ) {
00484                 $mxValidationResult = self::INVALID_ACCOUNT_NUMBER;
00485             }
00486         } else {
00487             $mxValidationResult = $this->_validateOldDebitInfo( $aDebitInformation );
00488         }
00489 
00490         return $mxValidationResult;
00491     }
00492 
00497     protected function _validateOldDebitInfo( $aDebitInfo )
00498     {
00499         $oStr       = getStr();
00500         $aDebitInfo = $this->_fixAccountNumber( $aDebitInfo );
00501 
00502         $mxValidationResult = true;
00503 
00504         if ( !$oStr->preg_match( "/^\d{5,8}$/", $aDebitInfo['lsblz'] ) ) {
00505             // Bank code is invalid
00506             $mxValidationResult = self::INVALID_BANK_CODE;
00507         }
00508 
00509         if ( true === $mxValidationResult && !$oStr->preg_match( "/^\d{10,12}$/", $aDebitInfo['lsktonr'] ) ) {
00510             // Account number is invalid
00511             $mxValidationResult = self::INVALID_ACCOUNT_NUMBER;
00512         }
00513 
00514 
00515         return $mxValidationResult;
00516     }
00517 
00523     protected function _fixAccountNumber( $aDebitInfo )
00524     {
00525         $oStr = getStr();
00526 
00527         if ( $oStr->strlen( $aDebitInfo['lsktonr'] ) < 10 ) {
00528             $sNewNum = str_repeat(
00529                            '0', 10 - $oStr->strlen( $aDebitInfo['lsktonr'] )
00530                        ) . $aDebitInfo['lsktonr'];
00531             $aDebitInfo['lsktonr'] = $sNewNum;
00532         }
00533 
00534         return $aDebitInfo;
00535     }
00536 
00543     protected function _isAllBankInformationSet( $aRequiredFields, $aBankInformation )
00544     {
00545         $blResult = true;
00546         foreach ( $aRequiredFields as $sFieldName ) {
00547             if ( !isset( $aBankInformation[$sFieldName] ) || !trim( $aBankInformation[$sFieldName] ) ) {
00548                 $blResult = false;
00549                 break;
00550             }
00551         }
00552 
00553         return $blResult;
00554     }
00555 
00561     protected function _cleanDebitInformation( $aDebitInformation )
00562     {
00563         $aDebitInformation['lsblz']   = str_replace( ' ', '', $aDebitInformation['lsblz'] );
00564         $aDebitInformation['lsktonr'] = str_replace( ' ', '', $aDebitInformation['lsktonr'] );
00565 
00566         return $aDebitInformation;
00567     }
00568 }