Controller instance shall now be fetched from registry
[core.git] / inc / classes / main / filter / validator / class_EmailValidatorFilter.php
1 <?php
2 /**
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.
7  *
8  * @author              Roland Haeder <webmaster@ship-simu.org>
9  * @version             0.0.0
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
13  *
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.
18  *
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.
23  *
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/>.
26  */
27 class EmailValidatorFilter extends BaseFilter implements Filterable {
28         /**
29          * Protected constructor
30          *
31          * @return      void
32          */
33         protected function __construct () {
34                 // Call parent constructor
35                 parent::__construct(__CLASS__);
36         }
37
38         /**
39          * Creates an instance of this filter class
40          *
41          * @return      $filterInstance                 An instance of this filter class
42          */
43         public final static function createEmailValidatorFilter () {
44                 // Get a new instance
45                 $filterInstance = new EmailValidatorFilter();
46
47                 // Return the instance
48                 return $filterInstance;
49         }
50
51         /**
52          * Executes the filter with given request and response objects
53          *
54          * @param       $requestInstance        An instance of a class with an Requestable interface
55          * @param       $responseInstance       An instance of a class with an Responseable interface
56          * @return      void
57          */
58         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
59                 // Get Email from request
60                 $email = $requestInstance->getRequestElement('email');
61
62                 // Is the Email set?
63                 if ((is_null($email)) || ($this->getConfigInstance()->readConfig('register_email_unique') === "Y")) {
64                         // Try it again
65                         $email1 = $requestInstance->getRequestElement('email1');
66                         $email2 = $requestInstance->getRequestElement('email2');
67
68                         // Is the email still not set?
69                         if ((is_null($email1)) || (is_null($email2))) {
70                                 // Not found in form so stop the filtering process
71                                 $requestInstance->requestIsValid(false);
72
73                                 // Add a message to the response
74                                 $responseInstance->addFatalMessage('email_unset');
75
76                                 // Abort here
77                                 return false;
78                         } elseif ((empty($email1)) || (empty($email2))) {
79                                 // Email is empty
80                                 $requestInstance->requestIsValid(false);
81
82                                 // Is the email empty?
83                                 if (empty($email1)) {
84                                         // Add a message to the response
85                                         $responseInstance->addFatalMessage('email1_empty');
86                                 } // END - if
87
88                                 // Is the confirmation empty?
89                                 if (empty($email2)) {
90                                         // Add a message to the response
91                                         $responseInstance->addFatalMessage('email2_empty');
92                                 } // END - if
93
94                                 // Abort here
95                                 return false;
96                         } elseif ($this->ifEmailIsTaken($email1)) {
97                                 // Email is already taken
98                                 $requestInstance->requestIsValid(false);
99
100                                 // Add a message to the response
101                                 $responseInstance->addFatalMessage('email_taken');
102
103                                 // Abort here
104                                 return false;
105                         } elseif ($email1 != $email2) {
106                                 // Emails didn't match
107                                 $requestInstance->requestIsValid(false);
108
109                                 // Add a message to the response
110                                 $responseInstance->addFatalMessage('emails_mismatch');
111
112                                 // Abort here
113                                 return false;
114                         } // END - elseif
115                 } elseif (empty($email)) {
116                         // Empty field!
117                         $requestInstance->requestIsValid(false);
118
119                         // Add a message to the response
120                         $responseInstance->addFatalMessage('email_empty');
121
122                         // Abort here
123                         return false;
124                 } // END - elseif
125         }
126
127         /**
128          * Check wether the email as already been taken
129          *
130          * @param       $email                  Email to check for existence
131          * @return      $alreadyTaken   Wether the email has been taken
132          */
133         private function ifEmailIsTaken ($email) {
134                 // Default is already taken
135                 $alreadyTaken = true;
136
137                 // Initialize instance
138                 $userInstance = null;
139
140                 // Get a registry instance
141                 $registry = Registry::getRegistry();
142
143                 // Is the user already there?
144                 if ($registry->instanceExists('user')) {
145                         // Use the instance for checking for the email
146                         $userInstance = $registry->getInstance('user');
147                         $userInstance->setEmailAddress($email);
148                 } else {
149                         // If this instance is created then the username *does* exist
150                         $userInstance = call_user_func_array(array($this->getConfigInstance()->readConfig('user_class'), 'createMemberByEmail'), array($email));
151
152                         // Remember this user instance in our registry for later usage
153                         $registry->addInstance('user', $userInstance);
154                 }
155
156                 // Does the email exist?
157                 if ($userInstance->ifEmailAddressExists() === false) {
158                         // This email has not being used yet
159                         $alreadyTaken = false;
160                 }
161
162                 // Return the result
163                 return $alreadyTaken;
164         }
165 }
166
167 // [EOF]
168 ?>