3 namespace CoreFramework\Filter\Validator\Email;
5 // Import framework stuff
6 use CoreFramework\Registry\Generic\Registry;
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.
14 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
33 class EmailValidatorFilter extends BaseFilter implements Filterable {
35 * Protected constructor
39 protected function __construct () {
40 // Call parent constructor
41 parent::__construct(__CLASS__);
45 * Creates an instance of this filter class
47 * @return $filterInstance An instance of this filter class
49 public static final function createEmailValidatorFilter () {
51 $filterInstance = new EmailValidatorFilter();
53 // Return the instance
54 return $filterInstance;
58 * Executes the filter with given request and response objects
60 * @param $requestInstance An instance of a class with an Requestable interface
61 * @param $responseInstance An instance of a class with an Responseable interface
63 * @throws FilterChainException If this filter fails to operate
65 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
66 // Get Email from request
67 $email = $requestInstance->getRequestElement('email');
70 if ((is_null($email)) || ($this->getConfigInstance()->getConfigEntry('register_email_unique') == 'Y')) {
72 $email1 = $requestInstance->getRequestElement('email1');
73 $email2 = $requestInstance->getRequestElement('email2');
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);
80 // Add a message to the response
81 $responseInstance->addFatalMessage('email_unset');
84 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
85 } elseif ((empty($email1)) || (empty($email2))) {
87 $requestInstance->requestIsValid(FALSE);
89 // Is the email empty?
91 // Add a message to the response
92 $responseInstance->addFatalMessage('email1_empty');
95 // Is the confirmation empty?
97 // Add a message to the response
98 $responseInstance->addFatalMessage('email2_empty');
102 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
103 } elseif ($this->ifEmailIsTaken($email1)) {
104 // Email is already taken
105 $requestInstance->requestIsValid(FALSE);
107 // Add a message to the response
108 $responseInstance->addFatalMessage('email_taken');
111 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
112 } elseif ($email1 != $email2) {
113 // Emails didn't match
114 $requestInstance->requestIsValid(FALSE);
116 // Add a message to the response
117 $responseInstance->addFatalMessage('emails_mismatch');
120 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
122 } elseif (empty($email)) {
124 $requestInstance->requestIsValid(FALSE);
126 // Add a message to the response
127 $responseInstance->addFatalMessage('email_empty');
130 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
135 * Check whether the email as already been taken
137 * @param $email Email to check for existence
138 * @return $alreadyTaken Whether the email has been taken
140 private function ifEmailIsTaken ($email) {
141 // Default is already taken
142 $alreadyTaken = TRUE;
144 // Initialize instance
145 $userInstance = NULL;
147 // Get a registry instance
148 $registry = Registry::getRegistry();
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);
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));
159 // Remember this user instance in our registry for later usage
160 $registry->addInstance('user', $userInstance);
163 // Does the email exist?
164 if ($userInstance->ifEmailAddressExists() === FALSE) {
165 // This email has not being used yet
166 $alreadyTaken = FALSE;
170 return $alreadyTaken;