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