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