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 
00077     static function getInstance()
00078     {
00079         if ( defined('OXID_PHP_UNIT')) {
00080             if ( ($oClassMod = modInstances::getMod(__CLASS__))  && is_object($oClassMod) ) {
00081                 return $oClassMod;
00082             } else {
00083                  $inst = oxNew( 'oxInputValidator' );
00084                  modInstances::addMod( __CLASS__, $inst );
00085                  return $inst;
00086             }
00087         }
00088 
00089         if ( !isset( self::$_instance ) ) {
00090             // allow modules
00091             self::$_instance = oxNew( 'oxInputValidator' );
00092         }
00093         return self::$_instance;
00094     }
00095 
00105     public function validateBasketAmount( $dAmount )
00106     {
00107         $dAmount = str_replace( ',', '.', $dAmount );
00108 
00109         if ( !is_numeric( $dAmount ) || $dAmount < 0) {
00110             $oEx = oxNew( 'oxArticleInputException' );
00111             $oEx->setMessage('EXCEPTION_INPUT_INVALIDAMOUNT');
00112             throw $oEx;
00113         }
00114 
00115         if ( !oxConfig::getInstance()->getConfigParam( 'blAllowUnevenAmounts' ) ) {
00116             $dAmount = round( ( string ) $dAmount );
00117         }
00118 
00119         //negative amounts are not allowed
00120         //$dAmount = abs($dAmount);
00121 
00122         return $dAmount;
00123     }
00124 
00133     public function validatePaymentInputData( $sPaymentId, & $aDynvalue )
00134     {
00135         $blOK = true;
00136 
00137         switch( $sPaymentId ) {
00138             case 'oxidcreditcard':
00139 
00140                 $blOK = false;
00141 
00142                 foreach ( $this->_aRequiredCCFields as $sFieldName ) {
00143                     if ( !isset( $aDynvalue[$sFieldName] ) || !trim( $aDynvalue[$sFieldName] ) ) {
00144                         break 2;
00145                     }
00146                 }
00147 
00148                 if ( in_array( $aDynvalue['kktype'], $this->_aPossibleCCType ) ) {
00149                     $sType = $aDynvalue['kktype'];
00150                 } else {
00151                     $sType = null;
00152                     break;
00153                 }
00154 
00155                 $oCardValidator = oxNew( "oxccvalidator" );
00156                 $blResult = $oCardValidator->isValidCard( $aDynvalue['kknumber'], $sType, $aDynvalue['kkmonth'].substr( $aDynvalue['kkyear'], 2, 2 ) );
00157                 if ( $blResult ) {
00158                     $blOK = true;
00159                 }
00160 
00161                 break;
00162 
00163             case "oxiddebitnote":
00164 
00165                 $blOK = false;
00166                 $oStr = getStr();
00167 
00168                 foreach ( $this->_aRequiredDCFields as $sFieldName ) {
00169                     if ( !isset( $aDynvalue[$sFieldName] ) || !trim( $aDynvalue[$sFieldName] ) ) {
00170                         break 2;
00171                     }
00172                 }
00173 
00174                 // cleaning up spaces
00175                 $aDynvalue['lsblz']   = str_replace( ' ', '', $aDynvalue['lsblz'] );
00176                 $aDynvalue['lsktonr'] = str_replace( ' ', '', $aDynvalue['lsktonr'] );
00177 
00178                 //if konto number is shorter than 10, add zeros in front of number
00179                 if ( $oStr->strlen( $aDynvalue['lsktonr'] ) < 10 ) {
00180                     $sNewNum = str_repeat( '0', 10 - $oStr->strlen( $aDynvalue['lsktonr'] ) ).$aDynvalue['lsktonr'];
00181                     $aDynvalue['lsktonr'] = $sNewNum;
00182                 }
00183 
00184                 if ( $oStr->preg_match( "/^\d{5,8}$/", $aDynvalue['lsblz'] ) && $oStr->preg_match( "/\d{10}/", $aDynvalue['lsktonr'] ) ) {
00185                     $blOK = true;
00186                 }
00187                 break;
00188         }
00189 
00190         return $blOK;
00191     }
00192 
00202     protected function _addValidationError( $sFieldName, $oErr )
00203     {
00204         return $this->_aInputValidationErrors[$sFieldName][] = $oErr;
00205     }
00206 
00221     public function checkLogin( $oUser, $sLogin, $aInvAddress )
00222     {
00223         // check only for users with password during registration
00224         // if user wants to change user name - we must check if passwords are ok before changing
00225         if ( $oUser->oxuser__oxpassword->value && $sLogin != $oUser->oxuser__oxusername->value ) {
00226 
00227             // on this case password must be taken directly from request
00228             $sNewPass = (isset( $aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword'] )?$aInvAddress['oxuser__oxpassword']:oxConfig::getParameter( 'user_password' );
00229             if ( !$sNewPass ) {
00230 
00231                 // 1. user forgot to enter password
00232                 $oEx = oxNew( 'oxInputException' );
00233                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00234 
00235                 return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00236             } else {
00237 
00238                 // 2. entered wrong password
00239                 if ( !$oUser->isSamePassword( $sNewPass ) ) {
00240                     $oEx = oxNew( 'oxUserException' );
00241                     $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
00242 
00243                     return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00244                 }
00245             }
00246         }
00247 
00248         if ( $oUser->checkIfEmailExists( $sLogin ) ) {
00249             //if exists then we do now allow to do that
00250             $oEx = oxNew( 'oxUserException' );
00251             $oLang = oxLang::getInstance();
00252             $oEx->setMessage( sprintf( $oLang->translateString( 'EXCEPTION_USER_USEREXISTS', $oLang->getTplLanguage() ), $sLogin ) );
00253 
00254             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00255         }
00256     }
00257 
00267     public function checkEmail(  $oUser, $sEmail )
00268     {
00269         // missing email address (user login name) ?
00270         if ( !$sEmail ) {
00271             $oEx = oxNew( 'oxInputException' );
00272             $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00273 
00274             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00275         }
00276 
00277         // invalid email address ?
00278         if ( !oxUtils::getInstance()->isValidEmail( $sEmail ) ) {
00279             $oEx = oxNew( 'oxInputException' );
00280             $oEx->setMessage( 'EXCEPTION_INPUT_NOVALIDEMAIL' );
00281 
00282             return $this->_addValidationError( "oxuser__oxusername", $oEx );
00283         }
00284     }
00285 
00297     public function checkPassword( $oUser, $sNewPass, $sConfPass, $blCheckLenght = false )
00298     {
00299         //  no password at all
00300         if ( $blCheckLenght && getStr()->strlen( $sNewPass ) == 0 ) {
00301             $oEx = oxNew( 'oxInputException' );
00302             $oEx->setMessage('EXCEPTION_INPUT_EMPTYPASS');
00303 
00304             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00305         }
00306 
00307         //  password is too short ?
00308         if ( $blCheckLenght &&  getStr()->strlen( $sNewPass ) < 6 ) {
00309             $oEx = oxNew( 'oxInputException' );
00310             $oEx->setMessage('EXCEPTION_INPUT_PASSTOOSHORT');
00311 
00312             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00313         }
00314 
00315         //  passwords do not match ?
00316         if ( $sNewPass != $sConfPass ) {
00317             $oEx = oxNew( 'oxUserException' );
00318             $oEx->setMessage('EXCEPTION_USER_PWDDONTMATCH');
00319 
00320             return $this->_addValidationError( "oxuser__oxpassword", $oEx );
00321         }
00322     }
00323 
00334     public function checkRequiredFields( $oUser, $aInvAddress, $aDelAddress )
00335     {
00336         // collecting info about required fields
00337         $aMustFields = array( 'oxuser__oxfname',
00338                               'oxuser__oxlname',
00339                               'oxuser__oxstreetnr',
00340                               'oxuser__oxstreet',
00341                               'oxuser__oxzip',
00342                               'oxuser__oxcity' );
00343 
00344         // config shoud override default fields
00345         $aMustFillFields = $this->getConfig()->getConfigParam( 'aMustFillFields' );
00346         if ( is_array( $aMustFillFields ) ) {
00347             $aMustFields = $aMustFillFields;
00348         }
00349 
00350         // assuring data to check
00351         $aInvAddress = is_array( $aInvAddress )?$aInvAddress:array();
00352         $aDelAddress = is_array( $aDelAddress )?$aDelAddress:array();
00353 
00354         // collecting fields
00355         $aFields = array_merge( $aInvAddress, $aDelAddress );
00356 
00357 
00358         // check delivery address ?
00359         $blCheckDel = false;
00360         if ( count( $aDelAddress ) ) {
00361             $blCheckDel = true;
00362         }
00363 
00364         // checking
00365         foreach ( $aMustFields as $sMustField ) {
00366             
00367             // A. not nice, but we keep all fields info in one config array, and must support baskwards compat.
00368             if ( !$blCheckDel && strpos( $sMustField, 'oxaddress__' ) === 0 ) {
00369                 continue;
00370             }
00371 
00372             if ( isset( $aFields[$sMustField] ) && is_array( $aFields[$sMustField] ) ) {
00373                 $this->checkRequiredArrayFields( $oUser, $sMustField, $aFields[$sMustField] );
00374             } elseif ( !isset( $aFields[$sMustField] ) || !trim( $aFields[$sMustField] ) ) {
00375                    $oEx = oxNew( 'oxInputException' );
00376                    $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00377 
00378                    $this->_addValidationError( $sMustField, $oEx );
00379             }
00380         }
00381     }
00382 
00392     public function checkRequiredArrayFields( $oUser, $sFieldName, $aFieldValues )
00393     {
00394         foreach ( $aFieldValues as $sValue ) {
00395             if ( !trim( $sValue ) ) {
00396                 $oEx = oxNew( 'oxInputException' );
00397                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS');
00398 
00399                 $this->_addValidationError( $sFieldName, $oEx );
00400             }
00401         }
00402     }
00403 
00413     public function checkCountries( $oUser, $aInvAddress, $aDelAddress )
00414     {
00415         $sBillCtry = isset( $aInvAddress['oxuser__oxcountryid'] ) ? $aInvAddress['oxuser__oxcountryid'] : null;
00416         $sDelCtry  = isset( $aDelAddress['oxaddress__oxcountryid'] ) ? $aDelAddress['oxaddress__oxcountryid'] : null;
00417 
00418         if ( $sBillCtry || $sDelCtry ) {
00419             $oDb = oxDb::getDb();
00420 
00421             if ( ( $sBillCtry == $sDelCtry ) || ( !$sBillCtry && $sDelCtry ) || ( $sBillCtry && !$sDelCtry ) ) {
00422                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
00423                 $sQ = "select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ";
00424             } else {
00425                 $sQ = "select ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sBillCtry )." ) and
00426                               ( select oxactive from oxcountry where oxid = ".$oDb->quote( $sDelCtry )." ) ";
00427             }
00428 
00429             if ( !$oDb->getOne( $sQ ) ) {
00430                 $oEx = oxNew( 'oxUserException' );
00431                 $oEx->setMessage('EXCEPTION_INPUT_NOTALLFIELDS' );
00432 
00433                 $this->_addValidationError( "oxuser__oxpassword", $oEx );
00434             }
00435         }
00436     }
00437 
00447     public function checkVatId( $oUser, $aInvAddress )
00448     {
00449         if ( $aInvAddress['oxuser__oxustid'] ) {
00450 
00451             if (!($sCountryId = $aInvAddress['oxuser__oxcountryid'])) {
00452                 // no country
00453                 return;
00454             }
00455             $oCountry = oxNew('oxcountry');
00456             if ( $oCountry->load( $sCountryId ) && $oCountry->isForeignCountry() && $oCountry->isInEU() ) {
00457 
00458                     if ( strncmp( $aInvAddress['oxuser__oxustid'], $oCountry->oxcountry__oxisoalpha2->value, 2 ) ) {
00459                         $oEx = oxNew( 'oxInputException' );
00460                         $oEx->setMessage( 'VAT_MESSAGE_ID_NOT_VALID' );
00461 
00462                         return $this->_addValidationError( "oxuser__oxustid", $oEx );
00463                     }
00464 
00465             }
00466         }
00467     }
00468 
00474     public function getFieldValidationErrors()
00475     {
00476         return $this->_aInputValidationErrors;
00477     }
00478 
00484     public function getFirstValidationError()
00485     {
00486         $oErr = null;
00487         $aErr = reset( $this->_aInputValidationErrors );
00488         if ( is_array( $aErr ) ) {
00489             $oErr = reset( $aErr );
00490         }
00491         return $oErr;
00492     }
00493 }