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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 // ttFileHelper class is used for attachment handling.
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.
40 function __construct(&$errors) {
41 $this->errors = &$errors;
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();
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() {
57 $mdb2 = getConnection();
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();
64 // No site id found, need to register.
65 $fields = array('name' => urlencode('time tracker'),
66 'origin' => urlencode('time tracker source'));
68 // Urlify the data for the POST.
69 foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
70 $fields_string = rtrim($fields_string, '&');
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);
81 // Execute a post request.
82 $result = curl_exec($ch);
87 $result_array = json_decode($result, true);
89 $this->errors->add($i18n->get('error.file_storage'));
91 else if ($result_array['error']) {
92 // Add an error from file storage facility if we have it.
93 $this->errors->add($result_array['error']);
95 else if ($result_array['id'] && $result_array['key']) {
96 $this->site_id = $result_array['id'];
97 $this->site_key = $result_array['key'];
99 // Registration successful. Store id and key locally for future use.
100 $sql = "insert into tt_site_config values('locker_id', $this->site_id, now(), null)";
102 $sql = "insert into tt_site_config values('locker_key', ".$mdb2->quote($this->site_key).", now(), null)";
105 $this->errors->add($i18n->get('error.file_storage'));
109 $this->site_id = $val['id'];
112 $sql = "select param_value as site_key from tt_site_config where param_name = 'locker_key'";
113 $res = $mdb2->query($sql);
114 $val = $res->fetchRow();
115 $this->site_key = $val['site_key'];
119 // putFile - puts uploaded file in remote storage.
120 function putFile($fields) {
123 $mdb2 = getConnection();
125 $group_id = $user->getGroup();
126 $org_id = $user->org_id;
128 $curl_fields = array('site_id' => urlencode($this->site_id),
129 'site_key' => urlencode($this->site_key),
130 'org_id' => urlencode($org_id),
131 'org_key' => urlencode($this->getOrgKey()),
132 'group_id' => urlencode($group_id),
133 'group_key' => urlencode($this->getGroupKey()),
134 'user_id' => urlencode($fields['user_id']), // May be null.
135 'user_key' => urlencode($fields['user_key']), // May be null.
136 'file_name' => urlencode($fields['file_name']),
137 'description' => urlencode($fields['description']),
138 'content' => urlencode(file_get_contents($_FILES['newfile']['tmp_name']))
141 // url-ify the data for the POST.
142 foreach($curl_fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
143 $fields_string = rtrim($fields_string, '&');
148 // Set the url, number of POST vars, POST data.
149 curl_setopt($ch, CURLOPT_URL, $this->putfile_uri);
150 curl_setopt($ch, CURLOPT_POST, count($fields));
151 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
152 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
154 // Execute a post request.
155 $result = curl_exec($ch);
160 // Delete uploaded file.
161 unlink($_FILES['newfile']['tmp_name']);
164 $this->errors->add($i18n->get('error.file_storage'));
168 $result_array = json_decode($result, true);
169 $file_id = (int) $result_array['file_id'];
170 $file_key = $result_array['file_key'];
171 $error = $result_array['error'];
173 if ($error || !$file_id || !$file_key) {
175 // Add an error from file storage facility if we have it.
176 $this->errors->add($error);
181 // File put was successful. Store file attributes locally.
182 $file_key = $mdb2->quote($file_key);
183 $entity_type = $mdb2->quote($fields['entity_type']);
184 $entity_id = (int) $fields['entity_id'];
185 $file_name = $mdb2->quote($fields['file_name']);
186 $description = $mdb2->quote($fields['description']);
188 $created_ip = $mdb2->quote($_SERVER['REMOTE_ADDR']);
189 $created_by = $user->id;
191 $columns = '(group_id, org_id, remote_id, file_key, entity_type, entity_id, file_name, description, created, created_ip, created_by)';
192 $values = "values($group_id, $org_id, $file_id, $file_key, $entity_type, $entity_id, $file_name, $description, $created, $created_ip, $created_by)";
193 $sql = "insert into tt_files $columns $values";
194 $affected = $mdb2->exec($sql);
195 return (!is_a($affected, 'PEAR_Error'));
198 // deleteFile - deletes a file from remote storage and its details from local database.
199 function deleteFile($fields) {
202 $mdb2 = getConnection();
204 $group_id = $user->getGroup();
205 $org_id = $user->org_id;
207 $curl_fields = array('site_id' => urlencode($this->site_id),
208 'site_key' => urlencode($this->site_key),
209 'org_id' => urlencode($org_id),
210 'org_key' => urlencode($this->getOrgKey()),
211 'group_id' => urlencode($group_id),
212 'group_key' => urlencode($this->getGroupKey()),
213 'user_id' => urlencode($fields['user_id']), // May be null.
214 'user_key' => urlencode($fields['user_key']), // May be null.
215 'file_id' => urlencode($fields['remote_id']),
216 'file_key' => urlencode($fields['file_key'])
219 // url-ify the data for the POST.
220 foreach($curl_fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
221 $fields_string = rtrim($fields_string, '&');
226 // Set the url, number of POST vars, POST data.
227 curl_setopt($ch, CURLOPT_URL, $this->putfile_uri);
228 curl_setopt($ch, CURLOPT_POST, count($fields));
229 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
230 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
232 // Execute a post request.
233 $result = curl_exec($ch);
239 $this->errors->add($i18n->get('error.file_storage'));
243 $result_array = json_decode($result, true);
244 $status = (int) $result_array['status'];
245 $error = $result_array['error'];
248 // Add an error from file storage facility if we have it.
249 $this->errors->add($error);
253 // There is no explicit error message, but still something not right.
254 $this->errors->add($i18n->get('error.file_storage'));
258 // Delete file reference from database.
259 $file_id = $fields['id'];
260 $sql = "delete from tt_files".
261 " where id = $file_id and org_id = $org_id and group_id = $group_id";
262 $affected = $mdb2->exec($sql);
263 if (is_a($affected, 'PEAR_Error')) {
264 $this->errors->add($i18n->get('error.db'));
268 // File successfully deleted from both file storage and database.
272 // getOrgKey obtains organization key from the database.
273 private function getOrgKey() {
275 $mdb2 = getConnection();
277 $org_id = $user->org_id;
278 $sql = "select group_key from tt_groups where id = $org_id and status = 1";
279 $res = $mdb2->query($sql);
280 $val = $res->fetchRow();
281 return $val['group_key'];
284 // getGrtoupKey obtains group key from the database.
285 private function getGroupKey() {
287 $mdb2 = getConnection();
289 $group_id = $user->getGroup();
290 $org_id = $user->org_id;
292 $sql = "select group_key from tt_groups where id = $group_id and org_id = $org_id and status = 1";
293 $res = $mdb2->query($sql);
294 $val = $res->fetchRow();
295 return $val['group_key'];
298 // getProjectFiles obtains a list of files for a project.
299 static function getProjectFiles($project_id) {
301 $mdb2 = getConnection();
303 $group_id = $user->getGroup();
304 $org_id = $user->org_id;
307 $sql = "select id, remote_id, file_name as name, description from tt_files".
308 " where entity_type = 'project' and entity_id = $project_id".
309 " and group_id = $group_id and org_id = $org_id and status = 1 order by id";
310 $res = $mdb2->query($sql);
311 if (!is_a($res, 'PEAR_Error')) {
312 while ($val = $res->fetchRow()) {
319 // get - obtains file details from local database.
320 static function get($id) {
322 $mdb2 = getConnection();
324 $group_id = $user->getGroup();
325 $org_id = $user->org_id;
327 $sql = "select id, remote_id, file_key, entity_type, entity_id, file_name, description, status from tt_files".
328 " where id = $id and group_id = $group_id and org_id = $org_id and (status = 0 or status = 1)";
329 $res = $mdb2->query($sql);
330 if (!is_a($res, 'PEAR_Error')) {
331 $val = $res->fetchRow();
332 if ($val && $val['id'])