3 namespace CoreFramework\Filter\Validator\Email;
5 // Import framework stuff
6 use CoreFramework\Filter\BaseFilter;
7 use CoreFramework\Filter\Filterable;
8 use CoreFramework\Registry\Registry;
9 use CoreFramework\Request\Requestable;
10 use 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 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class EmailValidatorFilter extends BaseFilter implements Filterable {
39 * Protected constructor
43 protected function __construct () {
44 // Call parent constructor
45 parent::__construct(__CLASS__);
49 * Creates an instance of this filter class
51 * @return $filterInstance An instance of this filter class
53 public static final function createEmailValidatorFilter () {
55 $filterInstance = new EmailValidatorFilter();
57 // Return the instance
58 return $filterInstance;
62 * Executes the filter with given request and response objects
64 * @param $requestInstance An instance of a class with an Requestable interface
65 * @param $responseInstance An instance of a class with an Responseable interface
67 * @throws FilterChainException If this filter fails to operate
69 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
70 // Get Email from request
71 $email = $requestInstance->getRequestElement('email');
74 if ((is_null($email)) || ($this->getConfigInstance()->getConfigEntry('register_email_unique') == 'Y')) {
76 $email1 = $requestInstance->getRequestElement('email1');
77 $email2 = $requestInstance->getRequestElement('email2');
79 // Is the email still not set?
80 if ((is_null($email1)) || (is_null($email2))) {
81 // Not found in form so stop the filtering process
82 $requestInstance->requestIsValid(FALSE);
84 // Add a message to the response
85 $responseInstance->addFatalMessage('email_unset');
88 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
89 } elseif ((empty($email1)) || (empty($email2))) {
91 $requestInstance->requestIsValid(FALSE);
93 // Is the email empty?
95 // Add a message to the response
96 $responseInstance->addFatalMessage('email1_empty');
99 // Is the confirmation empty?
100 if (empty($email2)) {
101 // Add a message to the response
102 $responseInstance->addFatalMessage('email2_empty');
106 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
107 } elseif ($this->ifEmailIsTaken($email1)) {
108 // Email is already taken
109 $requestInstance->requestIsValid(FALSE);
111 // Add a message to the response
112 $responseInstance->addFatalMessage('email_taken');
115 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
116 } elseif ($email1 != $email2) {
117 // Emails didn't match
118 $requestInstance->requestIsValid(FALSE);
120 // Add a message to the response
121 $responseInstance->addFatalMessage('emails_mismatch');
124 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
126 } elseif (empty($email)) {
128 $requestInstance->requestIsValid(FALSE);
130 // Add a message to the response
131 $responseInstance->addFatalMessage('email_empty');
134 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
139 * Check whether the email as already been taken
141 * @param $email Email to check for existence
142 * @return $alreadyTaken Whether the email has been taken
144 private function ifEmailIsTaken ($email) {
145 // Default is already taken
146 $alreadyTaken = TRUE;
148 // Initialize instance
149 $userInstance = NULL;
151 // Get a registry instance
152 $registry = Registry::getRegistry();
154 // Is the user already there?
155 if ($registry->instanceExists('user')) {
156 // Use the instance for checking for the email
157 $userInstance = $registry->getInstance('user');
158 $userInstance->setEmailAddress($email);
160 // If this instance is created then the username *does* exist
161 $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('user_class'), 'createMemberByEmail'), array($email));
163 // Remember this user instance in our registry for later usage
164 $registry->addInstance('user', $userInstance);
167 // Does the email exist?
168 if ($userInstance->ifEmailAddressExists() === FALSE) {
169 // This email has not being used yet
170 $alreadyTaken = FALSE;
174 return $alreadyTaken;