Security fix - improved access checks for task edit and deletes.
[timetracker.git] / tofile.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 require_once('initialize.php');
30 import('form.Form');
31 import('form.ActionForm');
32 import('ttReportHelper');
33
34 // Access checks.
35 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports'))) {
36   header('Location: access_denied.php');
37   exit();
38 }
39 // End of access checks.
40
41 // Use custom fields plugin if it is enabled.
42 if ($user->isPluginEnabled('cf')) {
43   require_once('plugins/CustomFields.class.php');
44   $custom_fields = new CustomFields($user->team_id);
45 }
46
47 // Report settings are stored in session bean before we get here.
48 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
49
50 // This file handles 2 types of export to a file:
51 // 1) xml
52 // 2) csv
53 // Export to pdf is handled separately in topdf.php.
54 $type = $request->getParameter('type');
55
56 // Also, there are 2 variations of report: totals only, or normal. Totals only means that the report
57 // is grouped by (either date, user, client, project, task, or cf_1) and user only needs to see subtotals by group.
58 $totals_only = $bean->getAttribute('chtotalsonly');
59
60 // Obtain items.
61 if ($totals_only)
62   $subtotals = ttReportHelper::getSubtotals($bean);
63 else
64   $items = ttReportHelper::getItems($bean);
65
66 // Build a string to use as filename for the files being downloaded.
67 $filename = strtolower($i18n->get('title.report')).'_'.$bean->mValues['start_date'].'_'.$bean->mValues['end_date'];
68
69 header('Pragma: public'); // This is needed for IE8 to download files over https.
70 header('Content-Type: text/html; charset=utf-8');
71 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
72 header('Cache-Control: no-store, no-cache, must-revalidate');
73 header('Cache-Control: post-check=0, pre-check=0', false);
74 header('Cache-Control: private', false);
75
76 // Handle 2 cases of possible exports individually.
77
78 // 1) entries exported to xml
79 if ('xml' == $type) {
80   header('Content-Type: application/xml');
81   header('Content-Disposition: attachment; filename="'.$filename.'.xml"');
82
83   print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
84   print "<rows>\n";
85
86   $group_by = $bean->getAttribute('group_by');
87   if ($totals_only) {
88     // Totals only report. Print subtotals.
89     foreach ($subtotals as $subtotal) {
90       print "<row>\n";
91       print "\t<".$group_by."><![CDATA[".$subtotal['name']."]]></".$group_by.">\n";
92       if ($bean->getAttribute('chduration')) {
93         $val = $subtotal['time'];
94         if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
95           $val = time_to_decimal($val);
96         print "\t<duration><![CDATA[".$val."]]></duration>\n";
97       }
98       if ($bean->getAttribute('chcost')) {
99         print "\t<cost><![CDATA[";
100         if ($user->canManageTeam() || $user->isClient())
101           print $subtotal['cost'];
102         else
103           print $subtotal['expenses'];
104         print "]]></cost>\n";
105       }
106       print "</row>\n";
107     }
108   } else {
109     // Normal report.
110     foreach ($items as $item) {
111       print "<row>\n";
112
113       print "\t<date><![CDATA[".$item['date']."]]></date>\n";
114       if ($user->canManageTeam() || $user->isClient()) print "\t<user><![CDATA[".$item['user']."]]></user>\n"; 
115       if ($bean->getAttribute('chclient')) print "\t<client><![CDATA[".$item['client']."]]></client>\n";
116       if ($bean->getAttribute('chproject')) print "\t<project><![CDATA[".$item['project']."]]></project>\n";
117       if ($bean->getAttribute('chtask')) print "\t<task><![CDATA[".$item['task']."]]></task>\n";
118       if ($bean->getAttribute('chcf_1')) print "\t<cf_1><![CDATA[".$item['cf_1']."]]></cf_1>\n";
119       if ($bean->getAttribute('chstart')) print "\t<start><![CDATA[".$item['start']."]]></start>\n";
120       if ($bean->getAttribute('chfinish')) print "\t<finish><![CDATA[".$item['finish']."]]></finish>\n";
121       if ($bean->getAttribute('chduration')) {
122         $duration = $item['duration'];
123         if($duration && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
124           $duration = time_to_decimal($duration);
125           print "\t<duration><![CDATA[".$duration."]]></duration>\n";
126       }
127       if ($bean->getAttribute('chnote')) print "\t<note><![CDATA[".$item['note']."]]></note>\n";
128       if ($bean->getAttribute('chcost')) {
129         print "\t<cost><![CDATA[";
130         if ($user->canManageTeam() || $user->isClient())
131           print $item['cost'];
132         else
133           print $item['expense'];
134         print "]]></cost>\n";
135       }
136       if ($bean->getAttribute('chpaid')) print "\t<paid><![CDATA[".$item['paid']."]]></paid>\n";
137       if ($bean->getAttribute('chip')) {
138         $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
139         print "\t<ip><![CDATA[".$ip."]]></ip>\n";
140       }
141       if ($bean->getAttribute('chinvoice')) print "\t<invoice><![CDATA[".$item['invoice']."]]></invoice>\n";
142
143       print "</row>\n";
144     }
145   }
146
147   print "</rows>";
148 }
149
150 // 2) entries exported to csv
151 if ('csv' == $type) {
152   header('Content-Type: application/csv');
153   header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
154
155   // Print UTF8 BOM first to identify encoding.
156   $bom = chr(239).chr(187).chr(191); // 0xEF 0xBB 0xBF in the beginning of the file is UTF8 BOM.
157   print $bom; // Without this Excel does not display UTF8 characters properly.
158
159   $group_by = $bean->getAttribute('group_by');
160   if ($totals_only) {
161     // Totals only report.
162
163     // Determine group_by header.
164     if ('cf_1' == $group_by)
165       $group_by_header = $custom_fields->fields[0]['label'];
166     else {
167       $key = 'label.'.$group_by;
168       $group_by_header = $i18n->get($key);
169     }
170
171     // Print headers.
172     print '"'.$group_by_header.'"';
173     if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
174     if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
175     print "\n";
176
177     // Print subtotals.
178     foreach ($subtotals as $subtotal) {
179       print '"'.$subtotal['name'].'"';
180       if ($bean->getAttribute('chduration')) {
181         $val = $subtotal['time'];
182         if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
183           $val = time_to_decimal($val);
184         print ',"'.$val.'"';
185       }
186       if ($bean->getAttribute('chcost')) {
187         if ($user->canManageTeam() || $user->isClient())
188           print ',"'.$subtotal['cost'].'"';
189         else
190           print ',"'.$subtotal['expenses'].'"';
191       }
192       print "\n";
193     }
194   } else {
195     // Normal report. Print headers.
196     print '"'.$i18n->get('label.date').'"';
197     if ($user->canManageTeam() || $user->isClient()) print ',"'.$i18n->get('label.user').'"';
198     if ($bean->getAttribute('chclient')) print ',"'.$i18n->get('label.client').'"';
199     if ($bean->getAttribute('chproject')) print ',"'.$i18n->get('label.project').'"';
200     if ($bean->getAttribute('chtask')) print ',"'.$i18n->get('label.task').'"';
201     if ($bean->getAttribute('chcf_1')) print ',"'.$custom_fields->fields[0]['label'].'"';
202     if ($bean->getAttribute('chstart')) print ',"'.$i18n->get('label.start').'"';
203     if ($bean->getAttribute('chfinish')) print ',"'.$i18n->get('label.finish').'"';
204     if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
205     if ($bean->getAttribute('chnote')) print ',"'.$i18n->get('label.note').'"';
206     if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
207     if ($bean->getAttribute('chpaid')) print ',"'.$i18n->get('label.paid').'"';
208     if ($bean->getAttribute('chip')) print ',"'.$i18n->get('label.ip').'"';
209     if ($bean->getAttribute('chinvoice')) print ',"'.$i18n->get('label.invoice').'"';
210     print "\n";
211
212     // Print items.
213     foreach ($items as $item) {
214       print '"'.$item['date'].'"';
215       if ($user->canManageTeam() || $user->isClient()) print ',"'.str_replace('"','""',$item['user']).'"';
216       if ($bean->getAttribute('chclient')) print ',"'.str_replace('"','""',$item['client']).'"';
217       if ($bean->getAttribute('chproject')) print ',"'.str_replace('"','""',$item['project']).'"';
218       if ($bean->getAttribute('chtask')) print ',"'.str_replace('"','""',$item['task']).'"';
219       if ($bean->getAttribute('chcf_1')) print ',"'.str_replace('"','""',$item['cf_1']).'"';
220       if ($bean->getAttribute('chstart')) print ',"'.$item['start'].'"';
221       if ($bean->getAttribute('chfinish')) print ',"'.$item['finish'].'"';
222       if ($bean->getAttribute('chduration')) {
223         $val = $item['duration'];
224         if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
225           $val = time_to_decimal($val);
226         print ',"'.$val.'"';
227       }
228       if ($bean->getAttribute('chnote')) print ',"'.str_replace('"','""',$item['note']).'"';
229       if ($bean->getAttribute('chcost')) {
230         if ($user->canManageTeam() || $user->isClient())
231           print ',"'.$item['cost'].'"';
232         else
233           print ',"'.$item['expense'].'"';
234       }
235       if ($bean->getAttribute('chpaid')) print ',"'.$item['paid'].'"';
236       if ($bean->getAttribute('chip')) {
237         $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
238         print ',"'.$ip.'"';
239       }
240       if ($bean->getAttribute('chinvoice')) print ',"'.str_replace('"','""',$item['invoice']).'"';
241       print "\n";
242     }
243   }
244 }