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\Filterable;
8 use Org\Mxchange\CoreFramework\Registry\Registry;
9 use Org\Mxchange\CoreFramework\Request\Requestable;
10 use Org\Mxchange\CoreFramework\Response\Responseable;
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.
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 <<<<<<< HEAD:framework/main/classes/filter/validator/class_EmailValidatorFilter.php
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
24 >>>>>>> Some updates::inc/main/classes/filter/validator/class_EmailValidatorFilter.php
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class EmailValidatorFilter extends BaseFilter implements Filterable {
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
53 * Creates an instance of this filter class
55 * @return $filterInstance An instance of this filter class
57 public static final function createEmailValidatorFilter () {
59 $filterInstance = new EmailValidatorFilter();
61 // Return the instance
62 return $filterInstance;
66 * Executes the filter with given request and response objects
68 * @param $requestInstance An instance of a class with an Requestable interface
69 * @param $responseInstance An instance of a class with an Responseable interface
71 * @throws FilterChainException If this filter fails to operate
73 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
74 // Get Email from request
75 $email = $requestInstance->getRequestElement('email');
78 if ((is_null($email)) || ($this->getConfigInstance()->getConfigEntry('register_email_unique') == 'Y')) {
80 $email1 = $requestInstance->getRequestElement('email1');
81 $email2 = $requestInstance->getRequestElement('email2');
83 // Is the email still not set?
84 if ((is_null($email1)) || (is_null($email2))) {
85 // Not found in form so stop the filtering process
86 $requestInstance->requestIsValid(false);
88 // Add a message to the response
89 $responseInstance->addFatalMessage('email_unset');
92 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
93 } elseif ((empty($email1)) || (empty($email2))) {
95 $requestInstance->requestIsValid(false);
97 // Is the email empty?
99 // Add a message to the response
100 $responseInstance->addFatalMessage('email1_empty');
103 // Is the confirmation empty?
104 if (empty($email2)) {
105 // Add a message to the response
106 $responseInstance->addFatalMessage('email2_empty');
110 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
111 } elseif ($this->ifEmailIsTaken($email1)) {
112 // Email is already taken
113 $requestInstance->requestIsValid(false);
115 // Add a message to the response
116 $responseInstance->addFatalMessage('email_taken');
119 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
120 } elseif ($email1 != $email2) {
121 // Emails didn't match
122 $requestInstance->requestIsValid(false);
124 // Add a message to the response
125 $responseInstance->addFatalMessage('emails_mismatch');
128 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
130 } elseif (empty($email)) {
132 $requestInstance->requestIsValid(false);
134 // Add a message to the response
135 $responseInstance->addFatalMessage('email_empty');
138 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
143 * Check whether the email as already been taken
145 * @param $email Email to check for existence
146 * @return $alreadyTaken Whether the email has been taken
148 private function ifEmailIsTaken ($email) {
149 // Default is already taken
150 $alreadyTaken = true;
152 // Initialize instance
153 $userInstance = NULL;
155 // Get a registry instance
156 $registry = Registry::getRegistry();
158 // Is the user already there?
159 if ($registry->instanceExists('user')) {
160 // Use the instance for checking for the email
161 $userInstance = $registry->getInstance('user');
162 $userInstance->setEmailAddress($email);
164 // If this instance is created then the username *does* exist
165 $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('user_class'), 'createMemberByEmail'), array($email));
167 // Remember this user instance in our registry for later usage
168 $registry->addInstance('user', $userInstance);
171 // Does the email exist?
172 if ($userInstance->ifEmailAddressExists() === false) {
173 // This email has not being used yet
174 $alreadyTaken = false;
178 return $alreadyTaken;