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