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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
|
<?php
/**
* Elgg filestore.
* This file contains classes, interfaces and functions for saving and retrieving data to various file
* stores.
*
* @package Elgg
* @subpackage API
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
* @author Marcus Povey
* @copyright Curverider Ltd 2008
* @link http://elgg.org/
*/
include_once("objects.php");
/**
* @class ElggFilestore
* This class defines the interface for all elgg data repositories.
* @author Marcus Povey
*/
abstract class ElggFilestore
{
/**
* Attempt to open the file $file for storage or writing.
*
* @param ElggFile $file
* @param string $mode "read", "write", "append"
* @return mixed A handle to the opened file or false on error.
*/
abstract public function open(ElggFile $file, $mode);
/**
* Write data to a given file handle.
*
* @param mixed $f The file handle - exactly what this is depends on the file system
* @param string $data The binary string of data to write
* @return int Number of bytes written.
*/
abstract public function write($f, $data);
/**
* Read data from a filestore.
*
* @param mixed $f The file handle
* @param int $length Length in bytes to read.
* @param int $offset The optional offset.
* @return mixed String of data or false on error.
*/
abstract public function read($f, $length, $offset = 0);
/**
* Seek a given position within a file handle.
*
* @param mixed $f The file handle.
* @param int $position The position.
*/
abstract public function seek($f, $position);
/**
* Close a given file handle.
*
* @param mixed $f
*/
abstract public function close($f);
/**
* Delete the file associated with a given file handle.
*
* @param ElggFile $file
*/
abstract public function delete(ElggFile $file);
/**
* Return the size in bytes for a given file.
*
* @param ElggFile $file
*/
abstract public function getFileSize(ElggFile $file);
/**
* Get the filestore's creation parameters as an associative array.
* Used for serialisation and for storing the creation details along side a file object.
*
* @return array
*/
abstract public function getParameters();
/**
* Set the parameters from the associative array produced by $this->getParameters().
*/
abstract public function setParameters(array $parameters);
}
/**
* @class ElggDiskFilestore
* This class uses disk storage to save data.
* @author Marcus Povey
*/
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)
{
$this->dir_root = $directory_root;
}
public function open(ElggFile $file, $mode)
{
$name = $file->getFilename();
$matrix = $this->make_file_matrix($name);
// Try and create the directory
try { $this->make_directory_root($this->dir_root . $matrix); } catch (Exception $e){}
switch ($mode)
{
case "read" : $mode = "r+b"; break;
case "write" : $mode = "w+b"; break;
case "append" : $mode = "a+b"; break;
default: throw new InvalidParameterException("Unrecognised file mode '$mode'");
}
return fopen($this->dir_root . $matrix . $name, $mode);
}
public function write($f, $data)
{
return fwrite($f, $data);
}
public function read($f, $length, $offset = 0)
{
if ($offset)
$this->seek($f, $offset);
return fread($f, $length);
}
public function close($f)
{
return fclose($f);
}
public function delete(ElggFile $file)
{
$name = $file->getFilename();
$matrix = $this->make_file_matrix($name);
$unlink = unlink($this->dir_root . $matrix . $name);
if ($unlink)
return $file->delete();
return false;
}
public function seek($f, $position)
{
return fseek($f, $position);
}
public function getFileSize(ElggFile $file)
{
$name = $file->getFilename();
$matrix = $this->make_file_matrix($name);
return filesize($this->dir_root . $matrix . $name);
}
/**
* Make the directory root.
*
* @param string $dirroot
*/
protected function make_directory_root($dirroot)
{
if (!@mkdir($dirroot, 0700, true))
throw new IOException("Could not make $dirroot");
return true;
}
/**
* Construct the filename matrix.
*
* @param string $filename
*/
protected function make_file_matrix($filename)
{
$matrix = "";
$len = strlen($filename);
if ($len>$this->matrix_depth)
$len = $this->matrix_depth;
for ($n = 0; $n < $len; $n++)
$matrix .= $filename[$n] . "/";
return $matrix;
}
public function getParameters()
{
return array("dir_root" => $this->dir_root);
}
public function setParameters(array $parameters)
{
if (isset($parameters['dir_root']))
{
$this->dir_root = $parameters['dir_root'];
return true;
}
return false;
}
}
/**
* @class ElggFile
* This class represents a physical file.
* @author Marcus Povey
*/
class ElggFile extends ElggObject
{
/** Filestore */
private $filestore;
/** File handle used to identify this file in a filestore. Created by open. */
private $handle;
protected function initialise_attributes()
{
parent::initialise_attributes();
$this->attributes['subtype'] = "file";
}
public function __construct($guid = null)
{
parent::__construct($guid);
// Set default filestore
$this->filestore = $this->getFilestore();
}
/**
* Set the filename of this file.
*
* @param string $name The filename.
*/
public function setFilename($name) { $this->title = $name; }
/**
* Return the filename.
*/
public function getFilename() { return $this->title; }
/**
* Set the optional file description.
*
* @param string $description The description.
*/
public function setDescription($description) { $this->description = $description; }
/**
* Open the file with the given mode
*
* @param string $mode Either read/write/append
*/
public function open($mode)
{
if (!$this->title)
throw new IOException("You must specify a name before opening a file.");
// See if file has already been saved
// seek on datastore, parameters and name
// Sanity check
if (
($mode!="read") &&
($mode!="write") &&
($mode!="append")
)
throw new InvalidParameterException("Unrecognised file mode '$mode'");
// Get the filestore
$fs = $this->getFilestore();
// Ensure that we save the file details to object store
//$this->save();
// Open the file handle
$this->handle = $fs->open($this, $mode);
return $this->handle;
}
/**
* Write some data.
*
* @param string $data The data
*/
public function write($data)
{
$fs = $this->getFilestore();
return $fs->write($this->handle, $data);
}
/**
* Read some data.
*
* @param int $length Amount to read.
* @param int $offset The offset to start from.
*/
public function read($length, $offset = 0)
{
$fs = $this->getFilestore();
return $fs->read($this->handle, $length, $offset);
}
/**
* Close the file and commit changes
*/
public function close()
{
$fs = $this->getFilestore();
if ($fs->close($this->handle))
{
$this->handle = NULL;
return true;
}
return false;
}
/**
* Delete this file.
*/
public function delete()
{
$fs = $this->getFilestore();
return $fs->delete($this);
}
/**
* Seek a position in the file.
*
* @param int $position
*/
public function seek($position)
{
$fs = $this->getFilestore();
return $fs->seek($this->handle, $position);
}
/**
* Return the size of the file in bytes.
*/
public function size()
{
return $this->filestore->getFileSize($this);
}
/**
* Set a filestore.
*
* @param ElggFilestore $filestore The file store.
*/
public function setFilestore(ElggFilestore $filestore)
{
$this->filestore = $filestore;
}
/**
* Return a filestore suitable for saving this file.
* This filestore is either a pre-registered filestore, a filestore loaded from metatags saved
* along side this file, or the system default.
*/
protected function getFilestore()
{
// Short circuit if already set.
if ($this->filestore)
return $this->filestore;
// If filestore meta set then retrieve filestore TODO: Better way of doing this?
$metas = get_metadata_for_entity($this->guid);
$parameters = array();
foreach ($metas as $meta)
{
if (strpos($meta->name, "filestore::")!==false)
{
// Filestore parameter tag
$comp = explode("::", $meta->name);
$name = $comp[1];
$parameters[$name] = $meta->value;
}
}
// If parameters loaded then create new filestore
if (count($parameters)!=0)
{
// Create new filestore object
if ((!isset($parameters['filestore'])) || (!class_exists($parameters['filestore'])))
throw new ClassNotFoundException("Filestore not found or class not saved with file!");
$this->filestore = new $parameters['filestore']();
// Set parameters
$this->filestore->setParameters($parameters);
}
// if still nothing then set filestore to default
if (!$this->filestore)
$this->filestore = get_default_filestore();
return $this->filestore;
}
public function save()
{
if (!parent::save())
return false;
// Save datastore metadata
$params = $this->filestore->getParameters();
foreach ($params as $k => $v)
$this->setMetaData("filestore::$k", $v);
// Now make a note of the filestore class
$this->setMetaData("filestore::filestore", get_class($this->filestore));
return true;
}
}
/// Variable holding the default datastore
$DEFAULT_FILE_STORE = NULL;
/**
* Return the default filestore.
*
* @return ElggFilestore
*/
function get_default_filestore()
{
global $DEFAULT_FILE_STORE;
return $DEFAULT_FILE_STORE;
}
/**
* Set the default filestore for the system.
*/
function set_default_filestore(ElggFilestore $filestore)
{
global $DEFAULT_FILE_STORE;
$DEFAULT_FILE_STORE = $filestore;
return true;
}
// Now register a default filestore
set_default_filestore(new ElggDiskFilestore($CONFIG->dataroot));
?>
|