1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-12-22 01:35:02 +00:00

Improve resilience and error output if the PHP Zip extension is not installed on first run

This commit is contained in:
Starbeamrainbowlabs 2021-07-20 23:15:48 +01:00
parent 256d6a59e6
commit 2e54a8a4d5
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 28 additions and 1 deletions

View file

@ -20,6 +20,8 @@ This file holds the changelog for Pepperminty Wiki. This is the master list of t
- [security] Fixed some potential XSS attacks in the page editor
- Fixed a weird bug in the `stats-update` action causing warnings
- search: Properly apply weightings of matches in page titles and tags
- Improved error handling on first run where the PHP Zip extension is not installed
- Also extract to `._extra_data` if the directory is empty
## v0.22

View file

@ -163,6 +163,24 @@ function path_resolve(string $path, string $basePath = null) {
return implode(DIRECTORY_SEPARATOR, $components);
}
/**
* Determines if a directory is empty or not.
* @ref https://stackoverflow.com/a/7497848/1460422
* @param string $dir The path to the directory to check.
* @return boolean True if the directory is empty, or false if it is not empty.
*/
function is_directory_empty(string $dir) : bool {
$handle = opendir($dir);
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
closedir($handle);
return false;
}
}
closedir($handle);
return true;
}
/**
* Converts a filepath to a page name.
* @param string $filepath The filepath to convert.

View file

@ -7,9 +7,11 @@
// If the extra data directory:
// - doesn't exist already
// - has an mtime before that of this file
// - is empty
// ...extract it again
if(!file_exists($paths->extra_data_directory) ||
filemtime(__FILE__) > filemtime($paths->extra_data_directory)) {
filemtime(__FILE__) > filemtime($paths->extra_data_directory)
|| is_directory_empty($paths->extra_data_directory)) {
$error_message_help = "<p>Have you checked that PHP has write access to the directory that <code>index.php</code> is located in (and all it's contents and subdirectories)? Try <code>sudo chown USERNAME:USERNAME -R path/to/directory</code> and <code>sudo chmod -R 0644 path/to/directory; sudo chmod -R +X path/too/directory</code>, where <code>USERNAME</code> is the username that the PHP process is running under.</p>";
@ -44,6 +46,11 @@ if(!file_exists($paths->extra_data_directory) ||
fclose($temp_file);
$extractor = new ZipArchive();
if(!class_exists("ZipArchive") || !($extractor instanceof ZipArchive)) {
if(file_exists($paths->extra_data_directory))
delete_recursive($paths->extra_data_directory);
exit(page_renderer::render_minimal("Unpacking error - $settings->sitename", "<p>Oops! $settings->sitename wasn't able to unpack itself because the ZipArchive doesn't exist or is faulty. Please install the PHP zip extension (on apt-based systems it's the <code>php-zip</code> package) and then try again later. You can check that it's installed by inspecting the output of <code>php -m</code>, or running the <a href='https://www.php.net/manual/en/function.phpinfo.php'><code>phpinfo()</code> command</a>."));
}
$extractor->open($temp_filename);
$extractor->extractTo($paths->extra_data_directory);
$extractor->close();