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