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 
00068     public function __construct()
00069     {
00070     }
00071 
00079     static function getInstance()
00080     {
00081         return oxRegistry::get("oxInputValidator");
00082     }
00083 
00093     public function validateBasketAmount( $dAmount )
00094     {
00095         $dAmount = str_replace( ',', '.', $dAmount );
00096 
00097         if ( !is_numeric( $dAmount ) || $dAmount < 0) {
00098             $oEx = oxNew( 'oxArticleInputException' );
00099             $oEx->setMessage('EXCEPTION_INPUT_INVALIDAMOUNT');
00100             throw $oEx;
00101         }
00102 
00103         if ( !oxRegistry::getConfig()->getConfigParam( 'blAllowUnevenAmounts' ) ) {
00104             $dAmount = round( ( string ) $dAmount );
00105         }
00106 
00107         //negative amounts are not allowed
00108         //$dAmount = abs($dAmount);
00109 
00110         return $dAmount;
00111     }
00112 
00121     public function validatePaymentInputData( $sPaymentId, & $aDynvalue )
00122     {
00123         $blOK = true;
00124 
00125         switch( $sPaymentId ) {
00126             case 'oxidcreditcard':
00127 
00128                 $blOK = false;
00129 
00130                 foreach ( $this->_aRequiredCCFields as $sFieldName ) {
00131                     if ( !isset( $aDynvalue[$sFieldName] ) || !trim( $aDynvalue[$sFieldName] ) ) {
00132                         break 2;
00133                     }
00134                 }
00135 
00136                 if ( in_array( $aDynvalue['kktype'], $this->_aPossibleCCType ) ) {
00137                     $sType = $aDynvalue['kktype'];
00138                 } else {
00139                     $sType = null;
00140                     break;
00141                 }
00142 
00143                 $oCardValidator = oxNew( "oxccvalidator" );
00144                 $blResult = $oCardValidator->isValidCard( $aDynvalue['kknumber'], $sType, $aDynvalue['kkmonth'].substr( $aDynvalue['kkyear'], 2, 2 ) );
00145                 if ( $blResult ) {
00146                     $blOK = true;
00147                 }
00148 
00149                 break;
00150 
00151             case "oxiddebitnote":
00152 
00153                 $blOK = false;
00154                 $oStr = getStr();
00155 
00156                 foreach ( $this->_aRequiredDCFields as $sFieldName ) {
00157                     if ( !isset( $aDynvalue[$sFieldName] ) || !trim( $aDynvalue[$sFieldName] ) ) {
00158                         break 2;
00159                     }
00160                 }
00161 
00162                 // cleaning up spaces
00163                 $aDynvalue['lsblz']   = str_replace( ' ', '', $aDynvalue['lsblz'] );
00164                 $aDynvalue['lsktonr'] = str_replace( ' ', '', $aDynvalue['lsktonr'] );
00165 
00166                 //if konto number is shorter than 10, add zeros in front of number
00167                 if ( $oStr->strlen( $aDynvalue['lsktonr'] ) < 10 ) {
00168                     $sNewNum = str_repeat( '0', 10 - $oStr->strlen( $aDynvalue['lsktonr'] ) ).$aDynvalue['lsktonr'];
00169                     $aDynvalue['lsktonr'] = $sNewNum;
00170                 }
00171 
00172                 if ( $oStr->preg_match( "/^\d{5,8}$/", $aDynvalue['lsblz'] ) && $oStr->preg_match( "/\d{10}/", $aDynvalue['lsktonr'] ) ) {
00173                     $blOK = true;
00174                 }
00175                 break;
00176         }
00177 
00178         return $blOK;
00179     }
00180 
00190     protected function _addValidationError( $sFieldName, $oErr )
00191     {
00192         return $this->_aInputValidationErrors[$sFieldName][] = $oErr;
00193     }
00194 
00209     public function checkLogin( $oUser, $sLogin, $aInvAddress )
00210     {
00211         // check only for users with password during registration
00212         // if user wants to change user name - we must check if passwords are ok before changing
00213         if ( $oUser->oxuser__oxpassword->value && $sLogin != $oUser->oxuser__oxusername->value ) {
00214 
00215             // on this case password must be taken directly from request
00216             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
00217             if ( !$sNewPass ) {
00218 
00219                 // 1. user forgot to enter password
00220                 $oEx = oxNew( 'oxInputException' );
00221                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00222 
00223                 return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00224             } else {
00225 
00226                 // 2. entered wrong password
00227                 if ( !$oUser->isSamePassword( $sNewPass ) ) {
00228                     $oEx = oxNew( 'oxUserException' );
00229                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
00230 
00231                     return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00232                 }
00233             }
00234         }
00235 
00236         if ( $oUser->checkIfEmailExists( $sLogin ) ) {
00237             //if exists then we do now allow to do that
00238             $oEx = oxNew( 'oxUserException' );
00239             $oLang = oxRegistry::getLang();
00240             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
00241 
00242             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00243         }
00244     }
00245 
00255     public function checkEmail(  $oUser, $sEmail )
00256     {
00257         // missing email address (user login name) ?
00258         if ( !$sEmail ) {
00259             $oEx = oxNew( 'oxInputException' );
00260             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00261 
00262             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00263         }
00264 
00265         // invalid email address ?
00266         if ( !oxRegistry::getUtils()->isValidEmail( $sEmail ) ) {
00267             $oEx = oxNew( 'oxInputException' );
00268             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
00269 
00270             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00271         }
00272     }
00273 
00285     public function checkPassword( $oUser, $sNewPass, $sConfPass, $blCheckLenght = false )
00286     {
00287         //  no password at all
00288         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
00289             $oEx = oxNew( 'oxInputException' );
00290             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
00291 
00292             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00293         }
00294 
00295         //  password is too short ?
00296         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
00297             $oEx = oxNew( 'oxInputException' );
00298             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
00299 
00300             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00301         }
00302 
00303         //  passwords do not match ?
00304         if ( $sNewPass != $sConfPass ) {
00305             $oEx = oxNew( 'oxUserException' );
00306             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
00307 
00308             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00309         }
00310     }
00311 
00322     public function checkRequiredFields( $oUser, $aInvAddress, $aDelAddress )
00323     {
00324         // collecting info about required fields
00325         $aMustFields = array( 'oxuser__oxfname',
00326                               'oxuser__oxlname',
00327                               'oxuser__oxstreetnr',
00328                               'oxuser__oxstreet',
00329                               'oxuser__oxzip',
00330                               'oxuser__oxcity' );
00331 
00332         // config should override default fields
00333         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
00334         if ( is_array( $aMustFillFields ) ) {
00335             $aMustFields = $aMustFillFields;
00336         }
00337 
00338         // assuring data to check
00339         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
00340         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
00341 
00342         // collecting fields
00343         $aFields = array_merge( $aInvAddress, $aDelAddress );
00344 
00345 
00346         // check delivery address ?
00347         $blCheckDel = false;
00348         if ( count( $aDelAddress ) ) {
00349             $blCheckDel = true;
00350         }
00351 
00352         // checking
00353         foreach ( $aMustFields as $sMustField ) {
00354 
00355             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
00356             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
00357                 continue;
00358             }
00359 
00360             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
00361                 $this->checkRequiredArrayFields( $oUser, $sMustField, $aFields[$sMustField] );
00362             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
00363                    $oEx = oxNew( 'oxInputException' );
00364                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00365 
00366                    $this->_addValidationError( $sMustField, $oEx );
00367             }
00368         }
00369     }
00370 
00380     public function checkRequiredArrayFields( $oUser, $sFieldName, $aFieldValues )
00381     {
00382         foreach ( $aFieldValues as $sValue ) {
00383             if ( !trim( $sValue ) ) {
00384                 $oEx = oxNew( 'oxInputException' );
00385                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00386 
00387                 $this->_addValidationError( $sFieldName, $oEx );
00388             }
00389         }
00390     }
00391 
00401     public function checkCountries( $oUser, $aInvAddress, $aDelAddress )
00402     {
00403         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
00404         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
00405 
00406         if ( $sBillCtry || $sDelCtry ) {
00407             $oDb = oxDb::getDb();
00408 
00409             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
00410                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
00411                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
00412             } else {
00413                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
00414                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
00415             }
00416 
00417             if ( !$oDb->getOne( $sQ ) ) {
00418                 $oEx = oxNew( 'oxUserException' );
00419                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
00420 
00421                 $this->_addValidationError( "oxuser__oxpassword", $oEx );
00422             }
00423         }
00424     }
00425 
00435     public function checkVatId( $oUser, $aInvAddress )
00436     {
00437         if ( $aInvAddress['oxuser__oxustid'] ) {
00438 
00439             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
00440                 // no country
00441                 return;
00442             }
00443             $oCountry = oxNew('oxcountry');
00444             if ( $oCountry->load( $sCountryId ) && $oCountry->isForeignCountry() && $oCountry->isInEU() ) {
00445 
00446                     if ( strncmp( $aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2 ) ) {
00447                         $oEx = oxNew( 'oxInputException' );
00448                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
00449 
00450                         return $this->_addValidationError( "oxuser__oxustid", $oEx );
00451                     }
00452 
00453             }
00454         }
00455     }
00456 
00462     public function getFieldValidationErrors()
00463     {
00464         return $this->_aInputValidationErrors;
00465     }
00466 
00472     public function getFirstValidationError()
00473     {
00474         $oErr = null;
00475         $aErr = reset( $this->_aInputValidationErrors );
00476         if ( is_array( $aErr ) ) {
00477             $oErr = reset( $aErr );
00478         }
00479         return $oErr;
00480     }
00481 }