+
+
+function findOnlyDirectory($topDir) {
+ // Ensure the directory exists and is indeed a directory
+ if (!is_dir($topDir)) {
+ die("The specified path is not a directory.");
+ }
+
+ // Open the directory
+ $dirHandle = opendir($topDir);
+ if ($dirHandle === false) {
+ die("Unable to open directory.");
+ }
+
+ $directories = [];
+
+ // Read through the directory contents
+ while (($entry = readdir($dirHandle)) !== false) {
+ $fullPath = $topDir . DIRECTORY_SEPARATOR . $entry;
+ // Check if the entry is a directory and not . or ..
+ if (is_dir($fullPath) && $entry !== '.' && $entry !== '..') {
+ $directories[] = $fullPath;
+ }
+ }
+
+ // Close the directory handle
+ closedir($dirHandle);
+
+ // Check if there is exactly one directory
+ if (count($directories) === 1) {
+ return $directories[0];
+ } elseif (count($directories) === 0) {
+ return "No directories found after extracting. Possibly missing php-zip extention. Aborting upgrade.";
+ } else {
+ return "Multiple directories found. Previous cleanup was unsuccessful for some reason, maybe a permissions error? Aborting upgrade.";
+ }
+}
+
+
+function deleteDirectoryContents($dir) {
+ error_log("Deleting directory contents at $dir");
+ if (!is_dir($dir)) {
+ echo "The provided path is not a directory.";
+ return false;
+ }
+
+ // Open the directory
+ $handle = opendir($dir);
+ if ($handle === false) {
+ echo "Failed to open the directory.";
+ return false;
+ }
+
+ // Loop through the directory contents
+ while (($item = readdir($handle)) !== false) {
+ // Skip the special entries "." and ".."
+ if ($item == "." || $item == "..") {
+ continue;
+ }
+
+ $itemPath = $dir."/".$item;
+
+ // If the item is a directory, recursively delete its contents
+ if (is_dir($itemPath)) {
+ deleteDirectoryContents($itemPath);
+ // Remove the empty directory
+ rmdir($itemPath);
+ } else {
+ // If the item is a file, delete it
+ unlink($itemPath);
+ }
+ }
+
+ // Close the directory handle
+ closedir($handle);
+
+ return true;
+}
+
+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);
+}