Added capability to add an attachment to time.php on first submit.
[timetracker.git] / WEB-INF / lib / ttFileHelper.class.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 // ttFileHelper class is used for attachment handling.
30 class ttFileHelper {
31   var $errors = null;       // Errors go here. Set in constructor by reference.
32   var $storage_uri = null;  // Location of file storage facility.
33   var $register_uri = null; // URI to register with file storage facility.
34   var $putfile_uri = null;  // URI to put file in file storage.
35   var $deletefile_uri = null;  // URI to delete file from file storage.
36   var $deletefiles_uri = null; // URI to delete multiple files from file storage.
37   var $getfile_uri = null;  // URI to get file from file storage.
38   var $site_id = null;      // Site id for file storage.
39   var $site_key = null;     // Site key for file storage.
40   var $file_data = null;     // Downloaded file data.
41
42   // Constructor.
43   function __construct(&$errors) {
44     $this->errors = &$errors;
45
46     if (defined('FILE_STORAGE_URI')) {
47       $this->storage_uri = FILE_STORAGE_URI;
48       $this->register_uri = $this->storage_uri.'register';
49       $this->putfile_uri = $this->storage_uri.'putfile';
50       $this->deletefile_uri = $this->storage_uri.'deletefile';
51       $this->deletefiles_uri = $this->storage_uri.'deletefiles';
52       $this->getfile_uri = $this->storage_uri.'getfile';
53       $this->checkSiteRegistration();
54     }
55   }
56
57   // checkSiteRegistration - obtains site id and key from local database.
58   // If not found, it tries to register with file storage facility.
59   function checkSiteRegistration() {
60
61     global $i18n;
62     $mdb2 = getConnection();
63
64     // Obtain site id.
65     $sql = "select param_value as id from tt_site_config where param_name = 'locker_id'";
66     $res = $mdb2->query($sql);
67     $val = $res->fetchRow();
68     if (!$val) {
69       // No site id found, need to register.
70       $fields = array('name' => urlencode('time tracker'),
71         'origin' => urlencode('time tracker source'));
72
73       // Urlify the data for the POST.
74       foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
75       $fields_string = rtrim($fields_string, '&');
76
77       // Open connection.
78       $ch = curl_init();
79
80       // Set the url, number of POST vars, POST data.
81       curl_setopt($ch, CURLOPT_URL, $this->register_uri);
82       curl_setopt($ch, CURLOPT_POST, true);
83       curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
84       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
85
86       // Execute a post request.
87       $result = curl_exec($ch);
88
89       // Close connection.
90       curl_close($ch);
91
92       $result_array = json_decode($result, true);
93       if (!$result_array) {
94         $this->errors->add($i18n->get('error.file_storage'));
95       }
96       else if ($result_array['error']) {
97         // Add an error from file storage facility if we have it.
98         $this->errors->add($result_array['error']);
99       }
100       else if ($result_array['id'] && $result_array['key']) {
101         $this->site_id = $result_array['id'];
102         $this->site_key = $result_array['key'];
103
104         // Registration successful. Store id and key locally for future use.
105         $sql = "insert into tt_site_config values('locker_id', $this->site_id, now(), null)";
106         $mdb2->exec($sql);
107         $sql = "insert into tt_site_config values('locker_key', ".$mdb2->quote($this->site_key).", now(), null)";
108         $mdb2->exec($sql);
109       } else {
110         $this->errors->add($i18n->get('error.file_storage'));
111       }
112     } else {
113       // Site id found.
114       $this->site_id = $val['id'];
115
116       // Obtain site key.
117       $sql = "select param_value as site_key from tt_site_config where param_name = 'locker_key'";
118       $res = $mdb2->query($sql);
119       $val = $res->fetchRow();
120       $this->site_key = $val['site_key'];
121     }
122   }
123
124   // putFile - puts uploaded file in remote storage.
125   function putFile($fields) {
126     global $i18n;
127     global $user;
128     $mdb2 = getConnection();
129
130     $group_id = $user->getGroup();
131     $org_id = $user->org_id;
132
133     $curl_fields = array('site_id' => urlencode($this->site_id),
134       'site_key' => urlencode($this->site_key),
135       'org_id' => urlencode($org_id),
136       'org_key' => urlencode($this->getOrgKey()),
137       'group_id' => urlencode($group_id),
138       'group_key' => urlencode($this->getGroupKey()),
139       'entity_type' => urlencode($fields['entity_type']),
140       'entity_id' => urlencode($fields['entity_id']),
141       'file_name' => urlencode($fields['file_name']),
142       'description' => urlencode($fields['description']),
143       'content' => urlencode(base64_encode(file_get_contents($_FILES['newfile']['tmp_name'])))
144     );
145
146     // url-ify the data for the POST.
147     foreach($curl_fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
148     $fields_string = rtrim($fields_string, '&');
149
150     // Open connection.
151     $ch = curl_init();
152
153     // Set the url, number of POST vars, POST data.
154     curl_setopt($ch, CURLOPT_URL, $this->putfile_uri);
155     curl_setopt($ch, CURLOPT_POST, true);
156     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
157     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
158
159     // Execute a post request.
160     $result = curl_exec($ch);
161
162     // Close connection.
163     curl_close($ch);
164
165     // Delete uploaded file.
166     unlink($_FILES['newfile']['tmp_name']);
167
168     if (!$result) {
169       $this->errors->add($i18n->get('error.file_storage'));
170       return false;
171     }
172
173     $result_array = json_decode($result, true);
174     $file_id = (int) $result_array['file_id'];
175     $file_key = $result_array['file_key'];
176     $error = $result_array['error'];
177
178     if ($error || !$file_id || !$file_key) {
179       if ($error) {
180         // Add an error from file storage facility if we have it.
181         $this->errors->add($error);
182       }
183       return false;
184     }
185
186     // File put was successful. Store file attributes locally.
187     $file_key = $mdb2->quote($file_key);
188     $entity_type = $mdb2->quote($fields['entity_type']);
189     $entity_id = (int) $fields['entity_id'];
190     $file_name = $mdb2->quote($fields['file_name']);
191     $description = $mdb2->quote($fields['description']);
192     $created = 'now()';
193     $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
194     $created_by = $user->id;
195
196     $columns = '(group_id, org_id, remote_id, file_key, entity_type, entity_id, file_name, description, created, created_ip, created_by)';
197     $values = "values($group_id, $org_id, $file_id, $file_key, $entity_type, $entity_id, $file_name, $description, $created, $created_ip, $created_by)";
198     $sql = "insert into tt_files $columns $values";
199     $affected = $mdb2->exec($sql);
200     return (!is_a($affected, 'PEAR_Error'));
201   }
202
203   // deleteFile - deletes a file from remote storage and its details from local database.
204   function deleteFile($fields) {
205     global $i18n;
206     global $user;
207     $mdb2 = getConnection();
208
209     $group_id = $user->getGroup();
210     $org_id = $user->org_id;
211
212     $curl_fields = array('site_id' => urlencode($this->site_id),
213       'site_key' => urlencode($this->site_key),
214       'org_id' => urlencode($org_id),
215       'org_key' => urlencode($this->getOrgKey()),
216       'group_id' => urlencode($group_id),
217       'group_key' => urlencode($this->getGroupKey()),
218       'entity_type' => urlencode($fields['entity_type']),
219       'entity_id' => urlencode($fields['entity_id']),
220       'file_id' => urlencode($fields['remote_id']),
221       'file_key' => urlencode($fields['file_key']),
222       'file_name' => urlencode($fields['file_name']));
223
224     // url-ify the data for the POST.
225     foreach($curl_fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
226     $fields_string = rtrim($fields_string, '&');
227
228     // Open connection.
229     $ch = curl_init();
230
231     // Set the url, number of POST vars, POST data.
232     curl_setopt($ch, CURLOPT_URL, $this->deletefile_uri);
233     curl_setopt($ch, CURLOPT_POST, true);
234     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
235     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
236
237     // Execute a post request.
238     $result = curl_exec($ch);
239
240     // Close connection.
241     curl_close($ch);
242
243     if (!$result) {
244       $this->errors->add($i18n->get('error.file_storage'));
245       return false;
246     }
247
248     $result_array = json_decode($result, true);
249     $status = (int) $result_array['status'];
250     $error = $result_array['error'];
251
252     if ($error) {
253       // Add an error from file storage facility if we have it.
254       $this->errors->add($error);
255     }
256     if ($status != 1) {
257       // There is no explicit error message, but still something not right.
258       $this->errors->add($i18n->get('error.file_storage'));
259     }
260
261     // Delete file reference from database even when remote file storage call fails.
262     // This is by design to keep things simple.
263     $file_id = (int) $fields['id'];
264     $entity_id = (int) $fields['entity_id'];
265     $sql = "delete from tt_files".
266       " where id = $file_id and org_id = $org_id and group_id = $group_id and entity_id = $entity_id";
267     $affected = $mdb2->exec($sql);
268     if (is_a($affected, 'PEAR_Error')) {
269       $this->errors->add($i18n->get('error.db'));
270       return false;
271     }
272
273     // File successfully deleted from both file storage and database.
274     return true;
275   }
276
277   // deleteEntityFiles - deletes all files associated with an entity.
278   function deleteEntityFiles($entity_id, $entity_type) {
279
280     if (!$this->entityHasFiles($entity_id, $entity_type))
281       return true; // No files to delete.
282     
283     global $i18n;
284     global $user;
285     $mdb2 = getConnection();
286
287     $group_id = $user->getGroup();
288     $org_id = $user->org_id;
289
290     $curl_fields = array('site_id' => urlencode($this->site_id),
291       'site_key' => urlencode($this->site_key),
292       'org_id' => urlencode($org_id),
293       'org_key' => urlencode($this->getOrgKey()),
294       'group_id' => urlencode($group_id),
295       'group_key' => urlencode($this->getGroupKey()),
296       'entity_type' => urlencode($entity_type),
297       'entity_id' => urlencode($entity_id));
298
299     // url-ify the data for the POST.
300     foreach($curl_fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
301     $fields_string = rtrim($fields_string, '&');
302
303     // Open connection.
304     $ch = curl_init();
305
306     // Set the url, number of POST vars, POST data.
307     curl_setopt($ch, CURLOPT_URL, $this->deletefiles_uri);
308     curl_setopt($ch, CURLOPT_POST, true);
309     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
310     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
311
312     // Execute a post request.
313     $result = curl_exec($ch);
314
315     // Close connection.
316     curl_close($ch);
317
318     if (!$result) {
319       $this->errors->add($i18n->get('error.file_storage'));
320       return false;
321     }
322
323     $result_array = json_decode($result, true);
324     $status = (int) $result_array['status'];
325     $error = $result_array['error'];
326
327     if ($error) {
328       // Add an error from file storage facility if we have it.
329       $this->errors->add($error);
330     }
331     if ($status != 1) {
332       // There is no explicit error message, but still something not right.
333       $this->errors->add($i18n->get('error.file_storage'));
334     }
335
336     // Many things can go wrong with a remote call to file storage facility.
337     // By design, we ignore such errors, and proceed with removal of entity
338     // records from the database.
339
340     // Delete all entity records from the database.
341     $file_id = $fields['id'];
342     $sql = "delete from tt_files".
343       " where entity_id = $entity_id".
344       " and entity_type = ".$mdb2->quote($entity_type).
345       " and org_id = $org_id and group_id = $group_id";
346     $affected = $mdb2->exec($sql);
347     if (is_a($affected, 'PEAR_Error')) {
348       $this->errors->add($i18n->get('error.db'));
349       return false;
350     }
351
352     return true;
353   }
354
355   // entityHasFiles determines if an entity has any files referenced in database.
356   private function entityHasFiles($entity_id, $entity_type) {
357     global $user;
358     $mdb2 = getConnection();
359
360     $group_id = $user->getGroup();
361     $org_id = $user->org_id;
362
363     $sql = "select id from tt_files where org_id = $org_id and group_id = $group_id".
364       " and entity_type = ".$mdb2->quote($entity_type)." and entity_id = $entity_id limit 1";
365     $res = $mdb2->query($sql);
366     $val = $res->fetchRow();
367     return $val['id'] > 0;
368   }
369
370   // getOrgKey obtains organization key from the database.
371   private function getOrgKey() {
372     global $user;
373     $mdb2 = getConnection();
374
375     $org_id = $user->org_id;
376     $sql = "select group_key from tt_groups where id = $org_id and status = 1";
377     $res = $mdb2->query($sql);
378     $val = $res->fetchRow();
379     return $val['group_key'];
380   }
381
382   // getGrtoupKey obtains group key from the database.
383   private function getGroupKey() {
384     global $user;
385     $mdb2 = getConnection();
386
387     $group_id = $user->getGroup();
388     $org_id = $user->org_id;
389
390     $sql = "select group_key from tt_groups where id = $group_id and org_id = $org_id and status = 1";
391     $res = $mdb2->query($sql);
392     $val = $res->fetchRow();
393     return $val['group_key'];
394   }
395
396   // getEntityFiles obtains a list of files for an entity.
397   static function getEntityFiles($id, $type) {
398     global $user;
399     $mdb2 = getConnection();
400
401     $group_id = $user->getGroup();
402     $org_id = $user->org_id;
403
404     $result = array();
405     $entity_type = $mdb2->quote($type);
406     $sql = "select id, remote_id, file_key, file_name as name, description from tt_files".
407       " where entity_type = $entity_type and entity_id = $id".
408       " and group_id = $group_id and org_id = $org_id and status = 1 order by id";
409     $res = $mdb2->query($sql);
410     if (!is_a($res, 'PEAR_Error')) {
411       while ($val = $res->fetchRow()) {
412         $result[] = $val;
413       }
414     }
415     return $result;
416   }
417
418   // get - obtains file details from local database. 
419   static function get($id) {
420     global $user;
421     $mdb2 = getConnection();
422
423     $group_id = $user->getGroup();
424     $org_id = $user->org_id;
425
426     $sql = "select id, remote_id, file_key, entity_type, entity_id, file_name, description, status from tt_files".
427       " where id = $id and group_id = $group_id and org_id = $org_id and (status = 0 or status = 1)";
428     $res = $mdb2->query($sql);
429     if (!is_a($res, 'PEAR_Error')) {
430       $val = $res->fetchRow();
431       if ($val && $val['id'])
432         return $val;
433     }
434     return false;
435   }
436
437   // update - updates file details in local database.
438   static function update($fields) {
439     global $user;
440     $mdb2 = getConnection();
441
442     $group_id = $user->getGroup();
443     $org_id = $user->org_id;
444
445     $file_id = (int) $fields['id'];
446     $description = $mdb2->quote($fields['description']);
447
448     $sql = "update tt_files set description = $description where id = $file_id".
449       " and group_id = $group_id and org_id = $org_id and (status = 0 or status = 1)";
450     $affected = $mdb2->exec($sql);
451     return !is_a($affected, 'PEAR_Error');
452   }
453
454
455   // getFile - downloads file from remote storage to memory.
456   function getFile($fields) {
457     global $i18n;
458     global $user;
459     $mdb2 = getConnection();
460
461     $group_id = $user->getGroup();
462     $org_id = $user->org_id;
463
464     $curl_fields = array('site_id' => urlencode($this->site_id),
465       'site_key' => urlencode($this->site_key),
466       'org_id' => urlencode($org_id),
467       'org_key' => urlencode($this->getOrgKey()),
468       'group_id' => urlencode($group_id),
469       'group_key' => urlencode($this->getGroupKey()),
470       'entity_type' => urlencode($fields['entity_type']),
471       'entity_id' => urlencode($fields['entity_id']),
472       'file_id' => urlencode($fields['remote_id']),
473       'file_key' => urlencode($fields['file_key']),
474       'file_name' => urlencode($fields['file_name']));
475
476     // url-ify the data for the POST.
477     foreach($curl_fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
478     $fields_string = rtrim($fields_string, '&');
479
480     // Open connection.
481     $ch = curl_init();
482
483     // Set the url, number of POST vars, POST data.
484     curl_setopt($ch, CURLOPT_URL, $this->getfile_uri);
485     curl_setopt($ch, CURLOPT_POST, true);
486     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
487     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
488
489     // Execute a post request.
490     $result = curl_exec($ch);
491
492     $error = curl_error();
493     $result_array2 = json_decode($result, true);
494
495     // Close connection.
496     curl_close($ch);
497
498     if (!$result) {
499       $this->errors->add($i18n->get('error.file_storage'));
500       return false;
501     }
502
503     $result_array = json_decode($result, true);
504     $status = (int) $result_array['status'];
505     $error = $result_array['error'];
506
507     if ($error) {
508       // Add an error from file storage facility if we have it.
509       $this->errors->add($error);
510       return false;
511     }
512     if ($status != 1) {
513       // There is no explicit error message, but still something not right.
514       $this->errors->add($i18n->get('error.file_storage'));
515       return false;
516     }
517
518     $this->file_data = $result_array['content'];
519     return true;
520   }
521
522
523   // getFileData - returns file data from memory.
524   function getFileData() {
525     return base64_decode($this->file_data);
526   }
527 }