forked from GroundApps/ShoppingList_Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UPDATE.php
361 lines (336 loc) · 10.1 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
ob_implicit_flush(true);
ob_end_flush();
include "CONSTANTS.php";
if(ISSET($_GET['debug'])){
error_reporting(~0); ini_set('display_errors', 1);
echo "<title>DEBUG</title>";
}
if(ISSET($_POST['update'])){
update($_POST['zipURL'], $_POST['newVersion']);
} else {
status();
}
function echoHead(){
echo <<<HEAD
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
HEAD;
}
function echoStyle(){
<<<STYLE
<style>
#message {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
#inner-message {
margin: 0 auto;
}
</style>
STYLE;
echo <<<BOOTSTRAP
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
BOOTSTRAP;
}
function echoPageStart(){
echo <<<PAGE_START
<!DOCTYPE html><html>
PAGE_START;
}
function echoPageEnd(){
echo <<<PAGE_END
</html>
PAGE_END;
}
function echoUpdateAvailable($curVersion, $newVersion, $updateText, $zipURL, $disabled){
echo <<<STATUS
<center>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Update Available - ShoppingList Backend</h3>
</div>
<div class="panel-body">
<b>Release Note: $updateText</b><br><br>
Download URL: $zipURL
</div>
<form class="form-inline" action="UPDATE.php" method="post">
<input type="hidden" name="zipURL" value="$zipURL">
<input type="hidden" name="newVersion" value="$newVersion">
<button id="updateButton" class="btn btn-lg btn-info" type="submit" name="update" $disabled>Update to version $newVersion</button>
</form>
</div>
</center>
STATUS;
}
function echoAllGood(){
echo <<<ALLGOOD
<center>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">ShoppingList Backend</h3>
</div>
<div class="panel-body">
<b>Congratulations!</b><br>
Everything is up to date.
</div>
</div>
</center>
ALLGOOD;
}
function echoChecklist($newVersion, $zipURL){
echo <<<CHECKLIST
<center>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Updating backend to version: $newVersion</h3>
</div>
<div class="panel-body">
Downloading from: $zipURL<br><br>
<input id="cb_download" type="checkbox" disabled="">
<label for="cb_download" id="progressDownload">Download (0%)</label><br>
<input id="cb_extract" type="checkbox" disabled="">
<label for="cb_extract"> Extract </label><br>
<input id="cb_backup" type="checkbox" disabled="">
<label for="cb_backup"> Create Backup </label><br>
<input id="cb_filemove" type="checkbox" disabled="">
<label for="cb_filemove" id="progressCopy"> Copy Files</label><br>
<input id="cb_databaseupdate" type="checkbox" disabled="">
<label id="databaseupdateLabel" for="cb_databaseupdate"> Update Database</label><br>
</div>
</div>
</div>
</center>
CHECKLIST;
}
function echoJavascript(){
echo <<<JS
<script type="text/javascript">
var progressDownloadElement = document.getElementById('progressDownload')
var progressElement = document.getElementById('progressDownload')
var downloadElement = document.getElementById('cb_download')
var extractElement = document.getElementById('cb_extract')
var backupElement = document.getElementById('cb_backup')
var filemoveElement = document.getElementById('cb_filemove')
var databaseElement = document.getElementById('cb_databaseupdate')
var databaseLabel = document.getElementById('databaseupdateLabel')
function updateProgress(percentage) {
progressElement.innerHTML = 'Download (' + percentage + '%' + ')';
}
function updateDownload(status) {
downloadElement.checked = status;
}
function updateExtract(status) {
extractElement.checked = status;
}
function updateBackup(status) {
backupElement.checked = status;
}
function updateFilemove(status) {
filemoveElement.checked = status;
}
function updateDatabase(status) {
databaseElement.checked = status;
}
function updateDatabaseNotNecessary() {
databaseLabel.style.setProperty("text-decoration", "line-through");
}
</script>
JS;
}
function status(){
$githubData = getGithubData();
echoHead();
echoPageStart();
echoStyle();
echoJavascript();
if($githubData['version'] == 0){
echoError('Could not connect to GitHub API.');
die();
}
if($githubData['version'] == BACKEND_VERSION){
$updateDisabled = permissionCheck();
echoUpdateAvailable(BACKEND_VERSION, $githubData['version'], $githubData['releasenote'], $githubData['zipurl'], $updateDisabled);
} else {
echoAllGood();
}
echoPageEnd();
}
function update($zipURL, $newVersion){
echoPageStart();
echoHead();
echoStyle();
echoChecklist($newVersion, $zipURL);
echoJavascript();
$zipFILE = downloadUpdate($zipURL);
unzipDownloadedUpdate($zipFILE);
backup();
copyNewFiles();
updateDatabase();
echoPageEnd();
}
function echoSuccess($message){
echo <<<SUCCESS
<div id="message">
<div style="padding: 5px;" >
<div id="inner-message" class="alert alert-success" role="alert">
<strong>Yeah!</strong>
$message
</div>
</div>
</div>
SUCCESS;
}
function echoError($errorMessage){
echo <<<ERROR
<div id="message">
<div style="padding: 5px;" >
<div id="inner-message" class="alert alert-danger" role="alert">
<strong>Oh no!</strong>
An error occured:<br>
$errorMessage
</div>
</div>
</div>
ERROR;
}
function permissionCheck(){
if (!is_writable(__DIR__)){
$user = get_current_user();
$dir = __DIR__;
echoError('No write permission for user "'.$user.'" for directory "'. $dir.'"');
return "disabled";
}
}
function downloadUpdate($zipurl){
$ch = curl_init() or die('Sorry cURL is not installed!');
$zipfile = __DIR__.'/update_'.$tag.'.zip';
$fp = fopen ($zipfile, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $zipurl);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_NOPROGRESS,false);
curl_setopt($ch,CURLOPT_PROGRESSFUNCTION,'progress');
curl_exec($ch);
curl_close($ch);
echo '<script>updateDownload(true);</script>';
return $zipfile;
if(!file_exists($zipfile)){
echoError('Could not download file from GitHub!');
}
}
function unzipDownloadedUpdate($zipfile){
$zip = new ZipArchive;
$res = $zip->open($zipfile);
if ($res === TRUE) {
$zip->extractTo(__DIR__);
$zip->close();
echo '<script>updateExtract(true);</script>';
unlink($zipfile);
} else {
die ('Could not extract file!');
}
}
function backup(){
$backupFolder = __DIR__.'/backup/';
delDir($backupFolder);
if(!mkdir($backupFolder, 0770)){
echoError('Could not create backup folder!');
exit;
}
$iteratorBackup = new DirectoryIterator(__DIR__);
foreach ($iteratorBackup as $backupFile) {
$backupFileName = $backupFile->getFilename();
//REMOVE CHECK FOR update.sql FOR RELEASE, CURRENTLY USED BECAUSE ZIP FROM GITHUB HAS NONE AND IT WOULD BE DELETED
if ($backupFileName != '.' && $backupFileName != '..' && $backupFileName != 'config.php' && $backupFileName != 'update.sql' && $backupFileName != '.htaccess' && !$backupFile->isDir() && $backupFileName[0] != '.'){
if(!rename($backupFile->getPathname(), $backupFolder.$backupFile->getFilename())){
die ('Could not backup files!');
}
}
}
echo '<script>updateBackup(true);</script>';
}
function copyNewFiles(){
$iterator = new DirectoryIterator(__DIR__);
foreach ($iterator as $fileinfo) {
if ($fileinfo->isDir() && strlen(strstr($fileinfo->getFilename(),"GroundApps-ShoppingList_Backend"))>0) {
$iteratorUpdateDir = new DirectoryIterator($fileinfo->getPathname());
foreach ($iteratorUpdateDir as $updateFile) {
$updateFileName = $updateFile->getFilename();
if ($updateFileName != '.' && $updateFileName != '..' && $updateFileName != 'config.php' && $updateFileName != '.htaccess'){
rename($updateFile->getPathname(), __DIR__ .'/'.$updateFileName);
echo '<script>updateFilemove("'.$updateFileName.'");</script>';
}
}
delDir($fileinfo->getPathname());
}
}
echo '<script>updateFilemove(true);</script>';
}
function getGithubData(){
$ch = curl_init() or die('Sorry cURL is not installed!');
$url = 'https://api.github.com/repos/GroundApps/ShoppingList_Backend/releases/latest';
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0';
curl_setopt($ch,CURLOPT_HTTPHEADER,array('User-Agent: '.$agent));
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
$tag = $obj->tag_name;
$zipurl = $obj->zipball_url;
$releasenote = $obj->body;
//$tag = "v1.1";
$zipurl = 'https://api.github.com/repos/GroundApps/ShoppingList_Backend/zipball/v1';
return array(
"version" => substr($tag,1),
"zipurl" => $zipurl,
"releasenote" => $releasenote,
);
}
function updateDatabase(){
include('config.php');
//IMPLEMENT DATABASE ALTERATION
// WITH PDO
$updateFILE = __DIR__."/update.sql";
if(!file_exists($updateFILE)){
echo '<script>updateDatabaseNotNecessary();</script>';
} else {
$lines = file($updateFILE);
foreach ($lines as $line){
echo $line.'<br>';
//SQL FOR EACH LINE IN update.sql
}
echo '<script>updateDatabase(true);</script>';
}
echoSuccess('Update done! Please check if everything works and then delete the backup folder.');
}
function progress($clientp,$dltotal,$dlnow,$ultotal,$ulnow){
if ($dltotal > 0){
echo '<script>updateProgress('.round(($dlnow * 100) / $dltotal).');</script>';
}
return(0);
}
function delDir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir."/".$object) == "dir") delDir($dir."/".$object); else unlink($dir."/".$object);
}
}
reset($objects);
rmdir($dir);
}
}
?>