Skip to content

Commit 8871a7b

Browse files
richard67QuyTon
andauthored
[4.4] Improve error messages of uploaded package check in Joomla Update (#41947)
* Joomlaupdate better error message - downgrade * Joomlaupdate better error message - download links * Joomlaupdate better error message - package naming * Joomlaupdate better error messages * Add name of selected package to error messages * Use escaped double quotes nstead of single quotes for html properties --------- Co-authored-by: Quy <[email protected]>
1 parent 2c0296b commit 8871a7b

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

administrator/components/com_joomlaupdate/src/Model/UpdateModel.php

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -985,9 +985,9 @@ public function upload()
985985

986986
// Check the uploaded file (throws RuntimeException when a check failed)
987987
if (\extension_loaded('zip')) {
988-
$this->checkPackageFileZip($userfile['tmp_name']);
988+
$this->checkPackageFileZip($userfile['tmp_name'], $userfile['name']);
989989
} else {
990-
$this->checkPackageFileNoZip($userfile['tmp_name']);
990+
$this->checkPackageFileNoZip($userfile['tmp_name'], $userfile['name']);
991991
}
992992

993993
// Build the appropriate paths.
@@ -1814,64 +1814,66 @@ public function collectError(string $context, \Throwable $error)
18141814
/**
18151815
* Check the update package with ZipArchive class from zip PHP extension
18161816
*
1817-
* @param string $filePath Full path to the update package to test
1817+
* @param string $filePath Full path to the uploaded update package (temporary file) to test
1818+
* @param string $packageName Name of the selected update package
18181819
*
18191820
* @return void
18201821
*
18211822
* @since 4.4.0
18221823
* @throws \RuntimeException
18231824
*/
1824-
private function checkPackageFileZip(string $filePath)
1825+
private function checkPackageFileZip(string $filePath, $packageName)
18251826
{
18261827
$zipArchive = new \ZipArchive();
18271828

18281829
if ($zipArchive->open($filePath) !== true) {
1829-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN'), 500);
1830+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN', $packageName), 500);
18301831
}
18311832

18321833
if ($zipArchive->locateName('installation/index.php') !== false) {
1833-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_INSTALL_PACKAGE'), 500);
1834+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_INSTALL_PACKAGE', $packageName), 500);
18341835
}
18351836

18361837
$manifestFile = $zipArchive->getFromName('administrator/manifests/files/joomla.xml');
18371838

18381839
if ($manifestFile === false) {
1839-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_MANIFEST_FILE'), 500);
1840+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_MANIFEST_FILE', $packageName), 500);
18401841
}
18411842

1842-
$this->checkManifestXML($manifestFile);
1843+
$this->checkManifestXML($manifestFile, $packageName);
18431844
}
18441845

18451846
/**
18461847
* Check the update package without using the ZipArchive class from zip PHP extension
18471848
*
1848-
* @param string $filePath Full path to the update package to test
1849+
* @param string $filePath Full path to the uploaded update package (temporary file) to test
1850+
* @param string $packageName Name of the selected update package
18491851
*
18501852
* @return void
18511853
*
18521854
* @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
18531855
* @since 4.4.0
18541856
* @throws \RuntimeException
18551857
*/
1856-
private function checkPackageFileNoZip(string $filePath)
1858+
private function checkPackageFileNoZip(string $filePath, $packageName)
18571859
{
18581860
// The file must exist and be readable
18591861
if (!file_exists($filePath) || !is_readable($filePath)) {
1860-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN'), 500);
1862+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN', $packageName), 500);
18611863
}
18621864

18631865
// The file must be at least 1KiB (anything less is not even a real file!)
18641866
$filesize = filesize($filePath);
18651867

18661868
if ($filesize < 1024) {
1867-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN'), 500);
1869+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN', $packageName), 500);
18681870
}
18691871

18701872
// Open the file
18711873
$fp = @fopen($filePath, 'rb');
18721874

18731875
if ($fp === false) {
1874-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN'), 500);
1876+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN', $packageName), 500);
18751877
}
18761878

18771879
// Read chunks of max. 1MiB size
@@ -1898,7 +1900,7 @@ private function checkPackageFileNoZip(string $filePath)
18981900
if ($fileChunk === false || strlen($fileChunk) !== $readsize) {
18991901
@fclose($fp);
19001902

1901-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN'), 500);
1903+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN', $packageName), 500);
19021904
}
19031905

19041906
$posFirstHeader = strpos($fileChunk, $headerSignature);
@@ -1917,7 +1919,7 @@ private function checkPackageFileNoZip(string $filePath)
19171919
if (substr($fileChunk, $pos - 46, 4) == $headerSignature && substr($fileChunk, $pos - 18, 2) == $sizeSignatureIndexPhp) {
19181920
@fclose($fp);
19191921

1920-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_INSTALL_PACKAGE'), 500);
1922+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_INSTALL_PACKAGE', $packageName), 500);
19211923
}
19221924

19231925
$offset = $pos + 22;
@@ -1952,14 +1954,14 @@ private function checkPackageFileNoZip(string $filePath)
19521954
if (!$headerFound) {
19531955
@fclose($fp);
19541956

1955-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN'), 500);
1957+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN', $packageName), 500);
19561958
}
19571959

19581960
// If no central directory file header found for the manifest XML file it's not a valid Joomla package
19591961
if (!$headerInfo) {
19601962
@fclose($fp);
19611963

1962-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_MANIFEST_FILE'), 500);
1964+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_MANIFEST_FILE', $packageName), 500);
19631965
}
19641966

19651967
// Read the local file header of the manifest XML file
@@ -1972,7 +1974,7 @@ private function checkPackageFileNoZip(string $filePath)
19721974
if (!$localHeaderInfo['Compressed']) {
19731975
@fclose($fp);
19741976

1975-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_MANIFEST_FILE'), 500);
1977+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_MANIFEST_FILE', $packageName), 500);
19761978
}
19771979

19781980
// Read the compressed manifest XML file content
@@ -2002,34 +2004,35 @@ private function checkPackageFileNoZip(string $filePath)
20022004
}
20032005

20042006
if (!$manifestFile) {
2005-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_MANIFEST_FILE'), 500);
2007+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_MANIFEST_FILE', $packageName), 500);
20062008
}
20072009

2008-
$this->checkManifestXML($manifestFile);
2010+
$this->checkManifestXML($manifestFile, $packageName);
20092011
}
20102012

20112013
/**
20122014
* Check content of manifest XML file in update package
20132015
*
2014-
* @param string $manifest Content of the manifest XML file
2016+
* @param string $manifest Content of the manifest XML file
2017+
* @param string $packageName Name of the selected update package
20152018
*
20162019
* @return void
20172020
*
20182021
* @since 4.4.0
20192022
* @throws \RuntimeException
20202023
*/
2021-
private function checkManifestXML(string $manifest)
2024+
private function checkManifestXML(string $manifest, $packageName)
20222025
{
20232026
$manifestXml = simplexml_load_string($manifest);
20242027

20252028
if (!$manifestXml) {
2026-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_VERSION_FOUND'), 500);
2029+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_VERSION_FOUND', $packageName), 500);
20272030
}
20282031

20292032
$versionPackage = (string) $manifestXml->version ?: '';
20302033

20312034
if (!$versionPackage) {
2032-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_VERSION_FOUND'), 500);
2035+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_VERSION_FOUND', $packageName), 500);
20332036
}
20342037

20352038
$currentVersion = JVERSION;
@@ -2040,7 +2043,7 @@ private function checkManifestXML(string $manifest)
20402043
}
20412044

20422045
if (version_compare($versionPackage, $currentVersion, 'lt')) {
2043-
throw new \RuntimeException(Text::_('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_DOWNGRADE'), 500);
2046+
throw new \RuntimeException(Text::sprintf('COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_DOWNGRADE', $packageName, $versionPackage, $currentVersion), 500);
20442047
}
20452048
}
20462049
}

administrator/language/en-GB/com_joomlaupdate.ini

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ COM_JOOMLAUPDATE_VIEW_UPDATE_FINALISE_HEAD_DESC="To complete the update Process
177177
COM_JOOMLAUPDATE_VIEW_UPDATE_PERCENT="Percent complete"
178178
COM_JOOMLAUPDATE_VIEW_UPLOAD_CAPTIVE_INTRO_BODY="Make sure that the update file you have uploaded comes from the official Joomla download page. Afterwards, please confirm that you want to install it by re-entering the login information for your site &quot;%s&quot; below."
179179
COM_JOOMLAUPDATE_VIEW_UPLOAD_CAPTIVE_INTRO_HEAD="Are you sure you want to install the file you uploaded?"
180-
COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_DOWNGRADE="The update package file has a lower version than the current Joomla version. You cannot downgrade a Joomla site."
181-
COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_INSTALL_PACKAGE="The uploaded package file looks like it is a full installation package of Joomla which can only be used for creating new sites. You can only use the \"Upgrade Package (.zip)\" to update your site."
182-
COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_MANIFEST_FILE="The uploaded package file is not a Joomla update package. It does not contain the \"administrator/manifests/files/joomla.xml\" file."
183-
COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_VERSION_FOUND="The uploaded package file is not a Joomla update package. It does not contain valid version information."
184-
COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN="The uploaded package file is either not a ZIP file or is corrupted."
180+
COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_DOWNGRADE="The uploaded package file \"%1$s\" has a lower version \"%2$s\" than the installed version \"%3$s\". You cannot downgrade a Joomla site.<br>Check <a class=\"alert-link\" href=\"https://downloads.joomla.org/latest\" target=\"_blank\" rel=\"noopener noreferrer\">the official Joomla download page</a> for the Joomla Upgrade Package of a <em><strong>newer version</strong></em>."
181+
COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_INSTALL_PACKAGE="The uploaded package file \"%s\" looks like it is a full installation package of Joomla which can only be used for creating new sites. You can only use the <em><strong>Upgrade Package</strong></em> to update your site.<br>Check <a class=\"alert-link\" href=\"https://downloads.joomla.org/latest\" target=\"_blank\" rel=\"noopener noreferrer\">the official Joomla download page</a> for the Joomla <em><strong>Upgrade Package</strong></em> of the right version."
182+
COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_MANIFEST_FILE="The uploaded package file \"%s\" is not a Joomla update package. It does not contain the \"administrator/manifests/files/joomla.xml\" file.<br>Check <a class=\"alert-link\" href=\"https://downloads.joomla.org/latest\" target=\"_blank\" rel=\"noopener noreferrer\">the official Joomla download page</a> for the right Joomla <em><strong>Upgrade Package</strong></em>."
183+
COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_NO_VERSION_FOUND="The uploaded package file \"%s\" is not a Joomla update package. It does not contain valid version information.<br>Check <a class=\"alert-link\" href=\"https://downloads.joomla.org/latest\" target=\"_blank\" rel=\"noopener noreferrer\">the official Joomla download page</a> for the right Joomla <em><strong>Upgrade Package</strong></em>."
184+
COM_JOOMLAUPDATE_VIEW_UPLOAD_ERROR_PACKAGE_OPEN="The uploaded package file \"%s\" is either not a ZIP file or is corrupted.<br>Check <a class=\"alert-link\" href=\"https://downloads.joomla.org/latest\" target=\"_blank\" rel=\"noopener noreferrer\">the official Joomla download page</a> for the desired Joomla <em><strong>Upgrade Package in ZIP format</strong></em>."
185185
COM_JOOMLAUPDATE_VIEW_UPLOAD_PACKAGE_FILE="Joomla package file"
186186
COM_JOOMLAUPDATE_XML_DESCRIPTION="Updates Joomla to the latest version with one click."
187187

0 commit comments

Comments
 (0)