From a2df1f2d91f88f9fdf97750e6c6cb48da7b08509 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 26 Jun 2017 23:35:36 -0700 Subject: [PATCH] CRM-20243 - Add check for deleted files CRM-20243 - CRM_Upgrade_Form - Delete orphaned files before upgrade CRM-20243 - CRM_Utils_Check - Simplify Auth/SASL checks CRM-20243 Attempt to alter file deletion for use in 4.6 --- CRM/Upgrade/Form.php | 48 +++++++++++++++++ CRM/Utils/Check/Source.php | 105 +++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 CRM/Utils/Check/Source.php diff --git a/CRM/Upgrade/Form.php b/CRM/Upgrade/Form.php index 58b34641be..96c8648d69 100644 --- a/CRM/Upgrade/Form.php +++ b/CRM/Upgrade/Form.php @@ -577,6 +577,13 @@ SET version = '$version' 'reset' => TRUE, )); + $task = new CRM_Queue_Task( + array('CRM_Upgrade_Form', 'doFileCleanup'), + array($postUpgradeMessageFile), + "Cleanup old files" + ); + $queue->createItem($task); + $revisions = $upgrade->getRevisionSequence(); foreach ($revisions as $rev) { // proceed only if $currentVer < $rev @@ -613,6 +620,47 @@ SET version = '$version' return $queue; } + /** + * Find any old, orphaned files that should have been deleted. + * + * These files can get left behind, eg, if you use the Joomla + * upgrade procedure. + * + * The earlier we can do this, the better - don't want upgrade logic + * to inadvertently rely on old/relocated files. + * + * @param \CRM_Queue_TaskContext $ctx + * @param string $postUpgradeMessageFile + * @return bool + */ + public static function doFileCleanup(CRM_Queue_TaskContext $ctx, $postUpgradeMessageFile) { + $source = new CRM_Utils_Check_Source(); + $files = $source->findOrphanedFiles(); + $errors = array(); + foreach ($files as $file) { + if (is_dir($file['path'])) { + @rmdir($file['path']); + } + else { + @unlink($file['path']); + } + + if (file_exists($file['path'])) { + $errors[] = sprintf("
  • %s
  • ", htmlentities($file['path'])); + } + } + + if (!empty($errors)) { + file_put_contents($postUpgradeMessageFile, + '

    ' . ts('Some old files could not be removed. Please remove them.') + . '', + FILE_APPEND + ); + } + + return TRUE; + } + /** * Perform an incremental version update. * diff --git a/CRM/Utils/Check/Source.php b/CRM/Utils/Check/Source.php new file mode 100644 index 0000000000..178af70525 --- /dev/null +++ b/CRM/Utils/Check/Source.php @@ -0,0 +1,105 @@ +getRemovedFiles() as $file) { + if (file_exists($file)) { + $orphans[] = array( + 'name' => $file, + 'path' => $file, + ); + } + } + + usort($orphans, function ($a, $b) { + // Children first, then parents. + $diff = strlen($b['name']) - strlen($a['name']); + if ($diff !== 0) { + return $diff; + } + if ($a['name'] === $b['name']) { + return 0; + } + return $a['name'] < $b['name'] ? -1 : 1; + }); + + return $orphans; + } + + /** + * @return array + */ + public function checkOrphans() { + $orphans = $this->findOrphanedFiles(); + if (empty($orphans)) { + return array(); + } + + $messages = array(); + $messages[] = new CRM_Utils_Check_Message( + __FUNCTION__, + ts('The local system includes old files which should not exist: "%1"', + array( + 1 => implode('", "', CRM_Utils_Array::collect('path', $orphans)), + )), + ts('Old files') + ); + + return $messages; + } + +} -- 2.25.1