Dutch translation improved.
[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 $getfile_uri = null;  // URI to get file from file storage.
36   var $site_id = null;      // Site id for file storage.
37   var $site_key = null;     // Site key for file storage.
38
39   // Constructor.
40   function __construct(&$errors) {
41     $this->errors = &$errors;
42
43     if (defined('FILE_STORAGE_URI')) {
44       $this->storage_uri = FILE_STORAGE_URI;
45       $this->register_uri = $this->storage_uri.'register';
46       $this->putfile_uri = $this->storage_uri.'putfile';
47       $this->getfile_uri = $this->storage_uri.'getfile';
48       $this->checkSiteRegistration();
49     }
50   }
51
52   // checkSiteRegistration - obtains site id and key from local database.
53   // If not found, it tries to register with file storage facility.
54   function checkSiteRegistration() {
55
56     global $i18n;
57     $mdb2 = getConnection();
58
59     // Obtain site id.
60     $sql = "select param_value as id from tt_site_config where param_name = 'locker_id'";
61     $res = $mdb2->query($sql);
62     $val = $res->fetchRow();
63     if (!$val) {
64       // No site id found, need to register.
65       $fields = array('name' => urlencode('time tracker'),
66         'origin' => urlencode('time tracker source'));
67
68       // Urlify the data for the POST.
69       foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
70       $fields_string = rtrim($fields_string, '&');
71
72       // Open connection.
73       $ch = curl_init();
74
75       // Set the url, number of POST vars, POST data.
76       curl_setopt($ch, CURLOPT_URL, $this->register_uri);
77       curl_setopt($ch, CURLOPT_POST, count($fields));
78       curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
79       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
80
81       // Execute a post request.
82       $result = curl_exec($ch);
83
84       // Close connection.
85       curl_close($ch);
86
87       $result_array = json_decode($result, true);
88       if ($result_array && $result_array['id'] && $result_array['key']) {
89
90         $this->site_id = $result_array['id'];
91         $this->site_key = $result_array['key'];
92
93         // Registration successful. Store id and key locally for future use.
94         $sql = "insert into tt_site_config values('locker_id', $this->site_id, now(), null)";
95         $mdb2->exec($sql);
96         $sql = "insert into tt_site_config values('locker_key', ".$mdb2->quote($this->site_key).", now(), null)";
97         $mdb2->exec($sql);
98       } else {
99         $this->errors->add($i18n->get('error.file_storage'));
100       }
101     } else {
102       // Site id found.
103       $this->site_id = $val['id'];
104
105       // Obtain site key.
106       $sql = "select param_value as site_key from tt_site_config where param_name = 'locker_key'";
107       $res = $mdb2->query($sql);
108       $val = $res->fetchRow();
109       $this->site_key = $val['site_key'];
110     }
111   }
112
113   // putFile - puts uploaded file in remote storage.
114   function putFile($fields) {
115     global $i18n;
116     global $user;
117     $mdb2 = getConnection();
118
119     $group_id = $user->getGroup();
120     $org_id = $user->org_id;
121
122     $fields = array('site_id' => urlencode($this->site_id),
123       'site_key' => urlencode($this->site_key),
124       'org_id' => urlencode($org_id),
125       'org_key' => urlencode($this->getOrgKey()),
126       'group_id' => urlencode($group_id),
127       'group_key' => urlencode($this->getGroupKey()),
128       'user_id' => urlencode($fields['user_id']),   // May be null.
129       'user_key' => urlencode($fields['user_key']), // May be null.
130       'file_name' => urlencode($fields['file_name']),
131       'description' => urlencode($fields['description']),
132       // TODO: add file content here, too. Will this work for large files?
133     );
134
135     // url-ify the data for the POST.
136     foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
137     $fields_string = rtrim($fields_string, '&');
138
139     // Open connection.
140     $ch = curl_init();
141
142     // Set the url, number of POST vars, POST data.
143     curl_setopt($ch, CURLOPT_URL, $this->putfile_uri);
144     curl_setopt($ch, CURLOPT_POST, count($fields));
145     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
146     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
147
148     // Execute a post rewuest.
149     $result = curl_exec($ch);
150
151     // Close connection.
152     curl_close($ch);
153
154     // Delete uploaded file.
155     unlink($_FILES['newfile']['tmp_name']);
156
157     if (!$result) return false;
158
159     $result_array = json_decode($result, true);
160     $file_id = (int) $result_array['file_id'];
161     $file_key = $result_array['file_key'];
162     $file_error = $result_array['file_error'];
163
164     if (!$file_id || !$file_key) {
165       if ($file_error) {
166         // Add an error from file storage facility if we have it.
167         $this->errors->add($file_error);
168       }
169       return false;
170     }
171
172     // File put was successful. Store file attributes locally.
173     $entity_type = $mdb2->quote($fields['entity_type']);
174     $entity_id = (int) $fields['entity_id'];
175     $file_name = $mdb2->quote($fields['file_name']);
176     $description = $mdb2->quote($fields['description']);
177     $created = 'now()';
178     $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
179     $created_by = $user->id;
180
181     $columns = '(group_id, org_id, remote_id, entity_type, entity_id, file_name, description, created, created_ip, created_by)';
182     $values = "values($group_id, $org_id, $file_id, $entity_type, $entity_id, $file_name, $description, $created, $created_ip, $created_by)";
183     $sql = "insert into tt_files $columns $values";
184     $affected = $mdb2->exec($sql);
185     return (!is_a($affected, 'PEAR_Error'));
186   }
187
188   // getOrgKey obtains organization key from the database.
189   private function getOrgKey() {
190     global $user;
191     $mdb2 = getConnection();
192
193     $org_id = $user->org_id;
194     $sql = "select group_key from tt_groups where id = $org_id and status = 1";
195     $res = $mdb2->query($sql);
196     $val = $res->fetchRow();
197     return $val['group_key'];
198   }
199
200   // getGrtoupKey obtains group key from the database.
201   private function getGroupKey() {
202     global $user;
203     $mdb2 = getConnection();
204
205     $group_id = $user->getGroup();
206     $org_id = $user->org_id;
207
208     $sql = "select group_key from tt_groups where id = $group_id and org_id = $org_id and status = 1";
209     $res = $mdb2->query($sql);
210     $val = $res->fetchRow();
211     return $val['group_key'];
212   }
213 }