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 
00087     static function getInstance()
00088     {
00089         if ( defined('OXID_PHP_UNIT')) {
00090             if ( ($oClassMod = modInstances::getMod(__CLASS__))  && is_object($oClassMod) ) {
00091                 return $oClassMod;
00092             } else {
00093                  $inst = oxNew( 'oxInputValidator' );
00094                  modInstances::addMod( __CLASS__, $inst );
00095                  return $inst;
00096             }
00097         }
00098 
00099         if ( !isset( self::$_instance ) ) {
00100             // allow modules
00101             self::$_instance = oxNew( 'oxInputValidator' );
00102         }
00103         return self::$_instance;
00104     }
00105 
00115     public function validateBasketAmount( $dAmount )
00116     {
00117         $dAmount = str_replace( ',', '.', $dAmount );
00118 
00119         if ( !is_numeric( $dAmount ) || $dAmount < 0) {
00123             $oEx = oxNew( 'oxArticleInputException' );
00124             $oEx->setMessage('EXCEPTION_INPUT_INVALIDAMOUNT');
00125             throw $oEx;
00126         }
00127 
00128         if ( !oxConfig::getInstance()->getConfigParam( 'blAllowUnevenAmounts' ) ) {
00129             $dAmount = round( ( string ) $dAmount );
00130         }
00131 
00132         //negative amounts are not allowed
00133         //$dAmount = abs($dAmount);
00134 
00135         return $dAmount;
00136     }
00137 
00152     public function checkLogin( $oUser, $sLogin, $aInvAddress )
00153     {
00154         // check only for users with password during registration
00155         // if user wants to change user name - we must check if passwords are ok before changing
00156         if ( $oUser->oxuser__oxpassword->value && $sLogin != $oUser->oxuser__oxusername->value ) {
00157 
00158             // on this case password must be taken directly from request
00159             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
00160             if ( !$sNewPass ) {
00161 
00162                 // 1. user forgot to enter password
00163                 $oEx = oxNew( 'oxInputException' );
00164                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00165 
00166                 return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00167             } else {
00168 
00169                 // 2. entered wrong password
00170                 if ( !$oUser->isSamePassword( $sNewPass ) ) {
00171                     $oEx = oxNew( 'oxUserException' );
00172                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
00173 
00174                     return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00175                 }
00176             }
00177         }
00178 
00179         if ( $oUser->checkIfEmailExists( $sLogin ) ) {
00180             //if exists then we do now allow to do that
00181             $oEx = oxNew( 'oxUserException' );
00182             $oLang = oxLang::getInstance();
00183             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
00184 
00185             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00186         }
00187     }
00188 
00198     public function checkEmail(  $oUser, $sEmail )
00199     {
00200         // missing email address (user login name) ?
00201         if ( !$sEmail ) {
00202             $oEx = oxNew( 'oxInputException' );
00203             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00204 
00205             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00206         }
00207 
00208         // invalid email address ?
00209         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
00210             $oEx = oxNew( 'oxInputException' );
00211             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
00212 
00213             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00214         }
00215     }
00216 
00228     public function checkPassword( $oUser, $sNewPass, $sConfPass, $blCheckLength = false )
00229     {
00230         //  no password at all
00231         if ( $blCheckLength && getStr()->strlen( $sNewPass ) == 0 ) {
00232             $oEx = oxNew( 'oxInputException' );
00233             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
00234 
00235             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00236         }
00237 
00238         //  password is too short ?
00239         if ( $blCheckLength &&  getStr()->strlen( $sNewPass ) < 6 ) {
00240             $oEx = oxNew( 'oxInputException' );
00241             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
00242 
00243             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00244         }
00245 
00246         //  passwords do not match ?
00247         if ( $sNewPass != $sConfPass ) {
00248             $oEx = oxNew( 'oxUserException' );
00249             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
00250 
00251             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00252         }
00253     }
00254 
00265     public function checkRequiredFields( $oUser, $aInvAddress, $aDelAddress )
00266     {
00267         // collecting info about required fields
00268         $aMustFields = array( 'oxuser__oxfname',
00269                               'oxuser__oxlname',
00270                               'oxuser__oxstreetnr',
00271                               'oxuser__oxstreet',
00272                               'oxuser__oxzip',
00273                               'oxuser__oxcity' );
00274 
00275         // config should override default fields
00276         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
00277         if ( is_array( $aMustFillFields ) ) {
00278             $aMustFields = $aMustFillFields;
00279         }
00280 
00281         // assuring data to check
00282         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
00283         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
00284 
00285         // collecting fields
00286         $aFields = array_merge( $aInvAddress, $aDelAddress );
00287 
00288 
00289         // check delivery address ?
00290         $blCheckDel = false;
00291         if ( count( $aDelAddress ) ) {
00292             $blCheckDel = true;
00293         }
00294 
00295         // checking
00296         foreach ( $aMustFields as $sMustField ) {
00297 
00298             // A. not nice, but we keep all fields info in one config array, and must support backward compatibility.
00299             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
00300                 continue;
00301             }
00302 
00303             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
00304                 $this->checkRequiredArrayFields( $oUser, $sMustField, $aFields[$sMustField] );
00305             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
00306                    $oEx = oxNew( 'oxInputException' );
00307                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00308 
00309                    $this->_addValidationError( $sMustField, $oEx );
00310             }
00311         }
00312     }
00313 
00323     public function checkRequiredArrayFields( $oUser, $sFieldName, $aFieldValues )
00324     {
00325         foreach ( $aFieldValues as $sValue ) {
00326             if ( !trim( $sValue ) ) {
00327                 $oEx = oxNew( 'oxInputException' );
00328                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00329 
00330                 $this->_addValidationError( $sFieldName, $oEx );
00331             }
00332         }
00333     }
00334 
00344     public function checkCountries( $oUser, $aInvAddress, $aDelAddress )
00345     {
00346         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
00347         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
00348 
00349         if ( $sBillCtry || $sDelCtry ) {
00350             $oDb = oxDb::getDb();
00351 
00352             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
00353                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
00354                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
00355             } else {
00356                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
00357                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
00358             }
00359 
00360             if ( !$oDb->getOne( $sQ ) ) {
00361                 $oEx = oxNew( 'oxUserException' );
00362                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
00363 
00364                 $this->_addValidationError( "oxuser__oxpassword", $oEx );
00365             }
00366         }
00367     }
00368 
00378     public function checkVatId( $oUser, $aInvAddress )
00379     {
00380         if ( $aInvAddress['oxuser__oxustid'] ) {
00381 
00382             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
00383                 // no country
00384                 return;
00385             }
00386             $oCountry = oxNew('oxcountry');
00387             if ( $oCountry->load( $sCountryId ) && $oCountry->isForeignCountry() && $oCountry->isInEU() ) {
00388 
00389                     if ( strncmp( $aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2 ) ) {
00390                         $oEx = oxNew( 'oxInputException' );
00391                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
00392 
00393                         return $this->_addValidationError( "oxuser__oxustid", $oEx );
00394                     }
00395 
00396             }
00397         }
00398     }
00399 
00405     public function getFieldValidationErrors()
00406     {
00407         return $this->_aInputValidationErrors;
00408     }
00409 
00415     public function getFirstValidationError()
00416     {
00417         $oErr = null;
00418         $aErr = reset( $this->_aInputValidationErrors );
00419         if ( is_array( $aErr ) ) {
00420             $oErr = reset( $aErr );
00421         }
00422         return $oErr;
00423     }
00424 
00433     public function validatePaymentInputData( $sPaymentId, & $aDynValue )
00434     {
00435         $mxValidationResult = true;
00436 
00437         switch( $sPaymentId ) {
00438             case 'oxidcreditcard':
00439                 $mxValidationResult = false;
00440 
00441                 $blAllCreditCardInformationSet = $this->_isAllBankInformationSet( $this->_aRequiredCCFields, $aDynValue );
00442                 $blCreditCardTypeExist = in_array( $aDynValue['kktype'], $this->_aPossibleCCType );
00443 
00444                 if ( $blAllCreditCardInformationSet && $blCreditCardTypeExist ) {
00445                     $oCardValidator = oxNew( "oxccvalidator" );
00446                     $mxValidationResult = $oCardValidator->isValidCard(
00447                                                     $aDynValue['kknumber'],
00448                                                     $aDynValue['kktype'],
00449                                                     $aDynValue['kkmonth'].substr( $aDynValue['kkyear'], 2, 2 )
00450                     );
00451                 }
00452                 break;
00453 
00454             case "oxiddebitnote":
00455                 $mxValidationResult = false;
00456 
00457                 if ( $this->_isAllBankInformationSet( $this->_aRequiredDCFields, $aDynValue ) ) {
00458                     $mxValidationResult = $this->_validateDebitNote( $aDynValue );
00459                 }
00460 
00461                 break;
00462         }
00463 
00464         return $mxValidationResult;
00465     }
00466 
00476     protected function _addValidationError( $sFieldName, $oErr )
00477     {
00478         return $this->_aInputValidationErrors[$sFieldName][] = $oErr;
00479     }
00480 
00486     protected function _validateDebitNote( $aDebitInformation )
00487     {
00488         $aDebitInformation = $this->_cleanDebitInformation( $aDebitInformation );
00489         $sBankCode = $aDebitInformation['lsblz'];
00490         $sAccountNumber = $aDebitInformation['lsktonr'];
00491         $oSepaValidator = oxNew( "oxSepaValidator" );
00492 
00493         if ( empty( $sBankCode ) || $oSepaValidator->isValidBIC( $sBankCode ) ) {
00494             $mxValidationResult = true;
00495             if ( !$oSepaValidator->isValidIBAN( $sAccountNumber ) ) {
00496                 $mxValidationResult = self::INVALID_ACCOUNT_NUMBER;
00497             }
00498         } else {
00499             $mxValidationResult = $this->_validateOldDebitInfo( $aDebitInformation );
00500         }
00501 
00502         return $mxValidationResult;
00503     }
00504 
00509     protected function _validateOldDebitInfo( $aDebitInfo )
00510     {
00511         $oStr       = getStr();
00512         $aDebitInfo = $this->_fixAccountNumber( $aDebitInfo );
00513 
00514         $mxValidationResult = true;
00515 
00516         if ( !$oStr->preg_match( "/^\d{5,8}$/", $aDebitInfo['lsblz'] ) ) {
00517             // Bank code is invalid
00518             $mxValidationResult = self::INVALID_BANK_CODE;
00519         }
00520 
00521         if ( true === $mxValidationResult && !$oStr->preg_match( "/^\d{10,12}$/", $aDebitInfo['lsktonr'] ) ) {
00522             // Account number is invalid
00523             $mxValidationResult = self::INVALID_ACCOUNT_NUMBER;
00524         }
00525 
00526 
00527         return $mxValidationResult;
00528     }
00529 
00535     protected function _fixAccountNumber( $aDebitInfo )
00536     {
00537         $oStr = getStr();
00538 
00539         if ( $oStr->strlen( $aDebitInfo['lsktonr'] ) < 10 ) {
00540             $sNewNum = str_repeat(
00541                            '0', 10 - $oStr->strlen( $aDebitInfo['lsktonr'] )
00542                        ) . $aDebitInfo['lsktonr'];
00543             $aDebitInfo['lsktonr'] = $sNewNum;
00544         }
00545 
00546         return $aDebitInfo;
00547     }
00548 
00555     protected function _isAllBankInformationSet( $aRequiredFields, $aBankInformation )
00556     {
00557         $blResult = true;
00558         foreach ( $aRequiredFields as $sFieldName ) {
00559             if ( !isset( $aBankInformation[$sFieldName] ) || !trim( $aBankInformation[$sFieldName] ) ) {
00560                 $blResult = false;
00561                 break;
00562             }
00563         }
00564 
00565         return $blResult;
00566     }
00567 
00573     protected function _cleanDebitInformation( $aDebitInformation )
00574     {
00575         $aDebitInformation['lsblz']   = str_replace( ' ', '', $aDebitInformation['lsblz'] );
00576         $aDebitInformation['lsktonr'] = str_replace( ' ', '', $aDebitInformation['lsktonr'] );
00577 
00578         return $aDebitInformation;
00579     }
00580 }