PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / ReportRenderer.php
matomo / app / core Last commit date
API 5 years ago Access 5 years ago Application 5 years ago Archive 5 years ago ArchiveProcessor 5 years ago Archiver 5 years ago AssetManager 5 years ago Auth 5 years ago Category 5 years ago CliMulti 5 years ago Columns 5 years ago Composer 5 years ago Concurrency 5 years ago Config 5 years ago Container 5 years ago CronArchive 5 years ago DataAccess 5 years ago DataFiles 5 years ago DataTable 5 years ago Db 5 years ago DeviceDetector 5 years ago Email 5 years ago Exception 5 years ago Http 5 years ago Intl 5 years ago Mail 5 years ago Measurable 5 years ago Menu 5 years ago Metrics 5 years ago Notification 5 years ago Period 5 years ago Plugin 5 years ago ProfessionalServices 5 years ago Report 5 years ago ReportRenderer 5 years ago Scheduler 5 years ago Segment 5 years ago Session 5 years ago Settings 5 years ago Tracker 5 years ago Translation 5 years ago UpdateCheck 5 years ago Updater 5 years ago Updates 5 years ago Validators 5 years ago View 5 years ago ViewDataTable 5 years ago Visualization 5 years ago Widget 5 years ago .htaccess 6 years ago Access.php 5 years ago Archive.php 5 years ago ArchiveProcessor.php 5 years ago AssetManager.php 5 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 5 years ago Common.php 5 years ago Config.php 5 years ago Console.php 5 years ago Context.php 5 years ago Cookie.php 5 years ago CronArchive.php 5 years ago DataArray.php 5 years ago DataTable.php 5 years ago Date.php 5 years ago Db.php 5 years ago DbHelper.php 5 years ago Development.php 5 years ago ErrorHandler.php 5 years ago EventDispatcher.php 5 years ago ExceptionHandler.php 5 years ago FileIntegrity.php 5 years ago Filechecks.php 5 years ago Filesystem.php 5 years ago FrontController.php 5 years ago Http.php 5 years ago IP.php 5 years ago Log.php 5 years ago LogDeleter.php 5 years ago Mail.php 5 years ago Metrics.php 5 years ago NoAccessException.php 5 years ago Nonce.php 5 years ago Notification.php 5 years ago NumberFormatter.php 5 years ago Option.php 5 years ago Period.php 5 years ago Piwik.php 5 years ago Plugin.php 5 years ago Profiler.php 5 years ago ProxyHeaders.php 5 years ago ProxyHttp.php 5 years ago QuickForm2.php 5 years ago RankingQuery.php 5 years ago ReportRenderer.php 5 years ago Segment.php 5 years ago Sequence.php 5 years ago Session.php 5 years ago SettingsPiwik.php 5 years ago SettingsServer.php 5 years ago Singleton.php 5 years ago Site.php 5 years ago SupportedBrowser.php 5 years ago TCPDF.php 5 years ago Theme.php 5 years ago Timer.php 5 years ago Tracker.php 5 years ago Twig.php 5 years ago Unzip.php 5 years ago UpdateCheck.php 5 years ago Updater.php 5 years ago UpdaterErrorException.php 5 years ago Updates.php 5 years ago Url.php 5 years ago UrlHelper.php 5 years ago Version.php 5 years ago View.php 5 years ago bootstrap.php 5 years ago dispatch.php 5 years ago testMinimumPhpVersion.php 5 years ago
ReportRenderer.php
285 lines
1 <?php
2 /**
3 * Matomo - 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 const TSV_FORMAT = 'tsv';
38
39 protected $idSite = 'all';
40
41 protected $report;
42
43 private static $availableReportRenderers = array(
44 self::PDF_FORMAT,
45 self::HTML_FORMAT,
46 self::CSV_FORMAT,
47 self::TSV_FORMAT,
48 );
49
50 /**
51 * Sets the site id
52 *
53 * @param int $idSite
54 */
55 public function setIdSite($idSite)
56 {
57 $this->idSite = $idSite;
58 }
59
60 public function setReport($report)
61 {
62 $this->report = $report;
63 }
64
65 protected static function getClassNameFromClassId($rendererType)
66 {
67 return 'Piwik\ReportRenderer\\' . self::normalizeRendererType($rendererType);
68 }
69
70 protected static function getInvalidClassIdExceptionMessage($rendererType)
71 {
72 return Piwik::translate(
73 'General_ExceptionInvalidReportRendererFormat',
74 array(self::normalizeRendererType($rendererType), implode(', ', self::$availableReportRenderers))
75 );
76 }
77
78 protected static function normalizeRendererType($rendererType)
79 {
80 return ucfirst(strtolower($rendererType));
81 }
82
83 /**
84 * Initialize locale settings.
85 * If not called, locale settings defaults to 'en'
86 *
87 * @param string $locale
88 */
89 abstract public function setLocale($locale);
90
91 /**
92 * Save rendering to disk
93 *
94 * @param string $filename without path & without format extension
95 * @return string path of file
96 */
97 abstract public function sendToDisk($filename);
98
99 /**
100 * Send rendering to browser with a 'download file' prompt
101 *
102 * @param string $filename without path & without format extension
103 */
104 abstract public function sendToBrowserDownload($filename);
105
106 /**
107 * Output rendering to browser
108 *
109 * @param string $filename without path & without format extension
110 */
111 abstract public function sendToBrowserInline($filename);
112
113 /**
114 * Get rendered report
115 */
116 abstract public function getRenderedReport();
117
118 /**
119 * Generate the first page.
120 *
121 * @param string $reportTitle
122 * @param string $prettyDate formatted date
123 * @param string $description
124 * @param array $reportMetadata metadata for all reports
125 * @param array $segment segment applied to all reports
126 */
127 abstract public function renderFrontPage($reportTitle, $prettyDate, $description, $reportMetadata, $segment);
128
129 /**
130 * Render the provided report.
131 * Multiple calls to this method before calling outputRendering appends each report content.
132 *
133 * @param array $processedReport @see API::getProcessedReport()
134 */
135 abstract public function renderReport($processedReport);
136
137 /**
138 * Get report attachments, ex. graph images
139 *
140 * @param $report
141 * @param $processedReports
142 * @param $prettyDate
143 * @return array
144 */
145 abstract public function getAttachments($report, $processedReports, $prettyDate);
146
147 /**
148 * Append $extension to $filename
149 *
150 * @static
151 * @param string $filename
152 * @param string $extension
153 * @return string filename with extension
154 */
155 protected static function makeFilenameWithExtension($filename, $extension)
156 {
157 // the filename can be used in HTTP headers, remove new lines to prevent HTTP header injection
158 $filename = str_replace(array("\n", "\t"), " ", $filename);
159
160 return $filename . "." . $extension;
161 }
162
163 /**
164 * Return $filename with temp directory and delete file
165 *
166 * @static
167 * @param $filename
168 * @return string path of file in temp directory
169 */
170 protected static function getOutputPath($filename)
171 {
172 $outputFilename = StaticContainer::get('path.tmp') . '/assets/' . $filename;
173
174 @chmod($outputFilename, 0600);
175
176 if(file_exists($outputFilename)){
177 @unlink($outputFilename);
178 }
179
180 return $outputFilename;
181 }
182
183 protected static function writeFile($filename, $extension, $content)
184 {
185 $filename = self::makeFilenameWithExtension($filename, $extension);
186 $outputFilename = self::getOutputPath($filename);
187
188 $bytesWritten = file_put_contents($outputFilename, $content);
189 if ($bytesWritten === false) {
190 throw new Exception("ReportRenderer: Could not write to file '" . $outputFilename . "'.");
191 }
192
193 return $outputFilename;
194 }
195
196 protected static function sendToBrowser($filename, $extension, $contentType, $content)
197 {
198 $filename = ReportRenderer::makeFilenameWithExtension($filename, $extension);
199
200 ProxyHttp::overrideCacheControlHeaders();
201 Common::sendHeader('Content-Description: File Transfer');
202 Common::sendHeader('Content-Type: ' . $contentType);
203 Common::sendHeader('Content-Disposition: attachment; filename="' . str_replace('"', '\'', basename($filename)) . '";');
204 Common::sendHeader('Content-Length: ' . strlen($content));
205
206 echo $content;
207 }
208
209 protected static function inlineToBrowser($contentType, $content)
210 {
211 Common::sendHeader('Content-Type: ' . $contentType);
212 echo $content;
213 }
214
215 /**
216 * Convert a dimension-less report to a multi-row two-column data table
217 *
218 * @static
219 * @param $reportMetadata array
220 * @param $report DataTable
221 * @param $reportColumns array
222 * @return array DataTable $report & array $columns
223 */
224 protected static function processTableFormat($reportMetadata, $report, $reportColumns)
225 {
226 $finalReport = $report;
227 if (empty($reportMetadata['dimension'])) {
228 $simpleReportMetrics = $report->getFirstRow();
229 if ($simpleReportMetrics) {
230 $finalReport = new Simple();
231 foreach ($simpleReportMetrics->getColumns() as $metricId => $metric) {
232 $newRow = new Row();
233 $newRow->addColumn("label", $reportColumns[$metricId]);
234 $newRow->addColumn("value", $metric);
235 $finalReport->addRow($newRow);
236 }
237 }
238
239 $reportColumns = array(
240 'label' => Piwik::translate('General_Name'),
241 'value' => Piwik::translate('General_Value'),
242 );
243 }
244
245 return array(
246 $finalReport,
247 $reportColumns,
248 );
249 }
250
251 public static function getStaticGraph($reportMetadata, $width, $height, $evolution, $segment)
252 {
253 $imageGraphUrl = $reportMetadata['imageGraphUrl'];
254
255 if ($evolution && !empty($reportMetadata['imageGraphEvolutionUrl'])) {
256 $imageGraphUrl = $reportMetadata['imageGraphEvolutionUrl'];
257 }
258
259 $requestGraph = $imageGraphUrl .
260 '&outputType=' . API::GRAPH_OUTPUT_PHP .
261 '&format=original&serialize=0' .
262 '&filter_truncate=' .
263 '&width=' . $width .
264 '&height=' . $height .
265 ($segment != null ? '&segment=' . urlencode($segment['definition']) : '');
266
267 $request = new Request($requestGraph);
268
269 try {
270 $imageGraph = $request->process();
271
272 // Get image data as string
273 ob_start();
274 imagepng($imageGraph);
275 $imageGraphData = ob_get_contents();
276 ob_end_clean();
277 imagedestroy($imageGraph);
278
279 return $imageGraphData;
280 } catch (Exception $e) {
281 throw new Exception("ImageGraph API returned an error: " . $e->getMessage() . "\n");
282 }
283 }
284 }
285