From c2b8c152bc6624cb6b701512f8c43b8200cf83be Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 6 Apr 2019 13:15:52 +0100 Subject: [PATCH] Implement unpacking logic --- core/05-functions.php | 23 +++++++++++++++++++++++ core/07-unpack.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 core/07-unpack.php diff --git a/core/05-functions.php b/core/05-functions.php index d414cca..fde0628 100644 --- a/core/05-functions.php +++ b/core/05-functions.php @@ -623,3 +623,26 @@ function email_users($usernames, $subject, $body) } return $emailsSent; } + +/** + * Recursively deletes a directory and it's contents. + * Adapted by Starbeamrainbowlabs + * @param string $path The path to the directory to delete. + * @param bool $delete_self Whether to delete the top-level directory. Set this to false to delete only a directory's contents + * @source https://stackoverflow.com/questions/4490637/recursive-delete + */ +function delete_recursive($path, $delete_self = true) { + $it = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($path), + RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ($it as $file) { + if (in_array($file->getBasename(), [".", ".."])) + continue; + if($file->isDir()) + rmdir($file->getPathname()); + else + unlink($file->getPathname()); + } + if($delete_self) rmdir($path); +} diff --git a/core/07-unpack.php b/core/07-unpack.php new file mode 100644 index 0000000..067cbf4 --- /dev/null +++ b/core/07-unpack.php @@ -0,0 +1,28 @@ +extra_data_directory) || + filemtime(__FILE__) > filemtime($paths->extra_data_directory)) { + if(file_exists($paths->extra_data_directory)) + delete_recursive($paths->extra_data_directory, false); + else + mkdir($paths->extra_data_directory, 0700); + touch($paths->extra_data_directory); + + $temp_file = tmpfile(); + $source = fopen(__FILE__, "r"); + + fseek($source, __COMPILER_HALT_OFFSET__); + stream_copy_to_stream($source, $temp_file); + + $temp_filename = stream_get_meta_data($temp_file)["uri"]; + + $extractor = new ZipArchive(); + $extractor->open($temp_filename); + $extractor->extractTo($paths->extra_data_directory); + $extractor->close(); + fclose($temp_file); +}