/
file.data.class.php
272 lines (216 loc) · 7.75 KB
/
file.data.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/*
* Xibo - Digital Signage - http://www.xibo.org.uk
* Copyright (C) 2010-13 Daniel Garner
*
* This file is part of Xibo.
*
* Xibo is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* Xibo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
*/
defined('XIBO') or die("Sorry, you are not allowed to directly access this page.<br /> Please press the back button in your browser.");
class File extends Data
{
/**
* Adds a new file and appends the first chunk.
* @param <type> $payload
* @param <type> $userId
* @return <type>
*/
public function NewFile($payload, $userId)
{
try {
$dbh = PDOConnect::init();
$sth = $dbh->prepare('INSERT INTO file (CreatedDT, UserID) VALUES (:createddt, :userid)');
$sth->execute(array(
'createddt' => time(),
'userid' => $userId
));
$fileId = $dbh->lastInsertId();
if (!$this->WriteToDisk($fileId, $payload))
throw new Exception('Unable to WriteToDisk');
return $fileId;
}
catch (Exception $e) {
Debug::LogEntry('error', $e->getMessage());
if (!$this->IsError())
$this->SetError(3);
return false;
}
}
/**
* Appends the next chunk to the file
* @param <type> $fileId
* @param <type> $payload
* @param <type> $userId
*/
public function Append($fileId, $payload)
{
try {
$dbh = PDOConnect::init();
// Directory location
$libraryFolder = Config::GetSetting('LIBRARY_LOCATION');
$libraryFolder = $libraryFolder . 'temp';
// Append should only be called on existing files, if this file does not exist then we
// need to error accordingly.
if (!file_exists($libraryFolder . '/' . $fileId))
$this->ThrowError(7);
return $this->WriteToDisk($fileId, $payload);
}
catch (Exception $e) {
Debug::LogEntry('error', $e->getMessage());
if (!$this->IsError())
$this->SetError(1, __('Unknown Error'));
return false;
}
}
/**
* Writes the file to disk
* @param <type> $fileId
* @param <type> $payload
*/
public function WriteToDisk($fileId, $payload)
{
try {
$dbh = PDOConnect::init();
// Directory location
$libraryFolder = Config::GetSetting('LIBRARY_LOCATION');
$libraryFolder = $libraryFolder . 'temp';
if (!File::EnsureLibraryExists($libraryFolder))
return false;
// Open a file pointer
if (!$fp = fopen($libraryFolder . '/' . $fileId, 'a'))
$this->ThrowError(5);
// Write the payload to the file handle.
if (fwrite($fp, $payload) === false)
$this->ThrowError(6);
// Close the file pointer
fclose($fp);
return true;
}
catch (Exception $e) {
Debug::LogEntry('error', $e->getMessage());
if (!$this->IsError())
$this->SetError(1, __('Unknown Error'));
return false;
}
}
/**
* Get the Path to a file
* @param int $fileId The File ID
*/
public function GetPath($fileId) {
if ($fileId == '' || $fileId == 0)
return $this->SetError(25001, __('Missing fileId'));
$libraryFolder = Config::GetSetting('LIBRARY_LOCATION');
$libraryFolder = $libraryFolder . 'temp';
return $libraryFolder . DIRECTORY_SEPARATOR . $fileId;
}
/**
* The current size of a file
* @param <type> $fileId
* @return <int> filesize
*/
public function Size($fileId)
{
// Directory location
$libraryFolder = Config::GetSetting("LIBRARY_LOCATION");
$libraryFolder = $libraryFolder . 'temp';
return filesize($libraryFolder . '/' . $fileId);
}
/**
* Generates a fileid
* @param <type> $userId
*/
public function GenerateFileId($userId)
{
try {
$dbh = PDOConnect::init();
$sth = $dbh->prepare('INSERT INTO file (CreatedDT, UserID) VALUES (:createddt, :userid)');
$sth->execute(array(
'createddt' => time(),
'userid' => $userId
));
$fileId = $dbh->lastInsertId();
return $fileId;
}
catch (Exception $e) {
Debug::LogEntry('error', $e->getMessage());
if (!$this->IsError())
$this->SetError(3, __('Unknown Error'));
return false;
}
}
public static function EnsureLibraryExists()
{
$libraryFolder = Config::GetSetting('LIBRARY_LOCATION');
// Check that this location exists - and if not create it..
if (!file_exists($libraryFolder))
mkdir($libraryFolder, 0777, true);
if (!file_exists($libraryFolder . '/temp'))
mkdir($libraryFolder . '/temp', 0777, true);
if (!file_exists($libraryFolder . '/cache'))
mkdir($libraryFolder . '/cache', 0777, true);
if (!file_exists($libraryFolder . '/screenshots'))
mkdir($libraryFolder . '/screenshots', 0777, true);
// Check that we are now writable - if not then error
if (!is_writable($libraryFolder))
{
Debug::Error('Library not writable');
return false;
}
return true;
}
public function GetLibraryCacheUri() {
$libraryFolder = Config::GetSetting('LIBRARY_LOCATION');
return $libraryFolder . '/cache';
}
/**
* Download a file
* @param string $url
* @param string $savePath
*/
public static function downloadFile($url, $savePath)
{
// Use CURL to download a file
// Open the file handle
$fileHandle = fopen($savePath, 'w+');
// Configure CURL with the file handle
$httpOptions = array(
CURLOPT_TIMEOUT => 50,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_USERAGENT => 'Xibo Digital Signage',
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_URL => $url,
CURLOPT_FILE => $fileHandle
);
// Proxy support
if (Config::GetSetting('PROXY_HOST') != '' && !Config::isProxyException($url)) {
$httpOptions[CURLOPT_PROXY] = Config::GetSetting('PROXY_HOST');
$httpOptions[CURLOPT_PROXYPORT] = Config::GetSetting('PROXY_PORT');
if (Config::GetSetting('PROXY_AUTH') != '')
$httpOptions[CURLOPT_PROXYUSERPWD] = Config::GetSetting('PROXY_AUTH');
}
$curl = curl_init();
// Set our options
curl_setopt_array($curl, $httpOptions);
// Exec saves the file
curl_exec($curl);
// Close the curl connection
curl_close($curl);
// Close the file handle
fclose($fileHandle);
}
}
?>