-
Notifications
You must be signed in to change notification settings - Fork 9
/
gertrude.php
132 lines (104 loc) · 2.52 KB
/
gertrude.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
$token_filename = "./.token";
$db_filename = "./gertrude.db";
if (isset($_GET["token"]))
$token = $_GET["token"];
else
$token = NULL;
// pour free.fr
function wa_flock($f, $mode) {
// sur serveur free => decommenter la ligne suivante
// return true;
return flock($f, $mode);
}
function check_token() {
global $token_filename;
global $token;
if (!file_exists($token_filename))
return 0;
$fp = fopen($token_filename, 'r');
$ftoken = trim(fgets($fp, 4096));
fclose($fp);
if ($token != $ftoken)
return 0;
return 1;
}
function get_token($force=0) {
global $token_filename;
if (!$force && file_exists($token_filename))
return 0;
$f = fopen($token_filename, "w");
if (wa_flock($f, LOCK_EX)) {
$token = uniqid(md5(rand()), true);
fwrite($f, $token);
wa_flock($f, LOCK_UN);
}
else {
$token = 0;
}
fclose($f);
return $token;
}
function rel_token() {
global $token_filename;
if (!check_token())
return 0;
unlink($token_filename);
return 1;
}
function upload() {
global $db_filename;
if (!check_token())
return 0;
$tmp_file = $_FILES['database']['tmp_name'];
if (!is_uploaded_file($tmp_file))
return 0;
if (file_exists($db_filename)) {
$backup_filename = "backup_" . time() . ".db";
if (!copy($db_filename, $backup_filename))
return 0;
}
if (!move_uploaded_file($tmp_file, $db_filename))
return 0;
return 1;
}
function download() {
global $db_filename;
if (!file_exists($db_filename))
return 0;
$fp = fopen($db_filename, 'r');
$data = fread($fp, filesize($db_filename));
return $data;
}
function get_version($prefix, $suffix) {
$dh = opendir(".");
while (false !== ($file = readdir($dh))) {
if (substr($file, 0, strlen($prefix)) == $prefix && substr($file, 0-strlen($suffix), strlen($suffix)) == $suffix) {
$version = substr($file, strlen($prefix), strlen($file)-strlen($prefix)-strlen($suffix));
return $version;
}
}
}
function execute($action) {
switch($action) {
case "has_token":
return check_token();
case "get_token":
return get_token(0);
case "force_token":
return get_token(1);
case "rel_token":
return rel_token();
case "upload":
return upload();
case "download":
return download();
case "get_exe_version":
return get_version("gertrude_", ".exe");
case "get_templates_version":
return get_version("templates_", ".zip");
}
}
if (isset($_GET["action"]))
echo execute($_GET["action"]);
?>