Location: PHPKode > projects > Php4dvd - movie database > php4dvd/lib/db/UserDataManager.class.php
<?php
/**
================================================================================ 
 LISENCE
================================================================================

    This file is part of php4dvd.

    php4dvd is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    php4dvd is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with php4dvd. If not, see <http://www.gnu.org/licenses/>.
**/

include_once($loc."/lib/objects/User.class.php");

class UserDataManager extends DataManager { 
	// Add a new user
	function add($User) {
		// Add user to the database
		$query  = "INSERT INTO `users` (email, username, password, permission, lastlogin) VALUES ";
		$query .= "('".addslashes($User->email)."', ";
		$query .= "'".addslashes($User->username)."', ";
		$query .= "'".addslashes($User->password)."', ";
		$query .= "'".addslashes($User->permission)."', ";
		$query .= "'".addslashes($User->lastlogin)."')";
		
		$this->db->insert($query);
		$User->id = $this->db->insertId();

		// Update the user object
		$User->update();

		return $User->id;
	}

	// Remove a user
	function remove($User) {
		// Query
		$query = "DELETE FROM `users` WHERE id = '".addslashes($User->id)."'";
		
		$this->db->delete($query);
	}

	// Get all users
	function getAllUsers($sort = "username") {
		// List to fill and return
		$return = array();
		
		// Query
		$query = "SELECT * FROM `users` ORDER BY ".addslashes($sort);
		
		if($rs = $this->db->select($query)) {
			// Create all users
			while($row = $rs->getNextRow()) {
				$User = new User();
				$User = Factory::FillObject($User, $row);
				$return[] = $User;
			}
		}
		
		// Return
		return $return;
	}
	
	// Get a user by its id
	function getUser($id) {
		// Query
		$query = "SELECT * FROM `users` WHERE id = '".addslashes($id)."'";

		if($rs = $this->db->select($query)) {
			// Create user
			if($row = $rs->getNextRow()) {
				$User = new User();
				$User = Factory::FillObject($User, $row);
				return $User;
			}
		}
		
		// Nothing found
		return false;
	}
	
	// Get a user by its username
	function getUserByName($username) {
		// Query
		$query = "SELECT * FROM `users` WHERE username = '".addslashes($username)."'";

		if($rs = $this->db->select($query)) {
			// Create user
			if($row = $rs->getNextRow()) {
				$User = new User();
				$User = Factory::FillObject($User, $row);
				return $User;
			}
		}
		
		// Nothing found
		return false;
	}
	
	// See if a user with this username already exists
	function existsUser($username, $email = "") {
		// By name
		$u = $this->getUserByName($username);
		if($u)
			return true;
		
		// By email
		if($email != "") {
			$u = $this->getUserByEmail($username);
			if($u)
				return true;
		}
		
		// Not found
		return false;
	}
	
	// Get a user by its email address
	function getUserByEmail($email) {
		// Query
		$query = "SELECT * FROM `users` WHERE email = '".addslashes($email)."'";

		if($rs = $this->db->select($query)) {
			// Create user
			if($row = $rs->getNextRow()) {
				$User = new User();
				$User = Factory::FillObject($User, $row);
				return $User;
			}
		}
		
		// Nothing found
		return false;
	}
}

?>
Return current item: Php4dvd - movie database