Guest User

Untitled

a guest
Feb 14th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. <?php
  2. /**
  3. * class dbc
  4. * @package Core
  5. *
  6. */
  7.  
  8. // Sanity check. Does this class already exist?
  9. if(!class_exists('dbc')) {
  10. class dbc {
  11. public function __construct() {
  12. // Make the constants class variables
  13. $this->host = host;
  14. $this->username = username;
  15. $this->password = password;
  16. $this->database = database;
  17. $this->charset = charset;
  18.  
  19. $this->connection = new mysqli($this->host, $this->username, $this->password, $this->database);
  20.  
  21. // Database connection error?
  22. if($this->checkConnection() == false) {
  23. die('Database connection error! <br />' . $this->connection->connect_error);
  24. return false;
  25. }
  26.  
  27. $this->connection->set_charset(charset);
  28. }
  29.  
  30. // Close the MySQLi connection
  31. public function __destruct() {
  32. if($this->connection) {
  33. $this->connection->close();
  34. }
  35. }
  36.  
  37. public function select($columns = array(), $table, $variables = '', $order = '') {
  38. // Are the required variables empty or equals null?
  39. if(empty($columns) || empty($table)) {
  40. return false;
  41. }
  42.  
  43. // Set $order and $variables to false
  44. if($order == '' || $order == null) {
  45. $order = false;
  46. }else if($variables == '' || $variables == null) {
  47. $variables = false;
  48. }
  49.  
  50. // Check if $columns is an array
  51. if(!is_array($columns)) {
  52. return false;
  53. }
  54.  
  55. // Set $fields as an array
  56. $fields = array();
  57. // Individualise the array
  58. foreach($columns as $field) {
  59. $fields[] = $field;
  60. }
  61.  
  62. // Sepeate all individuals with commas
  63. $fields = implode(', ', $fields);
  64.  
  65. // Finalise the MySQLi query
  66. if($variables == false && $order == false) {
  67. $query = "SELECT $fields FROM $table";
  68. }else if($variables != false && $order == false) {
  69. $query = "SELECT $fields FROM $table WHERE $variables";
  70. }else if($variables == false && $order != false) {
  71. $query = "SELECT $fields FROM $table ORDER BY $order";
  72. }else if($variables != false && $order != false) {
  73. $query = "SELECT $fields FROM $table WHERE $variables ORDER BY $order";
  74. }
  75.  
  76. // Prepare the query for executing
  77. $stmt = $this->connection->prepare($query);
  78.  
  79. // Execute the query
  80. $stmt->execute();
  81.  
  82. // Get the results of the query
  83. $result = $stmt->get_result();
  84.  
  85. // Set the results to a variable
  86. while($row = $result->fetch_assoc()) {
  87. $results[] = $row;
  88. }
  89.  
  90. // Return the results as array 0
  91. return $results[0];
  92. }
  93.  
  94. public function insert($table, $variables = array()) {
  95. // Are the required variables empty or equals null?
  96. if(empty($table) || empty($variables)) {
  97. return false;
  98. }
  99.  
  100. // Set both variables to an array
  101. $fields = array();
  102. $values = array();
  103. // Divide each value as a singular value
  104. foreach($variables as $field => $value) {
  105. $fields[] = $field;
  106. $values[] = "'" . $value . "'";
  107. }
  108.  
  109. // Split the fields & values with commas - to follow the mysqli query format
  110. $fields = '(' . implode(', ', $fields) . ')';
  111. $values = '(' . implode(', ', $values) . ')';
  112.  
  113. // Finalised Query
  114. $query = " INSERT INTO $table $fields VALUES $values";
  115.  
  116. // Prepare the query for executing
  117. $stmt = $this->connection->prepare($query);
  118.  
  119. if(false === $stmt) {
  120. return false;
  121. }
  122.  
  123. // Execute the prepared query
  124. $stmt->execute();
  125. }
  126.  
  127. public function checkConnection() {
  128. if($this->connection->connect_errno) {
  129. // Connection Unsucessful
  130. return false;
  131. }else {
  132. // Connection Successful
  133. return true;
  134. }
  135. }
  136.  
  137. public function affected() {
  138. return $this->connection->num_rows;
  139. }
  140. }
  141. }
  142.  
  143. ?>
  144.  
  145. <?php
  146.  
  147. // Database Connection Information (removed for security)
  148. define('host', '');
  149. define('username', '');
  150. define('password', '');
  151. define('database', '');
  152.  
  153. // Show PHP/MySQLi errors?
  154. define('DEBUG_MODE', true);
  155.  
  156. define('charset', 'UTF-8');
  157.  
  158. define('default_user_group', '1')
  159.  
  160. ?>
  161.  
  162. <?php
  163. /**
  164. * @package Core
  165. */
  166.  
  167. if(!class_exists('user')) {
  168. class user {
  169. public function __construct() {}
  170.  
  171. public function register($username, $password, $confirmpassword, $email, $firstname, $lastname) {
  172. if(empty($username) || empty($password) || empty($confirmpassword) || empty($email) || empty($firstname) || empty($lastname)) {
  173. return false;
  174. }
  175.  
  176. if($password === $confirmpassword) {
  177. $dbc = new dbc();
  178.  
  179. $hash = '_HG.g2Sxa.';
  180. $encryptpassword = md5(md5($hash . $password . $hash));
  181.  
  182. $status = default_user_group;
  183. if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
  184. $ip = $_SERVER['HTTP_CLIENT_IP'];
  185. }else if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  186. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  187. }else {
  188. $ip = $_SERVER['REMOVE_ADDR'];
  189. }
  190.  
  191. $ip = ip2long($ip);
  192.  
  193. $userData = array(
  194. 'username' => $username,
  195. 'password' => $encryptpassword,
  196. 'email' => $email,
  197. 'firstname' => $firstname,
  198. 'lastname' => $lastname,
  199. 'status' => $status,
  200. 'ip' => $ip
  201. );
  202.  
  203. $createUser = $dbc->insert('users', $userData);
  204. }
  205. }
  206. }
  207. }
  208. ?>
Add Comment
Please, Sign In to add comment