OXID eShop CE  4.10.2
 All Classes Namespaces Files Functions Variables Pages
oxinputvalidator.php
Go to the documentation of this file.
1 <?php
2 
8 {
9 
14 
18  const INVALID_BANK_CODE = -4;
19 
25  protected $_aRequiredCCFields = array('kktype',
26  'kknumber',
27  'kkmonth',
28  'kkyear',
29  'kkname',
30  'kkpruef'
31  );
32 
38  protected $_aInputValidationErrors = array();
39 
40 
41  protected $_oCompanyVatInValidator = null;
42 
48  protected $_aPossibleCCType = array('mcd', // Master Card
49  'vis', // Visa
50  'amx', // American Express
51  'dsc', // Discover
52  'dnc', // Diners Club
53  'jcb', // JCB
54  'swi', // Switch
55  'dlt', // Delta
56  'enr' // EnRoute
57  );
58 
64  protected $_aRequiredDCFields = array('lsbankname',
65  'lsktonr',
66  'lsktoinhaber'
67  );
68 
73  public function __construct()
74  {
75  }
76 
86  public function validateBasketAmount($dAmount)
87  {
88  $dAmount = str_replace(',', '.', $dAmount);
89 
90  if (!is_numeric($dAmount) || $dAmount < 0) {
94  $oEx = oxNew('oxArticleInputException');
95  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_INVALIDAMOUNT'));
96  throw $oEx;
97  }
98 
99  if (!oxRegistry::getConfig()->getConfigParam('blAllowUnevenAmounts')) {
100  $dAmount = round(( string ) $dAmount);
101  }
102 
103  //negative amounts are not allowed
104  //$dAmount = abs($dAmount);
105 
106  return $dAmount;
107  }
108 
123  public function checkLogin($oUser, $sLogin, $aInvAddress)
124  {
125  $sLogin = (isset($aInvAddress['oxuser__oxusername'])) ? $aInvAddress['oxuser__oxusername'] : $sLogin;
126 
127  // check only for users with password during registration
128  // if user wants to change user name - we must check if passwords are ok before changing
129  if ($oUser->oxuser__oxpassword->value && $sLogin != $oUser->oxuser__oxusername->value) {
130 
131  // on this case password must be taken directly from request
132  $sNewPass = (isset($aInvAddress['oxuser__oxpassword']) && $aInvAddress['oxuser__oxpassword']) ? $aInvAddress['oxuser__oxpassword'] : oxRegistry::getConfig()->getRequestParameter('user_password');
133  if (!$sNewPass) {
134 
135  // 1. user forgot to enter password
136  $oEx = oxNew('oxInputException');
137  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOTALLFIELDS'));
138 
139  return $this->_addValidationError("oxuser__oxpassword", $oEx);
140  } else {
141 
142  // 2. entered wrong password
143  if (!$oUser->isSamePassword($sNewPass)) {
144  $oEx = oxNew('oxUserException');
145  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_PASSWORD_DO_NOT_MATCH'));
146 
147  return $this->_addValidationError("oxuser__oxpassword", $oEx);
148  }
149  }
150  }
151 
152  if ($oUser->checkIfEmailExists($sLogin)) {
153  //if exists then we do now allow to do that
154  $oEx = oxNew('oxUserException');
155  $oEx->setMessage(sprintf(oxRegistry::getLang()->translateString('ERROR_MESSAGE_USER_USEREXISTS'), $sLogin));
156 
157  return $this->_addValidationError("oxuser__oxusername", $oEx);
158  }
159 
160  return $sLogin;
161  }
162 
172  public function checkEmail($oUser, $sEmail)
173  {
174  // missing email address (user login name) ?
175  if (!$sEmail) {
176  $oEx = oxNew('oxInputException');
177  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOTALLFIELDS'));
178 
179  return $this->_addValidationError("oxuser__oxusername", $oEx);
180  }
181 
182  // invalid email address ?
183  if (!oxRegistry::getUtils()->isValidEmail($sEmail)) {
184  $oEx = oxNew('oxInputException');
185  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOVALIDEMAIL'));
186 
187  return $this->_addValidationError("oxuser__oxusername", $oEx);
188  }
189  }
190 
202  public function checkPassword($oUser, $sNewPass, $sConfPass, $blCheckLength = false)
203  {
204  // no password at all
205  if ($blCheckLength && getStr()->strlen($sNewPass) == 0) {
206  $oEx = oxNew('oxInputException');
207  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_EMPTYPASS'));
208 
209  return $this->_addValidationError("oxuser__oxpassword", $oEx);
210  }
211 
212  // password is too short ?
213  if ($blCheckLength && getStr()->strlen($sNewPass) < 6) {
214  $oEx = oxNew('oxInputException');
215  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_PASSWORD_TOO_SHORT'));
216 
217  return $this->_addValidationError("oxuser__oxpassword", $oEx);
218  }
219 
220  // passwords do not match ?
221  if ($sNewPass != $sConfPass) {
222  $oEx = oxNew('oxUserException');
223  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_PASSWORD_DO_NOT_MATCH'));
224 
225  return $this->_addValidationError("oxuser__oxpassword", $oEx);
226  }
227  }
228 
237  public function checkRequiredFields($oUser, $aBillingAddress, $aDeliveryAddress)
238  {
240  $oRequiredAddressFields = oxNew('oxRequiredAddressFields');
241 
243  $oFieldsValidator = oxNew('oxRequiredFieldsValidator');
244 
246  $oUser = oxNew('oxUser');
247  $oBillingAddress = $this->_setFields($oUser, $aBillingAddress);
248  $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getBillingFields());
249  $oFieldsValidator->validateFields($oBillingAddress);
250  $aInvalidFields = $oFieldsValidator->getInvalidFields();
251 
252  if (!empty($aDeliveryAddress)) {
254  $oDeliveryAddress = $this->_setFields(oxNew('oxAddress'), $aDeliveryAddress);
255  $oFieldsValidator->setRequiredFields($oRequiredAddressFields->getDeliveryFields());
256  $oFieldsValidator->validateFields($oDeliveryAddress);
257  $aInvalidFields = array_merge($aInvalidFields, $oFieldsValidator->getInvalidFields());
258  }
259 
260  foreach ($aInvalidFields as $sField) {
261  $oEx = oxNew('oxInputException');
262  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOTALLFIELDS'));
263 
264  $this->_addValidationError($sField, $oEx);
265  }
266  }
267 
276  private function _setFields($oObject, $aFields)
277  {
278  $aFields = is_array($aFields) ? $aFields : array();
279  foreach ($aFields as $sKey => $sValue) {
280  $oObject->$sKey = oxNew('oxField', $sValue);
281  }
282 
283  return $oObject;
284  }
285 
295  public function checkRequiredArrayFields($oUser, $sFieldName, $aFieldValues)
296  {
297  foreach ($aFieldValues as $sValue) {
298  if (!trim($sValue)) {
299  $oEx = oxNew('oxInputException');
300  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOTALLFIELDS'));
301 
302  $this->_addValidationError($sFieldName, $oEx);
303  }
304  }
305  }
306 
314  public function checkCountries($oUser, $aInvAddress, $aDelAddress)
315  {
316  $sBillCtry = isset($aInvAddress['oxuser__oxcountryid']) ? $aInvAddress['oxuser__oxcountryid'] : null;
317  $sDelCtry = isset($aDelAddress['oxaddress__oxcountryid']) ? $aDelAddress['oxaddress__oxcountryid'] : null;
318 
319  if ($sBillCtry || $sDelCtry) {
320  $oDb = oxDb::getDb();
321 
322  if (($sBillCtry == $sDelCtry) || (!$sBillCtry && $sDelCtry) || ($sBillCtry && !$sDelCtry)) {
323  $sBillCtry = $sBillCtry ? $sBillCtry : $sDelCtry;
324  $sQ = "select oxactive from oxcountry where oxid = " . $oDb->quote($sBillCtry) . " ";
325  } else {
326  $sQ = "select ( select oxactive from oxcountry where oxid = " . $oDb->quote($sBillCtry) . " ) and
327  ( select oxactive from oxcountry where oxid = " . $oDb->quote($sDelCtry) . " ) ";
328  }
329 
330  if (!$oDb->getOne($sQ)) {
331  $oEx = oxNew('oxUserException');
332  $oEx->setMessage(oxRegistry::getLang()->translateString('ERROR_MESSAGE_INPUT_NOTALLFIELDS'));
333 
334  $this->_addValidationError("oxuser__oxpassword", $oEx);
335  }
336  }
337  }
338 
348  public function checkVatId($oUser, $aInvAddress)
349  {
350  if ($this->_hasRequiredParametersForVatInCheck($aInvAddress)) {
351 
352  $oCountry = $this->_getCountry($aInvAddress['oxuser__oxcountryid']);
353 
354  if ($oCountry && $oCountry->isInEU()) {
355 
356  $oVatInValidator = $this->getCompanyVatInValidator($oCountry);
357 
359  $oVatIn = oxNew('oxCompanyVatIn', $aInvAddress['oxuser__oxustid']);
360 
361  if (!$oVatInValidator->validate($oVatIn)) {
363  $oEx = oxNew('oxInputException');
364  $oEx->setMessage(oxRegistry::getLang()->translateString('VAT_MESSAGE_' . $oVatInValidator->getError()));
365 
366  return $this->_addValidationError("oxuser__oxustid", $oEx);
367  }
368  }
369  } elseif ($aInvAddress['oxuser__oxustid'] && !$aInvAddress['oxuser__oxcompany']) {
371  $oEx = oxNew('oxInputException');
372  $oEx->setMessage(oxRegistry::getLang()->translateString('VAT_MESSAGE_COMPANY_MISSING'));
373 
374  return $this->_addValidationError("oxuser__oxcompany", $oEx);
375  }
376  }
377 
378 
386  protected function _getCountry($sCountryId)
387  {
388  $oCountry = oxNew('oxCountry');
389  $oCountry->load($sCountryId);
390 
391  return $oCountry;
392  }
393 
399  public function getFieldValidationErrors()
400  {
402  }
403 
409  public function getFirstValidationError()
410  {
411  $oErr = null;
412  $aErr = reset($this->_aInputValidationErrors);
413  if (is_array($aErr)) {
414  $oErr = reset($aErr);
415  }
416 
417  return $oErr;
418  }
419 
428  public function validatePaymentInputData($sPaymentId, & $aDynValue)
429  {
430  $mxValidationResult = true;
431 
432  switch ($sPaymentId) {
433  case 'oxidcreditcard':
434  $mxValidationResult = false;
435 
436  $blAllCreditCardInformationSet = $this->_isAllBankInformationSet($this->_aRequiredCCFields, $aDynValue);
437  $blCreditCardTypeExist = in_array($aDynValue['kktype'], $this->_aPossibleCCType);
438 
439  if ($blAllCreditCardInformationSet && $blCreditCardTypeExist) {
440  $oCardValidator = oxNew("oxccvalidator");
441  $mxValidationResult = $oCardValidator->isValidCard(
442  $aDynValue['kknumber'],
443  $aDynValue['kktype'],
444  $aDynValue['kkmonth'] . substr($aDynValue['kkyear'], 2, 2)
445  );
446  }
447  break;
448 
449  case "oxiddebitnote":
450  $mxValidationResult = false;
451 
452  if ($this->_isAllBankInformationSet($this->_aRequiredDCFields, $aDynValue)) {
453  $mxValidationResult = $this->_validateDebitNote($aDynValue);
454  }
455 
456  break;
457  }
458 
459  return $mxValidationResult;
460  }
461 
471  protected function _addValidationError($sFieldName, $oErr)
472  {
473  return $this->_aInputValidationErrors[$sFieldName][] = $oErr;
474  }
475 
483  protected function _validateDebitNote($aDebitInformation)
484  {
485  $aDebitInformation = $this->_cleanDebitInformation($aDebitInformation);
486  $sBankCode = $aDebitInformation['lsblz'];
487  $sAccountNumber = $aDebitInformation['lsktonr'];
488  $oSepaValidator = oxNew("oxSepaValidator");
489 
490  if (empty($sBankCode) || $oSepaValidator->isValidBIC($sBankCode)) {
491  $mxValidationResult = true;
492  if (!$oSepaValidator->isValidIBAN($sAccountNumber)) {
493  $mxValidationResult = self::INVALID_ACCOUNT_NUMBER;
494  }
495  } else {
496  $mxValidationResult = self::INVALID_BANK_CODE;
497  if (!oxRegistry::getConfig()->getConfigParam('blSkipDebitOldBankInfo')) {
498  $mxValidationResult = $this->_validateOldDebitInfo($aDebitInformation);
499  }
500  }
501 
502  return $mxValidationResult;
503  }
504 
512  protected function _validateOldDebitInfo($aDebitInfo)
513  {
514  $oStr = getStr();
515  $aDebitInfo = $this->_fixAccountNumber($aDebitInfo);
516 
517  $mxValidationResult = true;
518 
519  if (!$oStr->preg_match("/^\d{5,8}$/", $aDebitInfo['lsblz'])) {
520  // Bank code is invalid
521  $mxValidationResult = self::INVALID_BANK_CODE;
522  }
523 
524  if (true === $mxValidationResult && !$oStr->preg_match("/^\d{10,12}$/", $aDebitInfo['lsktonr'])) {
525  // Account number is invalid
526  $mxValidationResult = self::INVALID_ACCOUNT_NUMBER;
527  }
528 
529 
530  return $mxValidationResult;
531  }
532 
540  protected function _fixAccountNumber($aDebitInfo)
541  {
542  $oStr = getStr();
543 
544  if ($oStr->strlen($aDebitInfo['lsktonr']) < 10) {
545  $sNewNum = str_repeat(
546  '0',
547  10 - $oStr->strlen($aDebitInfo['lsktonr'])
548  ) . $aDebitInfo['lsktonr'];
549  $aDebitInfo['lsktonr'] = $sNewNum;
550  }
551 
552  return $aDebitInfo;
553  }
554 
563  protected function _isAllBankInformationSet($aRequiredFields, $aBankInformation)
564  {
565  $blResult = true;
566  foreach ($aRequiredFields as $sFieldName) {
567  if (!isset($aBankInformation[$sFieldName]) || !trim($aBankInformation[$sFieldName])) {
568  $blResult = false;
569  break;
570  }
571  }
572 
573  return $blResult;
574  }
575 
583  protected function _cleanDebitInformation($aDebitInformation)
584  {
585  $aDebitInformation['lsblz'] = str_replace(' ', '', $aDebitInformation['lsblz']);
586  $aDebitInformation['lsktonr'] = str_replace(' ', '', $aDebitInformation['lsktonr']);
587 
588  return $aDebitInformation;
589  }
590 
598  protected function _hasRequiredParametersForVatInCheck($aInvAddress)
599  {
600  return $aInvAddress['oxuser__oxustid'] && $aInvAddress['oxuser__oxcountryid'] && $aInvAddress['oxuser__oxcompany'];
601  }
602 
613  private function _isVATIdentificationNumberInvalid($aInvAddress, $oCountry)
614  {
615  return (bool) strncmp($aInvAddress['oxuser__oxustid'], $oCountry->getVATIdentificationNumberPrefix(), 2);
616  }
617 
626  protected function _getVatIdValidator()
627  {
628  $oVatCheck = oxNew('oxOnlineVatIdCheck');
629 
630  return $oVatCheck;
631  }
632 
638  public function setCompanyVatInValidator($oCompanyVatInValidator)
639  {
640  $this->_oCompanyVatInValidator = $oCompanyVatInValidator;
641  }
642 
650  public function getCompanyVatInValidator($oCountry)
651  {
652  if (is_null($this->_oCompanyVatInValidator)) {
653 
655  $oVatInValidator = oxNew('oxCompanyVatInValidator', $oCountry);
656 
658  $oValidator = oxNew('oxCompanyVatInCountryChecker');
659 
660  $oVatInValidator->addChecker($oValidator);
661 
663  if (!oxRegistry::getConfig()->getConfigParam("blVatIdCheckDisabled")) {
664  $oOnlineValidator = oxNew('oxOnlineVatIdCheck');
665  $oVatInValidator->addChecker($oOnlineValidator);
666  }
667 
668  $this->setCompanyVatInValidator($oVatInValidator);
669  }
670 
672  }
673 }