3 * A generic class for handling guests
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class Guest extends BaseFrameworkSystem implements ManageableGuest, Registerable {
26 * Instance of the database result
28 private $resultInstance = null;
31 * Username of current user
33 private $userName = "";
36 * Email of current user
41 const EXCEPTION_USERNAME_NOT_FOUND = 0x700;
42 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x701;
43 const EXCEPTION_USER_PASS_MISMATCH = 0x702;
46 * Protected constructor
48 * @param $className Name of the class
51 protected function __construct ($className = "") {
52 // Is the class name empty? Then this is not a specialized user class
53 if (empty($className)) $className = __CLASS__;
55 // Call parent constructor
56 parent::__construct($className);
58 // Set part description
59 $this->setObjectDescription("Generic user class");
61 // Create unique ID number
62 $this->generateUniqueId();
65 $this->removeNumberFormaters();
66 $this->removeSystemArray();
70 * Creates an instance of this user class by a provided username. This
71 * factory method will check if the username is already taken and if not
72 * so it will throw an exception.
74 * @param $userName Username we need a class instance for
75 * @return $userInstance An instance of this user class
76 * @throws UsernameMissingException If the username does not exist
78 public final static function createGuestByUsername ($userName) {
80 $userInstance = new Guest();
83 $userInstance->setUserName($userName);
85 // Check if the username exists
86 if (!$userInstance->ifUsernameExists()) {
87 // Throw an exception here
88 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
91 // Return the instance
96 * Creates an instance of this user class by a provided email address. This
97 * factory method will not check if the email address is there.
99 * @param $email Email address of the user
100 * @return $userInstance An instance of this user class
102 public final static function createGuestByEmail ($email) {
103 // Get a new instance
104 $userInstance = new User();
107 $userInstance->setEmail($email);
109 // Return the instance
110 return $userInstance;
114 * "Getter" for databse entry
116 * @return $entry An array with database entries
117 * @throws NullPointerException If the database result is not found
118 * @throws InvalidDatabaseResultException If the database result is invalid
120 private function getDatabaseEntry () {
121 // Is there an instance?
122 if (is_null($this->resultInstance)) {
123 // Throw new exception
124 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
128 $this->resultInstance->rewind();
130 // Do we have an entry?
131 if (!$this->resultInstance->valid()) {
132 throw new InvalidDatabaseResultException(array($this, $this->resultInstance), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
136 $this->resultInstance->next();
139 $entry = $this->resultInstance->current();
146 * Setter for username
148 * @param $userName The username to set
151 public final function setUserName ($userName) {
152 $this->userName = $userName;
158 * @param $email The email to set
161 protected final function setEmail ($email) {
162 $this->email = $email;
166 * Getter for username
168 * @return $userName The username to get
170 public final function getUsername () {
171 return $this->userName;
177 * @return $email The email to get
179 public final function getEmail () {
184 * Determines wether the username exists or not
186 * @return $exists Wether the username exists
188 public function ifUsernameExists () {
189 // By default the username does not exist
192 // Is a previous result there?
193 if (is_null($this->resultInstance)) {
194 // Get a UserDatabaseWrapper instance
195 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
197 // Create a search criteria
198 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
200 // Add the username as a criteria and set limit to one entry
201 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
202 $criteriaInstance->setLimit(1);
204 // Get a search result
205 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
208 $this->resultInstance->rewind();
212 if ($this->resultInstance->next()) {
222 * Determines wether the email exists or not
224 * @return $exists Wether the email exists
226 public function ifEmailAddressExists () {
227 // By default the email does not exist
230 // Is a previous result there?
231 if (is_null($this->resultInstance)) {
232 // Get a UserDatabaseWrapper instance
233 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
235 // Create a search criteria
236 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
238 // Add the username as a criteria and set limit to one entry
239 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
240 $criteriaInstance->setLimit(1);
242 // Get a search resultInstance
243 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
246 $this->resultInstance->rewind();
250 if ($this->resultInstance->next()) {
260 * Checks if the supplied password hash in request matches with the stored
263 * @param $requestInstance A requestable class instance
264 * @return $matches Wether the supplied password hash matches
266 public function ifPasswordHashMatches (Requestable $requestInstance) {
267 // By default nothing matches... ;)
270 // Get a UserDatabaseWrapper instance
271 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
273 // Create a search criteria
274 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
276 // Add the username as a criteria and set limit to one entry
277 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
278 $criteriaInstance->setLimit(1);
280 // Get a search resultInstance
281 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
284 if ($this->resultInstance->next()) {
285 // Get the current entry (can only be one!)
286 $entry = $this->resultInstance->current();
288 // So does the hashes match?
289 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
290 $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
298 * Adds data for later complete update
300 * @param $column Column we want to update
301 * @param $value New value to store in database
305 public function addUpdateData ($column, $value) {
306 $this->partialStub("Column={$column}, value={$value}");
310 * "Getter" for user's password hash
312 * @return $passHash User's password hash from database result
314 public function getPasswordHash () {
315 // Default is missing password hash
318 // Get a database entry
319 $entry = $this->getDatabaseEntry();
321 // Is the password hash there?
322 if (isset($entry['pass_hash'])) {
324 $passHash = $entry['pass_hash'];
327 // And return the hash
332 * Updates the last activity timestamp and last performed action in the
333 * database result. You should call flushUpdates() to flush these updates
334 * to the database layer.
336 * @param $requestInstance A requestable class instance
339 public function updateLastActivity (Requestable $requestInstance) {
340 // No activity will be logged for guest accounts
344 * Flushs all updated entries to the database layer
348 public function flushUpdates () {
349 // No updates will be flushed to database!