-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
finalisation.php
275 lines (242 loc) · 8.61 KB
/
finalisation.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
<?php
/**
* @package Joomla.Administrator
* @subpackage com_joomlaupdate
*
* @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*
* Important Notes:
* - Unlike other files, this file requires multiple namespace declarations in order to overload core classes during the update process
* - Also unlike other files, the normal constant defined checks must be within the global namespace declaration and can't be outside of it
*/
namespace
{
// Require the restoration environment or fail cold. Prevents direct web access.
\defined('_JOOMLA_UPDATE') or die();
// Fake a miniature Joomla environment
if (!\defined('_JEXEC')) {
\define('_JEXEC', 1);
}
if (!\function_exists('jimport')) {
/**
* This is deprecated but it may still be used in the update finalisation script.
*
* @param string $path Ignored.
* @param string $base Ignored.
*
* @return boolean Always true.
*
* @since 1.7.0
*/
function jimport(string $path, ?string $base = null): bool
{
// Do nothing
return true;
}
}
if (!\function_exists('finalizeUpdate')) {
/**
* Run part of the Joomla! finalisation script, namely the part that cleans up unused files/folders
*
* @param string $siteRoot The root to the Joomla! site
* @param string $restorePath The base path to extract.php
*
* @return void
*
* @since 3.5.1
*/
function finalizeUpdate(string $siteRoot, string $restorePath): void
{
if (!\defined('JPATH_ROOT')) {
\define('JPATH_ROOT', $siteRoot);
}
$filePath = JPATH_ROOT . '/administrator/components/com_admin/script.php';
if (file_exists($filePath)) {
require_once $filePath;
}
// Make sure Joomla!'s code can figure out which files exist and need be removed
clearstatcache();
// Remove obsolete files - prevents errors occurring in some system plugins
if (class_exists('JoomlaInstallerScript')) {
(new JoomlaInstallerScript())->deleteUnexistingFiles();
}
/**
* Remove autoload_psr4.php so that namespace map is re-generated on the next request. This is needed
* when there are new classes added to extensions on new Joomla! release.
*/
$namespaceMapFile = JPATH_ROOT . '/administrator/cache/autoload_psr4.php';
if (is_file($namespaceMapFile)) {
\Joomla\Filesystem\File::delete($namespaceMapFile);
}
}
}
}
namespace Joomla\Filesystem
{
// Fake the File class
if (!class_exists('\Joomla\Filesystem\File')) {
/**
* File mock class
*
* @since 3.5.1
*/
abstract class File
{
/**
* Proxies checking a file exists to the native php version
*
* @param string $fileName The path to the file to be checked
*
* @return boolean
*
* @since 3.5.1
*/
public static function exists(string $fileName): bool
{
return @file_exists($fileName);
}
/**
* Delete a file and invalidate the PHP OPcache
*
* @param string $fileName The path to the file to be deleted
*
* @return boolean
*
* @since 3.5.1
*/
public static function delete(string $fileName): bool
{
self::invalidateFileCache($fileName);
return @unlink($fileName);
}
/**
* Rename a file and invalidate the PHP OPcache
*
* @param string $src The path to the source file
* @param string $dest The path to the destination file
*
* @return boolean True on success
*
* @since 4.0.1
*/
public static function move(string $src, string $dest): bool
{
self::invalidateFileCache($src);
$result = @rename($src, $dest);
if ($result) {
self::invalidateFileCache($dest);
}
return $result;
}
/**
* Invalidate opcache for a newly written/deleted file immediately, if opcache* functions exist and if this was a PHP file.
*
* @param string $filepath The path to the file just written to, to flush from opcache
* @param boolean $force If set to true, the script will be invalidated regardless of whether invalidation is necessary
*
* @return boolean TRUE if the opcode cache for script was invalidated/nothing to invalidate,
* or FALSE if the opcode cache is disabled or other conditions returning
* FALSE from opcache_invalidate (like file not found).
*
* @since 4.0.2
*/
public static function invalidateFileCache($filepath, $force = true)
{
return clearFileInOPCache($filepath);
}
}
}
// Fake the Folder class, mapping it to Restore's post-processing class
if (!class_exists('\Joomla\Filesystem\Folder')) {
/**
* Folder mock class
*
* @since 3.5.1
*/
abstract class Folder
{
/**
* Proxies checking a folder exists to the native php version
*
* @param string $folderName The path to the folder to be checked
*
* @return boolean
*
* @since 3.5.1
*/
public static function exists(string $folderName): bool
{
return @is_dir($folderName);
}
/**
* Delete a folder recursively and invalidate the PHP OPcache
*
* @param string $folderName The path to the folder to be deleted
*
* @return boolean
*
* @since 3.5.1
*/
public static function delete(string $folderName): bool
{
if (substr($folderName, -1) == '/') {
$folderName = substr($folderName, 0, -1);
}
if (!@file_exists($folderName) || !@is_dir($folderName) || !is_readable($folderName)) {
return false;
}
$di = new \DirectoryIterator($folderName);
/** @var \DirectoryIterator $item */
foreach ($di as $item) {
if ($item->isDot()) {
continue;
}
if ($item->isDir()) {
$status = self::delete($item->getPathname());
if (!$status) {
return false;
}
continue;
}
clearFileInOPCache($item->getPathname());
@unlink($item->getPathname());
}
return @rmdir($folderName);
}
}
}
if (!class_exists('\Joomla\CMS\Filesystem\File')) {
class_alias('\\Joomla\\Filesystem\\File', '\\Joomla\\CMS\\Filesystem\\File');
}
if (!class_exists('\Joomla\CMS\Filesystem\Folder')) {
class_alias('\\Joomla\\Filesystem\\Folder', '\\Joomla\\CMS\\Filesystem\\Folder');
}
}
namespace Joomla\CMS\Language
{
// Fake the Text class - we aren't going to show errors to people anyhow
if (!class_exists('\Joomla\CMS\Language\Text')) {
/**
* Text mock class
*
* @since 3.5.1
*/
abstract class Text
{
/**
* No need for translations in a non-interactive script, so always return an empty string here
*
* @param string $text A language constant
*
* @return string
*
* @since 3.5.1
*/
public static function sprintf(string $text): string
{
return '';
}
}
}
}