Skip to content

Commit

Permalink
COMCL-886: Apply CIVI-SA-2024-07
Browse files Browse the repository at this point in the history
Core commits

civicrm@f08aeac
  • Loading branch information
Muhammad Shahrukh authored and shahrukh-compuco committed Oct 25, 2024
1 parent 6d44c8b commit fba10f6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CRM/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function cleanup($value, $rmdir = TRUE) {

if ($value & 1) {
// clean templates_c
CRM_Utils_File::cleanDir($this->templateCompileDir, $rmdir);
CRM_Utils_File::cleanDir($this->templateCompileDir, $rmdir, FALSE);
CRM_Utils_File::createDir($this->templateCompileDir);
}
if ($value & 2) {
Expand Down
50 changes: 44 additions & 6 deletions CRM/Utils/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,47 @@ public static function cleanDir(string $target, bool $rmdir = TRUE, bool $verbos
throw new CRM_Core_Exception('Overly broad deletion');
}

$target = rtrim($target, '/' . DIRECTORY_SEPARATOR);

if (!file_exists($target) && !is_link($target)) {
return;
}

if (!is_dir($target)) {
CRM_Core_Session::setStatus(ts('cleanDir() can only remove directories. %1 is not a directory.', [1 => $target]), ts('Warning'), 'error');
return;
}

if (is_link($target) /* it's a directory based on a symlink... no need to recurse... */) {
if ($rmdir) {
static::try_unlink($target, 'symlink');
}
return;
}

if ($dh = @opendir($target)) {
while (FALSE !== ($sibling = readdir($dh))) {
if (!in_array($sibling, $exceptions)) {
$object = $target . DIRECTORY_SEPARATOR . $sibling;

if (is_dir($object)) {
CRM_Utils_File::cleanDir($object, $rmdir, $verbose);
if (is_link($object)) {
// Strangely, symlinks to directories under Windows need special treatment
if (PHP_OS_FAMILY === "Windows" && is_dir($object)) {
if (!rmdir($object)) {
CRM_Core_Session::setStatus(ts('Unable to remove directory symlink %1', [1 => $object]), ts('Warning'), 'error');
}
}
else {
CRM_Utils_File::try_unlink($object, "symlink");
}
}
elseif (is_dir($object)) {
CRM_Utils_File::cleanDir($object, TRUE, $verbose);
}
elseif (is_file($object)) {
if (!unlink($object)) {
CRM_Core_Session::setStatus(ts('Unable to remove file %1', [1 => $object]), ts('Warning'), 'error');
}
CRM_Utils_File::try_unlink($object, "file");
}
else {
CRM_Utils_File::try_unlink($object, "other filesystem object");
}
}
}
Expand All @@ -134,6 +163,15 @@ public static function cleanDir(string $target, bool $rmdir = TRUE, bool $verbos
}
}

/**
* Helper function to avoid repetition in cleanDir: execute unlink and produce a warning on failure.
*/
private static function try_unlink($object, $description) {
if (!unlink($object)) {
CRM_Core_Session::setStatus(ts('Unable to remove %1 %2', [1 => $description, 2 => $object]), ts('Warning'), 'error');
}
}

/**
* Concatenate several files.
*
Expand Down

0 comments on commit fba10f6

Please sign in to comment.