update copyright as changes will happen this year
[core.git] / inc / main / classes / user / class_BaseUser.php
1 <?php
2 /**
3  * A general user class 
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.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 implements Updateable {
25         // Exception constances
26         const EXCEPTION_USERNAME_NOT_FOUND   = 0x150;
27         const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
28         const EXCEPTION_USER_PASS_MISMATCH   = 0x152;
29         const EXCEPTION_USER_IS_GUEST        = 0x153;
30
31         /**
32          * Username of current user
33          */
34         private $userName = '';
35
36         /**
37          * User id of current user
38          */
39         private $userId = 0;
40
41         /**
42          * Email of current user
43          */
44         private $email = '';
45
46         /**
47          * Protected constructor
48          *
49          * @param       $className      Name of the class
50          * @return      void
51          */
52         protected function __construct ($className) {
53                 // Call parent constructor
54                 parent::__construct($className);
55         }
56
57         /**
58          * Setter for username
59          *
60          * @param       $userName       The username to set
61          * @return      void
62          */
63         public final function setUserName ($userName) {
64                 $this->userName = (string) $userName;
65         }
66
67         /**
68          * Getter for username
69          *
70          * @return      $userName       The username to get
71          */
72         public final function getUserName () {
73                 return $this->userName;
74         }
75
76         /**
77          * Setter for user id
78          *
79          * @param       $userId         The user id to set
80          * @return      void
81          * @todo        Find a way of casting here. "(int)" might destroy the user id > 32766
82          */
83         public final function setUserId ($userId) {
84                 $this->userId = $userId;
85         }
86
87         /**
88          * Getter for user id
89          *
90          * @return      $userId The user id to get
91          */
92         public final function getUserId () {
93                 return $this->userId;
94         }
95
96         /**
97          * Setter for email
98          *
99          * @param       $email  The email to set
100          * @return      void
101          */
102         protected final function setEmail ($email) {
103                 $this->email = (string) $email;
104         }
105
106         /**
107          * Getter for email
108          *
109          * @return      $email  The email to get
110          */
111         public final function getEmail () {
112                 return $this->email;
113         }
114
115         /**
116          * Determines whether the username exists or not
117          *
118          * @return      $exists         Whether the username exists
119          */
120         public function ifUsernameExists () {
121                 // By default the username does not exist
122                 $exists = FALSE;
123
124                 // Is a previous result there?
125                 if (!$this->getResultInstance() instanceof SearchableResult) {
126                         // Get a UserDatabaseWrapper instance
127                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
128
129                         // Create a search criteria
130                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
131
132                         // Add the username as a criteria and set limit to one entry
133                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
134                         $criteriaInstance->setLimit(1);
135
136                         // Get a search result
137                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
138
139                         // Set the index "solver"
140                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
141
142                         // And finally set it
143                         $this->setResultInstance($resultInstance);
144                 } // END - if
145
146                 // Rewind it
147                 $this->getResultInstance()->rewind();
148
149                 // Search for it
150                 if ($this->getResultInstance()->next()) {
151                         // Entry found
152                         $exists = TRUE;
153                 } // END - if
154
155                 // Return the status
156                 return $exists;
157         }
158
159         /**
160          * Determines whether the email exists or not
161          *
162          * @return      $exists         Whether the email exists
163          */
164         public function ifEmailAddressExists () {
165                 // By default the email does not exist
166                 $exists = FALSE;
167
168                 // Is a previous result there?
169                 if (!$this->getResultInstance() instanceof SearchableResult) {
170                         // Get a UserDatabaseWrapper instance
171                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
172
173                         // Create a search criteria
174                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
175
176                         // Add the username as a criteria and set limit to one entry
177                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
178                         $criteriaInstance->setLimit(1);
179
180                         // Get a search result
181                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
182
183                         // Set the index "solver"
184                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
185
186                         // And finally set it
187                         $this->setResultInstance($resultInstance);
188                 } // END - if
189
190                 // Rewind it
191                 $this->getResultInstance()->rewind();
192
193                 // Search for it
194                 if ($this->getResultInstance()->next()) {
195                         // Entry found
196                         $exists = TRUE;
197
198                         // Is the username set?
199                         if ($this->getUserName() == '') {
200                                 // Get current entry
201                                 $currEntry = $this->getResultInstance()->current();
202
203                                 // Set the username
204                                 $this->setUserName($currEntry['username']);
205                         } // END - if
206                 } // END - if
207
208                 // Return the status
209                 return $exists;
210         }
211
212         /**
213          * Checks if supplied password hash in request matches with the stored in
214          * database.
215          *
216          * @param       $requestInstance        A requestable class instance
217          * @return      $matches                        Whether the supplied password hash matches
218          */
219         public function ifPasswordHashMatches (Requestable $requestInstance) {
220                 // By default nothing matches... ;)
221                 $matches = FALSE;
222
223                 // Is a previous result there?
224                 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
225                         // Get a UserDatabaseWrapper instance
226                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
227
228                         // Create a search criteria
229                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
230
231                         // Add the username as a criteria and set limit to one entry
232                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
233                         $criteriaInstance->setLimit(1);
234
235                         // Get a search result
236                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
237
238                         // Set the index "solver"
239                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
240
241                         // And finally set it
242                         $this->setResultInstance($resultInstance);
243                 } // END - if
244
245                 // Rewind it and advance to first entry
246                 $this->getResultInstance()->rewind();
247
248                 // This call set the result instance to a clean state
249                 $this->getResultInstance()->next();
250
251                 // Search for it
252                 if ($this->getResultInstance()->find('pass_hash')) {
253                         // So does the hashes match?
254                         //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
255                         $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
256                 } // END - if
257
258                 // Return the status
259                 return $matches;
260         }
261
262         /**
263          * "Getter" for user's password hash
264          *
265          * @return      $passHash       User's password hash from database result
266          */
267         public final function getPasswordHash () {
268                 // Default is missing password hash
269                 $passHash = NULL;
270
271                 // Get a database entry
272                 $entry = $this->getDatabaseEntry();
273
274                 // Is the password hash there?
275                 if (isset($entry['pass_hash'])) {
276                         // Get it
277                         $passHash = $entry['pass_hash'];
278                 } // END - if
279
280                 // And return the hash
281                 return $passHash;
282         }
283
284         /**
285          * Getter for primary key value
286          *
287          * @return      $primaryValue   Value of the primary key based on database type
288          */
289         public final function getPrimaryKey () {
290                 // Get a user database wrapper
291                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
292
293                 // Get the primary key back from the wrapper
294                 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
295
296                 // Get that field
297                 $primaryValue = $this->getField($primaryKey);
298
299                 // Return the value
300                 return $primaryValue;
301         }
302
303         /**
304          * Updates a given field with new value
305          *
306          * @param       $fieldName              Field to update
307          * @param       $fieldValue             New value to store
308          * @return      void
309          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
310          */
311         public function updateDatabaseField ($fieldName, $fieldValue) {
312                 // Get a critieria instance
313                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
314
315                 // Add search criteria
316                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
317                 $searchInstance->setLimit(1);
318
319                 // Now get another criteria
320                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
321
322                 // Add criteria entry which we shall update
323                 $updateInstance->addCriteria($fieldName, $fieldValue);
324
325                 // Add the search criteria for searching for the right entry
326                 $updateInstance->setSearchInstance($searchInstance);
327
328                 // Set wrapper class name
329                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
330
331                 // Remember the update in database result
332                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
333         }
334
335         /**
336          * Checks whether the user status is 'confirmed'
337          *
338          * @return      $isConfirmed    Whether the user status is 'confirmed'
339          */
340         public function isConfirmed () {
341                 // Determine it
342                 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
343
344                 // Return it
345                 return $isConfirmed;
346         }
347
348         /**
349          * Checks whether the user status is 'guest'
350          *
351          * @return      $isGuest        Whether the user status is 'guest'
352          */
353         public function isGuest () {
354                 // Determine it
355                 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));
356
357                 // Return it
358                 return $isGuest;
359         }
360 }
361
362 // [EOF]
363 ?>