From 59a3662b03fb08d1740422519f561f3a86310c3e Mon Sep 17 00:00:00 2001 From: Duncan Cameron Date: Sun, 19 Dec 2021 14:28:14 +0000 Subject: [PATCH] Throw an exception when downloading or unzipping the new release file fails. Catch any unhandled exception to be able to send a json response to the browser. --- index.php | 376 +++++++++++++++++++++++++++--------------------------- 1 file changed, 191 insertions(+), 185 deletions(-) diff --git a/index.php b/index.php index 2eb3fa7..2f6f152 100755 --- a/index.php +++ b/index.php @@ -414,7 +414,7 @@ function downloadUpdate() curl_setopt($ch, CURLOPT_FILE, fopen($zipFile, 'w+')); $page = curl_exec($ch); if (!$page) { - echo "Error :- " . curl_error($ch); + throw new \UpdateException('Error: ' . curl_error($ch)); } curl_close($ch); @@ -589,7 +589,9 @@ function unZipFiles($toBeExtracted, $extractPath) throw new \UpdateException("Error: Unable to open the Zip File"); } /* Extract Zip File */ - $zip->extractTo($extractPath); + if (!$zip->extractTo($extractPath)) { + throw new \UpdateException("Error: Unable to extract the Zip File"); + } $zip->close(); } @@ -769,8 +771,8 @@ function replaceNewUpdater() $update->checkConfig(); $update->checkphpmodules(); -} catch (\UpdateException $e) { - throw $e; +} catch (\Exception $e) { + die($e->getMessage()); } /** @@ -779,194 +781,198 @@ function replaceNewUpdater() * */ if (isset($_POST['action'])) { - set_time_limit(0); - - //ensure that $action is integer - - $action = (int)$_POST['action']; - - header('Content-Type: application/json'); - $writeStep = true; - switch ($action) { - case 0: - $statusJson = $update->currentUpdateStep(); - echo json_encode(array('status' => $statusJson, 'autocontinue' => true)); - break; - case 1: - $updateMessage = $update->checkIfThereIsAnUpdate(); - $isThereAnUpdate = $update->availableUpdate(); - if ($isThereAnUpdate === false) { - echo(json_encode(array('continue' => false, 'response' => $updateMessage))); - } else { - echo(json_encode(array('continue' => true, 'response' => $updateMessage))); - } - break; - case 2: - echo(json_encode(array('continue' => true, 'autocontinue' => true, 'response' => 'Starting integrity check'))); - break; - case 3: - $unexpectedFiles = $update->checkRequiredFiles(); - if (count($unexpectedFiles) !== 0) { - $elements = "Error: The following files are either not expected and should be removed, or are missing but required and should be put back in place \n"; - foreach ($unexpectedFiles as $key => $fileName) { - $elements .= $key . "\n"; + try { + set_time_limit(0); + + //ensure that $action is integer + + $action = (int)$_POST['action']; + + header('Content-Type: application/json'); + $writeStep = true; + switch ($action) { + case 0: + $statusJson = $update->currentUpdateStep(); + echo json_encode(array('status' => $statusJson, 'autocontinue' => true)); + break; + case 1: + $updateMessage = $update->checkIfThereIsAnUpdate(); + $isThereAnUpdate = $update->availableUpdate(); + if ($isThereAnUpdate === false) { + echo(json_encode(array('continue' => false, 'response' => $updateMessage))); + } else { + echo(json_encode(array('continue' => true, 'response' => $updateMessage))); } - echo(json_encode(array('retry' => true, 'continue' => false, 'response' => $elements))); - } else { - echo(json_encode(array('continue' => true, 'response' => 'Integrity check successful', 'autocontinue' => true))); - } - break; - case 4: - $notWriteableFiles = $update->checkWritePermissions(); - if (count($notWriteableFiles) !== 0) { - $notWriteableElements = "Error: No write permission for the following files: \n";; - foreach ($notWriteableFiles as $key => $fileName) { - $notWriteableElements .= $fileName . "\n"; + break; + case 2: + echo(json_encode(array('continue' => true, 'autocontinue' => true, 'response' => 'Starting integrity check'))); + break; + case 3: + $unexpectedFiles = $update->checkRequiredFiles(); + if (count($unexpectedFiles) !== 0) { + $elements = "Error: The following files are either not expected and should be removed, or are missing but required and should be put back in place \n"; + foreach ($unexpectedFiles as $key => $fileName) { + $elements .= $key . "\n"; + } + echo(json_encode(array('retry' => true, 'continue' => false, 'response' => $elements))); + } else { + echo(json_encode(array('continue' => true, 'response' => 'Integrity check successful', 'autocontinue' => true))); } - echo(json_encode(array('retry' => true, 'continue' => false, 'response' => $notWriteableElements))); - } else { - echo(json_encode(array('continue' => true, 'response' => 'Write check successful.', 'autocontinue' => true))); - } - break; - case 5: - echo(json_encode(array('continue' => true, 'response' => 'Do you want a backup?
Yes
No
'))); - break; - case 6: - $createBackup = $_POST['create_backup']; - if ($createBackup === 'true') { - echo(json_encode(array('continue' => true, 'response' => 'Choose location where to backup the /lists directory. Please make sure to choose a location outside the web root:
'))); - } else { - echo(json_encode(array('continue' => true, 'response' => '', 'autocontinue' => true))); - } - break; - case 7: - $createBackup = $_POST['create_backup']; - if ($createBackup === 'true') { - $backupLocation = realpath(dirname($_POST['backup_location'])); - $phplistRootFolder = realpath(__DIR__ . '/../../'); - if (strpos($backupLocation, $phplistRootFolder) === 0) { - echo(json_encode(array('retry' => true, 'continue' => false, 'response' => 'Error: Please choose a folder outside of your phpList installation.'))); - break; + break; + case 4: + $notWriteableFiles = $update->checkWritePermissions(); + if (count($notWriteableFiles) !== 0) { + $notWriteableElements = "Error: No write permission for the following files: \n";; + foreach ($notWriteableFiles as $key => $fileName) { + $notWriteableElements .= $fileName . "\n"; + } + echo(json_encode(array('retry' => true, 'continue' => false, 'response' => $notWriteableElements))); + } else { + echo(json_encode(array('continue' => true, 'response' => 'Write check successful.', 'autocontinue' => true))); } - if (!preg_match("/^.*\.(zip)$/i", $_POST['backup_location'])) { - echo(json_encode(array('retry' => true, 'continue' => false, 'response' => 'Error: Please add .zip extension.'))); - break; + break; + case 5: + echo(json_encode(array('continue' => true, 'response' => 'Do you want a backup?
Yes
No
'))); + break; + case 6: + $createBackup = $_POST['create_backup']; + if ($createBackup === 'true') { + echo(json_encode(array('continue' => true, 'response' => 'Choose location where to backup the /lists directory. Please make sure to choose a location outside the web root:
'))); + } else { + echo(json_encode(array('continue' => true, 'response' => '', 'autocontinue' => true))); } + break; + case 7: + $createBackup = $_POST['create_backup']; + if ($createBackup === 'true') { + $backupLocation = realpath(dirname($_POST['backup_location'])); + $phplistRootFolder = realpath(__DIR__ . '/../../'); + if (strpos($backupLocation, $phplistRootFolder) === 0) { + echo(json_encode(array('retry' => true, 'continue' => false, 'response' => 'Error: Please choose a folder outside of your phpList installation.'))); + break; + } + if (!preg_match("/^.*\.(zip)$/i", $_POST['backup_location'])) { + echo(json_encode(array('retry' => true, 'continue' => false, 'response' => 'Error: Please add .zip extension.'))); + break; + } + try { + $update->backUpFiles($_POST['backup_location']); + echo(json_encode(array('continue' => true, 'response' => 'Backup has been created'))); + } catch (\Exception $e) { + echo(json_encode(array('retry' => true, 'continue' => false, 'response' => $e->getMessage()))); + break; + } + } else { + echo(json_encode(array('continue' => true, 'response' => 'No back up created', 'autocontinue' => true))); + } + + break; + case 8: + echo(json_encode(array('continue' => true, 'autocontinue' => true, 'response' => 'Download in progress'))); + break; + case 9: try { - $update->backUpFiles($_POST['backup_location']); - echo(json_encode(array('continue' => true, 'response' => 'Backup has been created'))); + $update->downloadUpdate(); + echo(json_encode(array('continue' => true, 'response' => 'The update has been downloaded!'))); } catch (\Exception $e) { - echo(json_encode(array('retry' => true, 'continue' => false, 'response' => $e->getMessage()))); - break; + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); } - } else { - echo(json_encode(array('continue' => true, 'response' => 'No back up created', 'autocontinue' => true))); - } - - break; - case 8: - echo(json_encode(array('continue' => true, 'autocontinue' => true, 'response' => 'Download in progress'))); - break; - case 9: - try { - $update->downloadUpdate(); - echo(json_encode(array('continue' => true, 'response' => 'The update has been downloaded!'))); - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - case 10: - $on = $update->addMaintenanceMode(); - if ($on === false) { - echo(json_encode(array('continue' => false, 'response' => 'Cannot set the maintenance mode on!'))); - } else { - echo(json_encode(array('continue' => true, 'response' => 'Set maintenance mode on', 'autocontinue' => true))); - } - break; - case 11: - try { - $update->replacePHPEntryPoints(); - echo(json_encode(array('continue' => true, 'response' => 'Replaced entry points', 'autocontinue' => true))); - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - case 12: - try { - $update->movePluginsInTempFolder(); - echo(json_encode(array('continue' => true, 'response' => 'Backing up the plugins', 'autocontinue' => true))); - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - case 13: - try { - $update->deleteFiles(); - echo(json_encode(array('continue' => true, 'response' => 'Old files have been deleted!', 'autocontinue' => true))); - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - case 14: - try { - $update->moveNewFiles(); - echo(json_encode(array('continue' => true, 'response' => 'Moved new files in place!', 'autocontinue' => true))); - - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - case 15: - try { - $update->movePluginsInPlace(); - echo(json_encode(array('continue' => true, 'response' => 'Moved plugins in place!', 'autocontinue' => true))); - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - case 16: - try { - $update->moveEntryPHPpoints(); - echo(json_encode(array('continue' => true, 'response' => 'Moved new entry points in place!', 'autocontinue' => true))); - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - case 17: - try { - $update->moveUpdater(); - echo(json_encode(array('continue' => true, 'response' => 'Moved new entry points in place!', 'autocontinue' => true))); - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - case 18: - try { - $update->deleteTemporaryFiles(); - echo(json_encode(array('continue' => true, 'response' => 'Deleted temporary files!', 'autocontinue' => true))); - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - case 19: - try { - $update->removeMaintenanceMode(); - echo(json_encode(array('continue' => true, 'response' => 'Removed maintenance mode', 'autocontinue' => true))); - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - case 20: - $writeStep = false; - try { - $update->replaceNewUpdater(); - $update->deauthUpdaterSession(); - echo(json_encode(array('continue' => true, 'nextUrl' => '../admin/', 'response' => 'Updated successfully.'))); - } catch (\Exception $e) { - echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); - } - break; - }; + break; + case 10: + $on = $update->addMaintenanceMode(); + if ($on === false) { + echo(json_encode(array('continue' => false, 'response' => 'Cannot set the maintenance mode on!'))); + } else { + echo(json_encode(array('continue' => true, 'response' => 'Set maintenance mode on', 'autocontinue' => true))); + } + break; + case 11: + try { + $update->replacePHPEntryPoints(); + echo(json_encode(array('continue' => true, 'response' => 'Replaced entry points', 'autocontinue' => true))); + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); + } + break; + case 12: + try { + $update->movePluginsInTempFolder(); + echo(json_encode(array('continue' => true, 'response' => 'Backing up the plugins', 'autocontinue' => true))); + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); + } + break; + case 13: + try { + $update->deleteFiles(); + echo(json_encode(array('continue' => true, 'response' => 'Old files have been deleted!', 'autocontinue' => true))); + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); + } + break; + case 14: + try { + $update->moveNewFiles(); + echo(json_encode(array('continue' => true, 'response' => 'Moved new files in place!', 'autocontinue' => true))); + + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); + } + break; + case 15: + try { + $update->movePluginsInPlace(); + echo(json_encode(array('continue' => true, 'response' => 'Moved plugins in place!', 'autocontinue' => true))); + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); + } + break; + case 16: + try { + $update->moveEntryPHPpoints(); + echo(json_encode(array('continue' => true, 'response' => 'Moved new entry points in place!', 'autocontinue' => true))); + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); + } + break; + case 17: + try { + $update->moveUpdater(); + echo(json_encode(array('continue' => true, 'response' => 'Moved new entry points in place!', 'autocontinue' => true))); + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); + } + break; + case 18: + try { + $update->deleteTemporaryFiles(); + echo(json_encode(array('continue' => true, 'response' => 'Deleted temporary files!', 'autocontinue' => true))); + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); + } + break; + case 19: + try { + $update->removeMaintenanceMode(); + echo(json_encode(array('continue' => true, 'response' => 'Removed maintenance mode', 'autocontinue' => true))); + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); + } + break; + case 20: + $writeStep = false; + try { + $update->replaceNewUpdater(); + $update->deauthUpdaterSession(); + echo(json_encode(array('continue' => true, 'nextUrl' => '../admin/', 'response' => 'Updated successfully.'))); + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => $e->getMessage()))); + } + break; + } + } catch (\Exception $e) { + echo(json_encode(array('continue' => false, 'response' => 'Error: ' . $e->getMessage()))); + } if ($writeStep) { try {