]> git.mxchange.org Git - core.git/blob - framework/main/classes/filter/validator/class_EmailValidatorFilter.php
3c0aad9f13b892572297235fcd197c7d47dd9b4a
[core.git] / framework / main / classes / filter / validator / class_EmailValidatorFilter.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filter\Validator\Email;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filter\BaseFilter;
7 use Org\Mxchange\CoreFramework\Filter\Chain\FilterChainException;
8 use Org\Mxchange\CoreFramework\Filter\Filterable;
9 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
10 use Org\Mxchange\CoreFramework\Request\Requestable;
11 use Org\Mxchange\CoreFramework\Response\Responseable;
12
13 /**
14  * A concrete filter for validating the email address. This filter may intercept
15  * the filter chain if no email address is given or if supplied email has an
16  * invalid form. It could also intercept our filter chain if email address is
17  * already used by some one if configuration requires this.
18  *
19  * @author              Roland Haeder <webmaster@shipsimu.org>
20  * @version             0.0.0
21  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
22  * @license             GNU GPL 3.0 or any newer version
23  * @link                http://www.shipsimu.org
24  *
25  * This program is free software: you can redistribute it and/or modify
26  * it under the terms of the GNU General Public License as published by
27  * the Free Software Foundation, either version 3 of the License, or
28  * (at your option) any later version.
29  *
30  * This program is distributed in the hope that it will be useful,
31  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  * GNU General Public License for more details.
34  *
35  * You should have received a copy of the GNU General Public License
36  * along with this program. If not, see <http://www.gnu.org/licenses/>.
37  */
38 class EmailValidatorFilter extends BaseFilter implements Filterable {
39         /**
40          * Protected constructor
41          *
42          * @return      void
43          */
44         protected function __construct () {
45                 // Call parent constructor
46                 parent::__construct(__CLASS__);
47         }
48
49         /**
50          * Creates an instance of this filter class
51          *
52          * @return      $filterInstance                 An instance of this filter class
53          */
54         public static final function createEmailValidatorFilter () {
55                 // Get a new instance
56                 $filterInstance = new EmailValidatorFilter();
57
58                 // Return the instance
59                 return $filterInstance;
60         }
61
62         /**
63          * Executes the filter with given request and response objects
64          *
65          * @param       $requestInstance        An instance of a class with an Requestable interface
66          * @param       $responseInstance       An instance of a class with an Responseable interface
67          * @return      void
68          * @throws      FilterChainException    If this filter fails to operate
69          */
70         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
71                 // Get Email from request
72                 $email = $requestInstance->getRequestElement('email');
73
74                 // Is the Email set?
75                 if ((is_null($email)) || ($this->getConfigInstance()->getConfigEntry('register_email_unique') == 'Y')) {
76                         // Try it again
77                         $email1 = $requestInstance->getRequestElement('email1');
78                         $email2 = $requestInstance->getRequestElement('email2');
79
80                         // Is the email still not set?
81                         if ((is_null($email1)) || (is_null($email2))) {
82                                 // Not found in form so stop the filtering process
83                                 $requestInstance->requestIsValid(false);
84
85                                 // Add a message to the response
86                                 $responseInstance->addFatalMessage('email_unset');
87
88                                 // Abort here
89                                 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
90                         } elseif ((empty($email1)) || (empty($email2))) {
91                                 // Email is empty
92                                 $requestInstance->requestIsValid(false);
93
94                                 // Is the email empty?
95                                 if (empty($email1)) {
96                                         // Add a message to the response
97                                         $responseInstance->addFatalMessage('email1_empty');
98                                 } // END - if
99
100                                 // Is the confirmation empty?
101                                 if (empty($email2)) {
102                                         // Add a message to the response
103                                         $responseInstance->addFatalMessage('email2_empty');
104                                 } // END - if
105
106                                 // Abort here
107                                 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
108                         } elseif ($this->ifEmailIsTaken($email1)) {
109                                 // Email is already taken
110                                 $requestInstance->requestIsValid(false);
111
112                                 // Add a message to the response
113                                 $responseInstance->addFatalMessage('email_taken');
114
115                                 // Abort here
116                                 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
117                         } elseif ($email1 != $email2) {
118                                 // Emails didn't match
119                                 $requestInstance->requestIsValid(false);
120
121                                 // Add a message to the response
122                                 $responseInstance->addFatalMessage('emails_mismatch');
123
124                                 // Abort here
125                                 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
126                         } // END - elseif
127                 } elseif (empty($email)) {
128                         // Empty field!
129                         $requestInstance->requestIsValid(false);
130
131                         // Add a message to the response
132                         $responseInstance->addFatalMessage('email_empty');
133
134                         // Abort here
135                         throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
136                 } // END - elseif
137         }
138
139         /**
140          * Check whether the email as already been taken
141          *
142          * @param       $email                  Email to check for existence
143          * @return      $alreadyTaken   Whether the email has been taken
144          */
145         private function ifEmailIsTaken ($email) {
146                 // Default is already taken
147                 $alreadyTaken = true;
148
149                 // Initialize instance
150                 $userInstance = NULL;
151
152                 // Get a registry instance
153                 $registry = GenericRegistry::getRegistry();
154
155                 // Is the user already there?
156                 if ($registry->instanceExists('user')) {
157                         // Use the instance for checking for the email
158                         $userInstance = $registry->getInstance('user');
159                         $userInstance->setEmailAddress($email);
160                 } else {
161                         // If this instance is created then the username *does* exist
162                         $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('user_class'), 'createMemberByEmail'), array($email));
163
164                         // Remember this user instance in our registry for later usage
165                         $registry->addInstance('user', $userInstance);
166                 }
167
168                 // Does the email exist?
169                 if ($userInstance->ifEmailAddressExists() === false) {
170                         // This email has not being used yet
171                         $alreadyTaken = false;
172                 }
173
174                 // Return the result
175                 return $alreadyTaken;
176         }
177
178 }