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