-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbManager.php
87 lines (62 loc) · 1.65 KB
/
dbManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
$db_file = "database.db";
$table_entries = "entries";
$table_visits = "visits";
class DataBase extends SQLite3 {
function __construct($db_file){
$this->open($db_file);
}
}
class dbManager {
private $db;
function __construct(){
global $db_file;
$this->db = new DataBase($db_file);
}
function add_entry($url){
global $table_entries;
$watcher = uniqid();
$trap = uniqid();
$created = date("Y-m-d H:i:s");
$values = "'$watcher','$trap','$url','$created'";
$sql_command = "INSERT INTO $table_entries VALUES($values)";
$this->db->exec($sql_command);
return $watcher;
}
function get_entry($id){
global $table_entries;
$query = "select * from $table_entries where id = '$id'";
return $this->db->querySingle($query,true);
}
function get_goto($link){
global $table_entries;
$query = "select goto from $table_entries where link = '$link'";
return $this->db->querySingle($query);
}
function add_visit($link){
global $table_visits;
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];//Work behind a proxy
if(!$ip){
$ip = $_SERVER['REMOTE_ADDR'];
}
$stamp = date("Y-m-d H:i:s");
$fields = "link, stamp, ip";
$values_str = "'$link', '$stamp', '$ip'";
$sql_command = "INSERT INTO $table_visits ($fields) VALUES($values_str)";
$this->db->exec($sql_command);
}
function visit($link){
$this->add_visit($link);
return $this->get_goto($link);
}
function get_visits($link){
global $table_visits;
$res = Array();
$query = "select * from $table_visits where link == '$link'";
$select = $this->db->query($query);
while( $row = $select->fetchArray(SQLITE3_ASSOC)){
$res[]=$row;
}
return $res;
}
}