632f3ea393ea02d78adf01e2ac8cf90f079addba
[shipsimu.git] / inc / classes / main / user / class_BaseUser.php
1 <?php
2 /**
3  * A general user class 
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseUser extends BaseFrameworkSystem {
25         /**
26          * Instance of the database result
27          */
28         private $resultInstance = null;
29
30         /**
31          * Username of current user
32          */
33         private $userName = "";
34
35         /**
36          * Email of current user
37          */
38         private $email = "";
39
40         /**
41          * Protected constructor
42          *
43          * @param       $className      Name of the class
44          * @return      void
45          */
46         protected function __construct ($className) {
47                 // Call parent constructor
48                 parent::__construct($className);
49
50                 // Clean up a little
51                 $this->removeNumberFormaters();
52                 $this->removeSystemArray();
53         }
54
55         /**
56          * "Getter" for databse entry
57          *
58          * @return      $entry  An array with database entries
59          * @throws      NullPointerException    If the database result is not found
60          * @throws      InvalidDatabaseResultException  If the database result is invalid
61          */
62         private function getDatabaseEntry () {
63                 // Is there an instance?
64                 if (is_null($this->getResultInstance())) {
65                         // Throw new exception
66                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
67                 } // END - if
68
69                 // Rewind it
70                 $this->getResultInstance()->rewind();
71
72                 // Do we have an entry?
73                 if (!$this->getResultInstance()->valid()) {
74                         throw new InvalidDatabaseResultException(array($this, $this->resultInstance), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
75                 } // END - if
76
77                 // Get next entry
78                 $this->getResultInstance()->next();
79
80                 // Fetch it
81                 $entry = $this->getResultInstance()->current();
82
83                 // And return it
84                 return $entry;
85         }
86
87         /**
88          * Setter for username
89          *
90          * @param       $userName       The username to set
91          * @return      void
92          */
93         public final function setUserName ($userName) {
94                 $this->userName = $userName;
95         }
96
97         /**
98          * Setter for email
99          *
100          * @param       $email  The email to set
101          * @return      void
102          */
103         protected final function setEmail ($email) {
104                 $this->email = $email;
105         }
106
107         /**
108          * Getter for username
109          *
110          * @return      $userName       The username to get
111          */
112         public final function getUsername () {
113                 return $this->userName;
114         }
115
116         /**
117          * Getter for email
118          *
119          * @return      $email  The email to get
120          */
121         public final function getEmail () {
122                 return $this->email;
123         }
124
125         /**
126          * Getter for database result instance
127          *
128          * @return      $resultInstance         An instance of a database result class
129          */
130         protected final function getResultInstance () {
131                 return $this->resultInstance;
132         }
133
134         /**
135          * Determines wether the username exists or not
136          *
137          * @return      $exists         Wether the username exists
138          */
139         public function ifUsernameExists () {
140                 // By default the username does not exist
141                 $exists = false;
142
143                 // Is a previous result there?
144                 if (is_null($this->getResultInstance())) {
145                         // Get a UserDatabaseWrapper instance
146                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
147
148                         // Create a search criteria
149                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
150
151                         // Add the username as a criteria and set limit to one entry
152                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
153                         $criteriaInstance->setLimit(1);
154
155                         // Get a search result
156                         $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
157                 } else {
158                         // Rewind it
159                         $this->getResultInstance()->rewind();
160                 }
161
162                 // Search for it
163                 if ($this->getResultInstance()->next()) {
164                         // Entry found
165                         $exists = true;
166                 } // END - if
167
168                 // Return the status
169                 return $exists;
170         }
171
172         /**
173          * Determines wether the email exists or not
174          *
175          * @return      $exists         Wether the email exists
176          */
177         public function ifEmailAddressExists () {
178                 // By default the email does not exist
179                 $exists = false;
180
181                 // Is a previous result there?
182                 if (is_null($this->getResultInstance())) {
183                         // Get a UserDatabaseWrapper instance
184                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
185
186                         // Create a search criteria
187                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
188
189                         // Add the username as a criteria and set limit to one entry
190                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
191                         $criteriaInstance->setLimit(1);
192
193                         // Get a search resultInstance
194                         $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
195                 } else {
196                         // Rewind it
197                         $this->getResultInstance()->rewind();
198                 }
199
200                 // Search for it
201                 if ($this->getResultInstance()->next()) {
202                         // Entry found
203                         $exists = true;
204                 } // END - if
205
206                 // Return the status
207                 return $exists;
208         }
209
210         /**
211          * Checks if the supplied password hash in request matches with the stored
212          * in database.
213          *
214          * @param       $requestInstance        A requestable class instance
215          * @return      $matches                        Wether the supplied password hash matches
216          */
217         public function ifPasswordHashMatches (Requestable $requestInstance) {
218                 // By default nothing matches... ;)
219                 $matches = false;
220
221                 // Get a UserDatabaseWrapper instance
222                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
223
224                 // Create a search criteria
225                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
226
227                 // Add the username as a criteria and set limit to one entry
228                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
229                 $criteriaInstance->setLimit(1);
230
231                 // Get a search resultInstance
232                 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
233
234                 // Search for it
235                 if ($this->getResultInstance()->next()) {
236                         // Get the current entry (can only be one!)
237                         $entry = $this->getResultInstance()->current();
238
239                         // So does the hashes match?
240                         //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
241                         $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
242                 } // END - if
243
244                 // Return the status
245                 return $matches;
246         }
247
248         /**
249          * "Getter" for user's password hash
250          *
251          * @return      $passHash       User's password hash from database result
252          */
253         public function getPasswordHash () {
254                 // Default is missing password hash
255                 $passHash = null;
256
257                 // Get a database entry
258                 $entry = $this->getDatabaseEntry();
259
260                 // Is the password hash there?
261                 if (isset($entry['pass_hash'])) {
262                         // Get it
263                         $passHash = $entry['pass_hash'];
264                 }
265
266                 // And return the hash
267                 return $passHash;
268         }
269
270         /**
271          * Getter for field name
272          *
273          * @param       $fieldName              Field name which we shall get
274          * @return      $fieldValue             Field value from the user
275          * @todo        Do we need to secure this here against missing results?
276          */
277         public function getField ($fieldName) {
278                 // Default field value
279                 $fieldValue = null;
280
281                 // Get current array
282                 $fieldArray = $this->getResultInstance()->current();
283
284                 // Does the field exist?
285                 if (isset($fieldArray[$fieldName])) {
286                         // Get it
287                         $fieldValue = $fieldArray[$fieldName];
288                 } // END - if
289
290                 // Return it
291                 return $fieldValue;
292         }
293 }
294
295 // [EOF]
296 ?>