File indexing completed on 2024-05-12 05:58:47

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  *
0023  * Created: 03.09.2018
0024  */
0025 class Default_Model_ReviewProfileData
0026 {
0027 
0028     const INVALID_USERNAME = 1;
0029     const INVALID_USERNAME_DEACTIVATED = 10;
0030     const INVALID_USERNAME_NOT_ALLOWED = 11;
0031     const INVALID_USERNAME_NOT_UNIQUE = 12;
0032 
0033     const INVALID_EMAIL = 2;
0034     const INVALID_EMAIL_DEACTIVATED = 20;
0035     const INVALID_EMAIL_NOT_UNIQUE = 21;
0036 
0037     const USERNAME_DEACTIVATED_TEXT = "_double";
0038     const EMAIL_DEACTIVATED_TEXT = "_double";
0039 
0040 
0041     protected $message;
0042     protected $errorCode;
0043 
0044     private $usernameValidationChain = array('isUsernameDeactivated', 'isUsernameValid', 'isUsernameUnique');
0045     private $emailValidationChain = array('isEmailDeactivated', 'isEmailValid', 'isEmailUnique');
0046 
0047     /**
0048      * @inheritDoc
0049      */
0050     public function __construct()
0051     {
0052         $this->errorCode = 0;
0053         $this->message = array();
0054     }
0055 
0056 
0057     public function hasValidProfile($member_data)
0058     {
0059         $this->errorCode = 0;
0060         $this->message = array();
0061 
0062         $result = $this->hasValidUsername($member_data);
0063         if (false == $result) {
0064             return false;
0065         }
0066 
0067         $result = $this->hasValidEmail($member_data);
0068         if (false == $result) {
0069             return false;
0070         }
0071 
0072         return true;
0073     }
0074 
0075     /**
0076      * @param $member_data
0077      *
0078      * @return boolean
0079      */
0080     public function hasValidUsername($member_data)
0081     {
0082         $result = true;
0083         foreach ($this->usernameValidationChain as $validator) {
0084             $result = $this->$validator($member_data);
0085             if (false == $result) {
0086                 //$this->errorCode |= self::INVALID_USERNAME;
0087                 return false;
0088             }
0089         }
0090 
0091         return $result;
0092     }
0093 
0094     public function hasValidEmail($member_data)
0095     {
0096         $result = true;
0097         foreach ($this->emailValidationChain as $validator) {
0098             $result = $this->$validator($member_data);
0099             if (false == $result) {
0100                 //$this->errorCode |= self::INVALID_EMAIL;
0101                 return false;
0102             }
0103         }
0104 
0105         return $result;
0106     }
0107 
0108     /**
0109      * @return mixed
0110      */
0111     public function getMessage()
0112     {
0113         return $this->message;
0114     }
0115 
0116     /**
0117      * @return int
0118      */
0119     public function getErrorCode()
0120     {
0121         return $this->errorCode;
0122     }
0123 
0124     /**
0125      * @param $member_data
0126      *
0127      * @return bool
0128      */
0129     private function isEmailValid($member_data)
0130     {
0131         $validatorEmail = new Zend_Validate_EmailAddress();
0132 
0133         if (false == $validatorEmail->isValid($member_data->mail)) {
0134             $this->message['email'][] = $validatorEmail->getMessages();
0135             $this->errorCode = $this::INVALID_EMAIL;
0136 
0137             return false;
0138         }
0139 
0140         return true;
0141     }
0142 
0143     /**
0144      * @param $member_data
0145      *
0146      * @return bool
0147      * @throws Zend_Db_Statement_Exception
0148      */
0149     private function isEmailUnique($member_data)
0150     {
0151         $sql = "
0152         SELECT `mail`, COUNT(*) AS `amount`
0153         FROM `member` AS `m`
0154         WHERE `m`.`is_active` = 1 AND `m`.`is_deleted` = 0 
0155         AND lower(`mail`) = lower(:mail)
0156         GROUP BY lower(`mail`)
0157         HAVING COUNT(*) > 1
0158         ";
0159 
0160         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('mail' => $member_data->mail))->fetch();
0161 
0162         if ((false === empty($result)) AND $result['amount'] > 1) {
0163             $this->message['email'][] = array('email is not unique');
0164             $this->errorCode = $this::INVALID_EMAIL_NOT_UNIQUE;
0165 
0166             return false;
0167         }
0168 
0169         return true;
0170     }
0171 
0172     /**
0173      * @param $member_data
0174      *
0175      * @return bool
0176      * @throws Zend_Validate_Exception
0177      */
0178     private function isEmailDeactivated($member_data)
0179     {
0180         if (strpos($member_data->mail, $this::EMAIL_DEACTIVATED_TEXT) > 0) {
0181             $this->message['email'][] = 'Email is deactivated';
0182             $this->errorCode = $this::INVALID_EMAIL_DEACTIVATED;
0183 
0184             return false;
0185         }
0186 
0187         return true;
0188     }
0189 
0190     /**
0191      * @param $member_data
0192      *
0193      * @return bool
0194      * @throws Zend_Validate_Exception
0195      */
0196     private function isUsernameDeactivated($member_data)
0197     {
0198         if (strpos($member_data->username, $this::USERNAME_DEACTIVATED_TEXT) > 0) {
0199             $this->message['username'][] = 'User is deactivated';
0200             $this->errorCode = $this::INVALID_USERNAME_DEACTIVATED;
0201 
0202             return false;
0203         }
0204 
0205         return true;
0206     }
0207 
0208     /**
0209      * @param $member_data
0210      *
0211      * @return bool
0212      * @throws Zend_Validate_Exception
0213      */
0214     private function isUsernameValid($member_data)
0215     {
0216         $usernameValidChars = new Zend_Validate_Regex('/^(?=.{3,40}$)(?![-])(?!.*[-]{2})[a-zA-Z0-9-]+(?<![-])$/');
0217 
0218         if (false == $usernameValidChars->isValid($member_data->username)) {
0219             $this->message['username'][] = $usernameValidChars->getMessages();
0220             $this->errorCode = $this::INVALID_USERNAME_NOT_ALLOWED;
0221 
0222             return false;
0223         }
0224 
0225         return true;
0226     }
0227 
0228     /**
0229      * @param $member_data
0230      *
0231      * @return bool
0232      * @throws Zend_Db_Statement_Exception
0233      */
0234     private function isUsernameUnique($member_data)
0235     {
0236         $sql = "
0237         SELECT `username`, `mail`, COUNT(*) AS `amount`
0238         FROM `member` AS `m`
0239         WHERE `m`.`is_active` = 1 AND `m`.`is_deleted` = 0 AND lower(`username`) = lower(:username)
0240         GROUP BY lower(`username`) -- , mail
0241         HAVING COUNT(*) > 1
0242         ";
0243 
0244         $result = Zend_Db_Table::getDefaultAdapter()->query($sql, array('username' => $member_data->username))->fetch();
0245 
0246         if (is_array($result) AND count($result) > 0) {
0247             $this->message['username'][] = array('username is not unique');
0248             $this->errorCode = $this::INVALID_USERNAME_NOT_UNIQUE;
0249 
0250             return false;
0251         }
0252 
0253         return true;
0254     }
0255 
0256 }