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