-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroundcube_upgrade_helper.php
147 lines (123 loc) · 3.96 KB
/
roundcube_upgrade_helper.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
<?php
$folder = '../roundcube';
$oldversion = NULL;
if (array_key_exists('oldversion', $_GET)) {
$oldversion = $_GET['oldversion'];
}
$newversion = NULL;
if (array_key_exists('newversion', $_GET)) {
$newversion = $_GET['newversion'];
}
if (array_key_exists('action', $_GET)) {
switch ($_GET['action']) {
case 'backup':
if ($oldversion != NULL) {
backupAsZip($folder, $oldversion);
echo "Backup done.";
} else {
echo "'oldversion' parameter required!";
}
break;
case 'download':
if ($newversion != NULL) {
getLatest($newversion);
echo "Got latest version.";
} else {
echo "'newversion' parameter required!";
}
break;
case 'update':
if ($newversion != NULL && $oldversion != NULL) {
extractTarGz($newversion);
echo "Extracted.. ";
move($oldversion, $newversion);
echo "Moved.. ";
copyConfig($oldversion);
echo "Copied config.. ";
cleanup($oldversion);
echo "Cleanup done.. ";
echo "Done updating!";
} else {
echo "'newversion' or 'oldversion' parameter missing!";
}
break;
default:
echo "Valid actions: backup, download, update";
}
} else {
echo "'action' parameter required!";
}
function backupAsZip($folder, $oldversion) {
// Get real path for our folder
$rootPath = realpath($folder);
// Initialize archive object
$zip = new ZipArchive;
$zip->open('roundcubemail-' . $oldversion . '.zip', ZipArchive::CREATE);
// Create recursive directory iterator
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
// Get real path for current file
$filePath = $file->getRealPath();
// Add current file to archive
$zip->addFile($filePath);
}
// Zip archive will be created only after closing object
$zip->close();
}
function getLatest($newversion) {
$url = 'http://garr.dl.sourceforge.net/project/roundcubemail/roundcubemail/' . $newversion . '/roundcubemail-' . $newversion . '.tar.gz';
$path = __DIR__ . '/roundcubemail-' . $newversion . '.tar.gz';
echo "Downloading " . $url . " to " . $path . '<br>';
# open file to write
$fp = fopen ($path, 'w+');
# start curl
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
# set return transfer to false
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
# increase timeout to download big file
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
# follow redirects
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
# write data to local file
curl_setopt( $ch, CURLOPT_FILE, $fp );
# execute curl
curl_exec( $ch );
# close curl
curl_close( $ch );
# close local file
fclose( $fp );
}
function extractTarGz($newversion) {
rename(__DIR__ . '/roundcubemail-' . $newversion . '.tar.gz', __DIR__ . '/roundcubemail.tar.gz');
$p = new PharData(__DIR__ . '/roundcubemail.tar.gz');
$p->decompress();
$phar = new PharData(__DIR__ . '/roundcubemail.tar');
$phar->extractTo('../');
}
function move($oldversion, $newversion) {
rename($folder, $folder . '-' . $oldversion);
rename('../roundcubemail-' . $newversion, $folder);
}
function copyConfig($oldversion) {
copy($folder . '-' . $oldversion . '/config/db.inc.php', $folder . '/config/db.inc.php');
copy($folder . '-' . $oldversion . '/config/main.inc.php', $folder . '/config/main.inc.php');
}
function cleanup($oldversion) {
unlink(__DIR__ . '/roundcubemail.tar.gz');
unlink(__DIR__ . '/roundcubemail.tar');
delTree($folder . '-' . $oldversion);
}
function delTree($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
?>