3 * A concrete filter for validating the email address. This filter may intercept
4 * the filter chain if no email address is given or if the supplied email has an
5 * invalid form. It could also intercept the filter chain if the email address
6 * is already used by some one if configuration requires this.
8 * @author Roland Haeder <webmaster@ship-simu.org>
10 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
11 * @license GNU GPL 3.0 or any newer version
12 * @link http://www.ship-simu.org
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class EmailValidatorFilter extends BaseFrameworkSystem implements Filterable {
29 * Protected constructor
33 protected function __construct () {
34 // Call parent constructor
35 parent::__construct(__CLASS__);
37 // Set part description
38 $this->setObjectDescription("A filter for Email validation");
40 // Create unique ID number
41 $this->createUniqueID();
44 $this->removeNumberFormaters();
45 $this->removeSystemArray();
49 * Creates an instance of this filter class
51 * @return $filterInstance An instance of this filter class
53 public final static 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
68 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
69 // Get Email from request
70 $email = $requestInstance->getRequestElement('email');
73 if ((is_null($email)) || ($this->getConfigInstance()->readConfig('register_email_unique') == "Y")) {
75 $email1 = $requestInstance->getRequestElement('email1');
76 $email2 = $requestInstance->getRequestElement('email2');
78 // Is the email still not set?
79 if ((is_null($email1)) || (is_null($email2))) {
80 // Not found in form so stop the filtering process
81 $requestInstance->requestIsValid(false);
83 // Add a message to the response
84 $responseInstance->addFatalMessage('email_unset');
88 } elseif ((empty($email1)) || (empty($email2))) {
90 $requestInstance->requestIsValid(false);
92 // Is the email empty?
94 // Add a message to the response
95 $responseInstance->addFatalMessage('email1_empty');
98 // Is the confirmation empty?
100 // Add a message to the response
101 $responseInstance->addFatalMessage('email2_empty');
106 } elseif ($this->ifEmailIsTaken($email1)) {
107 // Email is already taken
108 $requestInstance->requestIsValid(false);
110 // Add a message to the response
111 $responseInstance->addFatalMessage('email_taken');
115 } elseif ($email1 != $email2) {
116 // Emails didn't match
117 $requestInstance->requestIsValid(false);
119 // Add a message to the response
120 $responseInstance->addFatalMessage('emails_mismatch');
125 } elseif (empty($email)) {
127 $requestInstance->requestIsValid(false);
129 // Add a message to the response
130 $responseInstance->addFatalMessage('email_empty');
138 * Check wether the email as already been taken
140 * @param $email Email to check for existence
141 * @return $alreadyTaken Wether the email has been taken
143 private function ifEmailIsTaken ($email) {
144 // Default is already taken
145 $alreadyTaken = true;
147 // Initialize instance
148 $userInstance = null;
150 // Get a registry instance
151 $registry = Registry::getRegistry();
153 // Is the user already there?
154 if ($registry->instanceExists('user')) {
155 // Use the instance for checking for the email
156 $userInstance = $registry->getInstance('user');
157 $userInstance->setEmailAddress($email);
159 // If this instance is created then the username *does* exist
160 $userInstance = User::createUserByEmail($email);
162 // Remember this user instance in our registry for later usage
163 $registry->addInstance('user', $userInstance);
166 // Does the email exist?
167 if (!$userInstance->ifEmailAddressExists()) {
168 // This email has not being used yet
169 $alreadyTaken = false;
173 return $alreadyTaken;