A bit more work in progress on attachment handling.
[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     $mdb2 = getConnection();
56
57     // Obtain site id.
58     $sql = "select param_value as id from tt_site_config where param_name = 'locker_id'";
59     $res = $mdb2->query($sql);
60     $val = $res->fetchRow();
61     if (!$val) {
62       // No site id found, need to register.
63       $fields = array('name' => urlencode('time tracker'),
64         'origin' => urlencode('time tracker source'));
65
66       // Urlify the data for the POST.
67       foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
68       $fields_string = rtrim($fields_string, '&');
69
70       // Open connection.
71       $ch = curl_init();
72
73       // Set the url, number of POST vars, POST data.
74       curl_setopt($ch, CURLOPT_URL, $this->register_uri);
75       curl_setopt($ch, CURLOPT_POST, count($fields));
76       curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
77       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
78
79       // Execute a post request.
80       $result = curl_exec($ch);
81
82       // Close connection.
83       curl_close($ch);
84
85       if ($result) {
86         $result_array = json_decode($result, true);
87         if ($result_array && $result_array['id'] && $result_array['key']) {
88
89           $this->site_id = $mdb2->quote($result_array['id']);
90           $this->site_key = $mdb2->quote($result_array['key']);
91
92           // Registration successful. Store id and key locally for future use.
93           $sql = "insert into tt_site_config values('locker_id', $site_id, now(), null)";
94           $mdb2->exec($sql);
95           $sql = "insert into tt_site_config values('locker_key', $key, now(), null)";
96           $mdb2->exec($sql);
97         }
98       }
99     } else {
100       // Site id found, need to update site attributes.
101       $this->site_id = $val['id'];
102
103       // Obtain site key.
104       $sql = "select param_value as site_key from tt_site_config where param_name = 'locker_key'";
105       $res = $mdb2->query($sql);
106       $val = $res->fetchRow();
107       $this->site_key = $val['site_key'];
108     }
109   }
110
111   // putFile - puts uploaded file in remote storage.
112   function putFile($description) {
113
114     $url = $this->storage_uri;
115     $fields = array('description' => urlencode($description),
116 //      'fname' => urlencode($_POST['first_name']),
117 //      'title' => urlencode($_POST['title']),
118 //      'company' => urlencode($_POST['institution']),
119 //      'age' => urlencode($_POST['age']),
120 //      'email' => urlencode($_POST['email']),
121 //      'phone' => urlencode($_POST['phone'])
122     );
123
124     // url-ify the data for the POST.
125     foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
126     $fields_string = rtrim($fields_string, '&');
127
128     // Open connection.
129     $ch = curl_init();
130
131     // Set the url, number of POST vars, POST data.
132     curl_setopt($ch, CURLOPT_URL, $url);
133     curl_setopt($ch, CURLOPT_POST, count($fields));
134     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
135     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
136
137     // Execute a post rewuest.
138     $result = curl_exec($ch);
139
140     // Close connection.
141     curl_close($ch);
142
143     if ($result) {
144       $result_array = json_decode($result, true);
145       $file_id = $mdb2->quote($result_array['id']);
146     }
147
148     unlink($_FILES['newfile']['tmp_name']);
149     return false; // Not implemented.
150 /*
151     // Create a temporary file.
152     $dirName = dirname(TEMPLATE_DIR . '_c/.');
153     $filename = tempnam($dirName, 'import_');
154
155     // If the file is compressed - uncompress it.
156     if ($compressed) {
157       if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
158         $this->errors->add($i18n->get('error.sys'));
159         return;
160       }
161       unlink($_FILES['newfile']['tmp_name']);
162     } else {
163       if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
164         $this->errors->add($i18n->get('error.upload'));
165         return;
166       }
167     }*/
168   }
169 }