diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2018-11-29 05:55:22 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-29 05:55:22 +0300 |
commit | 1bea8652a119d9098c695bba6c15bcd7d7fbaea9 (patch) | |
tree | 205c42b5a714cca7b83e9ed5f065e18c74b35cf6 /plugins | |
parent | 039e62dd87fead33238ea93a2c4fbef1d91cab71 (diff) |
When creating HTML reports in tmp/assets/ include a random string in the filename (#13607)
* When creating HTML reports in tmp/assets/ include a random string in the filename
* allow saveOnDisk option only for sendReport
* fix test and update changelog
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/MobileMessaging/tests/Integration/MobileMessagingTest.php | 17 | ||||
-rw-r--r-- | plugins/ScheduledReports/API.php | 35 |
2 files changed, 32 insertions, 20 deletions
diff --git a/plugins/MobileMessaging/tests/Integration/MobileMessagingTest.php b/plugins/MobileMessaging/tests/Integration/MobileMessagingTest.php index 64a5f80435..6979857c9d 100644 --- a/plugins/MobileMessaging/tests/Integration/MobileMessagingTest.php +++ b/plugins/MobileMessaging/tests/Integration/MobileMessagingTest.php @@ -61,17 +61,12 @@ class MobileMessagingTest extends IntegrationTestCase array("phoneNumbers" => array('33698896656')) ); - list($outputFilename, $prettyDate, $websiteName, $additionalFiles) = - $APIScheduledReports->generateReport( - $reportId, - '01-01-2010', - 'en', - 2 - ); - - $handle = fopen($outputFilename, "r"); - $contents = fread($handle, filesize($outputFilename)); - fclose($handle); + $contents = $APIScheduledReports->generateReport( + $reportId, + '01-01-2010', + 'en', + APIScheduledReports::OUTPUT_RETURN + ); $this->assertEquals( \Piwik\Piwik::translate('MobileMessaging_MultiSites_Must_Be_Activated'), diff --git a/plugins/ScheduledReports/API.php b/plugins/ScheduledReports/API.php index 5cbae39905..291c28b294 100644 --- a/plugins/ScheduledReports/API.php +++ b/plugins/ScheduledReports/API.php @@ -62,6 +62,8 @@ class API extends \Piwik\Plugin\API const OUTPUT_INLINE = 3; const OUTPUT_RETURN = 4; + private $enableSaveReportOnDisk = false; + // static cache storing reports public static $cache = array(); @@ -296,7 +298,7 @@ class API extends \Piwik\Plugin\API * @param int $idReport ID of the report to generate. * @param string $date YYYY-MM-DD * @param bool|false|string $language If not passed, will use default language. - * @param bool|false|int $outputType 1 = download report, 2 = save report to disk, 3 = output report in browser, 4 = return report content to caller, defaults to download + * @param bool|false|int $outputType 1 = download report, 3 = output report in browser, 4 = return report content to caller, defaults to download * @param bool|false|string $period Defaults to 'day'. If not specified, will default to the report's period set when creating the report * @param bool|false|string $reportFormat 'pdf', 'html' or any other format provided via the ScheduledReports.getReportFormats hook * @param bool|false|array $parameters array of parameters @@ -306,6 +308,10 @@ class API extends \Piwik\Plugin\API { Piwik::checkUserIsNotAnonymous(); + if (!$this->enableSaveReportOnDisk && $outputType == self::OUTPUT_SAVE_ON_DISK) { + $outputType = self::OUTPUT_DOWNLOAD; + } + // load specified language if (empty($language)) { $language = Translate::getLanguageDefault(); @@ -523,8 +529,10 @@ class API extends \Piwik\Plugin\API switch ($outputType) { case self::OUTPUT_SAVE_ON_DISK: + // only used for SendReport $outputFilename = strtoupper($reportFormat) . ' ' . ucfirst($reportType) . ' Report - ' . $idReport . '.' . $date . '.' . $idSite . '.' . $language; + $outputFilename .= ' - ' . Common::getRandomString(40,'abcdefghijklmnoprstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVXYZ_'); $outputFilename = $reportRenderer->sendToDisk($outputFilename); $additionalFiles = $this->getAttachments($reportRenderer, $report, $processedReports, $prettyDate); @@ -580,14 +588,23 @@ class API extends \Piwik\Plugin\API $language = \Piwik\Plugins\LanguagesManager\API::getInstance()->getLanguageForUser($report['login']); // generate report - list($outputFilename, $prettyDate, $reportSubject, $reportTitle, $additionalFiles) = - $this->generateReport( - $idReport, - $date, - $language, - self::OUTPUT_SAVE_ON_DISK, - $report['period'] - ); + $this->enableSaveReportOnDisk = true; + try { + list($outputFilename, $prettyDate, $reportSubject, $reportTitle, $additionalFiles) = + $this->generateReport( + $idReport, + $date, + $language, + self::OUTPUT_SAVE_ON_DISK, + $report['period'] + ); + + } catch (Exception $e) { + $this->enableSaveReportOnDisk = false; + throw $e; + } + + $this->enableSaveReportOnDisk = false; if (!file_exists($outputFilename)) { throw new Exception("The report file wasn't found in $outputFilename"); |