MODX Revolution PHP Cross Reference Content Management Systems

Source: /core/model/modx/processors/security/user/_validation.php - 186 lines - 8111 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * Handles common validation for user processors
   4   *
   5   * @package modx
   6   */
   7  class modUserValidation {
   8      /** @var modX $modx */
   9      public $modx;
  10      /** @var modUserCreateProcessor|modUserUpdateProcessor $processor */
  11      public $processor;
  12      /** @var modUser $user */
  13      public $user;
  14      /** @var modUserProfile $profile */
  15      public $profile;
  16  
  17      function __construct(modObjectProcessor &$processor,modUser &$user,modUserProfile &$profile) {
  18          $this->processor =& $processor;
  19          $this->modx =& $processor->modx;
  20          $this->user =& $user;
  21          $this->profile =& $profile;
  22      }
  23  
  24      public function validate() {
  25          $this->checkUsername();
  26          $this->checkPassword();
  27          $this->checkEmail();
  28          $this->checkPhone();
  29          $this->checkCellPhone();
  30          $this->checkBirthDate();
  31          $this->checkBlocked();
  32  
  33          return !$this->processor->hasErrors();
  34      }
  35  
  36      public function checkUsername() {
  37          $username = $this->processor->getProperty('username');
  38          if (empty($username)) {
  39              $this->processor->addFieldError('username',$this->modx->lexicon('user_err_not_specified_username'));
  40          } elseif (!preg_match('/^[^\'\\x3c\\x3e\\(\\);\\x22]+$/', $username)) {
  41              $this->processor->addFieldError('username',$this->modx->lexicon('user_err_username_invalid'));
  42          } else if (!empty($username)) {
  43              if ($this->alreadyExists($username)) {
  44                  $this->processor->addFieldError('username',$this->modx->lexicon('user_err_already_exists'));
  45              }
  46              $this->user->set('username',$username);
  47          }
  48      }
  49  
  50      public function alreadyExists($name) {
  51          return $this->modx->getCount('modUser',array(
  52              'username' => $name,
  53              'id:!=' => $this->user->get('id'),
  54          )) > 0;
  55      }
  56  
  57      public function checkPassword() {
  58          $newPassword = $this->processor->getProperty('newpassword',null);
  59          $id = $this->processor->getProperty('id');
  60          if ($newPassword !== null && $newPassword != 'false' || empty($id)) {
  61              $passwordNotifyMethod = $this->processor->getProperty('passwordnotifymethod',null);
  62              if (empty($passwordNotifyMethod)) {
  63                  $this->processor->addFieldError('password_notify_method',$this->modx->lexicon('user_err_not_specified_notification_method'));
  64              }
  65              $passwordGenerationMethod = $this->processor->getProperty('passwordgenmethod','g');
  66              if ($passwordGenerationMethod == 'g') {
  67                  $len = $this->modx->getOption('password_generated_length',null,8);
  68                  $autoPassword = $this->generatePassword($len);
  69                  $this->user->set('password', $autoPassword);
  70                  $this->processor->newPassword= $autoPassword;
  71              } else {
  72                  $specifiedPassword = $this->processor->getProperty('specifiedpassword');
  73                  $confirmPassword = $this->processor->getProperty('confirmpassword');
  74                  if (empty($specifiedPassword)) {
  75                      $this->processor->addFieldError('specifiedpassword',$this->modx->lexicon('user_err_not_specified_password'));
  76                  } elseif ($specifiedPassword != $confirmPassword) {
  77                      $this->processor->addFieldError('confirmpassword',$this->modx->lexicon('user_err_password_no_match'));
  78                  } elseif (strlen($specifiedPassword) < $this->modx->getOption('password_min_length',null,6)) {
  79                      $this->processor->addFieldError('specifiedpassword',$this->modx->lexicon('user_err_password_too_short'));
  80                  } elseif (!preg_match('/^[^\'\\x3c\\x3e\\(\\);\\x22]+$/', $specifiedPassword)) {
  81                      $this->processor->addFieldError('specifiedpassword', $this->modx->lexicon('user_err_password_invalid'));
  82                  } else {
  83                      $this->user->set('password',$specifiedPassword);
  84                      $this->processor->newPassword = $specifiedPassword;
  85                  }
  86              }
  87          }
  88          return $this->processor->newPassword;
  89      }
  90  
  91      public function checkEmail() {
  92          $email = $this->processor->getProperty('email');
  93          if (empty($email)) {
  94              $this->processor->addFieldError('email',$this->modx->lexicon('user_err_not_specified_email'));
  95          }
  96  
  97          if (!$this->modx->getOption('allow_multiple_emails',null,true)) {
  98              /** @var modUserProfile $emailExists */
  99              $emailExists = $this->modx->getObject('modUserProfile',array('email' => $email));
 100              if ($emailExists) {
 101                  if ($emailExists->get('internalKey') != $this->processor->getProperty('id')) {
 102                      $this->processor->addFieldError('email',$this->modx->lexicon('user_err_already_exists_email'));
 103                  }
 104              }
 105          }
 106          return $email;
 107      }
 108  
 109      public function checkPhone() {
 110          $phone = $this->processor->getProperty('phone');
 111          if (!empty($phone)) {
 112              if ($this->modx->getOption('clean_phone_number',null,false)) {
 113                  $phone = str_replace(' ','',$phone);
 114                  $phone = str_replace('-','',$phone);
 115                  $phone = str_replace('(','',$phone);
 116                  $phone = str_replace(')','',$phone);
 117                  $phone = str_replace('+','',$phone);
 118                  $this->processor->setProperty('phone',$phone);
 119                  $this->profile->set('phone',$phone);
 120              }
 121          }
 122      }
 123  
 124      public function checkCellPhone() {
 125          $phone = $this->processor->getProperty('mobilephone');
 126          if (!empty($phone)) {
 127              if ($this->modx->getOption('clean_phone_number',null,false)) {
 128                  $phone = str_replace(' ','',$phone);
 129                  $phone = str_replace('-','',$phone);
 130                  $phone = str_replace('(','',$phone);
 131                  $phone = str_replace(')','',$phone);
 132                  $phone = str_replace('+','',$phone);
 133                  $this->processor->setProperty('mobilephone',$phone);
 134                  $this->profile->set('mobilephone',$phone);
 135              }
 136          }
 137      }
 138  
 139      public function checkBirthDate() {
 140          $birthDate = $this->processor->getProperty('dob');
 141          if (!empty($birthDate)) {
 142              $birthDate = strtotime($birthDate);
 143              if (empty($birthDate)) {
 144                  $this->processor->addFieldError('dob',$this->modx->lexicon('user_err_not_specified_dob'));
 145              }
 146              $this->processor->setProperty('dob',$birthDate);
 147              $this->profile->set('dob',$birthDate);
 148          }
 149      }
 150  
 151      public function checkBlocked() {
 152          /* blocked until */
 153          $blockedUntil = $this->processor->getProperty('blockeduntil');
 154          if (!empty($blockedUntil)) {
 155              $blockedUntil = str_replace('-','/',$blockedUntil);
 156              if (!$blockedUntil = strtotime($blockedUntil)) {
 157                  $this->processor->addFieldError('blockeduntil',$this->modx->lexicon('user_err_not_specified_blockeduntil'));
 158              }
 159              $this->processor->setProperty('blockeduntil',$blockedUntil);
 160              $this->profile->set('blockeduntil',$blockedUntil);
 161          }
 162  
 163          /* blocked after */
 164          $blockedAfter = $this->processor->getProperty('blockedafter');
 165          if (!empty($blockedAfter)) {
 166              $blockedAfter = str_replace('-','/',$blockedAfter);
 167              if (!$blockedAfter = strtotime($blockedAfter)) {
 168                  $this->processor->addFieldError('blockedafter',$this->modx->lexicon('user_err_not_specified_blockedafter'));
 169              }
 170              $this->processor->setProperty('blockedafter',$blockedAfter);
 171              $this->profile->set('blockedafter',$blockedAfter);
 172          }
 173      }
 174  
 175      public function generatePassword($length = 10) {
 176          $allowable_characters = 'abcdefghjkmnpqrstuvxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
 177          $ps_len = strlen($allowable_characters);
 178          srand((double) microtime() * 1000000);
 179          $pass = '';
 180          for ($i = 0; $i < $length; $i++) {
 181                  $pass .= $allowable_characters[mt_rand(0, $ps_len -1)];
 182          }
 183          return $pass;
 184      }
 185  
 186  }

title

Description

title

Description

title

Description

title

title

Body