Advertisement
Guest User

a

a guest
May 25th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. <?php
  2. class Database {
  3.  
  4. private $con;
  5.  
  6. private $host;
  7. private $user;
  8. private $pass;
  9. private $data;
  10.  
  11. public function __construct($host, $user, $pass, $data) {
  12. $this->host = $host;
  13. $this->user = $user;
  14. $this->pass = $pass;
  15. $this->data = $data;
  16. }
  17.  
  18. public function connect() {
  19. try {
  20. $this->con = new PDO('mysql:host='.$this->host.';dbname='.$this->data.';charset=utf8', $this->user, $this->pass);
  21. $this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  22. $this->con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  23. return true;
  24. } catch (Exception $e) {
  25. echo 'Unable to connect to database. Please check credentials and try again.';
  26. return false;
  27. }
  28. }
  29.  
  30. public function getSites() {
  31. $stmt = $this->con->prepare("SELECT * FROM fx_sites WHERE active=1");
  32. $stmt->execute();
  33. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  34. }
  35.  
  36. public function getAllSites() {
  37. $stmt = $this->con->prepare("SELECT * FROM fx_sites");
  38. $stmt->execute();
  39. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  40. }
  41.  
  42. public function getSite($value) {
  43. $stmt = $this->con->prepare("SELECT * FROM fx_sites WHERE id=:value LIMIT 1");
  44. $stmt->bindParam(":value", $value);
  45. $stmt->execute();
  46. return $stmt->fetch(PDO::FETCH_ASSOC);
  47. }
  48.  
  49. public function getSiteByIp($value) {
  50. $stmt = $this->con->prepare("SELECT * FROM fx_sites WHERE ip_address=:value LIMIT 1");
  51. $stmt->bindParam(":value", $value);
  52. $stmt->execute();
  53. return $stmt->fetch(PDO::FETCH_ASSOC);
  54. }
  55.  
  56. public function getSiteBy($column, $value) {
  57. $stmt = $this->con->prepare("SELECT * FROM fx_sites WHERE $column=:value LIMIT 1");
  58. $stmt->bindParam(":value", $value);
  59. $stmt->execute();
  60. return $stmt->fetch(PDO::FETCH_ASSOC);
  61. }
  62.  
  63. public function getVoteStats() {
  64. $stmt = $this->con->prepare("SELECT COUNT(*) AS amount, MONTH(callback_date) AS month FROM fx_votes WHERE callback_date IS NOT NULL AND YEAR(callback_date) = YEAR(CURDATE()) GROUP BY month ORDER BY month ASC");
  65. $stmt->execute();
  66. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  67. }
  68.  
  69. public function deleteSite($id) {
  70. $stmt = $this->con->prepare("DELETE FROM fx_sites WHERE id=:id LIMIT 1");
  71. $stmt->bindParam(":id", $id);
  72. $stmt->execute();
  73. }
  74.  
  75. public function setActive($id, $active) {
  76. $stmt = $this->con->prepare("UPDATE fx_sites SET active=:active WHERE id=:id LIMIT 1");
  77. $stmt->bindParam(":id", $id);
  78. $stmt->bindParam(":active", $active);
  79. $stmt->execute();
  80. }
  81.  
  82. public function addSite($data) {
  83. $stmt = $this->con->prepare("INSERT INTO fx_sites (title, url, site_id) VALUES (:title, :url, :site_id)");
  84. foreach ($data as $key => $value) {
  85. $stmt->bindParam($key, $value);
  86. }
  87. $stmt->execute();
  88. }
  89.  
  90. public function startVote($siteId, $username, $ip, $uid) {
  91. $stmt = $this->con->prepare("INSERT INTO fx_votes (username, site_id, ip_address, uid) VALUES (:user, :sid, :addr, :uid)");
  92. $stmt->bindParam(":user", $username);
  93. $stmt->bindParam(":sid", $siteId);
  94. $stmt->bindParam(":addr", $ip);
  95. $stmt->bindParam(":uid", $uid);
  96. $stmt->execute();
  97. }
  98.  
  99. public function getMostRecentVote($siteId, $username, $addr) {
  100. $stmt = $this->con->prepare("SELECT * FROM fx_votes WHERE site_id=:sid AND (username=:user OR ip_address=:addr) AND started > DATE_SUB(now(), INTERVAL 12 HOUR) AND callback_date IS NULL ORDER BY started DESC LIMIT 1");
  101. $stmt->bindParam(":user", $username);
  102. $stmt->bindParam(":sid", $siteId);
  103. $stmt->bindParam(":addr", $addr);
  104. $stmt->execute();
  105. return $stmt->fetch(PDO::FETCH_ASSOC);
  106. }
  107.  
  108. public function getVote($siteId, $username, $addr) {
  109. $stmt = $this->con->prepare("SELECT * FROM fx_votes WHERE site_id=:sid AND username=:user AND callback_date > DATE_SUB(now(), INTERVAL 12 HOUR) AND callback_date IS NOT NULL");
  110. $stmt->bindParam(":user", $username);
  111. $stmt->bindParam(":sid", $siteId);
  112. $stmt->execute();
  113. return $stmt->fetch(PDO::FETCH_ASSOC);
  114. }
  115.  
  116. public function getVotesById($siteId) {
  117. $stmt = $this->con->prepare("SELECT COUNT(*) FROM fx_votes WHERE site_id=:sid AND callback_date IS NOT NULL");
  118. $stmt->bindParam(":sid", $siteId);
  119. $stmt->execute();
  120. return $stmt->fetchColumn();
  121. }
  122.  
  123. public function getVoteByUid($uid) {
  124. $stmt = $this->con->prepare("SELECT * FROM fx_votes WHERE uid=:uid");
  125. $stmt->bindParam(":uid", $uid);
  126. $stmt->execute();
  127. return $stmt->fetch(PDO::FETCH_ASSOC);
  128. }
  129.  
  130. public function insertVote($uid) {
  131. $stmt = $this->con->prepare("UPDATE fx_votes SET callback_date = NOW() WHERE uid=:uid AND started > DATE_SUB(now(), INTERVAL 12 HOUR) AND callback_date IS NULL");
  132. $stmt->bindParam(":uid", $uid);
  133. $stmt->execute();
  134. }
  135.  
  136. public function getVotm() {
  137. $stmt = $this->con->prepare("SELECT COUNT(*) AS votes,username FROM fx_votes WHERE YEAR(callback_date) = YEAR(CURDATE()) AND MONTH(callback_date) = MONTH(CURDATE()) GROUP BY username ORDER BY votes DESC LIMIT 5");
  138. $stmt->execute();
  139. return $stmt->fetch(PDO::FETCH_ASSOC);
  140. }
  141.  
  142. public function getVotes() {
  143. $stmt = $this->con->prepare("SELECT COUNT(*) AS votes FROM fx_votes WHERE YEAR(callback_date) = YEAR(CURDATE()) AND MONTH(callback_date) = MONTH(CURDATE()) ORDER BY votes DESC LIMIT 5");
  144. $stmt->execute();
  145. return $stmt->fetch(PDO::FETCH_ASSOC);
  146. }
  147.  
  148. public function updateSite($id, $title, $voteId, $url) {
  149. $stmt = $this->con->prepare("UPDATE fx_sites SET title=:title, url=:url, site_id=:sid WHERE id=:id");
  150. $stmt->execute(array(
  151. "title" => $title,
  152. "url" => $url,
  153. "sid" => $voteId,
  154. "id" => $id
  155. ));
  156. }
  157.  
  158. public function insert($table, $vars) {
  159. $keys = array_keys($vars);
  160. $query = "INSERT INTO $table (";
  161. for ($i = 0; $i < count($keys); $i++) {
  162. $query .= ''.$keys[$i].($i < count($keys) - 1 ? ", " : ") VALUES (");
  163. }
  164. for ($i = 0; $i < count($keys); $i++) {
  165. $query .= ':'.$keys[$i].($i < count($keys) - 1 ? ", " : ")");
  166. }
  167. $stmt = $this->con->prepare($query);
  168. $stmt->execute($vars);
  169. }
  170.  
  171. public function update($table, $key, $vars) {
  172. $keys = array_keys($vars);
  173. $query = "UPDATE $table SET ";
  174. for ($i = 0; $i < count($keys); $i++) {
  175. $query .= "".$keys[$i]."=:".$keys[$i]."".($i < count($keys) - 1 ? ", " : " WHERE id=$key");
  176. }
  177. $stmt = $this->con->prepare($query);
  178. $stmt->execute($vars);
  179. }
  180. }
  181. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement