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