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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
<?php
/**
* A filestore that uses disk as storage.
*
* @warning This should be used by a wrapper class
* like {@link ElggFile}.
*
* @package Elgg.Core
* @subpackage FileStore.Disk
* @link http://docs.elgg.org/DataModel/FileStore/Disk
*/
class ElggDiskFilestore extends ElggFilestore {
/**
* Directory root.
*/
private $dir_root;
/**
* Default depth of file directory matrix
*/
private $matrix_depth = 5;
/**
* Construct a disk filestore using the given directory root.
*
* @param string $directory_root Root directory, must end in "/"
*/
public function __construct($directory_root = "") {
global $CONFIG;
if ($directory_root) {
$this->dir_root = $directory_root;
} else {
$this->dir_root = $CONFIG->dataroot;
}
}
/**
* Open a file for reading, writing, or both.
*
* @note All files are opened binary safe.
* @warning This will try to create the a directory if it doesn't exist,
* even in read-only mode.
*
* @param ElggFile $file The file to open
* @param string $mode read, write, or append.
*
* @throws InvalidParameterException
* @return resource File pointer resource
* @todo This really shouldn't try to create directories if not writing.
*/
public function open(ElggFile $file, $mode) {
$fullname = $this->getFilenameOnFilestore($file);
// Split into path and name
$ls = strrpos($fullname, "/");
if ($ls === false) {
$ls = 0;
}
$path = substr($fullname, 0, $ls);
$name = substr($fullname, $ls);
// Try and create the directory
try {
$this->makeDirectoryRoot($path);
} catch (Exception $e) {
}
if (($mode != 'write') && (!file_exists($fullname))) {
return false;
}
switch ($mode) {
case "read" :
$mode = "rb";
break;
case "write" :
$mode = "w+b";
break;
case "append" :
$mode = "a+b";
break;
default:
$msg = elgg_echo('InvalidParameterException:UnrecognisedFileMode', array($mode));
throw new InvalidParameterException($msg);
}
return fopen($fullname, $mode);
}
/**
* Write data to a file.
*
* @param resource $f File pointer resource
* @param mixed $data The data to write.
*
* @return bool
*/
public function write($f, $data) {
return fwrite($f, $data);
}
/**
* Read data from a file.
*
* @param resource $f File pointer resource
* @param int $length The number of bytes to read
* @param int $offset The number of bytes to start after
*
* @return mixed Contents of file or false on fail.
*/
public function read($f, $length, $offset = 0) {
if ($offset) {
$this->seek($f, $offset);
}
return fread($f, $length);
}
/**
* Close a file pointer
*
* @param resource $f A file pointer resource
*
* @return bool
*/
public function close($f) {
return fclose($f);
}
/**
* Delete an ElggFile file.
*
* @param ElggFile $file File to delete
*
* @return bool
*/
public function delete(ElggFile $file) {
$filename = $this->getFilenameOnFilestore($file);
if (file_exists($filename)) {
return unlink($filename);
} else {
return true;
}
}
/**
* Seek to the specified position.
*
* @param resource $f File resource
* @param int $position Position in bytes
*
* @return bool
*/
public function seek($f, $position) {
return fseek($f, $position);
}
/**
* Return the current location of the internal pointer
*
* @param resource $f File pointer resource
*
* @return int|false
*/
public function tell($f) {
return ftell($f);
}
/**
* Tests for end of file on a file pointer
*
* @param resource $f File pointer resource
*
* @return bool
*/
public function eof($f) {
return feof($f);
}
/**
* Returns the file size of an ElggFile file.
*
* @param ElggFile $file File object
*
* @return int The file size
*/
public function getFileSize(ElggFile $file) {
return filesize($this->getFilenameOnFilestore($file));
}
/**
* Returns the filename as saved on disk for an ElggFile object
*
* @param ElggFile $file File object
*
* @return string The full path of where the file is stored
* @throws InvalidParameterException
*/
public function getFilenameOnFilestore(ElggFile $file) {
$owner_guid = $file->getOwnerGuid();
if (!$owner_guid) {
$owner_guid = elgg_get_logged_in_user_guid();
}
if (!$owner_guid) {
$msg = elgg_echo('InvalidParameterException:MissingOwner',
array($file->getFilename(), $file->guid));
throw new InvalidParameterException($msg);
}
return $this->dir_root . $this->makefileMatrix($owner_guid) . $file->getFilename();
}
/**
* Returns the contents of the ElggFile file.
*
* @param ElggFile $file File object
*
* @return mixed
*/
public function grabFile(ElggFile $file) {
return file_get_contents($file->getFilenameOnFilestore());
}
/**
* Tests if an ElggFile file exists.
*
* @param ElggFile $file File object
*
* @return bool
*/
public function exists(ElggFile $file) {
return file_exists($this->getFilenameOnFilestore($file));
}
/**
* Returns the size of all data stored under a directory in the disk store.
*
* @param string $prefix Optional/ The prefix to check under.
* @param string $container_guid The guid of the entity whose data you want to check.
*
* @return int|false
*/
public function getSize($prefix = '', $container_guid) {
if ($container_guid) {
return get_dir_size($this->dir_root . $this->makefileMatrix($container_guid) . $prefix);
} else {
return false;
}
}
/**
* Create a directory $dirroot
*
* @param string $dirroot The full path of the directory to create
*
* @throws IOException
* @return true
* @deprecated 1.8 Use ElggDiskFilestore::makeDirectoryRoot()
*/
protected function make_directory_root($dirroot) {
elgg_deprecated_notice('ElggDiskFilestore::make_directory_root() is deprecated by ::makeDirectoryRoot()', 1.8);
return $this->makeDirectoryRoot($dirroot);
}
/**
* Create a directory $dirroot
*
* @param string $dirroot The full path of the directory to create
*
* @throws IOException
* @return true
*/
protected function makeDirectoryRoot($dirroot) {
if (!file_exists($dirroot)) {
if (!@mkdir($dirroot, 0700, true)) {
throw new IOException(elgg_echo('IOException:CouldNotMake', array($dirroot)));
}
}
return true;
}
/**
* Multibyte string tokeniser.
*
* Splits a string into an array. Will fail safely if mbstring is
* not installed.
*
* @param string $string String
* @param string $charset The charset, defaults to UTF8
*
* @return array
* @deprecated 1.8 Files are stored by date and guid; no need for this.
*/
private function mb_str_split($string, $charset = 'UTF8') {
elgg_deprecated_notice('ElggDiskFilestore::mb_str_split() is deprecated.', 1.8);
if (is_callable('mb_substr')) {
$length = mb_strlen($string);
$array = array();
while ($length) {
$array[] = mb_substr($string, 0, 1, $charset);
$string = mb_substr($string, 1, $length, $charset);
$length = mb_strlen($string);
}
return $array;
} else {
return str_split($string);
}
}
/**
* Construct a file path matrix for an entity.
*
* @param int $identifier The guide of the entity to store the data under.
*
* @return string The path where the entity's data will be stored.
* @deprecated 1.8 Use ElggDiskFilestore::makeFileMatrix()
*/
protected function make_file_matrix($identifier) {
elgg_deprecated_notice('ElggDiskFilestore::make_file_matrix() is deprecated by ::makeFileMatrix()', 1.8);
return $this->makefileMatrix($identifier);
}
/**
* Construct a file path matrix for an entity.
*
* @param int $guid The guide of the entity to store the data under.
*
* @return string The path where the entity's data will be stored.
*/
protected function makeFileMatrix($guid) {
$entity = get_entity($guid);
if (!($entity instanceof ElggEntity) || !$entity->time_created) {
return false;
}
$time_created = date('Y/m/d', $entity->time_created);
return "$time_created/$entity->guid/";
}
/**
* Construct a filename matrix.
*
* Generates a matrix using the entity's creation time and
* unique guid.
*
* File path matrixes are:
* YYYY/MM/DD/guid/
*
* @param int $guid The entity to contrust a matrix for
*
* @return string The
*/
protected function user_file_matrix($guid) {
elgg_deprecated_notice('ElggDiskFilestore::user_file_matrix() is deprecated by ::makeFileMatrix()', 1.8);
return $this->makeFileMatrix($guid);
}
/**
* Returns a list of attributes to save to the database when saving
* the ElggFile object using this file store.
*
* @return array
*/
public function getParameters() {
return array("dir_root" => $this->dir_root);
}
/**
* Sets parameters that should be saved to database.
*
* @param array $parameters Set parameters to save to DB for this filestore.
*
* @return bool
*/
public function setParameters(array $parameters) {
if (isset($parameters['dir_root'])) {
$this->dir_root = $parameters['dir_root'];
return true;
}
return false;
}
}
|