More progress with attachments.
[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 stroage 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       if ($result) {
88         $result_array = json_decode($result, true);
89         if ($result_array && $result_array['id'] && $result_array['key']) {
90
91           $this->site_id = $mdb2->quote($result_array['id']);
92           $this->site_key = $mdb2->quote($result_array['key']);
93
94           // Registration successful. Store id and key locally for future use.
95           $sql = "insert into tt_site_config values('locker_id', $site_id, now(), null)";
96           $mdb2->exec($sql);
97           $sql = "insert into tt_site_config values('locker_key', $key, now(), null)";
98           $mdb2->exec($sql);
99         } else {
100           $this->errors->add($i18n->get('error.file_storage'));
101         }
102       }
103     } else {
104       // Site id found, need to update site attributes.
105       $this->site_id = $val['id'];
106
107       // Obtain site key.
108       $sql = "select param_value as site_key from tt_site_config where param_name = 'locker_key'";
109       $res = $mdb2->query($sql);
110       $val = $res->fetchRow();
111       $this->site_key = $val['site_key'];
112     }
113   }
114
115   // putFile - puts uploaded file in remote storage.
116   function putFile($fields) {
117     $mdb2 = getConnection();
118
119     $fields = array('site_id' => urlencode($this->site_id),
120       'site_key' => urlencode($this->site_key),
121       //'org_id' => urlencode($this->org_id),       // TODO: obtain this properly.
122       //'org_key' => urlencode($this->org_key),     // TODO: obtain this properly.
123       //'group_id' => urlencode($this->group_id),   // TODO: obtain this properly.
124       //'group_key' => urlencode($this->group_key), // TODO: obtain this properly.
125       //'user_id' => urlencode($this->user_id),     // TODO: obtain this properly.
126       //'user_key' => urlencode($this->user_key),   // TODO: obtain this properly.
127       'file_name' => urlencode($fields['file_name']),
128       'description' => urlencode($fields['description']),
129       // TODO: add file content here, too. Will this work for large files?
130       //
131     );
132
133     // url-ify the data for the POST.
134     foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
135     $fields_string = rtrim($fields_string, '&');
136
137     // Open connection.
138     $ch = curl_init();
139
140     // Set the url, number of POST vars, POST data.
141     curl_setopt($ch, CURLOPT_URL, $this->putfile_uri);
142     curl_setopt($ch, CURLOPT_POST, count($fields));
143     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
144     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
145
146     // Execute a post rewuest.
147     $result = curl_exec($ch);
148
149     // Close connection.
150     curl_close($ch);
151
152     if ($result) {
153       $result_array = json_decode($result, true);
154       $file_id = $result_array['file_id'];
155       $file_key = $result_array['file_key'];
156     }
157
158     unlink($_FILES['newfile']['tmp_name']);
159     return false; // Not implemented.
160 /*
161     // Create a temporary file.
162     $dirName = dirname(TEMPLATE_DIR . '_c/.');
163     $filename = tempnam($dirName, 'import_');
164
165     // If the file is compressed - uncompress it.
166     if ($compressed) {
167       if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
168         $this->errors->add($i18n->get('error.sys'));
169         return;
170       }
171       unlink($_FILES['newfile']['tmp_name']);
172     } else {
173       if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
174         $this->errors->add($i18n->get('error.upload'));
175         return;
176       }
177     }*/
178   }
179 }