3 namespace Org\Mxchange\CoreFramework\Filter\Validator\Email;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filter\BaseFilter;
7 use Org\Mxchange\CoreFramework\Filter\Chain\FilterChainException;
8 use Org\Mxchange\CoreFramework\Filter\Filterable;
9 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
10 use Org\Mxchange\CoreFramework\Request\Requestable;
11 use Org\Mxchange\CoreFramework\Response\Responseable;
14 * A concrete filter for validating the email address. This filter may intercept
15 * the filter chain if no email address is given or if supplied email has an
16 * invalid form. It could also intercept our filter chain if email address is
17 * already used by some one if configuration requires this.
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class EmailValidatorFilter extends BaseFilter implements Filterable {
40 * Protected constructor
44 protected function __construct () {
45 // Call parent constructor
46 parent::__construct(__CLASS__);
50 * Creates an instance of this filter class
52 * @return $filterInstance An instance of this filter class
54 public static final function createEmailValidatorFilter () {
56 $filterInstance = new EmailValidatorFilter();
58 // Return the instance
59 return $filterInstance;
63 * Executes the filter with given request and response objects
65 * @param $requestInstance An instance of a class with an Requestable interface
66 * @param $responseInstance An instance of a class with an Responseable interface
68 * @throws FilterChainException If this filter fails to operate
70 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
71 // Get Email from request
72 $email = $requestInstance->getRequestElement('email');
75 if ((is_null($email)) || ($this->getConfigInstance()->getConfigEntry('register_email_unique') == 'Y')) {
77 $email1 = $requestInstance->getRequestElement('email1');
78 $email2 = $requestInstance->getRequestElement('email2');
80 // Is the email still not set?
81 if ((is_null($email1)) || (is_null($email2))) {
82 // Not found in form so stop the filtering process
83 $requestInstance->requestIsValid(false);
85 // Add a message to the response
86 $responseInstance->addFatalMessage('email_unset');
89 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
90 } elseif ((empty($email1)) || (empty($email2))) {
92 $requestInstance->requestIsValid(false);
94 // Is the email empty?
96 // Add a message to the response
97 $responseInstance->addFatalMessage('email1_empty');
100 // Is the confirmation empty?
101 if (empty($email2)) {
102 // Add a message to the response
103 $responseInstance->addFatalMessage('email2_empty');
107 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
108 } elseif ($this->ifEmailIsTaken($email1)) {
109 // Email is already taken
110 $requestInstance->requestIsValid(false);
112 // Add a message to the response
113 $responseInstance->addFatalMessage('email_taken');
116 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
117 } elseif ($email1 != $email2) {
118 // Emails didn't match
119 $requestInstance->requestIsValid(false);
121 // Add a message to the response
122 $responseInstance->addFatalMessage('emails_mismatch');
125 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
127 } elseif (empty($email)) {
129 $requestInstance->requestIsValid(false);
131 // Add a message to the response
132 $responseInstance->addFatalMessage('email_empty');
135 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
140 * Check whether the email as already been taken
142 * @param $email Email to check for existence
143 * @return $alreadyTaken Whether the email has been taken
145 private function ifEmailIsTaken ($email) {
146 // Default is already taken
147 $alreadyTaken = true;
149 // Initialize instance
150 $userInstance = NULL;
152 // Get a registry instance
153 $registry = GenericRegistry::getRegistry();
155 // Is the user already there?
156 if ($registry->instanceExists('user')) {
157 // Use the instance for checking for the email
158 $userInstance = $registry->getInstance('user');
159 $userInstance->setEmailAddress($email);
161 // If this instance is created then the username *does* exist
162 $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('user_class'), 'createMemberByEmail'), array($email));
164 // Remember this user instance in our registry for later usage
165 $registry->addInstance('user', $userInstance);
168 // Does the email exist?
169 if ($userInstance->ifEmailAddressExists() === false) {
170 // This email has not being used yet
171 $alreadyTaken = false;
175 return $alreadyTaken;