Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /********************************************************************************
- Class: DbConnection.
- *********************************************************************************/
- class DbConnection {
- /* Database connection. */
- public $DbConn = NULL;
- /* Database information */
- /*
- public $DbKey = NULL, // Database prefix.
- $DbHost = NULL, // Database hostname.
- $DbUser = NULL, // Database username.
- $DbPass = NULL, // Database password.
- $DbData = NULL; // Name of the database.
- */
- public $DbKey = "t_", // Database prefix.
- $DbHost = "localhost", // Database hostname.
- $DbUser = "test_user", // Database username.
- $DbPass = "test_pass", // Database password.
- $DbData = "test_db"; // Name of the database.
- /* Error message */
- private $DbError = NULL;
- /* Function: Default constructor */
- function __construct() {
- }
- /* End of Function: Default constructor */
- /* SET: Error message */
- function setDbError( $err ) {
- $this->DbError = $err;
- }
- /* End of SET: Error message */
- /* GET: Error message */
- function getDbError() {
- return $this->DbError;
- }
- /* End of GET: Error message */
- /* Function: Connect with the database */
- function DbConnect() {
- /* Try to connect with the database */
- $this->DbConn = mysql_connect( $this->DbHost, $this->DbUser, $this->DbPass );
- if( ! $this->DbConn ) {
- $this->setDbError( "DbConnection: 001." );
- return false;
- }
- /* Try to select the database */
- if( ! mysql_select_db( $this->DbData, $this->DbConn) ) {
- $this->setDbError( "DbConnection: 002." );
- return false;
- }
- return true;
- }
- /* End of Function: Connect with the database */
- /* Function: Close the database connection */
- function DbClose() {
- /* Try to close the database connection */
- if( $this->DbConn ) {
- mysql_close( $this->DbConn );
- }
- }
- /* End of Function: Close the database connection */
- /* Function: Executing database SQL query */
- function DbQuery( $sql ) {
- /* Try to connect with the database */
- if( ! $this->DbConnect() ) {
- return false;
- }
- /* Try to execute the SQL query */
- if( ! mysql_query( $sql ) ) {
- return false;
- }
- /* Close the database connection */
- $this->DbClose();
- return true;
- }
- /* End of Function: Executing database SQL query */
- /* Function: Grab the result(s) from the database using SQL query */
- function DbResults( $sql ) {
- /* Define Results */
- $results = NULL;
- /* Get the result(s) from the database using SQL query */
- try {
- $results = mysql_query( $sql );
- } catch ( Exception $ex ) {
- $this->setDbError( "DbConnection: 003." );
- return false;
- }
- /* Validate results */
- if( ! $results ) {
- $this->setDbError( "DbConnection: 006." );
- return false;
- }
- /* Return the results */
- return $results;
- }
- /* End of Function: Grab the result(s) from the database using SQL query */
- }
- /********************************************************************************
- Class: DbConnection.
- *********************************************************************************/
- /********************************************************************************
- Class: DbLogin.
- *********************************************************************************/
- class DbLogin extends DbConnection {
- /* User details */
- private $id = NULL, // User ID
- $user = NULL, // User name
- $pass = NULL, // User password
- $table = "users"; // Table name for the users
- /* Error message */
- private $error = NULL; // Error message
- /* Function: Default constructor */
- function __construct( $u = NULL, $p = NULL ) {
- $this->user = $u; // Set the username
- $this->pass = sha1( $p ); // Set the password
- }
- /* End of Function: Default constructor */
- /* SET: Error message */
- function setError( $err ) {
- $this->error = $err;
- }
- /* End of SET: Error message */
- /* GET: Error message */
- function getError() {
- return $this->error;
- }
- /* End of GET: Error message */
- /* GET: User ID */
- function getId() {
- return $this->id;
- }
- /* End of GET: User ID */
- /* GET: Username */
- function getUser() {
- return $this->user;
- }
- /* End of GET: Username */
- /* SET: User details */
- function setUserDetails( $results ) {
- /* Try to SET the ID and username */
- try {
- list( $id_, $u_ ) = mysql_fetch_array( $results );
- $this->id = $id_;
- $this->user = $u_;
- } catch ( Exception $ex ) {
- $this->setError( "DbLogin: 002." );
- return false;
- }
- return true;
- }
- /* End of SET: User details */
- /* Function: Check login */
- function login() {
- /* Try to connect with the database */
- if( ! $this->DbConnect() ) {
- return false;
- }
- /* SQL Query */
- $sql = "SELECT id, username, password FROM " .
- mysql_real_escape_string( $this->DbKey ) .
- $this->table . " WHERE username='" .
- mysql_real_escape_string( $this->user ) .
- "' AND password='" .
- mysql_real_escape_string( $this->pass ) . "'";
- /* Get the results from the database using the SQL query */
- $results = $this->DbResults( $sql );
- /* Validate results */
- if( ! $results ) {
- $this->DbClose(); // Close the database connection
- $this->setError( $this->getDbError() ); // Set the error message
- return false;
- }
- /* Get the count of the results */
- $flag = mysql_num_rows( $results );
- /* Validate the flag */
- if( $flag != 1 ) {
- $this->DbClose(); // Close the database connection
- $this->setError( "Incorect username or password!" ); // Set the error message
- return false;
- }
- /* Try to set the details of the user */
- if( ! $this->setUserDetails( $results ) ) {
- $this->DbClose(); // Close the database connection
- $this->setError( "DbLogin: 001." ); // Set the error message
- return false;
- }
- $this->DbClose(); // Close the database connection
- /* Successflly Login */
- return true;
- }
- /* End of Function: Check login */
- }
- /********************************************************************************
- Class: DbLogin.
- *********************************************************************************/
- class DbUsers extends DbConnection {
- function __construct() {
- }
- }
- class DbMenu extends DbConnection {
- function __construct() {
- }
- }
- class DbForums extends DbConnection {
- function __construct() {
- }
- }
- class DbSubForums extends DbConnection {
- function __construct() {
- }
- }
- class DbThemes extends DbConnection {
- function __construct() {
- }
- }
- class DbPost extends DbConnection {
- function __construct() {
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement