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 stroage facility.
54 function checkSiteRegistration() {
55 $mdb2 = getConnection();
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();
62 // No site id found, need to register.
63 $fields = array('name' => urlencode('time tracker'),
64 'origin' => urlencode('time tracker source'));
66 // Urlify the data for the POST.
67 foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
68 $fields_string = rtrim($fields_string, '&');
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);
79 // Execute a post request.
80 $result = curl_exec($ch);
86 $result_array = json_decode($result, true);
87 if ($result_array && $result_array['id'] && $result_array['key']) {
89 $this->site_id = $mdb2->quote($result_array['id']);
90 $this->site_key = $mdb2->quote($result_array['key']);
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)";
95 $sql = "insert into tt_site_config values('locker_key', $key, now(), null)";
100 // Site id found, need to update site attributes.
101 $this->site_id = $val['id'];
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'];
111 // putFile - puts uploaded file in remote storage.
112 function putFile($description) {
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'])
124 // url-ify the data for the POST.
125 foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
126 $fields_string = rtrim($fields_string, '&');
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);
137 // Execute a post rewuest.
138 $result = curl_exec($ch);
144 $result_array = json_decode($result, true);
145 $file_id = $mdb2->quote($result_array['id']);
148 unlink($_FILES['newfile']['tmp_name']);
149 return false; // Not implemented.
151 // Create a temporary file.
152 $dirName = dirname(TEMPLATE_DIR . '_c/.');
153 $filename = tempnam($dirName, 'import_');
155 // If the file is compressed - uncompress it.
157 if (!$this->uncompress($_FILES['xmlfile']['tmp_name'], $filename)) {
158 $this->errors->add($i18n->get('error.sys'));
161 unlink($_FILES['newfile']['tmp_name']);
163 if (!move_uploaded_file($_FILES['xmlfile']['tmp_name'], $filename)) {
164 $this->errors->add($i18n->get('error.upload'));