3 namespace CoreFramework\Filter\Validator\Email;
5 // Import framework stuff
6 use CoreFramework\Registry\Registry;
7 use CoreFramework\Request\Requestable;
8 use CoreFramework\Response\Responseable;
11 * A concrete filter for validating the email address. This filter may intercept
12 * the filter chain if no email address is given or if supplied email has an
13 * invalid form. It could also intercept our filter chain if email address is
14 * already used by some one if configuration requires this.
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class EmailValidatorFilter extends BaseFilter implements Filterable {
37 * Protected constructor
41 protected function __construct () {
42 // Call parent constructor
43 parent::__construct(__CLASS__);
47 * Creates an instance of this filter class
49 * @return $filterInstance An instance of this filter class
51 public static final function createEmailValidatorFilter () {
53 $filterInstance = new EmailValidatorFilter();
55 // Return the instance
56 return $filterInstance;
60 * Executes the filter with given request and response objects
62 * @param $requestInstance An instance of a class with an Requestable interface
63 * @param $responseInstance An instance of a class with an Responseable interface
65 * @throws FilterChainException If this filter fails to operate
67 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
68 // Get Email from request
69 $email = $requestInstance->getRequestElement('email');
72 if ((is_null($email)) || ($this->getConfigInstance()->getConfigEntry('register_email_unique') == 'Y')) {
74 $email1 = $requestInstance->getRequestElement('email1');
75 $email2 = $requestInstance->getRequestElement('email2');
77 // Is the email still not set?
78 if ((is_null($email1)) || (is_null($email2))) {
79 // Not found in form so stop the filtering process
80 $requestInstance->requestIsValid(FALSE);
82 // Add a message to the response
83 $responseInstance->addFatalMessage('email_unset');
86 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
87 } elseif ((empty($email1)) || (empty($email2))) {
89 $requestInstance->requestIsValid(FALSE);
91 // Is the email empty?
93 // Add a message to the response
94 $responseInstance->addFatalMessage('email1_empty');
97 // Is the confirmation empty?
99 // Add a message to the response
100 $responseInstance->addFatalMessage('email2_empty');
104 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
105 } elseif ($this->ifEmailIsTaken($email1)) {
106 // Email is already taken
107 $requestInstance->requestIsValid(FALSE);
109 // Add a message to the response
110 $responseInstance->addFatalMessage('email_taken');
113 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
114 } elseif ($email1 != $email2) {
115 // Emails didn't match
116 $requestInstance->requestIsValid(FALSE);
118 // Add a message to the response
119 $responseInstance->addFatalMessage('emails_mismatch');
122 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
124 } elseif (empty($email)) {
126 $requestInstance->requestIsValid(FALSE);
128 // Add a message to the response
129 $responseInstance->addFatalMessage('email_empty');
132 throw new FilterChainException($this, self::EXCEPTION_FILTER_CHAIN_INTERCEPTED);
137 * Check whether the email as already been taken
139 * @param $email Email to check for existence
140 * @return $alreadyTaken Whether the email has been taken
142 private function ifEmailIsTaken ($email) {
143 // Default is already taken
144 $alreadyTaken = TRUE;
146 // Initialize instance
147 $userInstance = NULL;
149 // Get a registry instance
150 $registry = Registry::getRegistry();
152 // Is the user already there?
153 if ($registry->instanceExists('user')) {
154 // Use the instance for checking for the email
155 $userInstance = $registry->getInstance('user');
156 $userInstance->setEmailAddress($email);
158 // If this instance is created then the username *does* exist
159 $userInstance = call_user_func_array(array($this->getConfigInstance()->getConfigEntry('user_class'), 'createMemberByEmail'), array($email));
161 // Remember this user instance in our registry for later usage
162 $registry->addInstance('user', $userInstance);
165 // Does the email exist?
166 if ($userInstance->ifEmailAddressExists() === FALSE) {
167 // This email has not being used yet
168 $alreadyTaken = FALSE;
172 return $alreadyTaken;