-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-v2.php
319 lines (291 loc) · 9.68 KB
/
update-v2.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/*require __DIR__. '/protected/bootstrap/autoload.php';
$app = require_once __DIR__. '/protected/bootstrap/start.php';
$request = $app['request'];
$client = (new \Stack\Builder)
->push('Illuminate\Cookie\Guard', $app['encrypter'])
->push('Illuminate\Cookie\Queue', $app['cookie'])
->push('Illuminate\Session\Middleware', $app['session'], null);
$stack = $client->resolve($app);
$stack->handle($request);*/
function public_path($path = '') {
return __DIR__ . ($path ? '/'.$path : $path);
}
function app_path($path = '') {
return __DIR__ . '/protected/app' . ($path ? '/'.$path : $path);
}
function content_path($path = '') {
return __DIR__ . '/content' . ($path ? '/'.$path : $path);
}
if(!function_exists('dd')) {
function dd($v) {
die(var_dump($v));
}
}
/*
* Update
*/
$updateError = '';
function add_update_error($error) {
global $updateError;
$updateError .= "<div class='alert alert-warning'>" . $error . "</div>";
}
function has_update_error() {
global $updateError;
return !empty($updateError);
}
function get_update_errors() {
global $updateError;
return $updateError;
}
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir."/".$object))
rrmdir($dir."/".$object);
else
unlink($dir."/".$object);
}
}
rmdir($dir);
}
}
function ensure_writable($path, $isFile = false) {
$type = "Directory";
if($isFile)
$type = "File";
if(!is_writable($path)) {
add_update_error("<div>{$type} <span class='nwp'>'{$path}'</span> is not writable. Make it writable.</div>");
return;
}
if(is_dir($path)) {
foreach (glob(rtrim($path, '/') . '/*') as $dir) {
if(!is_writable($path)) {
add_update_error("<div><span class='nwp'>'{$dir}'</span> is not writable. Make it writable.</div>");
}
}
}
}
function writeConfigFile($path, $config) {
$configFileContent = "<?php \n return " . var_export($config, true) . ";";
file_put_contents($path, $configFileContent);
}
/*
* Move media folder
*/
function moveMediaDirectory($simulate = false) {
$sourcePath = public_path('media');
$contentPath = public_path('content');
$targetPath = public_path('content/media');
if(!file_exists($sourcePath))
return;
ensure_writable($sourcePath);
ensure_writable($contentPath);
if(file_exists($targetPath)) {
ensure_writable($targetPath);
}
if($simulate)
return;
//Remove target path to enable moving
//rrmdir($targetPath);
foreach (glob($sourcePath . '/*') as $file) {
rename($file, $targetPath . '/' . basename($file));
}
}
/*
* Get config(DB and admin) and store in new config file.
*/
function extractConfig($simulate = false) {
$newConfigFile = public_path('config.php');
ensure_writable($newConfigFile, true);
if($simulate)
return;
$dbConfig = require(app_path('config/database.php'));
$dbConfig = $dbConfig['connections']['mysql'];
$newConfig = require($newConfigFile);
$newConfig['DB_HOST'] = $dbConfig['host'];
$newConfig['DB_DATABASE'] = $dbConfig['database'];
$newConfig['DB_USERNAME'] = $dbConfig['username'];
$newConfig['DB_PASSWORD'] = $dbConfig['password'];
writeConfigFile($newConfigFile, $newConfig);
}
/*
* Running migrations
*/
//\Artisan::call('migrate', array('--force' => true));
/*
* Delete old version files
*/
function getDirectoriesToDelete() {
$v2Directories = ['application', 'content'];
$toDelete = [];
foreach (glob('./*', GLOB_ONLYDIR) as $directory) {
if(in_array(basename($directory), $v2Directories))
continue;
$toDelete[] = $directory;
}
return $toDelete;
}
function checkWritePermissions($directories) {
$notWritable = [];
foreach ($directories as $directory) {
if(!is_writable($directory))
$notWritable[] = $directory;
else {
foreach (glob(rtrim($directory, '/') . '/*') as $subdir) {
if(!is_writable($subdir))
$notWritable[] = $subdir;
}
}
}
return empty($notWritable) ? true : $notWritable;
}
function deleteOldFiles($simulate = false) {
$installDir = __DIR__;
//The base directory should be writable to delete the old directories and files
ensure_writable($installDir);
$directoriesToDelete = getDirectoriesToDelete();
$permissionCheck = checkWritePermissions($directoriesToDelete);
if($permissionCheck !== true) {
$error = "<b>These folders are not writable. Make them writable</b><br><ol>";
foreach ($permissionCheck as $dir) {
$error .= "<li><span class='nwp'>" . public_path(str_replace("./", '', $dir)) . "</span></li>";
}
$error .= "</ol>";
add_update_error($error);
return;
}
if($simulate)
return;
foreach ($directoriesToDelete as $dir) {
rrmdir($dir);
}
}
function run_update_steps($simulate = false) {
moveMediaDirectory($simulate);
extractConfig($simulate);
deleteOldFiles($simulate);
}
function after_update_checks() {
}
function display_update_errors($updateDone = false) {
global $updateError;
if(!$updateDone)
echo("<h3>Fix these issues to start the update:</h3>");
else
echo("<h3>Update has been done. But there are some permission issues you need to fix to run it properly:</h3>");
?>
<div class="row">
<div class="form-group">
<label for="inputID" class="col-sm-2 control-label">Default writable permission: </label>
<div class="col-sm-4">
<input type="text" id="permissionField" value="755" class="form-control pull-left" style="width: 60px;">
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<div class="alert alert-info">
<h4>Note: The required permission number to make files/folders writable may vary with the host.</h4>
<p><b>Follow these steps:</b></p>
<ol>
<li>Contact your host to know the proper permission number.</li>
<li>Then change the permission number suitable for your host in the field "Default writable permission:" above and the commands will be updated automatically.</li>
<li>Then run all the commands to make the required files/folders writable.</li>
<li>Then REFRESH this page</li>
</ol>
</div>
</div>
</div>
<?php
echo $updateError;
echo '<h4>All Commands:</h4><textarea rows="5" class="form-control" id="allCommands"></textarea><br><br>';
echo '<div class="text-center"><span class="btn btn-success btn-lg" onclick="window.location.href = window.location.href;">Click here to retry after fixing the above issues</span></div>';
}
if(empty($_GET['do'])) {
$onSuccessHtml = '<a class="btn btn-success btn-lg" href="'. $_SERVER['PHP_SELF'] . '?do=true">Go to update script</a>';
include('check.php');
die();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Update Socioquiz</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<style>
body {
padding-bottom: 100px;
}
</style>
</head>
<body>
<br><br><h1 class="text-center">Updating to SocioQuiz v2.0</h1>
<hr>
<div class="container">
<div class="row">
<div class="col-md-12">
<?php
/*
* Simulate the steps first
*/
run_update_steps(true);
if(has_update_error()) {
display_update_errors();
} else {
//Run the actual steps
run_update_steps();
?>
<div class="alert alert-success text-center">
<h2>Update successful!</h2>
</div>
<?php
after_update_checks();
if(has_update_error()) {
display_update_errors(true);
}
}
?>
</div>
</div>
</div>
<style>
.command {
display: block;
margin-top:5px;
padding: 4px 8px;
}
</style>
<script>
function updateCommands() {
var commands = [];
$('.nwp').each(function () {
var permission = $('#permissionField').val();
var parent = $(this).parent();
parent.children('.command').remove();
var path = $(this).text();
path = path.replace(/'/g, '');
parent.append('<div class="command alert alert-info"><div style="margin-bottom: 3px;"><b>Command:</b></div><input class="command-input form-control" type="text" style="width: 100%;"></div>')
var command = "chmod -R " + permission + ' "'+ path +'"';
commands.push(command);
parent.find('input').val(command)
})
$('#allCommands').html(commands.join("\n"))
$('.command-input').focus(function () {
$(this).select();
})
}
$('#permissionField').change(updateCommands);
$('#permissionField').keyup(updateCommands);
updateCommands();
</script>
</body>
</html>