Security fix - improved access checks for task edit and deletes.
[timetracker.git] / topdf.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 /*
30  * This file generates a report in PDF format using TCPDF library from http://www.tcpdf.org/.
31  * If installed, it is expected to be in WEB-INF/lib/tcpdf/ folder.
32  */
33 require_once('initialize.php');
34 import('form.Form');
35 import('form.ActionForm');
36 import('ttReportHelper');
37
38 // Access checks.
39 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports'))) {
40   header('Location: access_denied.php');
41   exit();
42 }
43 // End of access checks.
44
45 // Check whether TCPDF library is available.
46 if (!file_exists('WEB-INF/lib/tcpdf/'))
47   die('TCPDF library is not found in WEB-INF/lib/tcpdf/');
48
49 // Include TCPDF library.
50 require_once('WEB-INF/lib/tcpdf/tcpdf.php');
51
52 // Use custom fields plugin if it is enabled.
53 if ($user->isPluginEnabled('cf')) {
54   require_once('plugins/CustomFields.class.php');
55   $custom_fields = new CustomFields($user->team_id);
56 }
57
58 // Report settings are stored in session bean before we get here.
59 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
60
61 // There are 2 variations of report: totals only, or normal. Totals only means that the report
62 // is grouped by either date, user, client, project, task or cf_1 and user only needs to see subtotals by group.
63 $totals_only = ($bean->getAttribute('chtotalsonly') == '1');
64
65 // Determine group by header.
66 $group_by = $bean->getAttribute('group_by');
67 if ('no_grouping' != $group_by) {
68   if ('cf_1' == $group_by)
69     $group_by_header = $custom_fields->fields[0]['label'];
70   else {
71     $key = 'label.'.$group_by;
72     $group_by_header = $i18n->get($key);
73   }
74 }
75
76 // Obtain items for report.
77 if (!$totals_only)
78   $items = ttReportHelper::getItems($bean); // Individual entries.
79 if ($totals_only || 'no_grouping' != $group_by)
80   $subtotals = ttReportHelper::getSubtotals($bean); // Subtotals for groups of items.
81 $totals = ttReportHelper::getTotals($bean); // Totals for the entire report.
82
83 // Assign variables that are used to print subtotals.
84 if ($items && 'no_grouping' != $group_by) {
85   $print_subtotals = true;
86   $first_pass = true;
87   $prev_grouped_by = '';
88   $cur_grouped_by = '';
89 }
90
91 // Build a string to use as filename for the files being downloaded.
92 $filename = strtolower($i18n->get('title.report')).'_'.$bean->mValues['start_date'].'_'.$bean->mValues['end_date'];
93
94 // Start preparing HTML to build PDF from.
95 $styleHeader = 'style="background-color:#a6ccf7;"';
96 $styleSubtotal = 'style="background-color:#e0e0e0;"';
97 $styleCentered = 'style="text-align:center;"';
98 $styleRightAligned = 'style="text-align:right;"';
99
100 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
101 $html = '<h1 style="text-align:center;">'.$title.'</h1>';
102 $html .= '<table border="1" cellpadding="3" cellspacing="0" width="100%">';
103
104 if ($totals_only) {
105   // We are building a "totals only" report with only subtotals and total.
106   $colspan = 1; // Column span for an empty row.
107   // Table header.
108   $html .= '<thead>';
109   $html .= "<tr $styleHeader>";
110   $html .= '<td>'.htmlspecialchars($group_by_header).'</td>';
111   if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.duration').'</td>'; }
112   if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.cost').'</td>'; }
113   $html .= '</tr>';
114   $html .= '</thead>';
115   // Print subtotals.
116   foreach ($subtotals as $subtotal) {
117     $html .= '<tr>';
118     $html .= '<td>'.htmlspecialchars($subtotal['name']).'</td>';
119     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotal['time'].'</td>';
120     if ($bean->getAttribute('chcost')) {
121       $html .= "<td $styleRightAligned>";
122       if ($user->canManageTeam() || $user->isClient())
123         $html .= $subtotal['cost'];
124       else
125         $html .= $subtotal['expenses'];
126       $html .= '</td>'; 
127     }
128     $html .= '</tr>';
129   }
130   // Print totals.
131   $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
132   $html .= "<tr $styleSubtotal>";
133   $html .= '<td>'.$i18n->get('label.total').'</td>';
134   if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
135   if ($bean->getAttribute('chcost')) {
136       $html .= "<td $styleRightAligned>";
137       $html .= htmlspecialchars($user->currency).' ';
138       if ($user->canManageTeam() || $user->isClient())
139         $html .= $totals['cost'];
140       else
141         $html .= $totals['expenses'];
142       $html .= '</td>';
143     }
144   $html .= '</tr>';
145   $html .= '</table>';
146 } else {
147   // We are building a normal report with items, optionally grouped with subtotals, and total.
148   $colspan = 1; // Column span for an empty row.
149   // Table header.
150   $html .= '<thead>';
151   $html .= "<tr $styleHeader>";
152   $html .= '<td>'.$i18n->get('label.date').'</td>';
153   if ($user->canManageTeam() || $user->isClient()) { $colspan++; $html .= '<td>'.$i18n->get('label.user').'</td>'; }
154   if ($bean->getAttribute('chclient')) { $colspan++; $html .= '<td>'.$i18n->get('label.client').'</td>'; }
155   if ($bean->getAttribute('chproject')) { $colspan++; $html .= '<td>'.$i18n->get('label.project').'</td>'; }
156   if ($bean->getAttribute('chtask')) { $colspan++; $html .= '<td>'.$i18n->get('label.task').'</td>'; }
157   if ($bean->getAttribute('chcf_1')) { $colspan++; $html .= '<td>'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>'; }
158   if ($bean->getAttribute('chstart')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.start').'</td>'; }
159   if ($bean->getAttribute('chfinish')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.finish').'</td>'; }
160   if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.duration').'</td>'; }
161   if ($bean->getAttribute('chnote')) { $colspan++; $html .= '<td>'.$i18n->get('label.note').'</td>'; }
162   if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.cost').'</td>'; }
163   if ($bean->getAttribute('chpaid')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.paid').'</td>'; }
164   if ($bean->getAttribute('chip')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.ip').'</td>'; }
165   if ($bean->getAttribute('chinvoice')) { $colspan++; $html .= '<td>'.$i18n->get('label.invoice').'</td>'; }
166   $html .= '</tr>';
167   $html .= '</thead>';
168
169   foreach ($items as $item) {
170     // Print a subtotal for a block of grouped values.
171     $cur_date = $item['date'];
172     if ($print_subtotals) {
173       $cur_grouped_by = $item['grouped_by'];
174       if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
175         $html .= '<tr style="background-color:#e0e0e0;">';
176         $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
177         if ($user->canManageTeam() || $user->isClient()) {
178             $html .= '<td>';
179             if ($group_by == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
180             $html .= '</td>';
181         }
182         if ($bean->getAttribute('chclient')) {
183             $html .= '<td>';
184             if ($group_by == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
185             $html .= '</td>';
186         }
187         if ($bean->getAttribute('chproject')) {
188             $html .= '<td>';
189             if ($group_by == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
190             $html .= '</td>';
191         }
192         if ($bean->getAttribute('chtask')) {
193             $html .= '<td>';
194             if ($group_by == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
195             $html .= '</td>';
196         }
197         if ($bean->getAttribute('chcf_1')) {
198             $html .= '<td>';
199             if ($group_by == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
200             $html .= '</td>';
201         }
202         if ($bean->getAttribute('chstart')) $html .= '<td></td>';
203         if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
204         if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
205         if ($bean->getAttribute('chnote')) $html .= '<td></td>';
206         if ($bean->getAttribute('chcost')) {
207           $html .= "<td $styleRightAligned>";
208           if ($user->canManageTeam() || $user->isClient())
209             $html .= $subtotals[$prev_grouped_by]['cost'];
210           else
211             $html .= $subtotals[$prev_grouped_by]['expenses'];
212           $html .= '</td>';
213         }
214         if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
215         if ($bean->getAttribute('chip')) $html .= '<td></td>';
216         if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
217         $html .= '</tr>';
218         $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
219       }
220       $first_pass = false; 
221     }
222
223     // Print a regular row.
224     $html .= '<tr>';
225     $html .= '<td>'.$item['date'].'</td>';
226     if ($user->canManageTeam() || $user->isClient()) $html .= '<td>'.htmlspecialchars($item['user']).'</td>';
227     if ($bean->getAttribute('chclient')) $html .= '<td>'.htmlspecialchars($item['client']).'</td>';
228     if ($bean->getAttribute('chproject')) $html .= '<td>'.htmlspecialchars($item['project']).'</td>';
229     if ($bean->getAttribute('chtask')) $html .= '<td>'.htmlspecialchars($item['task']).'</td>';
230     if ($bean->getAttribute('chcf_1')) $html .= '<td>'.htmlspecialchars($item['cf_1']).'</td>';
231     if ($bean->getAttribute('chstart')) $html .= "<td $styleRightAligned>".$item['start'].'</td>';
232     if ($bean->getAttribute('chfinish')) $html .= "<td $styleRightAligned>".$item['finish'].'</td>';
233     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$item['duration'].'</td>';
234     if ($bean->getAttribute('chnote')) $html .= '<td>'.htmlspecialchars($item['note']).'</td>';
235     if ($bean->getAttribute('chcost')) {
236       $html .= "<td $styleRightAligned>";
237       if ($user->canManageTeam() || $user->isClient())
238         $html .= $item['cost'];
239       else
240         $html .= $item['expense'];
241       $html .= '</td>';
242     }
243     if ($bean->getAttribute('chpaid')) {
244         $html .= '<td>';
245         $html .= $item['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
246         $html .= '</td>';
247     }
248     if ($bean->getAttribute('chip')) {
249         $html .= '<td>';
250         $html .= $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
251         $html .= '</td>';
252     }
253     if ($bean->getAttribute('chinvoice')) $html .= '<td>'.htmlspecialchars($item['invoice']).'</td>';
254     $html .= '</tr>';
255
256     $prev_date = $item['date'];
257     if ($print_subtotals) $prev_grouped_by = $item['grouped_by'];
258   }
259
260   // Print a terminating subtotal.
261   if ($print_subtotals) {
262     $html .= '<tr style="background-color:#e0e0e0;">';
263     $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
264     if ($user->canManageTeam() || $user->isClient()) {
265       $html .= '<td>';
266       if ($group_by == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
267       $html .= '</td>';
268     }
269     if ($bean->getAttribute('chclient')) {
270       $html .= '<td>';
271       if ($group_by == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
272       $html .= '</td>';
273     }
274     if ($bean->getAttribute('chproject')) {
275       $html .= '<td>';
276       if ($group_by == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
277       $html .= '</td>';
278     }
279     if ($bean->getAttribute('chtask')) {
280       $html .= '<td>';
281       if ($group_by == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
282       $html .= '</td>';
283     }
284     if ($bean->getAttribute('chcf_1')) {
285       $html .= '<td>';
286       if ($group_by == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
287       $html .= '</td>';
288     }
289     if ($bean->getAttribute('chstart')) $html .= '<td></td>';
290     if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
291     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
292     if ($bean->getAttribute('chnote')) $html .= '<td></td>';
293     if ($bean->getAttribute('chcost')) {
294       $html .= "<td $styleRightAligned>";
295       if ($user->canManageTeam() || $user->isClient())
296         $html .= $subtotals[$prev_grouped_by]['cost'];
297       else
298         $html .= $subtotals[$prev_grouped_by]['expenses'];
299       $html .= '</td>';
300     }
301     if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
302     if ($bean->getAttribute('chip')) $html .= '<td></td>';
303     if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
304     $html .= '</tr>';
305   }
306
307   // Print totals.
308   $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
309   $html .= '<tr style="background-color:#e0e0e0;">';
310   $html .= '<td>'.$i18n->get('label.total').'</td>';
311   if ($user->canManageTeam() || $user->isClient()) $html .= '<td></td>';
312   if ($bean->getAttribute('chclient')) $html .= '<td></td>';
313   if ($bean->getAttribute('chproject')) $html .= '<td></td>';
314   if ($bean->getAttribute('chtask')) $html .= '<td></td>';
315   if ($bean->getAttribute('chcf_1')) $html .= '<td></td>';
316   if ($bean->getAttribute('chstart')) $html .= '<td></td>';
317   if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
318   if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
319   if ($bean->getAttribute('chnote')) $html .= '<td></td>';
320   if ($bean->getAttribute('chcost')) {
321     $html .= "<td $styleRightAligned>".htmlspecialchars($user->currency).' ';
322     if ($user->canManageTeam() || $user->isClient())
323       $html .= $totals['cost'];
324     else
325       $html .= $totals['expenses'];
326     $html .= '</td>';
327   }
328   if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
329   if ($bean->getAttribute('chip')) $html .= '<td></td>';
330   if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
331   $html .= '</tr>';
332   $html .= '</table>';
333 }
334
335 // Output footer.
336 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false)) // By default we print it unless explicitely defined as false.
337   $html .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
338
339 // By this time we have html ready.
340
341 // Determine title for report.
342 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
343
344 header('Pragma: public'); // This is needed for IE8 to download files over https.
345 header('Content-Type: text/html; charset=utf-8');
346 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
347 header('Cache-Control: no-store, no-cache, must-revalidate');
348 header('Cache-Control: post-check=0, pre-check=0', false);
349 header('Cache-Control: private', false);
350
351 header('Content-Type: application/pdf');
352 header('Content-Disposition: attachment; filename="'.$filename.'.pdf"');
353
354
355 // Beginning of TCPDF code here.
356
357 // Extend TCPDF class so that we can use custom header and footer.
358 class ttPDF extends TCPDF {
359
360   public $image_file = 'images/tt_logo.png'; // Image file for the logo in header.
361   public $page_word = 'Page'; // Localized "Page" word in footer, ex: Page 1/2.
362
363   // SetImageFile - sets image file name.
364   public function SetImageFile($imgFile) {
365     $this->image_file = $imgFile;
366   }
367
368   // SetPageWord - sets page word for footer.
369   public function SetPageWord($pageWord) {
370     $this->page_word = $pageWord;
371   }
372
373   // Page header.
374   public function Header() {
375     // Print logo, which is the only element of our custom header.
376     $this->Image($this->image_file, 10, 10, '', '', '', '', 'T', false, 300, 'C', false, false, 0, false, false, false);
377   }
378
379   // Page footer.
380   public function Footer() {
381     // Position at 15 mm from bottom.
382     $this->SetY(-15);
383     // Set font.
384     $this->SetFont('freeserif', 'I', 8);
385     // Print localized page number.
386     $this->Cell(0, 10, $this->page_word.' '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
387   }
388 }
389
390 // Create new PDF document.
391 $pdf = new ttPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
392
393 // If custom logo file exists - set it.
394 if (file_exists('images/'.$user->team_id.'.png'))
395   $pdf->SetImageFile('images/'.$user->team_id.'.png');
396
397 // Set page word for the footer.
398 $pdf->SetPageWord($i18n->get('label.page'));
399
400 // Set document information.
401 $pdf->SetCreator(PDF_CREATOR);
402 $pdf->SetAuthor('Anuko Time Tracker');
403 $pdf->SetTitle('Anuko Time Tracker Report');
404 $pdf->SetSubject('Anuko Time Tracker Report');
405 $pdf->SetKeywords('Anuko, time, tracker, report');
406
407 // Set margins.
408 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
409 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
410 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
411
412 // Set auto page breaks.
413 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
414
415 // Set image scale factor.
416 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
417
418 // Add a page.
419 $pdf->AddPage();
420
421 // Set font (freeserif seems to work for all languages).
422 $pdf->SetFont('freeserif', '', 10); // helvetica here does not work for Russian.
423
424 // Write HTML.
425 $pdf->writeHTML($html, true, false, false, false, '');
426
427 // Close and output PDF document.
428 // $pdf->Output('timesheet.pdf', 'I'); // This will display inline in browser.
429 $pdf->Output($filename.'.pdf', 'D'); // D is for downloads.
430
431 // End of of TCPDF code.