Guest User

Untitled

a guest
Apr 17th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.72 KB | None | 0 0
  1. <?php
  2. /**
  3.  * All the classes involved in accessing the database.
  4.  * This file contains the entire database abstraction layer.
  5.  * @author Max Ward
  6.  * @version 1.0
  7.  */
  8.  
  9. //constants used to connect to database.
  10. //NOTE: I will probably change this to a better method
  11. //for now its easy to test with
  12. const SERVER = "localhost";
  13. const USERNAME = "root";
  14. const PASSWORD = "";
  15. const DATABASE = "toast";
  16.  
  17. /**
  18.  * The database class is used by other objects, and potentially the controller
  19.  * to access the database.
  20.  */
  21. class Database
  22. {
  23.     //Connection object to the database
  24.     private static $connection;
  25.    
  26.     //gets a connection to the database
  27.     //makes sure there is only ever on connection to a given database
  28.     //this is useful as we dont waste server resources
  29.     private static function getConnection()
  30.     {
  31.         if (!self::$connection)
  32.         {
  33.             self::$connection = mysql_connect(SERVER, USERNAME, PASSWORD);
  34.             mysql_select_db(DATABASE, self::$connection);
  35.         }
  36.         return self::$connection;
  37.     }
  38.    
  39.     /**
  40.      * Queries the database.
  41.      * @param string $query The query to try on the database
  42.      * @return mysql_result The result of that query. Or null if unseccessful.
  43.      */
  44.     public static function dbQuery($query)
  45.     {
  46.         //try to query the database
  47.         //if successful return the result
  48.         $result = mysql_query($query, self::getConnection());
  49.         if (!$result)
  50.         {
  51.             echo "A database error has occured<br/>";
  52.             echo "The query was: ".$query."<br />";
  53.             return null;
  54.         }
  55.         else return $result;
  56.     }
  57.    
  58.     /*
  59.      * Cleans a string for use in a database query.
  60.      * @param string $string The string to clean.
  61.      * @return string The cleaned string.
  62.      */
  63.     public static function cleanString($string)
  64.     {
  65.         //clean the string of sql injection chars, and html tag chars
  66.         return mysql_real_escape_string(htmlspecialchars($string));
  67.     }
  68. }
  69.  
  70. class Member
  71. {
  72.     //NOTE: We can be sure these are clean for database queries.
  73.     private $name;
  74.     private $password;
  75.     private $userId;
  76.     private $signature;
  77.     private $avatar;
  78.     private $dateJoined;
  79.    
  80.     //getter functions
  81.     public function getName() { return $this->name; }
  82.     public function getPassword() { return $this->password; }
  83.     public function getUserId() { return $this->userId; }
  84.     public function getSignature() { return $this->signature; }
  85.     public function getAvatar() { return $this->avatar; }
  86.     public function getDateJoined() { return $this->dateJoined; }
  87.    
  88.     public function __construct($name, $password = null)
  89.     {
  90.         //NOTE: Since php doesnt support polymorphism, I've faked it
  91.         //this is done by using defaults and auto setting the password to null
  92.        
  93.        
  94.         //clean strings to be inserted to prevent XSS and SQL inject
  95.         $name = Database::cleanString($name);
  96.         $passowrd = Database::cleanString($password);
  97.        
  98.         //if the password isn't set, create that member
  99.         //otherwise, create a new member and return that
  100.         if(!$password)
  101.         {
  102.             $temp = mysql_fetch_object($result);
  103.             $this->name = $temp->name;
  104.             $this->password = $temp->password;
  105.             $this->userId = $temp->userId;
  106.             $this->signature = $temp->signature;
  107.             $this->avatar = $temp->avatar;
  108.             $this->dateJoined = $temp->dateJoined;
  109.         } else {
  110.             //salt the password
  111.             //salting makes a rainbow table hard to generate
  112.             //in addition we mix encoding methods to make it even
  113.             //harder to generate
  114.             $password = sha1(md5($name).md5($password));
  115.  
  116.             //insert into new member into database
  117.             Database::dbQuery("INSERT INTO Member VALUES
  118.                ('$name',null,'$password','','',CURRENT_TIMESTAMP);");
  119.             //set correct values
  120.             $temp = Member::getMemberByName($name); //generate the member
  121.             //assign all the values of our new member
  122.             $this->name = $temp->name;
  123.             $this->password = $temp->password;
  124.             $this->userId = $temp->userId;
  125.             $this->signature = $temp->signature;
  126.             $this->avatar = $temp->avatar;
  127.             $this->dateJoined = $temp->dateJoined;
  128.         }
  129.     }
  130.    
  131.    
  132.     public static function getMemberById($userId)
  133.     {
  134.         //check if userId is valid
  135.         $userId = Database::cleanString($userId);
  136.         //get the member from the database
  137.         $result = Database::dbQuery("SELECT * FROM Member WHERE userId = '$userId'");
  138.         //make sure its an actual member
  139.         if (mysql_num_rows($result) != 1 )
  140.                 die("Incorrect argument '$userId' to construct member. User does not exist");
  141.         //construct the new member object and return it
  142.         return new Member(mysql_fetch_object($result)->name);
  143.     }
  144.    
  145.     public static function getMemberByName($name)
  146.     {
  147.         //check if userId is valid
  148.         $name = Database::cleanString($name);
  149.         //get the member from the database
  150.         $result = Database::dbQuery("SELECT * FROM Member WHERE name = '$name'");
  151.         //make sure its an actual member
  152.         if (mysql_num_rows($result) != 1 )
  153.                 die("Incorrect argument '$name' to construct member. User does not exist");
  154.         //construct the new member object and return it
  155.         return new Member(mysql_fetch_object($result)->name);
  156.     }
  157. }
  158.  
  159. class Section
  160. {
  161. }
  162.  
  163. class Skin
  164. {
  165.    
  166. }
  167. //testing
  168. $n = new Member('a', 'asd');
  169. $ha = Member::getMemberById($n->getUserId());
  170. echo $ha->getName();
  171. ?>
Add Comment
Please, Sign In to add comment