3 namespace CoreFramework\Filter\Validator\Email;
5 // Import framework stuff
6 use CoreFramework\Registry\Generic\Registry;
7 use CoreFramework\Request\Requestable;
10 * A concrete filter for validating the email address. This filter may intercept
11 * the filter chain if no email address is given or if supplied email has an
12 * invalid form. It could also intercept our filter chain if email address is
13 * already used by some one if configuration requires this.
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class EmailValidatorFilter extends BaseFilter implements Filterable {
36 * Protected constructor
40 protected function __construct () {
41 // Call parent constructor
42 parent::__construct(__CLASS__);
46 * Creates an instance of this filter class
48 * @return $filterInstance An instance of this filter class
50 public static final function createEmailValidatorFilter () {
52 $filterInstance = new EmailValidatorFilter();
54 // Return the instance
55 return $filterInstance;
59 * Executes the filter with given request and response objects
61 * @param $requestInstance An instance of a class with an Requestable interface
62 * @param $responseInstance An instance of a class with an Responseable interface
64 * @throws FilterChainException If this filter fails to operate
66 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
67 // Get Email from request
68 $email = $requestInstance->getRequestElement('email');
71 if ((is_null($email)) || ($this->getConfigInstance()->getConfigEntry('register_email_unique') == 'Y')) {
73 $email1 = $requestInstance->getRequestElement('email1');
74 $email2 = $requestInstance->getRequestElement('email2');
76 // Is the email still not set?
77 if ((is_null($email1)) || (is_null($email2))) {
78 // Not found in form so stop the filtering process
79 $requestInstance->requestIsValid(FALSE);
81 // Add a message to the response
82 $responseInstance->addFatalMessage('email_unset');
85 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
86 } elseif ((empty($email1)) || (empty($email2))) {
88 $requestInstance->requestIsValid(FALSE);
90 // Is the email empty?
92 // Add a message to the response
93 $responseInstance->addFatalMessage('email1_empty');
96 // Is the confirmation empty?
98 // Add a message to the response
99 $responseInstance->addFatalMessage('email2_empty');
103 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
104 } elseif ($this->ifEmailIsTaken($email1)) {
105 // Email is already taken
106 $requestInstance->requestIsValid(FALSE);
108 // Add a message to the response
109 $responseInstance->addFatalMessage('email_taken');
112 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
113 } elseif ($email1 != $email2) {
114 // Emails didn't match
115 $requestInstance->requestIsValid(FALSE);
117 // Add a message to the response
118 $responseInstance->addFatalMessage('emails_mismatch');
121 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
123 } elseif (empty($email)) {
125 $requestInstance->requestIsValid(FALSE);
127 // Add a message to the response
128 $responseInstance->addFatalMessage('email_empty');
131 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
136 * Check whether the email as already been taken
138 * @param $email Email to check for existence
139 * @return $alreadyTaken Whether the email has been taken
141 private function ifEmailIsTaken ($email) {
142 // Default is already taken
143 $alreadyTaken = TRUE;
145 // Initialize instance
146 $userInstance = NULL;
148 // Get a registry instance
149 $registry = Registry::getRegistry();
151 // Is the user already there?
152 if ($registry->instanceExists('user')) {
153 // Use the instance for checking for the email
154 $userInstance = $registry->getInstance('user');
155 $userInstance->setEmailAddress($email);
157 // If this instance is created then the username *does* exist
158 $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('user_class'), 'createMemberByEmail'), array($email));
160 // Remember this user instance in our registry for later usage
161 $registry->addInstance('user', $userInstance);
164 // Does the email exist?
165 if ($userInstance->ifEmailAddressExists() === FALSE) {
166 // This email has not being used yet
167 $alreadyTaken = FALSE;
171 return $alreadyTaken;