Advertisement
Guest User

Untitled

a guest
Apr 9th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. <?php
  2. define('__ROOT__', dirname((__FILE__)));
  3.  
  4. session_start();
  5. session_regenerate_id(true);
  6.  
  7. /**********************************************************************/
  8. // Configuraion //
  9. /**********************************************************************/
  10.  
  11. // SQL Database Connection
  12. $dbuser = 'root';
  13. $dbpass = '';
  14.  
  15. // Date settings
  16. date_default_timezone_set('Asia/Bishkek');
  17.  
  18. /**********************************************************************/
  19. // End configuration //
  20. /**********************************************************************/
  21.  
  22. try {
  23. //open the database
  24. $db = new PDO('mysql:host=localhost;dbname=test;charset=utf8', $dbuser, $dbpass);
  25. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  26. } catch(PDOException $e) {
  27. error_log('Database exception: '.$e->getMessage(), 1, $supportEmail);
  28. echo('Exception: '.$e->getMessage());
  29. }
  30.  
  31. function WifiAddUser($guest) {
  32. // Add a user to the database
  33. global $db;
  34.  
  35. try {
  36. // Insert a new user database
  37. $sql = "INSERT INTO guests (username, password, expires, notes, download, upload) VALUES (:username, :password, :expires, :notes, :download, :upload)";
  38. $sth = $db->prepare($sql);
  39. $sth->bindParam(':username', $guest['username'], PDO::PARAM_STR);
  40. $sth->bindParam(':password', $guest['password'], PDO::PARAM_STR);
  41. $sth->bindParam(':expires', $guest['expires'], PDO::PARAM_INT);
  42. $sth->bindParam(':notes', $guest['notes'], PDO::PARAM_STR);
  43. $sth->bindParam(':download', $guest['download'], PDO::PARAM_INT);
  44. $sth->bindParam(':upload', $guest['upload'], PDO::PARAM_INT);
  45. $sth->execute();
  46. } catch(PDOException $e) {
  47. error_log('addUser exception: '.$guest['username'].' Message: '.$e->getMessage(), 0);
  48. return false;
  49. }
  50. // Everything went okay
  51. return true;
  52. }
  53.  
  54. function WifiRemoveUser($id) {
  55. global $db;
  56.  
  57. try {
  58. $sql = 'DELETE FROM guests WHERE id=:id';
  59. $sth = $db->prepare($sql);
  60. $sth->bindParam(':id', $id, PDO::PARAM_STR);
  61. $sth->execute();
  62. } catch(PDOException $e) {
  63. error_log('removeUser exception: '.$id.' Message: '.$e->getMessage(), 0);
  64. return false;
  65. }
  66. // Everything went okay
  67. return true;
  68. }
  69.  
  70. function WifiGetGuests() {
  71. global $db;
  72.  
  73. try {
  74.  
  75. // Select all the users from the database
  76. $sql = 'SELECT * FROM guests';
  77. $result = $db->query($sql);
  78. $db = NULL; // Close the connection
  79. } catch(PDOException $e) {
  80. error_log('GetGuests exception: '.$e->getMessage(), 0);
  81. $db = NULL; // Close the connection
  82. }
  83. return $result;
  84. }
  85.  
  86. function authorizeCMS($username, $password) {
  87. global $db;
  88.  
  89. try {
  90. // See if user is in the database
  91. $sth = $db->prepare('SELECT id, password FROM accounts WHERE username = :username');
  92. $sth->bindParam(':username', $username, PDO::PARAM_STR);
  93. $sth->execute();
  94. $result = $sth->fetch();
  95. $db = NULL; // Close the connection
  96. } catch(PDOException $e) {
  97. error_log('authorizeCMS exception: '.$e->getMessage(), 0);
  98. return false;
  99. }
  100.  
  101. $id = $result['id'];
  102.  
  103. // See if the user has a valid password and remaining time
  104. if (password_verify($password, $result['password']))
  105. {
  106. return $id;
  107. }
  108. // Always return false if something goes wrong
  109. return false;
  110. }
  111.  
  112. function GetName($id) {
  113. global $db;
  114.  
  115. try {
  116. // See if user is in the database
  117. $sth = $db->prepare('SELECT name FROM accounts WHERE id = :id');
  118. $sth->bindParam(':id', $id, PDO::PARAM_INT);
  119. $sth->execute();
  120. $result = $sth->fetch();
  121. $db = NULL; // Close the connection
  122. } catch(PDOException $e) {
  123. error_log('GetName exception: '.$e->getMessage(), 0);
  124. return false;
  125. }
  126.  
  127. $name = $result['name'];
  128.  
  129. return $name;
  130. }
  131.  
  132. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement