This repository was archived by the owner on Dec 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstaller.php
More file actions
258 lines (234 loc) · 10.4 KB
/
installer.php
File metadata and controls
258 lines (234 loc) · 10.4 KB
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
<?php
// this installation file should be automatically placed into a temporary directory
// in order to let it unpack the package contents and stuff
$dir = dirname(__FILE__);
$file = "prails.tar.bz2";
function recurse_copy($src, $dst) {
$dir = opendir($src);
$arr_rollback = Array();
$rollback = false;
if (!is_dir($dst)) {
array_push($arr_rollback, $dst);
if (!@mkdir($dst)) {
echo "Error creating directory ".$dst;
$rollback = true;
}
}
while (!$rollback && false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
if (!recurse_copy($src . '/' . $file, $dst . '/' . $file)) {
$rollback = true;
}
} else {
array_push($arr_rollback, Array($src."/".$file, $dst."/".$file));
// first create a backup of the original file, if existent
if (file_exists($dst."/".$file)) {
if (!copy($dst."/".$file, "backup.".md5($dst."/".$file))) {
echo "Error creating backup for file ".$dst."/".$file;
$rollback = true;
break;
}
}
if (!@copy($src . '/' . $file, $dst . '/' . $file)) {
echo "Error copying file ".$src."/".$file." to ".$dst."/".$file."";
$rollback = true;
}
}
}
}
if ($rollback) {
$arr_rollback = array_reverse($arr_rollback);
foreach ($arr_rollback as $entry) {
if (is_array($entry) && file_exists("backup.".md5($entry[1]))) {
// if we have a backup file, restore it
if (@copy("backup.".md5($entry[1]), $entry[1])) {
@unlink("backup.".md5($entry[1]));
}
} else if (is_dir($entry)) {
// if we created a directory during copy, remove it
@unlink($entry);
}
}
}
closedir($dir);
return !$rollback;
}
if ($_GET["version"]) {
$version = $_GET["version"];
$url = "https://raw.github.com/AndCake/prails/master/dist/prails-$version.tar.bz2";
if (!($fileContent=file_get_contents($url))) {
die("Error while downloading the Prails update.");
}
if (!file_put_contents($file, $fileContent)) {
die("Error while downloading the Prails update. Please check permissions in ".$dir." .");
}
die("success\ncache/installer.php\nInstalling new version...");
} else {
$warnings = "";
// unpack everything
$disabled = explode(', ', ini_get('disable_functions'));
if (in_array('exec', $disabled)) {
die("Error while unpacking Prails update: cannot be unpacked due to disabled 'exec' function. Please check server configuration.");
}
if (!file_exists($file)) {
die("Error while unpacking Prails update: package not found.");
}
if (PHP_OS == "WINNT") {
exec("..\\7za.exe x ".$file);
exec("..\\7za.exe x ".str_replace(".bz2", "", $file));
} else {
exec("tar xvjf ".$file."");
}
if (!file_exists("prails")) {
die("Error while unpacking Prails update: unpacking failed.");
}
// run the actual installation
// configuration file needs to be merged, so create a backup...
$oldConf = file_get_contents("../conf/configuration.php");
if (!copy("../conf/configuration.php", "backup.configuration.php") || !file_exists("backup.configuration.php")) {
die("Error creating backup for configuration.");
}
// backup .htaccess
if (!copy("../.htaccess", "backup.htaccess") || !file_exists("backup.htaccess")) {
die("Error creating backup for .htaccess .");
}
copy("../.groups", "backup.groups");
copy("../.users", "backup.users");
copy("../favicon.ico", "backup.favicon.ico");
// this should copy all files to the current installation directory
if (!recurse_copy("prails", "..")) {
die("Error installing new prails version.");
}
@unlink($file);
if (PHP_OS == "WINNT") {
exec("rd /s /q prails");
} else {
exec("rm -rf prails");
}
// copy back the .groups and .users file
if (copy("backup.groups", "../.groups")) unlink("backup.groups"); else $warnings .= "Unable to restore groups. Backup stored in ".$dir."/backup.groups .<br/>";
if (copy("backup.users", "../.users")) unlink("backup.users"); else $warnings .= "Unable to restore users. Backup stored in ".$dir."/backups.users .<br/>";
copy("backup.favicon.ico", "../favicon.ico");
$users = file("../.users");
$adminFound = false;
foreach ($users as &$user) {
list($name, $pwd) = explode(":", $user);
if ($name == "admin") {
$adminFound = true;
}
if (strlen(trim($pwd)) != 32 || !preg_match('/^[a-f0-9]+$/mi', trim($pwd))) {
$pwd = md5(trim($pwd));
$user = $name.":".$pwd;
}
$user = trim($user);
}
if (!$adminFound) {
$pwd = substr(md5(time()), 0, 6);
$users[] = "admin:".md5($pwd);
$groups = file("../.groups");
$adminFound = false;
foreach ($groups as &$group) {
list($name, $users) = explode("=", $group);
if ($name == "admin") {
$users .= ",admin";
$adminFound = true;
$group = $name."=".$users;
}
$group = trim($group);
}
if (!$adminFound) $groups[] = "admin=admin";
@file_put_contents("../.groups", implode("\n", $groups)) or $warnings .= "Unable to add new admin user to the admin group.<br/>";
$warnings .= "An admin account has been added to your users to let you manage them. It's password is: ".$pwd."<br/>";
}
@file_put_contents("../.users", implode("\n", $users)) or $warnings .= "Unable to add new admin to list of users.<br/>";
// merge .htaccess
$oldHt = file_get_contents("backup.htaccess");
$newHt = file_get_contents("../.htaccess");
$startMarker = "#--START_CUSTOM--#";
$endMarker = "#--END_CUSTOM--#";
$start = strpos($oldHt, $startMarker) + strlen($startMarker);
$len = (strpos($oldHt, $endMarker, $start) - 1) - $start;
$area = substr($oldHt, $start, $len);
$start = strpos($newHt, $startMarker) + strlen($startMarker);
$len = (strpos($newHt, $endMarker, $start) - 1) - $start;
$file = substr($newHt, 0, $start)."\n".$area."\n".substr($newHt, $start+$len);
if (!@file_put_contents("../.htaccess", $file)) {
$warnings .= "Unable to re-integrate custom .htaccess rules into the newer file. Backup stored in ".$dir."/backup.htaccess .<br/>";
}
if (!@file_get_contents("http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].dirname($_SERVER['SCRIPT_NAME'])."/../index.php")) {
// problem with htaccess
// try to resolve it...
file_put_contents("../.htaccess", str_replace('php_value short_open_tag 1', '', $file));
if (!@file_get_contents("http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].dirname($_SERVER['SCRIPT_NAME'])."/../index.php")) {
die("Error in .htaccess file. Please manually check it!");
}
}
unlink("backup.htaccess");
// merge configuration
$newConf = $newBackupConf = file_get_contents("../conf/configuration.php");
preg_match_all('@/\*<KEEP-([0-9]+)>\*/(([^/]|/[^*]|/\*[^<]|/\*<[^/]|/\*</[^K]|/\*</K[^E]|/\*</KE[^E]|/\*</KEE[^P])*)/\*</KEEP-\1>\*/@', $oldConf, $matches);
$conf = Array();
foreach ($matches[0] as $key=>$match) {
$conf[$matches[1][$key]] = $matches[2][$key];
}
preg_match_all('@/\*<KEEP-([0-9]+)>\*/(([^/]|/[^*]|/\*[^<]|/\*<[^/]|/\*</[^K]|/\*</K[^E]|/\*</KE[^E]|/\*</KEE[^P])*)/\*</KEEP-\1>\*/@', $newConf, $matches);
foreach ($matches[0] as $key => $match) {
$value = $conf[$matches[1][$key]];
// make sure that the DB is re-deployed the first time after upgrade
if (strpos($value, "FIRST_RUN") !== false) {
$lines = explode("\n", $value);
foreach ($lines as &$line) {
if (preg_match('/\s*"FIRST_RUN"\s*=>\s*[a-zA-Z]+\s*,/', $line)) {
$line = '"FIRST_RUN" => true,';
}
}
$value = implode("\n", $lines);
}
$newConf = str_replace($matches[2][$key], $value, $newConf);
}
file_put_contents("../conf/configuration.php", $newConf);
exec("php -l ../conf/configuration.php", $error, $code);
if ($code != 0) {
// we have a syntax error in the code
file_put_contents("../conf/configuration.php", $newBackupConf);
$warnings .= "Error while restoring configuration options. ";
$warnings .= "Configuration has been reset to installation default. Original configuration has been saved in ".$dir."/backup.configuration.php .<br/>";
} else {
if (!@unlink("backup.configuration.php")) {
$warnings .= "Unable to remove backup configuration file.<br/>";
}
}
$oldHandler = file_get_contents("backup.".md5("../modules/main/main_handler.php"));
if (strlen($oldHandler) < 0) {
$warnings .= "Unable to find global handler backup.";
} else {
$newHandler = file_get_contents("../modules/main/main_handler.php");
$startPos = strpos($newHandler, "/** BEGIN_CODE **/") + strlen("/** BEGIN_CODE **/");
if ($startPos - strlen("/** BEGIN_CODE **/") <= 0) {
die("Error finding global home handler start!");
}
$endPos = strpos($newHandler, "/** END_CODE **/");
if ($endPos === false) {
die("Error finding global home handler end!");
}
$newPre = substr($newHandler, 0, $startPos);
$newPost = substr($newHandler, $endPos);
$oStartPos = strpos($oldHandler, "/** BEGIN_CODE **/") + strlen("/** BEGIN_CODE **/");
$oEndPos = strpos($oldHandler, "/** END_CODE **/");
$oldContent = substr($oldHandler, $oStartPos, $oEndPos - $oStartPos);
$mergedHandler = $newPre . $oldContent . $newPost;
if (!@file_put_contents("../modules/main/main_handler.php", $mergedHandler)) {
$warnings .= "Unable to update global handler code<br/>";
} else {
exec("php -l ../modules/main/main_handler.php", $error, $code);
if ($code != 0) {
@file_put_contents("../modules/main/main_handler.php", $newHandler);
$warnings .= "Error while merging global home handler. ";
$warnings .= "It has been reset to installation default. Original handler code is saved in ".$dir."/backup.".md5("../modules/main/main_handler.php")."<br/>";
}
}
}
die("success\n?event=builder:updateSystem&empty=cache&warnings=".urlencode($warnings)."\nRe-deploying database and cleaning caches...");
}
?>