1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik;
use Exception;
use Piwik\API\Request;
use Piwik\Container\StaticContainer;
use Piwik\DataTable\Row;
use Piwik\DataTable\Simple;
use Piwik\Plugins\ImageGraph\API;
/**
* A Report Renderer produces user friendly renderings of any given Piwik report.
* All new Renderers must be copied in ReportRenderer and added to the $availableReportRenderers.
*/
abstract class ReportRenderer extends BaseFactory
{
public const DEFAULT_REPORT_FONT_FAMILY = 'dejavusans';
public const REPORT_TEXT_COLOR = "13,13,13";
public const REPORT_TITLE_TEXT_COLOR = "13,13,13";
public const TABLE_HEADER_BG_COLOR = "255,255,255";
public const TABLE_HEADER_TEXT_COLOR = "13,13,13";
public const TABLE_HEADER_TEXT_TRANSFORM = "uppercase";
public const TABLE_HEADER_TEXT_WEIGHT = "normal";
public const TABLE_CELL_BORDER_COLOR = "217,217,217";
public const TABLE_BG_COLOR = "242,242,242";
public const HTML_FORMAT = 'html';
public const PDF_FORMAT = 'pdf';
public const CSV_FORMAT = 'csv';
public const TSV_FORMAT = 'tsv';
protected $idSite = 'all';
protected $report;
private static $availableReportRenderers = [
self::PDF_FORMAT,
self::HTML_FORMAT,
self::CSV_FORMAT,
self::TSV_FORMAT,
];
/**
* Sets the site id
*
* @param int $idSite
*/
public function setIdSite($idSite)
{
$this->idSite = $idSite;
}
public function setReport($report)
{
$this->report = $report;
}
protected static function getClassNameFromClassId($rendererType)
{
return 'Piwik\ReportRenderer\\' . self::normalizeRendererType($rendererType);
}
protected static function getInvalidClassIdExceptionMessage($rendererType)
{
return Piwik::translate(
'General_ExceptionInvalidReportRendererFormat',
[self::normalizeRendererType($rendererType), implode(', ', self::$availableReportRenderers)]
);
}
protected static function normalizeRendererType($rendererType)
{
return ucfirst(strtolower($rendererType));
}
/**
* Initialize locale settings.
* If not called, locale settings defaults to 'en'
*
* @param string $locale
*/
abstract public function setLocale($locale);
/**
* Save rendering to disk
*
* @param string $filename without path & without format extension
* @return string path of file
*/
abstract public function sendToDisk($filename);
/**
* Send rendering to browser with a 'download file' prompt
*
* @param string $filename without path & without format extension
*/
abstract public function sendToBrowserDownload($filename);
/**
* Output rendering to browser
*
* @param string $filename without path & without format extension
*/
abstract public function sendToBrowserInline($filename);
/**
* Get rendered report
*/
abstract public function getRenderedReport();
/**
* Generate the first page.
*
* @param string $reportTitle
* @param string $prettyDate formatted date
* @param string $description
* @param array $reportMetadata metadata for all reports
* @param array $segment segment applied to all reports
*/
abstract public function renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata, $segment);
/**
* Render the provided report.
* Multiple calls to this method before calling outputRendering appends each report content.
*
* @param array $processedReport @see API::getProcessedReport()
*/
abstract public function renderReport($processedReport);
/**
* Get report attachments, ex. graph images
*
* @param $report
* @param $processedReports
* @param $prettyDate
* @return array
*/
abstract public function getAttachments($report, $processedReports, $prettyDate);
/**
* Append $extension to $filename
*
* @static
* @param string $filename
* @param string $extension
* @return string filename with extension
*/
protected static function makeFilenameWithExtension($filename, $extension)
{
// the filename can be used in HTTP headers, remove new lines to prevent HTTP header injection
$filename = str_replace(["\n", "\t"], " ", $filename);
return $filename . "." . $extension;
}
/**
* Return $filename with temp directory and delete file
*
* @static
* @param $filename
* @return string path of file in temp directory
*/
protected static function getOutputPath($filename)
{
$baseAssetsDir = StaticContainer::get('path.tmp') . '/assets/';
$outputFilename = $baseAssetsDir . $filename;
if (!is_dir($baseAssetsDir)) {
Filesystem::mkdir($baseAssetsDir);
}
@chmod($outputFilename, 0600);
if (file_exists($outputFilename)) {
@unlink($outputFilename);
}
return $outputFilename;
}
protected static function writeFile($filename, $extension, $content)
{
$filename = self::makeFilenameWithExtension($filename, $extension);
$outputFilename = self::getOutputPath($filename);
$bytesWritten = file_put_contents($outputFilename, $content);
if ($bytesWritten === false) {
throw new Exception("ReportRenderer: Could not write to file '" . $outputFilename . "'.");
}
return $outputFilename;
}
protected static function sendToBrowser($filename, $extension, $contentType, $content)
{
$filename = ReportRenderer::makeFilenameWithExtension($filename, $extension);
ProxyHttp::overrideCacheControlHeaders();
Common::sendHeader('Content-Description: File Transfer');
Common::sendHeader('Content-Type: ' . $contentType);
Common::sendHeader('Content-Disposition: attachment; filename="' . str_replace('"', '\'', basename($filename)) . '";');
Common::sendHeader('Content-Length: ' . strlen($content));
echo $content;
}
protected static function inlineToBrowser($contentType, $content)
{
Common::sendHeader('Content-Type: ' . $contentType);
echo $content;
}
/**
* Convert a dimension-less report to a multi-row two-column data table
*
* @static
* @param $reportMetadata array
* @param $report DataTable
* @param $reportColumns array
* @return array DataTable $report & array $columns
*/
protected static function processTableFormat($reportMetadata, $report, $reportColumns)
{
$finalReport = $report;
if (empty($reportMetadata['dimension'])) {
$simpleReportMetrics = $report->getFirstRow();
if ($simpleReportMetrics) {
$finalReport = new Simple();
foreach ($simpleReportMetrics->getColumns() as $metricId => $metric) {
$newRow = new Row();
$newRow->addColumn("label", $reportColumns[$metricId]);
$newRow->addColumn("value", $metric);
$finalReport->addRow($newRow);
}
}
$reportColumns = [
'label' => Piwik::translate('General_Name'),
'value' => Piwik::translate('General_Value'),
];
}
return [
$finalReport,
$reportColumns,
];
}
public static function getStaticGraph($reportMetadata, $width, $height, $evolution, $segment)
{
$imageGraphUrl = $reportMetadata['imageGraphUrl'];
if ($evolution && !empty($reportMetadata['imageGraphEvolutionUrl'])) {
$imageGraphUrl = $reportMetadata['imageGraphEvolutionUrl'];
}
$queryString = Url::getQueryStringFromUrl($imageGraphUrl);
if (!is_string($queryString) || $queryString === '') {
$queryString = $imageGraphUrl;
}
$requestGraph = UrlHelper::getArrayFromQueryString($queryString);
$requestGraph['outputType'] = API::GRAPH_OUTPUT_PHP;
$requestGraph['format'] = 'original';
$requestGraph['serialize'] = 0;
$requestGraph['filter_truncate'] = '';
$requestGraph['width'] = $width;
$requestGraph['height'] = $height;
if ($segment != null) {
$requestGraph['segment'] = urlencode($segment['definition']);
}
$request = new Request($requestGraph);
try {
$imageGraph = $request->process();
// Get image data as string
ob_start();
imagepng($imageGraph);
$imageGraphData = ob_get_contents();
ob_end_clean();
imagedestroy($imageGraph);
return $imageGraphData;
} catch (Exception $e) {
throw new Exception("ImageGraph API returned an error: " . $e->getMessage() . "\n");
}
}
}
|