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