Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.24 KB | None | 0 0
  1. <?php
  2.  
  3.     /********************************************************************************
  4.         Class: DbConnection.
  5.     *********************************************************************************/
  6.     class DbConnection {
  7.        
  8.         /* Database connection. */
  9.         public  $DbConn     =   NULL;
  10.        
  11.         /* Database information */
  12.         /*
  13.         public  $DbKey      =   NULL,   // Database prefix.
  14.                 $DbHost     =   NULL,   // Database hostname.
  15.                 $DbUser     =   NULL,   // Database username.
  16.                 $DbPass     =   NULL,   // Database password.
  17.                 $DbData     =   NULL;   // Name of the database.
  18.         */
  19.                
  20.         public  $DbKey      =   "t_",   // Database prefix.
  21.                 $DbHost     =   "localhost",    // Database hostname.
  22.                 $DbUser     =   "test_user",    // Database username.
  23.                 $DbPass     =   "test_pass",    // Database password.
  24.                 $DbData     =   "test_db";  // Name of the database.
  25.                
  26.         /* Error message */
  27.         private $DbError    =   NULL;
  28.        
  29.        
  30.        
  31.         /* Function: Default constructor */
  32.         function __construct() {
  33.            
  34.         }
  35.         /* End of Function: Default constructor */
  36.        
  37.        
  38.        
  39.        
  40.         /* SET: Error message */
  41.         function setDbError( $err ) {
  42.             $this->DbError = $err;
  43.         }
  44.         /* End of SET: Error message */
  45.        
  46.         /* GET: Error message */
  47.         function getDbError() {
  48.             return $this->DbError;
  49.         }
  50.         /* End of GET: Error message */
  51.        
  52.        
  53.        
  54.         /* Function: Connect with the database */
  55.         function DbConnect() {
  56.            
  57.             /* Try to connect with the database */
  58.             $this->DbConn = mysql_connect( $this->DbHost, $this->DbUser, $this->DbPass );
  59.             if( ! $this->DbConn ) {
  60.                 $this->setDbError( "DbConnection: 001." );
  61.                 return false;
  62.             }
  63.            
  64.             /* Try to select the database */
  65.             if( ! mysql_select_db( $this->DbData, $this->DbConn) ) {
  66.                 $this->setDbError( "DbConnection: 002." );
  67.                 return false;
  68.             }
  69.            
  70.             return true;
  71.         }
  72.         /* End of Function: Connect with the database */
  73.        
  74.        
  75.        
  76.         /* Function: Close the database connection */
  77.         function DbClose() {
  78.            
  79.             /* Try to close the database connection */
  80.             if( $this->DbConn ) {
  81.                 mysql_close( $this->DbConn );
  82.             }
  83.            
  84.         }
  85.         /* End of Function: Close the database connection */
  86.        
  87.        
  88.        
  89.        
  90.         /* Function: Executing database SQL query */
  91.         function DbQuery( $sql ) {
  92.            
  93.             /* Try to connect with the database */
  94.             if( ! $this->DbConnect() ) {
  95.                 return false;
  96.             }
  97.            
  98.             /* Try to execute the SQL query */
  99.             if( ! mysql_query( $sql ) ) {
  100.                 return false;
  101.             }
  102.            
  103.             /* Close the database connection */
  104.             $this->DbClose();
  105.            
  106.             return true;
  107.         }
  108.         /* End of Function: Executing database SQL query */
  109.        
  110.        
  111.        
  112.        
  113.         /* Function: Grab the result(s) from the database using SQL query */
  114.         function DbResults( $sql ) {
  115.            
  116.             /* Define Results */
  117.             $results = NULL;
  118.            
  119.             /* Get the result(s) from the database using SQL query */
  120.             try {
  121.                 $results = mysql_query( $sql );
  122.             } catch (  Exception $ex ) {
  123.                 $this->setDbError( "DbConnection: 003." );
  124.                 return false;
  125.             }
  126.            
  127.             /* Validate results */
  128.             if( ! $results ) {
  129.                 $this->setDbError( "DbConnection: 006." );
  130.                 return false;
  131.             }
  132.            
  133.             /* Return the results */
  134.             return $results;
  135.         }
  136.         /* End of Function: Grab the result(s) from the database using SQL query */
  137.        
  138.     }
  139.     /********************************************************************************
  140.         Class: DbConnection.
  141.     *********************************************************************************/
  142.    
  143.    
  144.    
  145.    
  146.     /********************************************************************************
  147.         Class: DbLogin.
  148.     *********************************************************************************/
  149.     class DbLogin extends DbConnection {
  150.        
  151.         /* User details */
  152.         private $id     =   NULL,           // User ID
  153.                 $user   =   NULL,           // User name
  154.                 $pass   =   NULL,           // User password
  155.                 $table  =   "users";        // Table name for the users
  156.                
  157.         /* Error message */
  158.         private $error  =   NULL;       // Error message
  159.                
  160.        
  161.         /* Function: Default constructor */
  162.         function __construct( $u = NULL, $p = NULL ) {
  163.            
  164.             $this->user = $u;   // Set the username
  165.             $this->pass = sha1( $p );   // Set the password
  166.            
  167.         }
  168.         /* End of Function: Default constructor */
  169.        
  170.        
  171.        
  172.         /* SET: Error message */
  173.         function setError( $err ) {
  174.             $this->error = $err;
  175.         }
  176.         /* End of SET: Error message */
  177.        
  178.        
  179.         /* GET: Error message */
  180.         function getError() {
  181.             return $this->error;
  182.         }
  183.         /* End of GET: Error message */
  184.        
  185.        
  186.         /* GET: User ID */
  187.         function getId() {
  188.             return $this->id;
  189.         }
  190.         /* End of GET: User ID */
  191.        
  192.        
  193.         /* GET: Username */
  194.         function getUser() {
  195.             return $this->user;
  196.         }
  197.         /* End of GET: Username */
  198.        
  199.        
  200.        
  201.         /* SET: User details */
  202.         function setUserDetails( $results ) {
  203.            
  204.             /* Try to SET the ID and username */
  205.             try {
  206.                 list( $id_, $u_ ) = mysql_fetch_array( $results );
  207.                 $this->id = $id_;
  208.                 $this->user = $u_;
  209.             } catch ( Exception $ex ) {
  210.                 $this->setError( "DbLogin: 002." );
  211.                 return false;
  212.             }
  213.            
  214.             return true;
  215.            
  216.         }
  217.         /* End of SET: User details */
  218.        
  219.        
  220.         /* Function: Check login */
  221.         function login() {
  222.            
  223.             /* Try to connect with the database */
  224.             if( ! $this->DbConnect() ) {
  225.                 return false;
  226.             }
  227.            
  228.             /* SQL Query */
  229.             $sql = "SELECT id, username, password FROM " .
  230.                     mysql_real_escape_string( $this->DbKey ) .
  231.                     $this->table . " WHERE username='" .
  232.                     mysql_real_escape_string( $this->user ) .
  233.                     "' AND password='" .
  234.                     mysql_real_escape_string( $this->pass ) . "'";
  235.                    
  236.             /* Get the results from the database using the SQL query */
  237.             $results = $this->DbResults( $sql );
  238.            
  239.             /* Validate results */
  240.             if( ! $results ) {
  241.                 $this->DbClose();                           // Close the database connection
  242.                 $this->setError( $this->getDbError() );     // Set the error message
  243.                 return false;
  244.             }
  245.            
  246.             /* Get the count of the results */
  247.             $flag = mysql_num_rows( $results );
  248.            
  249.             /* Validate the flag */
  250.             if( $flag != 1 ) {
  251.                 $this->DbClose();                                           // Close the database connection
  252.                 $this->setError( "Incorect username or password!" );        // Set the error message
  253.                 return false;
  254.             }
  255.            
  256.             /* Try to set the details of the user */
  257.             if( ! $this->setUserDetails( $results ) ) {
  258.                 $this->DbClose();                       // Close the database connection
  259.                 $this->setError( "DbLogin: 001." );     // Set the error message
  260.                 return false;
  261.             }
  262.            
  263.             $this->DbClose();           // Close the database connection
  264.            
  265.             /* Successflly Login */
  266.             return true;
  267.         }
  268.         /* End of Function: Check login */
  269.        
  270.     }
  271.     /********************************************************************************
  272.         Class: DbLogin.
  273.     *********************************************************************************/
  274.    
  275.    
  276.    
  277.    
  278.    
  279.     class DbUsers extends DbConnection {
  280.        
  281.         function __construct() {
  282.            
  283.         }
  284.        
  285.     }
  286.    
  287.    
  288.    
  289.    
  290.     class DbMenu extends DbConnection {
  291.        
  292.         function __construct() {
  293.            
  294.         }
  295.        
  296.     }
  297.    
  298.    
  299.    
  300.    
  301.     class DbForums extends DbConnection {
  302.        
  303.         function __construct() {
  304.            
  305.         }
  306.        
  307.     }
  308.    
  309.    
  310.    
  311.    
  312.     class DbSubForums extends DbConnection {
  313.        
  314.         function __construct() {
  315.            
  316.         }
  317.        
  318.     }
  319.    
  320.    
  321.    
  322.    
  323.     class DbThemes extends DbConnection {
  324.        
  325.         function __construct() {
  326.            
  327.         }
  328.        
  329.     }
  330.    
  331.    
  332.    
  333.    
  334.     class DbPost extends DbConnection {
  335.        
  336.         function __construct() {
  337.            
  338.         }
  339.        
  340.     }
  341.  
  342. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement