forked from FrancescoBorzi/TC-Unused-Guid-Search-web
-
Notifications
You must be signed in to change notification settings - Fork 6
/
updater.php
183 lines (169 loc) · 5.9 KB
/
updater.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
==========================================================================================
SETTINGS
==========================================================================================
*/
ini_set('memory_limit', '2048M');
set_time_limit ( 0 );
$config_file = 'config.php';
$update_dir = './updates'; // the number will automatically be added from your config. Like: ./updates434
/*
==========================================================================================
DO NOT CHANGE ANYTHING BELOW THIS LINE
==========================================================================================
*/
$db = false;
$last_applied = array();
$total_new = 0;
// Load config
if(file_exists($config_file)){
require_once($config_file);
if(!isset($dbs)){ die('Config file does not contain server information! ($dbs variable)'); }
forEach($dbs as $version=>$details){
// Connect to this specific server
$db_host = $details[0];
$db_user = $details[1];
$db_pass = $details[2];
$db_name = $details[3];
$db = new Database($db_host, $db_user, $db_pass, $db_name);
// Load any previously applied updates
$already_applied_updates = array();
$tmp = $db->query("SELECT * FROM `updates`")->resultSet();
if($db->rowCount()>0){
foreach($tmp as $previous_update){
$already_applied_updates[$version][] = strtolower(trim($previous_update['name']));
}
}
// Scan update files in the right folder
$update_dir_for_version = $update_dir.$version;
if(file_exists($update_dir_for_version)){
$update_files = preg_grep('~\.(sql|txt)$~', scandir($update_dir_for_version));
// Sort files naturally and case insensitive by filename
natcasesort($update_files);
}else{
die('Update folder not found! ('.$update_dir_for_version.')');
}
// Apply update files to this server.
$new_updates = 0;
forEach($update_files as $update_file){
// Already applied?
$update_filename = strtolower(trim($update_file));
if(!in_array($update_filename,$already_applied_updates[$version])){
// New update. Let's apply
$total_new++;
$new_updates++;
$sql = file_get_contents( $update_dir_for_version.'/'.$update_file );
$r = $db->query($sql)->execute();
if($r){
$db->query("INSERT INTO updates (`name`,`hash`,`state`,`timestamp`,`speed`)VALUES(:name,:hash,:state,NOW(),:speed)");
$db->bind(":name", $update_file); // file
$db->bind(":hash", strtoupper(sha1($update_file))); // hash
$db->bind(":state", 'RELEASED'); // state
$db->bind(":speed", 0); // query speed in milliseconds. set to zero, we don't measure.
$db->execute();
}else{
echo 'Update "'.$update_file.'" in version '.version($version).' failed. Please fix the file and refresh the page..<br>';
exit();
}
}else{
// Already applied
}
}
echo 'Version '.version($version).' already has '.count($already_applied_updates[$version]).' update(s) applied to it..<br>';
echo 'Just now applied '.$new_updates.' new update(s) to version '.version($version).'..<br>';
echo '<hr>';
// Grab latest update applied for this version
$tmp = $db->query("SELECT * FROM `updates` ORDER BY `name` DESC LIMIT 1")->single();
$last_applied[$version] = $tmp['name'];
}
// Nothing at all? Well, mention it so we know.
if($total_new<1){
echo "You are already up to date. Nothing to apply..<br>";
}
}else{
die('Config file not found!');
}
/*
==========================================================================================
ALL DONE
==========================================================================================
*/
forEach($last_applied as $version=>$filename){
echo "Last update applied to version ".version($version).": ".$filename.'<br>';
}
echo "<strong>All done!</strong>";
/*
==========================================================================================
HELPERS
==========================================================================================
*/
function version($without_dots){
return implode('.',str_split($without_dots));
}
/*
==========================================================================================
SMALL PDO DATABASE CLASS
==========================================================================================
*/
class Database {
private $dbh;
private $stmt;
public function __construct($host, $user, $pass, $name) {
// Set DSN
$dsn = 'mysql:host=' . $host . ';dbname=' . $name . ';charset=utf8';
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Attempt new instance
try {
$this->dbh = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
$this->error = $e->getMessage();
die('A database connection could not be established: '.$this->error);
}
}
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
return $this;
}
public function bind($pos, $value, $type = null) {
if( is_null($type) ) {
switch( true ) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($pos, $value, $type);
return $this;
}
public function execute() {
return $this->stmt->execute();
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function resultSet() {
$this->execute();
return $this->stmt->fetchAll();
}
public function single() {
$this->execute();
return $this->stmt->fetch();
}
}
?>