-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate_util.php
More file actions
207 lines (167 loc) · 4.93 KB
/
update_util.php
File metadata and controls
207 lines (167 loc) · 4.93 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
<?php
define('PACKAGE_PATHNAME', DATA_PATH . '/update_package');
// Delete a file tree (from http://php.net/rmdir#110489)
function del_tree($dir) {
if (!is_dir($dir)) {
return true;
}
$files = array_diff(scandir($dir), array('.', '..'));
foreach ($files as $filename) {
$filename = $dir . '/' . $filename;
if (is_dir($filename)) {
@chmod($filename, 0777);
del_tree($filename);
} else {
unlink($filename);
}
}
return rmdir($dir);
}
// Do a recursive copy (from http://fr2.php.net/manual/en/function.copy.php#91010)
function recurse_copy($src, $dst) {
$dir = opendir($src);
@mkdir($dst);
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($src . '/' . $file)) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
} else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
return true;
}
// Remove previous backup of data.
function remove_data_backup() {
return del_tree(DATA_PATH . '.back');
}
// Create a backup of data.
function data_backup() {
$from = DATA_PATH;
$to = $from .'.back';
if (file_exists($from)) {
return recurse_copy($from, $to);
}
return false;
}
// Check if a directory exists and if it is writable.
function check_directory($dir) {
if (!file_exists($dir)) {
$res = @mkdir($dir, 0775);
if (!$res) {
return false;
}
}
if (!is_writable($dir)) {
$res = chmod($dir, 0775);
if (!$res) {
return false;
}
}
$index = $dir . '/index.html';
$data = <<<EOF
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB" lang="en-GB">
<head>
<meta charset="UTF-8" />
<meta http-equiv="Refresh" content="0; url=/" />
<title>Redirection</title>
<meta name="robots" content="noindex" />
</head>
<body>
<p><a href="/">Redirection</a></p>
</body>
</html>
EOF;
if (!file_exists($index)) {
@file_put_contents($index, $data);
}
return true;
}
// Save and unzip FreshRSS package.
function save_package($url) {
// First, download package from $url.
$zip_filename = PACKAGE_PATHNAME . '.zip';
$zip_file = fopen($zip_filename, 'w+');
$c = curl_init($url);
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_BINARYTRANSFER, true);
curl_setopt($c, CURLOPT_FILE, $zip_file);
curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
fclose($zip_file);
if ($status !== 200) {
return false;
}
// And unzip package in PACKAGE_PATHNAME (should be under DATA_PATHNAME).
$zip = new ZipArchive;
if ($zip->open($zip_filename) === false) {
return false;
}
$zip->extractTo(PACKAGE_PATHNAME);
$zip->close();
@unlink($zip_filename);
return true;
}
// Move custom themes to not delete them
function save_custom_themes($destination) {
$themes_base_dir = PUBLIC_PATH . '/themes';
$themes_dirs = array_diff(scandir($themes_base_dir), array('.', '..'));
foreach ($themes_dirs as $theme_dir) {
if (!preg_match('/^xTheme-/i', $theme_dir)) {
continue;
}
$old_theme_path = $themes_base_dir . '/' . $theme_dir;
$new_theme_path = $destination . '/' . $theme_dir;
recurse_copy($old_theme_path, $new_theme_path);
}
}
// Deploy FreshRSS package by replacing old version by the new one.
function deploy_package() {
$base_pathname = array_pop(array_diff(scandir(PACKAGE_PATHNAME),
array('.', '..')));
$base_pathname = PACKAGE_PATHNAME . '/' . $base_pathname;
save_custom_themes($base_pathname . '/p/themes');
// Remove old version.
del_tree(APP_PATH);
del_tree(LIB_PATH);
del_tree(PUBLIC_PATH);
del_tree(FRESHRSS_PATH . '/cli');
unlink(FRESHRSS_PATH . '/constants.php');
// Deprecated. Next lines could be removed. It was needed for an update from 1.10 previous to newer one. See #1812
$sharesPHPfilePath = DATA_PATH . '/shares.php';
if (file_exists($sharesPHPfilePath)) {
unlink($sharesPHPfilePath);
}
// Copy FRSS package at the good place.
recurse_copy($base_pathname . '/app', APP_PATH);
recurse_copy($base_pathname . '/lib', LIB_PATH);
recurse_copy($base_pathname . '/p', PUBLIC_PATH);
recurse_copy($base_pathname . '/cli', FRESHRSS_PATH . '/cli');
copy($base_pathname . '/constants.php', FRESHRSS_PATH . '/constants.php');
// These functions can fail because config.default.php has been introduced
// in the 0.9.4 version.
@copy($base_pathname . '/data/config.default.php',
DATA_PATH . '/config.default.php');
@copy($base_pathname . '/data/users/_/config.default.php',
DATA_PATH . '/users/_/config.default.php');
// These functions can fail because files have been moved in the 1.7.0
// version.
@copy($base_pathname . '/config.default.php',
FRESHRSS_PATH . '/config.default.php');
@copy($base_pathname . '/config-user.default.php',
FRESHRSS_PATH . '/config-user.default.php');
if (function_exists('opcache_reset')) {
opcache_reset();
}
return true;
}
// Remove files of FRSS package.
function clean_package() {
return del_tree(PACKAGE_PATHNAME);
}
// NOTE: don't remove this close tag!
?>