oxinputvalidator.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxInputValidator extends oxSuperCfg
00008 {
00014     private static $_instance = null;
00015 
00021     protected $_aRequiredCCFields = array( 'kktype',
00022                                            'kknumber',
00023                                            'kkmonth',
00024                                            'kkyear',
00025                                            'kkname',
00026                                            'kkpruef'
00027                                           );
00028 
00034     protected $_aInputValidationErrors = array();
00035 
00041     protected $_aPossibleCCType = array( 'mcd', // Master Card
00042                                          'vis', // Visa
00043                                          'amx', // American Express
00044                                          'dsc', // Discover
00045                                          'dnc', // Diners Club
00046                                          'jcb', // JCB
00047                                          'swi', // Switch
00048                                          'dlt', // Delta
00049                                          'enr'  // EnRoute
00050                                         );
00051 
00057     protected $_aRequiredDCFields = array( 'lsbankname',
00058                                            'lsblz',
00059                                            'lsktonr',
00060                                            'lsktoinhaber'
00061                                          );
00062 
00067     public function __construct()
00068     {
00069     }
00070 
00078     static function getInstance()
00079     {
00080         return oxRegistry::get("oxInputValidator");
00081     }
00082 
00092     public function validateBasketAmount( $dAmount )
00093     {
00094         $dAmount = str_replace( ',', '.', $dAmount );
00095 
00096         if ( !is_numeric( $dAmount ) || $dAmount < 0) {
00097             $oEx = oxNew( 'oxArticleInputException' );
00098             $oEx->setMessage('ERROR_MESSAGE_INPUT_INVALIDAMOUNT');
00099             throw $oEx;
00100         }
00101 
00102         if ( !oxRegistry::getConfig()->getConfigParam( 'blAllowUnevenAmounts' ) ) {
00103             $dAmount = round( ( string ) $dAmount );
00104         }
00105 
00106         //negative amounts are not allowed
00107         //$dAmount = abs($dAmount);
00108 
00109         return $dAmount;
00110     }
00111 
00120     public function validatePaymentInputData( $sPaymentId, & $aDynvalue )
00121     {
00122         $mxValidationResult = true;
00123 
00124         switch( $sPaymentId ) {
00125             case 'oxidcreditcard':
00126 
00127                 $mxValidationResult = false;
00128 
00129                 foreach ( $this->_aRequiredCCFields as $sFieldName ) {
00130                     if ( !isset( $aDynvalue[$sFieldName] ) || !trim( $aDynvalue[$sFieldName] ) ) {
00131                         break 2;
00132                     }
00133                 }
00134 
00135                 if ( in_array( $aDynvalue['kktype'], $this->_aPossibleCCType ) ) {
00136                     $sType = $aDynvalue['kktype'];
00137                 } else {
00138                     $sType = null;
00139                     break;
00140                 }
00141 
00142                 $oCardValidator = oxNew( "oxccvalidator" );
00143                 $blResult = $oCardValidator->isValidCard( $aDynvalue['kknumber'], $sType, $aDynvalue['kkmonth'].substr( $aDynvalue['kkyear'], 2, 2 ) );
00144                 if ( $blResult ) {
00145                     $mxValidationResult = true;
00146                 }
00147 
00148                 break;
00149 
00150             case "oxiddebitnote":
00151 
00152                 $mxValidationResult = false;
00153                 $oStr = getStr();
00154 
00155                 foreach ( $this->_aRequiredDCFields as $sFieldName ) {
00156                     if ( !isset( $aDynvalue[$sFieldName] ) || !trim( $aDynvalue[$sFieldName] ) ) {
00157                         break 2;
00158                     }
00159                 }
00160 
00161                 // Cleaning up spaces
00162                 $aDynvalue['lsblz']   = str_replace( ' ', '', $aDynvalue['lsblz'] );
00163                 $aDynvalue['lsktonr'] = str_replace( ' ', '', $aDynvalue['lsktonr'] );
00164 
00165                 $oSepaValidator = oxNew( "oxSepaValidator" );
00166 
00167                 // Check BIC / IBAN
00168                 if ( $oSepaValidator->isValidBIC($aDynvalue['lsblz']) && $oSepaValidator->isValidIBAN($aDynvalue['lsktonr']) ) {
00169                     $mxValidationResult = true;
00170                 }
00171 
00172                 // If can't meet BIC / IBAN formats check account number and bank code with old validation
00173                 if ( !$mxValidationResult ) {
00174                     // If account number is shorter than 10, add zeros in front of number
00175                     if ( $oStr->strlen( $aDynvalue['lsktonr'] ) < 10 ) {
00176                         $sNewNum = str_repeat( '0', 10 - $oStr->strlen( $aDynvalue['lsktonr'] ) ).$aDynvalue['lsktonr'];
00177                         $aDynvalue['lsktonr'] = $sNewNum;
00178                     }
00179 
00180                     if ( $oStr->preg_match( "/^\d{5,8}$/", $aDynvalue['lsblz'] ) ) {
00181                         if ( !$oStr->preg_match( "/\d{10}/", $aDynvalue['lsktonr'] ) ) {
00182                             // Account number is invalid
00183                             $mxValidationResult = -5;
00184                             break;
00185                         } else {
00186                             $mxValidationResult = true;
00187                         }
00188                     } else {
00189                         // Bank code is invalid
00190                         $mxValidationResult = -4;
00191                     }
00192                 }
00193 
00194 
00195                 break;
00196         }
00197 
00198         return $mxValidationResult;
00199     }
00200 
00210     protected function _addValidationError( $sFieldName, $oErr )
00211     {
00212         return $this->_aInputValidationErrors[$sFieldName][] = $oErr;
00213     }
00214 
00229     public function checkLogin( $oUser, $sLogin, $aInvAddress )
00230     {
00231         // check only for users with password during registration
00232         // if user wants to change user name - we must check if passwords are ok before changing
00233         if ( $oUser->oxuser__oxpassword->value && $sLogin != $oUser->oxuser__oxusername->value ) {
00234 
00235             // on this case password must be taken directly from request
00236             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
00237             if ( !$sNewPass ) {
00238 
00239                 // 1. user forgot to enter password
00240                 $oEx = oxNew( 'oxInputException' );
00241                 $oEx->setMessage('ERROR_MESSAGE_INPUT_NOTALLFIELDS');
00242 
00243                 return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00244             } else {
00245 
00246                 // 2. entered wrong password
00247                 if ( !$oUser->isSamePassword( $sNewPass ) ) {
00248                     $oEx = oxNew( 'oxUserException' );
00249                     $oEx->setMessage('ERROR_MESSAGE_USER_PWDDONTMATCH');
00250 
00251                     return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00252                 }
00253             }
00254         }
00255 
00256         if ( $oUser->checkIfEmailExists( $sLogin ) ) {
00257             //if exists then we do now allow to do that
00258             $oEx = oxNew( 'oxUserException' );
00259             $oLang = oxRegistry::getLang();
00260             $oEx->setMessage( sprintf( $oLang->translateString( 'ERROR_MESSAGE_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
00261 
00262             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00263         }
00264     }
00265 
00275     public function checkEmail(  $oUser, $sEmail )
00276     {
00277         // missing email address (user login name) ?
00278         if ( !$sEmail ) {
00279             $oEx = oxNew( 'oxInputException' );
00280             $oEx->setMessage('ERROR_MESSAGE_INPUT_NOTALLFIELDS');
00281 
00282             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00283         }
00284 
00285         // invalid email address ?
00286         if ( !oxRegistry::getUtils()->isValidEmail( $sEmail ) ) {
00287             $oEx = oxNew( 'oxInputException' );
00288             $oEx->setMessage( 'ERROR_MESSAGE_INPUT_NOVALIDEMAIL' );
00289 
00290             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00291         }
00292     }
00293 
00305     public function checkPassword( $oUser, $sNewPass, $sConfPass, $blCheckLenght = false )
00306     {
00307         //  no password at all
00308         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
00309             $oEx = oxNew( 'oxInputException' );
00310             $oEx->setMessage('ERROR_MESSAGE_INPUT_EMPTYPASS');
00311 
00312             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00313         }
00314 
00315         //  password is too short ?
00316         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
00317             $oEx = oxNew( 'oxInputException' );
00318             $oEx->setMessage('ERROR_MESSAGE_PASSWORD_TOO_SHORT');
00319 
00320             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00321         }
00322 
00323         //  passwords do not match ?
00324         if ( $sNewPass != $sConfPass ) {
00325             $oEx = oxNew( 'oxUserException' );
00326             $oEx->setMessage('ERROR_MESSAGE_USER_PWDDONTMATCH');
00327 
00328             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00329         }
00330     }
00331 
00342     public function checkRequiredFields( $oUser, $aInvAddress, $aDelAddress )
00343     {
00344         // collecting info about required fields
00345         $aMustFields = array( 'oxuser__oxfname',
00346                               'oxuser__oxlname',
00347                               'oxuser__oxstreetnr',
00348                               'oxuser__oxstreet',
00349                               'oxuser__oxzip',
00350                               'oxuser__oxcity' );
00351 
00352         // config should override default fields
00353         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
00354         if ( is_array( $aMustFillFields ) ) {
00355             $aMustFields = $aMustFillFields;
00356         }
00357 
00358         // assuring data to check
00359         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
00360         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
00361 
00362         // collecting fields
00363         $aFields = array_merge( $aInvAddress, $aDelAddress );
00364 
00365 
00366         // check delivery address ?
00367         $blCheckDel = false;
00368         if ( count( $aDelAddress ) ) {
00369             $blCheckDel = true;
00370         }
00371 
00372         // checking
00373         foreach ( $aMustFields as $sMustField ) {
00374 
00375             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
00376             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
00377                 continue;
00378             }
00379 
00380             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
00381                 $this->checkRequiredArrayFields( $oUser, $sMustField, $aFields[$sMustField] );
00382             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
00383                    $oEx = oxNew( 'oxInputException' );
00384                    $oEx->setMessage('ERROR_MESSAGE_INPUT_NOTALLFIELDS');
00385 
00386                    $this->_addValidationError( $sMustField, $oEx );
00387             }
00388         }
00389     }
00390 
00400     public function checkRequiredArrayFields( $oUser, $sFieldName, $aFieldValues )
00401     {
00402         foreach ( $aFieldValues as $sValue ) {
00403             if ( !trim( $sValue ) ) {
00404                 $oEx = oxNew( 'oxInputException' );
00405                 $oEx->setMessage('ERROR_MESSAGE_INPUT_NOTALLFIELDS');
00406 
00407                 $this->_addValidationError( $sFieldName, $oEx );
00408             }
00409         }
00410     }
00411 
00421     public function checkCountries( $oUser, $aInvAddress, $aDelAddress )
00422     {
00423         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
00424         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
00425 
00426         if ( $sBillCtry || $sDelCtry ) {
00427             $oDb = oxDb::getDb();
00428 
00429             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
00430                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
00431                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
00432             } else {
00433                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
00434                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
00435             }
00436 
00437             if ( !$oDb->getOne( $sQ ) ) {
00438                 $oEx = oxNew( 'oxUserException' );
00439                 $oEx->setMessage('ERROR_MESSAGE_INPUT_NOTALLFIELDS' );
00440 
00441                 $this->_addValidationError( "oxuser__oxpassword", $oEx );
00442             }
00443         }
00444     }
00445 
00455     public function checkVatId( $oUser, $aInvAddress )
00456     {
00457         if ( $aInvAddress['oxuser__oxustid'] ) {
00458 
00459             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
00460                 // no country
00461                 return;
00462             }
00463             $oCountry = oxNew('oxcountry');
00464             if ( $oCountry->load( $sCountryId ) && $oCountry->isForeignCountry() && $oCountry->isInEU() ) {
00465 
00466                     if ( strncmp( $aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2 ) ) {
00467                         $oEx = oxNew( 'oxInputException' );
00468                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
00469 
00470                         return $this->_addValidationError( "oxuser__oxustid", $oEx );
00471                     }
00472 
00473             }
00474         }
00475     }
00476 
00482     public function getFieldValidationErrors()
00483     {
00484         return $this->_aInputValidationErrors;
00485     }
00486 
00492     public function getFirstValidationError()
00493     {
00494         $oErr = null;
00495         $aErr = reset( $this->_aInputValidationErrors );
00496         if ( is_array( $aErr ) ) {
00497             $oErr = reset( $aErr );
00498         }
00499         return $oErr;
00500     }
00501 }