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