]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/user/class_BaseUser.php
User class / resending of confirm link updated:
[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          * Username of current user
27          */
28         private $userName = "";
29
30         /**
31          * Email of current user
32          */
33         private $email = "";
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44
45                 // Clean up a little
46                 $this->removeNumberFormaters();
47                 $this->removeSystemArray();
48         }
49
50         /**
51          * "Getter" for databse entry
52          *
53          * @return      $entry  An array with database entries
54          * @throws      NullPointerException    If the database result is not found
55          * @throws      InvalidDatabaseResultException  If the database result is invalid
56          */
57         private function getDatabaseEntry () {
58                 // Is there an instance?
59                 if (is_null($this->getResultInstance())) {
60                         // Throw new exception
61                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
62                 } // END - if
63
64                 // Rewind it
65                 $this->getResultInstance()->rewind();
66
67                 // Do we have an entry?
68                 if (!$this->getResultInstance()->valid()) {
69                         throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
70                 } // END - if
71
72                 // Get next entry
73                 $this->getResultInstance()->next();
74
75                 // Fetch it
76                 $entry = $this->getResultInstance()->current();
77
78                 // And return it
79                 return $entry;
80         }
81
82         /**
83          * Setter for username
84          *
85          * @param       $userName       The username to set
86          * @return      void
87          */
88         public final function setUserName ($userName) {
89                 $this->userName = $userName;
90         }
91
92         /**
93          * Setter for email
94          *
95          * @param       $email  The email to set
96          * @return      void
97          */
98         protected final function setEmail ($email) {
99                 $this->email = $email;
100         }
101
102         /**
103          * Getter for username
104          *
105          * @return      $userName       The username to get
106          */
107         public final function getUsername () {
108                 return $this->userName;
109         }
110
111         /**
112          * Getter for email
113          *
114          * @return      $email  The email to get
115          */
116         public final function getEmail () {
117                 return $this->email;
118         }
119
120         /**
121          * Determines wether the username exists or not
122          *
123          * @return      $exists         Wether the username exists
124          */
125         public function ifUsernameExists () {
126                 // By default the username does not exist
127                 $exists = false;
128
129                 // Is a previous result there?
130                 if (is_null($this->getResultInstance())) {
131                         // Get a UserDatabaseWrapper instance
132                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
133
134                         // Create a search criteria
135                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
136
137                         // Add the username as a criteria and set limit to one entry
138                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
139                         $criteriaInstance->setLimit(1);
140
141                         // Get a search result
142                         $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
143                 } else {
144                         // Rewind it
145                         $this->getResultInstance()->rewind();
146                 }
147
148                 // Search for it
149                 if ($this->getResultInstance()->next()) {
150                         // Entry found
151                         $exists = true;
152                 } // END - if
153
154                 // Return the status
155                 return $exists;
156         }
157
158         /**
159          * Determines wether the email exists or not
160          *
161          * @return      $exists         Wether the email exists
162          */
163         public function ifEmailAddressExists () {
164                 // By default the email does not exist
165                 $exists = false;
166
167                 // Is a previous result there?
168                 if (is_null($this->getResultInstance())) {
169                         // Get a UserDatabaseWrapper instance
170                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
171
172                         // Create a search criteria
173                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
174
175                         // Add the username as a criteria and set limit to one entry
176                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
177                         $criteriaInstance->setLimit(1);
178
179                         // Get a search resultInstance
180                         $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
181                 } else {
182                         // Rewind it
183                         $this->getResultInstance()->rewind();
184                 }
185
186                 // Search for it
187                 if ($this->getResultInstance()->next()) {
188                         // Entry found
189                         $exists = true;
190
191                         // Is the username set?
192                         if ($this->getUserName() == "") {
193                                 // Get current entry
194                                 $currEntry = $this->getResultInstance()->current();
195
196                                 // Set the username
197                                 $this->setUserName($currEntry['username']);
198                         } // END - if
199                 } // END - if
200
201                 // Return the status
202                 return $exists;
203         }
204
205         /**
206          * Checks if the supplied password hash in request matches with the stored
207          * in database.
208          *
209          * @param       $requestInstance        A requestable class instance
210          * @return      $matches                        Wether the supplied password hash matches
211          */
212         public function ifPasswordHashMatches (Requestable $requestInstance) {
213                 // By default nothing matches... ;)
214                 $matches = false;
215
216                 // Get a UserDatabaseWrapper instance
217                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
218
219                 // Create a search criteria
220                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
221
222                 // Add the username as a criteria and set limit to one entry
223                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
224                 $criteriaInstance->setLimit(1);
225
226                 // Get a search resultInstance
227                 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
228
229                 // Search for it
230                 if ($this->getResultInstance()->next()) {
231                         // Get the current entry (can only be one!)
232                         $entry = $this->getResultInstance()->current();
233
234                         // So does the hashes match?
235                         //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
236                         $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
237                 } // END - if
238
239                 // Return the status
240                 return $matches;
241         }
242
243         /**
244          * "Getter" for user's password hash
245          *
246          * @return      $passHash       User's password hash from database result
247          */
248         public final function getPasswordHash () {
249                 // Default is missing password hash
250                 $passHash = null;
251
252                 // Get a database entry
253                 $entry = $this->getDatabaseEntry();
254
255                 // Is the password hash there?
256                 if (isset($entry['pass_hash'])) {
257                         // Get it
258                         $passHash = $entry['pass_hash'];
259                 }
260
261                 // And return the hash
262                 return $passHash;
263         }
264
265         /**
266          * Getter for field name
267          *
268          * @param       $fieldName              Field name which we shall get
269          * @return      $fieldValue             Field value from the user
270          * @throws      NullPointerException    If the result instance is null
271          */
272         public final function getField ($fieldName) {
273                 // Default field value
274                 $fieldValue = null;
275
276                 // Get result instance
277                 $resultInstance = $this->getResultInstance();
278
279                 // Is this instance null?
280                 if (is_null($resultInstance)) {
281                         // Then the user instance is no longer valid (expired cookies?)
282                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
283                 } // END - if
284
285                 // Get current array
286                 $fieldArray = $resultInstance->current();
287
288                 // Does the field exist?
289                 if (isset($fieldArray[$fieldName])) {
290                         // Get it
291                         $fieldValue = $fieldArray[$fieldName];
292                 } // END - if
293
294                 // Return it
295                 return $fieldValue;
296         }
297
298         /**
299          * Getter for primary key value
300          *
301          * @return      $primaryValue   Value of the primary key based on database type
302          */
303         public final function getPrimaryKey () {
304                 // Get a user database wrapper
305                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
306
307                 // Get the primary key back from the wrapper
308                 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
309
310                 // Get that field
311                 $primaryValue = $this->getField($primaryKey);
312
313                 // Return the value
314                 return $primaryValue;
315         }
316 }
317
318 // [EOF]
319 ?>