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 supplied email has an
5 * invalid form. It could also intercept our filter chain if email address is
6 * 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 BaseFilter implements Filterable {
29 * Protected constructor
33 protected function __construct () {
34 // Call parent constructor
35 parent::__construct(__CLASS__);
39 * Creates an instance of this filter class
41 * @param $controllerInstance An instance of a Controller class
42 * @return $filterInstance An instance of this filter class
44 public final static function createEmailValidatorFilter (Controller $controllerInstance) {
46 $filterInstance = new EmailValidatorFilter();
49 $filterInstance->setControllerInstance($controllerInstance);
51 // Return the instance
52 return $filterInstance;
56 * Executes the filter with given request and response objects
58 * @param $requestInstance An instance of a class with an Requestable interface
59 * @param $responseInstance An instance of a class with an Responseable interface
62 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
63 // Get Email from request
64 $email = $requestInstance->getRequestElement('email');
67 if ((is_null($email)) || ($this->getConfigInstance()->readConfig('register_email_unique') === "Y")) {
69 $email1 = $requestInstance->getRequestElement('email1');
70 $email2 = $requestInstance->getRequestElement('email2');
72 // Is the email still not set?
73 if ((is_null($email1)) || (is_null($email2))) {
74 // Not found in form so stop the filtering process
75 $requestInstance->requestIsValid(false);
77 // Add a message to the response
78 $responseInstance->addFatalMessage('email_unset');
82 } elseif ((empty($email1)) || (empty($email2))) {
84 $requestInstance->requestIsValid(false);
86 // Is the email empty?
88 // Add a message to the response
89 $responseInstance->addFatalMessage('email1_empty');
92 // Is the confirmation empty?
94 // Add a message to the response
95 $responseInstance->addFatalMessage('email2_empty');
100 } elseif ($this->ifEmailIsTaken($email1)) {
101 // Email is already taken
102 $requestInstance->requestIsValid(false);
104 // Add a message to the response
105 $responseInstance->addFatalMessage('email_taken');
109 } elseif ($email1 != $email2) {
110 // Emails didn't match
111 $requestInstance->requestIsValid(false);
113 // Add a message to the response
114 $responseInstance->addFatalMessage('emails_mismatch');
119 } elseif (empty($email)) {
121 $requestInstance->requestIsValid(false);
123 // Add a message to the response
124 $responseInstance->addFatalMessage('email_empty');
132 * Check wether the email as already been taken
134 * @param $email Email to check for existence
135 * @return $alreadyTaken Wether the email has been taken
137 private function ifEmailIsTaken ($email) {
138 // Default is already taken
139 $alreadyTaken = true;
141 // Initialize instance
142 $userInstance = null;
144 // Get a registry instance
145 $registry = Registry::getRegistry();
147 // Is the user already there?
148 if ($registry->instanceExists('user')) {
149 // Use the instance for checking for the email
150 $userInstance = $registry->getInstance('user');
151 $userInstance->setEmailAddress($email);
153 // If this instance is created then the username *does* exist
154 $userInstance = call_user_func_array(array($this->getConfigInstance()->readConfig('user_class'), 'createMemberByEmail'), array($email));
156 // Remember this user instance in our registry for later usage
157 $registry->addInstance('user', $userInstance);
160 // Does the email exist?
161 if ($userInstance->ifEmailAddressExists() === false) {
162 // This email has not being used yet
163 $alreadyTaken = false;
167 return $alreadyTaken;