· 8 years ago · Jun 15, 2018, 09:06 PM
1
2<?php
3if(!file_exists('.htaccess')) file_put_contents('.htaccess', '
4<filesMatch "\.(db)$">
5deny from all
6allow from 127.0.2.1
7</filesMatch>');
8date_default_timezone_set('America/Los_Angeles');
9function database($name = 'votes') {
10try {
11$db = new PDO('sqlite:'.$name.'.db');
12return $db;
13} Catch(Exception $e) {
14die($e->getMessage());
15}
16
17}
18
19function make_tables() {
20
21database()->exec("CREATE TABLE IF NOT EXISTS `voters`(id INTEGER PRIMARY KEY AUTOINCREMENT, ip TEXT, last_voted TEXT, vote TEXT)");
22database()->exec("CREATE TABLE IF NOT EXISTS `votes`(id INTEGER PRIMARY KEY AUTOINCREMENT, item TEXT, vote_count INT DEFAULT(0) )");
23}
24function vote($item) {
25if(has_voted()) return false;
26elseif(!item_exists($item)) return false;
27$curtime = time();
28$insert = database()->prepare("INSERT into `voters`(ip,last_voted,vote) VALUES('{$_SERVER['REMOTE_ADDR']}', '$curtime', ?)");
29$insert->execute(array($item));
30if($insert->errorCode() == 00000):
31if(update_vote_count($item))
32 return true;
33 else
34 return false;
35 endif;
36}
37function update_vote_count($item) {
38$update = database()->prepare("UPDATE `votes` SET `vote_count` = `vote_count` + 1 WHERE `item` = ? ");
39$update->execute(array($item));
40if($update->errorCode() == 00000) return true; else return false;
41}
42function has_voted() {
43$check = database()->query("SELECT `last_voted` FROM `voters` WHERE `ip` = '{$_SERVER['REMOTE_ADDR']}' ");
44$voted =$check->fetchColumn();
45$curtime = time();
46$delay = $curtime - $voted;
47if($delay > 43200) return false; else return $delay;
48}
49
50
51function add_item($item_name) {
52if(item_exists($item_name)) return false;
53$insert = database()->prepare("INSERT into `votes`(item) VALUES (?)");
54$insert->execute(array($item_name));
55if($insert->errorCode() == 00000) return true; else return false;
56}
57function item_exists($item_name) {
58$check = database()->prepare("SELECT COUNT(*) FROM `votes` WHERE `item` = ? ");
59$check->execute(array($item_name));
60if($check->fetchColumn() > 0) return true; else return false;
61}
62
63function delete_item($item) {
64if(item_exists($item)) {
65$delete = database()->prepare('DELETE FROM `votes` WHERE `item` = ?');
66$delete->prepare(array($item));
67if($delete->errorCode() == 00000) return true; else return false;
68}
69
70}
71function wipe_tables($in_case = '') {
72if($in_case == 'Yes I want to do this') {
73database()->exec("DROP TABLE IF EXISTS `voters`");
74database()->exec("DROP TABLE IF EXISTS `votes`");
75}
76}
77
78
79
80/*
81
82
83How to use :
84You create your tables by calling make_tables();
85
86If you want to wipe your tables for a fresh start use wipe_tables("Yes I want to do this");
87The string within that function is to ensure you don't use it by mistake.
88Make sure you don't use wipe_tables() after using any of the other functions.
89
90To add items you use add_item('Item name'); You can't add the same item twice.
91To delete items you use delete_item('Item name');
92
93
94To add votes you use vote('Item name'); the script automatically checks to see if the user has voted within the
95past 12 hours , if they have it does not vote, if they did, it adds a vote to the voters table and
96updatesthe vote count of the item.
97
98You can check if the current user as voted by using has_voted();
99if they have voted it will return the amount of seconds since the last vote
100
101
102
103*/
104?>