All empty double-quoted strings replaced with single-quotes
[core.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 - 2009 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         // 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
30         /**
31          * Username of current user
32          */
33         private $userName = '';
34
35         /**
36          * User id of current user
37          */
38         private $userId = 0;
39
40         /**
41          * Email of current user
42          */
43         private $email = '';
44
45         /**
46          * Protected constructor
47          *
48          * @param       $className      Name of the class
49          * @return      void
50          */
51         protected function __construct ($className) {
52                 // Call parent constructor
53                 parent::__construct($className);
54
55                 // Clean up a little
56                 $this->removeNumberFormaters();
57                 $this->removeSystemArray();
58         }
59
60         /**
61          * Setter for username
62          *
63          * @param       $userName       The username to set
64          * @return      void
65          */
66         public final function setUserName ($userName) {
67                 $this->userName = (string) $userName;
68         }
69
70         /**
71          * Getter for username
72          *
73          * @return      $userName       The username to get
74          */
75         public final function getUserName () {
76                 return $this->userName;
77         }
78
79         /**
80          * Setter for user id
81          *
82          * @param       $userId         The user id to set
83          * @return      void
84          * @todo        Find a way of casting here. "(int)" might destroy the user id > 32766
85          */
86         public final function setUserId ($userId) {
87                 $this->userId = $userId;
88         }
89
90         /**
91          * Getter for user id
92          *
93          * @return      $userId The user id to get
94          */
95         public final function getUserId () {
96                 return $this->userId;
97         }
98
99         /**
100          * Setter for email
101          *
102          * @param       $email  The email to set
103          * @return      void
104          */
105         protected final function setEmail ($email) {
106                 $this->email = (string) $email;
107         }
108
109         /**
110          * Getter for email
111          *
112          * @return      $email  The email to get
113          */
114         public final function getEmail () {
115                 return $this->email;
116         }
117
118         /**
119          * Determines wether the username exists or not
120          *
121          * @return      $exists         Wether the username exists
122          */
123         public function ifUsernameExists () {
124                 // By default the username does not exist
125                 $exists = false;
126
127                 // Is a previous result there?
128                 if (is_null($this->getResultInstance())) {
129                         // Get a UserDatabaseWrapper instance
130                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
131
132                         // Create a search criteria
133                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
134
135                         // Add the username as a criteria and set limit to one entry
136                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
137                         $criteriaInstance->setLimit(1);
138
139                         // Get a search result
140                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
141
142                         // Set the index "solver"
143                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
144
145                         // And finally set it
146                         $this->setResultInstance($resultInstance);
147                 } // END - if
148
149                 // Rewind it
150                 $this->getResultInstance()->rewind();
151
152                 // Search for it
153                 if ($this->getResultInstance()->next()) {
154                         // Entry found
155                         $exists = true;
156                 } // END - if
157
158                 // Return the status
159                 return $exists;
160         }
161
162         /**
163          * Determines wether the email exists or not
164          *
165          * @return      $exists         Wether the email exists
166          */
167         public function ifEmailAddressExists () {
168                 // By default the email does not exist
169                 $exists = false;
170
171                 // Is a previous result there?
172                 if (is_null($this->getResultInstance())) {
173                         // Get a UserDatabaseWrapper instance
174                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
175
176                         // Create a search criteria
177                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
178
179                         // Add the username as a criteria and set limit to one entry
180                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
181                         $criteriaInstance->setLimit(1);
182
183                         // Get a search result
184                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
185
186                         // Set the index "solver"
187                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
188
189                         // And finally set it
190                         $this->setResultInstance($resultInstance);
191                 } // END - if
192
193                 // Rewind it
194                 $this->getResultInstance()->rewind();
195
196                 // Search for it
197                 if ($this->getResultInstance()->next()) {
198                         // Entry found
199                         $exists = true;
200
201                         // Is the username set?
202                         if ($this->getUserName() == '') {
203                                 // Get current entry
204                                 $currEntry = $this->getResultInstance()->current();
205
206                                 // Set the username
207                                 $this->setUserName($currEntry['username']);
208                         } // END - if
209                 } // END - if
210
211                 // Return the status
212                 return $exists;
213         }
214
215         /**
216          * Checks if supplied password hash in request matches with the stored in
217          * database.
218          *
219          * @param       $requestInstance        A requestable class instance
220          * @return      $matches                        Wether the supplied password hash matches
221          */
222         public function ifPasswordHashMatches (Requestable $requestInstance) {
223                 // By default nothing matches... ;)
224                 $matches = false;
225
226                 // Is a previous result there?
227                 if (is_null($this->getResultInstance())) {
228                         // Get a UserDatabaseWrapper instance
229                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
230
231                         // Create a search criteria
232                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
233
234                         // Add the username as a criteria and set limit to one entry
235                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
236                         $criteriaInstance->setLimit(1);
237
238                         // Get a search result
239                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
240
241                         // Set the index "solver"
242                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
243
244                         // And finally set it
245                         $this->setResultInstance($resultInstance);
246                 } // END - if
247
248                 // Rewind it
249                 $this->getResultInstance()->rewind();
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')."/".$entry['pass_hash'];
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          * @throws      DatabaseUpdateSupportException  If this class does not support database updates
310          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
311          */
312         public function updateDatabaseField ($fieldName, $fieldValue) {
313                 // Is updating database fields allowed by interface?
314                 if (!$this instanceof Updateable) {
315                         // Update not supported!
316                         throw new DatabaseUpdateSupportException($this, self::EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED);
317                 } // END - if
318
319                 // Get a critieria instance
320                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
321
322                 // Add search criteria
323                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
324                 $searchInstance->setLimit(1);
325
326                 // Now get another criteria
327                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
328
329                 // Add criteria entry which we shall update
330                 $updateInstance->addCriteria($fieldName, $fieldValue);
331
332                 // Add the search criteria for searching for the right entry
333                 $updateInstance->setSearchInstance($searchInstance);
334
335                 // Set wrapper class name
336                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
337
338                 // Remember the update in database result
339                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
340         }
341 }
342
343 // [EOF]
344 ?>