-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.admin.php
260 lines (207 loc) · 7.02 KB
/
updater.admin.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
<?php
//This is a module for pluck, an opensource content management system
//Website: http://www.pluck-cms.org
//Make sure the file isn't accessed directly
defined('IN_PLUCK') or exit('Access denied!');
$version = "";
function updater_pages_admin() {
global $lang;
$module_page_admin[] = array(
'func' => 'Main',
'title' => $lang['updater']['main']
);
$module_page_admin[] = array(
'func' => 'REQ',
'title' => $lang['updater']['requirements']
);
$module_page_admin[] = array(
'func' => 'AVAIL',
'title' => $lang['updater']['available']
);
$module_page_admin[] = array(
'func' => 'UPDATE',
'title' => $lang['updater']['update']
);
$module_page_admin[] = array(
'func' => 'BACKUP',
'title' => $lang['updater']['backup']
);
return $module_page_admin;
}
function updater_page_admin_Main() {
global $lang;
showmenudiv($lang['updater']['requirements'],$lang['updater']['requirements_info'],'data/modules/updater/images/css.png','admin.php?module=updater&page=REQ',false);
showmenudiv($lang['updater']['backup'],$lang['updater']['backup_info'],'data/modules/updater/images/theme.png','admin.php?module=updater&page=BACKUP',false);
showmenudiv($lang['updater']['available'],$lang['updater']['available_info'],'data/modules/updater/images/theme.png','admin.php?module=updater&page=AVAIL',false);
showmenudiv($lang['updater']['update'],$lang['updater']['update_info'],'data/modules/updater/images/theme.png','admin.php?module=updater&page=UPDATE',false);
}
function updater_page_admin_REQ() {
global $lang;
echo "<p><a href=\"?module=updater\"><<< Back</a></p>";
//do requirements check here
//check writable: data, files
print("<b>Check folders</b> <br />");
foreach (array('files', 'data', 'images', 'docs', 'data/inc', 'data/modules', 'data/trash', 'data/themes', 'data/themes/default', 'data/themes/oldstyle', 'data/settings', 'data/settings/langpref.php') as $check)
check_writable($check);
unset($check);
print(" <br /><b> Check plugins </b><br />");
//check availability libcurl
if (extension_loaded('curl')){
//installed
?>
<span>
<img src="data/image/update-no.png" width="15" height="15" alt="<?php echo $lang['install']['good']; ?>" />
</span>
<span> /<?php echo $lang['updater']['curl_installed']; ?></span>
<br />
<?php
} else {
//not installed
?>
<span>
<img src="data/image/error.png" width="15" height="15" alt="<?php echo $lang['install']['false']; ?>" />
</span>
<span> <?php echo $lang['updater']['curl_notinstalled']; ?></span>
<br />
<?php
}
print(" <br /> <b>Checks Complete </b><br /><br />");
echo "<p><a href=\"?module=updater\"><<< Back</a></p>";
}
function updater_page_admin_BACKUP() {
global $lang;
echo "<p><a href=\"?module=updater\"><<< Back</a></p>";
$backupfile = createSiteBackup();
show_error($lang['updater']['backupcreated'],3);
print("<a href=".$backupfile.">Download backup here</a>");
echo "<p><a href=\"?module=updater\"><<< Back</a></p>";
}
function updater_page_admin_AVAIL() {
global $lang;
echo "<p><a href=\"?module=updater\"><<< Back</a></p>";
if (extension_loaded('curl')){
//installed
$updateversion = checkUpdate();
switch(check_update_version($updateversion)) {
case 'yes':
show_error($lang['updater']['yes'],1);
break;
case 'urgent':
show_error($lang['updater']['urgent'],1);
break;
case 'no':
show_error($lang['updater']['no'],3);
break;
}
} else {
//not installed
show_error($lang['updater']['curl_notinstalled'],1);
}
echo "<p><a href=\"?module=updater\"><<< Back</a></p>";
}
function updater_page_admin_UPDATE() {
global $lang;
echo "<p><a href=\"?module=updater\"><<< Back</a></p>";
//do update here
//Backup install to /files
createSiteBackup();
$updateversion = checkUpdate();
switch(check_update_version($updateversion)) {
case 'no':
show_error($lang['updater']['not_needed'],3);
return;
}
//Download latest version
$urlfile = 'https://github.com/pluck-cms/pluck/archive/'.$updateversion.'.tar.gz';
$tmp_name = "files/pluck".$updateversion.".tar.gz";
$tmp_name_tar = "files/pluck".$updateversion.".tar";
$tmp_name_folder = "files/pluck-".$updateversion;
copy($urlfile, $tmp_name);
//Unpack latest version
// decompress from gz
$p = new PharData($tmp_name);
$exts = explode('.', $tmp_name);
array_shift($exts);
array_pop($exts);
$ext = implode('.', $exts);
$p->decompress($ext);
// unarchive from the tar
$phar = new PharData($tmp_name_tar);
$phar->extractTo('files');
//After extraction: delete the tar.gz-file.
unlink($tmp_name);
unlink($tmp_name_tar);
//Move Data folders over {image, inc, modules\albums, modules\blog, modules\contactform. modules\multitheme, modules\tinymce, modules\viewsite, modules\.htaccess, index.html. reset.css}
rcopy($tmp_name_folder."/", ".");
rrmdir($tmp_name_folder);
echo "<p><a href=\"?module=updater\"><<< Back</a></p>";
}
function createSiteBackup(){
global $lang;
try
{
$date = date("Y-M-d");
$tarfilename = 'files/website-backup-'.$date.'.tar';
$a = new PharData($tarfilename);
// ADD FILES TO archive.tar FILE
$a->buildFromDirectory('.');
// COMPRESS archive.tar FILE. COMPRESSED FILE WILL BE archive.tar.gz
$a->compress(Phar::GZ);
// NOTE THAT BOTH FILES WILL EXISTS. SO IF YOU WANT YOU CAN UNLINK archive.tar
unlink($tarfilename);
}
catch (Exception $e)
{
echo "Exception : " . $e;
}
return $tarfilename.".gz";
}
function checkUpdate(){
//do update check here
$url = 'https://github.com/pluck-cms/pluck/releases/latest';
// Initialize session and set URL.
$geturl = curl_init();
curl_setopt($geturl, CURLOPT_URL, $url);
// Dont check ssl certifical
curl_setopt($geturl, CURLOPT_SSL_VERIFYPEER, false);
// Go redirect
curl_setopt($geturl, CURLOPT_FOLLOWLOCATION, true);
// Return data
curl_setopt($geturl, CURLOPT_RETURNTRANSFER, true);
// Get the response and close the channel.
$response = curl_exec($geturl);
curl_close($geturl);
// Find latest release
// <span class\=\"css-truncate-target\" style="max-width: 125px">4.7.11</span>
preg_match('/\<span class\=\"css-truncate-target\" style\=\"max-width: 125px\"\>(.*)\<\/span\>/', $response, $match);
// Current latest release string
$update_available = strip_tags($match[0]);
// Remove v char if we are using normal releases
$update_available = str_replace('v', '', $update_available);
return $update_available;
}
// Function to remove folders and files
function rrmdir($dir) {
if (is_dir($dir)) {
$files = scandir($dir);
foreach ($files as $file)
if ($file != "." && $file != "..") rrmdir("$dir/$file");
rmdir($dir);
}
else if (file_exists($dir)) unlink($dir);
}
// Function to Copy folders and files
function rcopy($src, $dst) {
// if (file_exists ( $dst ))
// rrmdir ( $dst );
if (is_dir ( $src ) && !file_exists ( $dst )) {
mkdir ( $dst );
}
if(is_dir($src)){
$files = scandir ( $src );
foreach ( $files as $file )
if ($file != "." && $file != "..")
rcopy ( "$src/$file", "$dst/$file" );
} else if (file_exists ( $src ))
copy ( $src, $dst );
}