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     protected $_aRequiredCCFields = array('kktype',
00026                                           'kknumber',
00027                                           'kkmonth',
00028                                           'kkyear',
00029                                           'kkname',
00030                                           'kkpruef'
00031     );
00032 
00038     protected $_aInputValidationErrors = array();
00039 
00040 
00041     protected $_oCompanyVatInValidator = null;
00042 
00048     protected $_aPossibleCCType = array('mcd', // Master Card
00049                                         'vis', // Visa
00050                                         'amx', // American Express
00051                                         'dsc', // Discover
00052                                         'dnc', // Diners Club
00053                                         'jcb', // JCB
00054                                         'swi', // Switch
00055                                         'dlt', // Delta
00056                                         'enr' // EnRoute
00057     );
00058 
00064     protected $_aRequiredDCFields = array('lsbankname',
00065                                           'lsktonr',
00066                                           'lsktoinhaber'
00067     );
00068 
00073     public function __construct()
00074     {
00075     }
00076 
00086     public function validateBasketAmount($dAmount)
00087     {
00088         $dAmount = str_replace(',', '.', $dAmount);
00089 
00090         if (!is_numeric($dAmount) || $dAmount < 0) {
00094             $oEx = oxNew('oxArticleInputException');
00095             $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_INVALIDAMOUNT'));
00096             throw $oEx;
00097         }
00098 
00099         if (!oxRegistry::getConfig()->getConfigParam('blAllowUnevenAmounts')) {
00100             $dAmount = round(( string ) $dAmount);
00101         }
00102 
00103         //negative amounts are not allowed
00104         //$dAmount = abs($dAmount);
00105 
00106         return $dAmount;
00107     }
00108 
00123     public function checkLogin($oUser, $sLogin, $aInvAddress)
00124     {
00125         $sLogin = (isset($aInvAddress['oxuser__oxusername'])) ? $aInvAddress['oxuser__oxusername'] : $sLogin;
00126 
00127         // check only for users with password during registration
00128         // if user wants to change user name - we must check if passwords are ok before changing
00129         if ($oUser->oxuser__oxpassword->value && $sLogin != $oUser->oxuser__oxusername->value) {
00130 
00131             // on this case password must be taken directly from request
00132             $sNewPass = (isset($aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword']) ? $aInvAddress['oxuser__oxpassword'] : oxRegistry::getConfig()->getRequestParameter('user_password');
00133             if (!$sNewPass) {
00134 
00135                 // 1. user forgot to enter password
00136                 $oEx = oxNew('oxInputException');
00137                 $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOTALLFIELDS'));
00138 
00139                 return $this->_addValidationError("oxuser__oxpassword", $oEx);
00140             } else {
00141 
00142                 // 2. entered wrong password
00143                 if (!$oUser->isSamePassword($sNewPass)) {
00144                     $oEx = oxNew('oxUserException');
00145                     $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_PASSWORD_DO_NOT_MATCH'));
00146 
00147                     return $this->_addValidationError("oxuser__oxpassword", $oEx);
00148                 }
00149             }
00150         }
00151 
00152         if ($oUser->checkIfEmailExists($sLogin)) {
00153             //if exists then we do now allow to do that
00154             $oEx = oxNew('oxUserException');
00155             $oEx->setMessage(sprintf(oxRegistry::getLang()->translateString('ERROR_MESSAGE_USER_USEREXISTS'), $sLogin));
00156 
00157             return $this->_addValidationError("oxuser__oxusername", $oEx);
00158         }
00159 
00160         return $sLogin;
00161     }
00162 
00172     public function checkEmail($oUser, $sEmail)
00173     {
00174         // missing email address (user login name) ?
00175         if (!$sEmail) {
00176             $oEx = oxNew('oxInputException');
00177             $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOTALLFIELDS'));
00178 
00179             return $this->_addValidationError("oxuser__oxusername", $oEx);
00180         }
00181 
00182         // invalid email address ?
00183         if (!oxRegistry::getUtils()->isValidEmail($sEmail)) {
00184             $oEx = oxNew('oxInputException');
00185             $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOVALIDEMAIL'));
00186 
00187             return $this->_addValidationError("oxuser__oxusername", $oEx);
00188         }
00189     }
00190 
00202     public function checkPassword($oUser, $sNewPass, $sConfPass, $blCheckLength = false)
00203     {
00204         //  no password at all
00205         if ($blCheckLength && getStr()->strlen($sNewPass) == 0) {
00206             $oEx = oxNew('oxInputException');
00207             $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_EMPTYPASS'));
00208 
00209             return $this->_addValidationError("oxuser__oxpassword", $oEx);
00210         }
00211 
00212         //  password is too short ?
00213         if ($blCheckLength && getStr()->strlen($sNewPass) < 6) {
00214             $oEx = oxNew('oxInputException');
00215             $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_PASSWORD_TOO_SHORT'));
00216 
00217             return $this->_addValidationError("oxuser__oxpassword", $oEx);
00218         }
00219 
00220         //  passwords do not match ?
00221         if ($sNewPass != $sConfPass) {
00222             $oEx = oxNew('oxUserException');
00223             $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_PASSWORD_DO_NOT_MATCH'));
00224 
00225             return $this->_addValidationError("oxuser__oxpassword", $oEx);
00226         }
00227     }
00228 
00237     public function checkRequiredFields($oUser, $aBillingAddress, $aDeliveryAddress)
00238     {
00240         $oRequiredAddressFields = oxNew('oxRequiredAddressFields');
00241 
00243         $oFieldsValidator = oxNew('oxRequiredFieldsValidator');
00244 
00246         $oUser = oxNew('oxUser');
00247         $oBillingAddress = $this->_setFields($oUser, $aBillingAddress);
00248         $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getBillingFields());
00249         $oFieldsValidator->validateFields($oBillingAddress);
00250         $aInvalidFields = $oFieldsValidator->getInvalidFields();
00251 
00252         if (!empty($aDeliveryAddress)) {
00254             $oDeliveryAddress = $this->_setFields(oxNew('oxAddress'), $aDeliveryAddress);
00255             $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getDeliveryFields());
00256             $oFieldsValidator->validateFields($oDeliveryAddress);
00257             $aInvalidFields = array_merge($aInvalidFields, $oFieldsValidator->getInvalidFields());
00258         }
00259 
00260         foreach ($aInvalidFields as $sField) {
00261             $oEx = oxNew('oxInputException');
00262             $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOTALLFIELDS'));
00263 
00264             $this->_addValidationError($sField, $oEx);
00265         }
00266     }
00267 
00276     private function _setFields($oObject, $aFields)
00277     {
00278         $aFields = is_array($aFields) ? $aFields : array();
00279         foreach ($aFields as $sKey => $sValue) {
00280             $oObject->$sKey = oxNew('oxField', $sValue);
00281         }
00282 
00283         return $oObject;
00284     }
00285 
00295     public function checkRequiredArrayFields($oUser, $sFieldName, $aFieldValues)
00296     {
00297         foreach ($aFieldValues as $sValue) {
00298             if (!trim($sValue)) {
00299                 $oEx = oxNew('oxInputException');
00300                 $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOTALLFIELDS'));
00301 
00302                 $this->_addValidationError($sFieldName, $oEx);
00303             }
00304         }
00305     }
00306 
00314     public function checkCountries($oUser, $aInvAddress, $aDelAddress)
00315     {
00316         $sBillCtry = isset($aInvAddress['oxuser__oxcountryid']) ? $aInvAddress['oxuser__oxcountryid'] : null;
00317         $sDelCtry = isset($aDelAddress['oxaddress__oxcountryid']) ? $aDelAddress['oxaddress__oxcountryid'] : null;
00318 
00319         if ($sBillCtry || $sDelCtry) {
00320             $oDb = oxDb::getDb();
00321 
00322             if (($sBillCtry == $sDelCtry) || (!$sBillCtry && $sDelCtry) || ($sBillCtry && !$sDelCtry)) {
00323                 $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
00324                 $sQ = "select oxactive from oxcountry where oxid = " . $oDb->quote($sBillCtry) . " ";
00325             } else {
00326                 $sQ = "select ( select oxactive from oxcountry where oxid = " . $oDb->quote($sBillCtry) . " ) and
00327                               ( select oxactive from oxcountry where oxid = " . $oDb->quote($sDelCtry) . " ) ";
00328             }
00329 
00330             if (!$oDb->getOne($sQ)) {
00331                 $oEx = oxNew('oxUserException');
00332                 $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOTALLFIELDS'));
00333 
00334                 $this->_addValidationError("oxuser__oxpassword", $oEx);
00335             }
00336         }
00337     }
00338 
00348     public function checkVatId($oUser, $aInvAddress)
00349     {
00350         if ($this->_hasRequiredParametersForVatInCheck($aInvAddress)) {
00351 
00352             $oCountry = $this->_getCountry($aInvAddress['oxuser__oxcountryid']);
00353 
00354             if ($oCountry && $oCountry->isInEU()) {
00355 
00356                 $oVatInValidator = $this->getCompanyVatInValidator($oCountry);
00357 
00359                 $oVatIn = oxNew('oxCompanyVatIn', $aInvAddress['oxuser__oxustid']);
00360 
00361                 if (!$oVatInValidator->validate($oVatIn)) {
00363                     $oEx = oxNew('oxInputException');
00364                     $oEx->setMessage(oxRegistry::getLang()->translateString('VAT_MESSAGE_' . $oVatInValidator->getError()));
00365 
00366                     return $this->_addValidationError("oxuser__oxustid", $oEx);
00367                 }
00368             }
00369         } elseif ($aInvAddress['oxuser__oxustid'] && !$aInvAddress['oxuser__oxcompany']) {
00371             $oEx = oxNew('oxInputException');
00372             $oEx->setMessage(oxRegistry::getLang()->translateString('VAT_MESSAGE_COMPANY_MISSING'));
00373 
00374             return $this->_addValidationError("oxuser__oxcompany", $oEx);
00375         }
00376     }
00377 
00378 
00386     protected function _getCountry($sCountryId)
00387     {
00388         $oCountry = oxNew('oxCountry');
00389         $oCountry->load($sCountryId);
00390 
00391         return $oCountry;
00392     }
00393 
00399     public function getFieldValidationErrors()
00400     {
00401         return $this->_aInputValidationErrors;
00402     }
00403 
00409     public function getFirstValidationError()
00410     {
00411         $oErr = null;
00412         $aErr = reset($this->_aInputValidationErrors);
00413         if (is_array($aErr)) {
00414             $oErr = reset($aErr);
00415         }
00416 
00417         return $oErr;
00418     }
00419 
00428     public function validatePaymentInputData($sPaymentId, & $aDynValue)
00429     {
00430         $mxValidationResult = true;
00431 
00432         switch ($sPaymentId) {
00433             case 'oxidcreditcard':
00434                 $mxValidationResult = false;
00435 
00436                 $blAllCreditCardInformationSet = $this->_isAllBankInformationSet($this->_aRequiredCCFields, $aDynValue);
00437                 $blCreditCardTypeExist = in_array($aDynValue['kktype'], $this->_aPossibleCCType);
00438 
00439                 if ($blAllCreditCardInformationSet && $blCreditCardTypeExist) {
00440                     $oCardValidator = oxNew("oxccvalidator");
00441                     $mxValidationResult = $oCardValidator->isValidCard(
00442                         $aDynValue['kknumber'],
00443                         $aDynValue['kktype'],
00444                         $aDynValue['kkmonth'] . substr($aDynValue['kkyear'], 2, 2)
00445                     );
00446                 }
00447                 break;
00448 
00449             case "oxiddebitnote":
00450                 $mxValidationResult = false;
00451 
00452                 if ($this->_isAllBankInformationSet($this->_aRequiredDCFields, $aDynValue)) {
00453                     $mxValidationResult = $this->_validateDebitNote($aDynValue);
00454                 }
00455 
00456                 break;
00457         }
00458 
00459         return $mxValidationResult;
00460     }
00461 
00471     protected function _addValidationError($sFieldName, $oErr)
00472     {
00473         return $this->_aInputValidationErrors[$sFieldName][] = $oErr;
00474     }
00475 
00483     protected function _validateDebitNote($aDebitInformation)
00484     {
00485         $aDebitInformation = $this->_cleanDebitInformation($aDebitInformation);
00486         $sBankCode = $aDebitInformation['lsblz'];
00487         $sAccountNumber = $aDebitInformation['lsktonr'];
00488         $oSepaValidator = oxNew("oxSepaValidator");
00489 
00490         if (empty($sBankCode) || $oSepaValidator->isValidBIC($sBankCode)) {
00491             $mxValidationResult = true;
00492             if (!$oSepaValidator->isValidIBAN($sAccountNumber)) {
00493                 $mxValidationResult = self::INVALID_ACCOUNT_NUMBER;
00494             }
00495         } else {
00496             $mxValidationResult = self::INVALID_BANK_CODE;
00497             if (!oxRegistry::getConfig()->getConfigParam('blSkipDebitOldBankInfo')) {
00498                 $mxValidationResult = $this->_validateOldDebitInfo($aDebitInformation);
00499             }
00500         }
00501 
00502         return $mxValidationResult;
00503     }
00504 
00512     protected function _validateOldDebitInfo($aDebitInfo)
00513     {
00514         $oStr = getStr();
00515         $aDebitInfo = $this->_fixAccountNumber($aDebitInfo);
00516 
00517         $mxValidationResult = true;
00518 
00519         if (!$oStr->preg_match("/^\d{5,8}$/", $aDebitInfo['lsblz'])) {
00520             // Bank code is invalid
00521             $mxValidationResult = self::INVALID_BANK_CODE;
00522         }
00523 
00524         if (true === $mxValidationResult && !$oStr->preg_match("/^\d{10,12}$/", $aDebitInfo['lsktonr'])) {
00525             // Account number is invalid
00526             $mxValidationResult = self::INVALID_ACCOUNT_NUMBER;
00527         }
00528 
00529 
00530         return $mxValidationResult;
00531     }
00532 
00540     protected function _fixAccountNumber($aDebitInfo)
00541     {
00542         $oStr = getStr();
00543 
00544         if ($oStr->strlen($aDebitInfo['lsktonr']) < 10) {
00545             $sNewNum = str_repeat(
00546                 '0',
00547                 10 - $oStr->strlen($aDebitInfo['lsktonr'])
00548             ) . $aDebitInfo['lsktonr'];
00549             $aDebitInfo['lsktonr'] = $sNewNum;
00550         }
00551 
00552         return $aDebitInfo;
00553     }
00554 
00563     protected function _isAllBankInformationSet($aRequiredFields, $aBankInformation)
00564     {
00565         $blResult = true;
00566         foreach ($aRequiredFields as $sFieldName) {
00567             if (!isset($aBankInformation[$sFieldName]) || !trim($aBankInformation[$sFieldName])) {
00568                 $blResult = false;
00569                 break;
00570             }
00571         }
00572 
00573         return $blResult;
00574     }
00575 
00583     protected function _cleanDebitInformation($aDebitInformation)
00584     {
00585         $aDebitInformation['lsblz'] = str_replace(' ', '', $aDebitInformation['lsblz']);
00586         $aDebitInformation['lsktonr'] = str_replace(' ', '', $aDebitInformation['lsktonr']);
00587 
00588         return $aDebitInformation;
00589     }
00590 
00598     protected function _hasRequiredParametersForVatInCheck($aInvAddress)
00599     {
00600         return $aInvAddress['oxuser__oxustid'] && $aInvAddress['oxuser__oxcountryid'] && $aInvAddress['oxuser__oxcompany'];
00601     }
00602 
00613     private function _isVATIdentificationNumberInvalid($aInvAddress, $oCountry)
00614     {
00615         return (bool) strncmp($aInvAddress['oxuser__oxustid'], $oCountry->getVATIdentificationNumberPrefix(), 2);
00616     }
00617 
00626     protected function _getVatIdValidator()
00627     {
00628         $oVatCheck = oxNew('oxOnlineVatIdCheck');
00629 
00630         return $oVatCheck;
00631     }
00632 
00638     public function setCompanyVatInValidator($oCompanyVatInValidator)
00639     {
00640         $this->_oCompanyVatInValidator = $oCompanyVatInValidator;
00641     }
00642 
00650     public function getCompanyVatInValidator($oCountry)
00651     {
00652         if (is_null($this->_oCompanyVatInValidator)) {
00653 
00655             $oVatInValidator = oxNew('oxCompanyVatInValidator', $oCountry);
00656 
00658             $oValidator = oxNew('oxCompanyVatInCountryChecker');
00659 
00660             $oVatInValidator->addChecker($oValidator);
00661 
00663             if (!oxRegistry::getConfig()->getConfigParam("blVatIdCheckDisabled")) {
00664                 $oOnlineValidator = oxNew('oxOnlineVatIdCheck');
00665                 $oVatInValidator->addChecker($oOnlineValidator);
00666             }
00667 
00668             $this->setCompanyVatInValidator($oVatInValidator);
00669         }
00670 
00671         return $this->_oCompanyVatInValidator;
00672     }
00673 }