forked from elabftw/elabftw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.php
291 lines (257 loc) · 10.3 KB
/
update.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
// elabftw update file. Run it after each git pull.
// php update.php on normal server
// /Applications/MAMP/bin/php/php5.3.6/bin/php update.php for MAMP install
//
$die_msg = "There was a problem in the database update :/ Please report a bug : https://github.com/NicolasCARPi/elabftw/issues?state=open";
require_once 'inc/functions.php';
// make a simple query
function q($sql) {
global $pdo;
try {
$req = $pdo->prepare($sql);
$req->execute();
}
catch (PDOException $e)
{
echo "\nThe update failed. Here is the error :\n\n".$e->getMessage();
die();
}
}
function add_field($table, $field, $params, $added) {
global $pdo;
// first test if it's here already
$sql = "SHOW COLUMNS FROM $table";
$req = $pdo->prepare($sql);
$req->execute();
$field_is_here = false;
while ($show = $req->fetch()) {
if (in_array($field, $show)) {
$field_is_here = true;
}
}
// add field if it's not here
if (!$field_is_here) {
$sql = "ALTER TABLE $table ADD $field $params";
$req = $pdo->prepare($sql);
$result = $req->execute();
if($result) {
echo $added;
} else {
die($die_msg);
}
}
}
function rm_field($table, $field, $added) {
global $pdo;
// first test if it's here already
$sql = "SHOW COLUMNS FROM $table";
$req = $pdo->prepare($sql);
$req->execute();
$field_is_here = false;
while ($show = $req->fetch()) {
if (in_array($field, $show)) {
$field_is_here = true;
}
}
// rm field if it's here
if ($field_is_here) {
$sql = "ALTER TABLE $table DROP $field";
$req = $pdo->prepare($sql);
$result = $req->execute();
if($result) {
echo $added;
} else {
die($die_msg);
}
}
}
// check if it's run from cli or web; do nothing if it's from web
if(php_sapi_name() != 'cli' || !empty($_SERVER['REMOTE_ADDR'])) {
die("<p>Thank you for using eLabFTW. <br />To update your database, run this file only from the command line.</p>");
}
// UPDATE the config file path
// check for config file
if (!file_exists('config.php')) {
if (file_exists('admin/config.php')) { // update
// copy the file
if (rename('admin/config.php', 'config.php')) {
echo ">>> Config file is now in the root directory\n";
echo "!!! You can now safely delete the admin directory if you wish: 'rm -rf admin'\n";
} else {
echo "!!! Please move 'admin/config.php' to the root directory : 'mv admin/config.php .'\n";
exit;
}
} else {
die("There is something seriously wrong with your install. I could not find the file config.php !");
}
}
require_once 'inc/connect.php';
// START //
// BIG _('Team') AND GROUPS UPDATE
// _('Create') table teams
$sql = "SHOW TABLES";
$req = $pdo->prepare($sql);
$req->execute();
$table_is_here = false;
while ($show = $req->fetch()) {
if (in_array('teams', $show)) {
$table_is_here = true;
}
}
// BIG update coming up
if (!$table_is_here) {
// create teams table
q("_('Create') TABLE IF NOT EXISTS `teams` (
`team_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`team_name` text NOT NULL,
`deletable_xp` tinyint(1) NOT NULL,
`link_name` text NOT NULL,
`link_href` text NOT NULL,
`datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ( `team_id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8");
// populate table teams
q("INSERT INTO teams (team_name, deletable_xp, link_name, link_href) VALUES
('".get_config('lab_name')."', '".get_config('deletable_xp')."', '".get_config('link_name')."', '".get_config('link_href')."')");
// add teams and group to other tables
q("ALTER TABLE experiments ADD team int(10) unsigned not null after id;");
q("ALTER TABLE items ADD team int(10) unsigned not null after id;");
q("ALTER TABLE items_types ADD team int(10) unsigned not null after id;");
q("ALTER TABLE status ADD team int(10) unsigned not null after id;");
q("ALTER TABLE users ADD team int(10) unsigned not null after password;");
q("ALTER TABLE users ADD usergroup int(10) unsigned not null after team;");
// populate tables
q("UPDATE experiments SET team = 1;");
q("UPDATE items SET team = 1;");
q("UPDATE items_types SET team = 1;");
q("UPDATE status SET team = 1;");
q("UPDATE users SET team = 1;");
q("UPDATE users SET usergroup = 1 WHERE is_admin = 1;");
q("UPDATE users SET usergroup = 4 WHERE is_admin = 0;");
q("UPDATE users SET usergroup = 3 WHERE can_lock = 1;");
// remove unused fields
q("ALTER TABLE users DROP is_admin;");
q("ALTER TABLE users DROP can_lock;");
// add timestamp to locks
q("ALTER TABLE `experiments` ADD `lockedwhen` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP AFTER `lockedby`;");
// add team to experiments_templates
q("ALTER TABLE `experiments_templates` ADD `team` INT(10) unsigned not null after id;");
q("UPDATE experiments_templates SET team = 1;");
// create table groups
q("_('Create') TABLE IF NOT EXISTS `groups` (
`group_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`group_name` text NOT NULL,
`is_sysadmin` tinyint(1) NOT NULL,
`is_admin` text NOT NULL,
`can_lock` text NOT NULL,
PRIMARY KEY ( `group_id` )
) ENGINE=InnoDB DEFAULT CHARSET=utf8");
// Populate table
q("INSERT INTO `groups` (`group_id`, `group_name`, `is_sysadmin`, `is_admin`, `can_lock`) VALUES
(1, 'Sysadmins', 1, 1, 0),
(2, 'Admins', 0, 1, 0),
(3, 'Chiefs', 0, 1, 1),
(4, 'Users', 0, 0, 0);");
// Remove the configs from the config table because now they are in the teams table
q("DELETE FROM `config` WHERE `config`.`conf_name` = 'deletable_xp';
DELETE FROM `config` WHERE `config`.`conf_name` = 'link_name';
DELETE FROM `config` WHERE `config`.`conf_name` = 'link_href';
DELETE FROM `config` WHERE `config`.`conf_name` = 'lab_name'");
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
echo ">>> BIG UPDATE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
echo ">>> One eLabFTW install can now host several teams !\n";
echo ">>> There is now groups with set of permissions. \n";
echo ">>> There is now a new sysadmin group for elabftw configuration\n";
echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
}
// remove theme from users
rm_field('users', 'theme', ">>> Removed custom themes.\n");
// add logs
$sql = "SHOW TABLES";
$req = $pdo->prepare($sql);
$req->execute();
$table_is_here = false;
while ($show = $req->fetch()) {
if (in_array('logs', $show)) {
$table_is_here = true;
}
}
if (!$table_is_here) {
q("_('Create') TABLE IF NOT EXISTS `logs` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`type` varchar(255) NOT NULL,
`datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user` text,
`body` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
echo ">>> Logs are now stored in the database.\n";
}
// TIMESTAMPS
// check if there is the timestamp columns
$sql = "SHOW COLUMNS FROM experiments";
$req = $pdo->prepare($sql);
$req->execute();
$field_is_here = false;
while ($show = $req->fetch()) {
if (in_array('timestamped', $show)) {
$field_is_here = true;
}
}
// add field if it's not here
if (!$field_is_here) {
q("ALTER TABLE `experiments` ADD `timestamped` BOOLEAN NOT NULL DEFAULT FALSE AFTER `lockedwhen`, ADD `timestampedby` INT NULL DEFAULT NULL AFTER `timestamped`, ADD `timestampedwhen` TIMESTAMP NULL AFTER `timestampedby`, ADD `timestamptoken` TEXT NULL AFTER `timestampedwhen`;");
q("INSERT INTO `config` (`conf_name`, `conf_value`) VALUES ('stamplogin', NULL), ('stamppass', NULL);");
echo ">>> You can now timestamp experiments. See the wiki for more infos.\n";
}
// add md5 field to uploads
add_field('uploads', 'md5', 'VARCHAR(32) NULL DEFAULT NULL', ">>> Uploaded files are now md5 summed upon upload.\n");
// change the unused date column in uploads to a datetime one with current timestamp on insert
rm_field('uploads', 'date', ">>> Removed unused field.\n");
add_field('uploads', 'datetime', "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `type`", ">>> Added timestamp to uploads\n");
// this is run each time (but doesn't hurt)
q("UPDATE uploads SET datetime = CURRENT_TIMESTAMP WHERE datetime = '0000-00-00 00:00:00'");
// add timestamp conf for teams
add_field('teams', 'stamplogin', "TEXT NULL DEFAULT NULL", ">>> Added timestamp team config (login)\n");
add_field('teams', 'stamppass', "TEXT NULL DEFAULT NULL", ">>> Added timestamp team config (pass)\n");
// add stampshare configuration
// check if we need to
$sql = "SELECT COUNT(*) AS confcnt FROM config";
$req = $pdo->prepare($sql);
$req->execute();
$confcnt = $req->fetch(PDO::FETCH_ASSOC);
if ($confcnt['confcnt'] < 14) {
$sql = "INSERT INTO config (conf_name, conf_value) VALUES ('stampshare', null)";
$req = $pdo->prepare($sql);
$res = $req->execute();
if ($res) {
echo ">>> Added timestamp credentials sharing\n";
} else {
die($die_msg);
}
}
// add lang to users
add_field('users', 'lang', "VARCHAR(5) NOT NULL DEFAULT 'en-GB'", ">>> You can now select a language!\n");
// add default lang to config
// remove the notice that will appear if there is no lang in config yet
error_reporting(E_ALL & ~E_NOTICE);
if (strlen(get_config('lang')) != 5) {
q("INSERT INTO `config` (`conf_name`, `conf_value`) VALUES ('lang', 'en-GB');");
}
// update lang, put locale with _ instead of -
// put everyone in english, it's simpler
if (strpos(get_config('lang'), '-')) {
q("UPDATE users SET `lang` = 'en_GB';");
}
// add elab_root in config.php
if (!defined(ELAB_ROOT)) {
$path = substr(realpath(__FILE__), 0, -10);
$text2add = "define('ELAB_ROOT', '".$path."');";
if (file_put_contents('config.php', $text2add, FILE_APPEND)) {
echo ">>> Added constant ELAB_ROOT in file config.php\n";
} else {
echo "!!! Error writing file config.php. Please fix permissions for server to write to it. Or edit it yourself (look at config.php-EXAMPLE)";
}
}
// END
echo "[SUCCESS] You are now running the latest version of eLabFTW. Have a great day! :)\n";