1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
ar_pinp::allow( 'ar_store_files', array( 'ls', 'get', 'save', 'delete', 'touch', 'exists' ) ); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* prevent mess detector from warning for the private static fields |
7
|
|
|
* @SuppressWarnings(PHPMD.UnusedPrivateField) |
8
|
|
|
*/ |
9
|
|
|
class ar_store_files extends arBase { |
10
|
|
|
|
11
|
|
|
protected static function parseName( $fname ) { |
12
|
|
|
list( $nls, $name ) = explode('_', $fname, 2); |
13
|
|
|
return array( |
14
|
|
|
'nls' => $nls, |
15
|
|
|
'name' => $name |
16
|
|
|
); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected static function compileName( $name, $nls ) { |
20
|
|
|
if ( !$nls ) { |
21
|
|
|
$ob = ar::context()->getObject(); |
22
|
|
|
$nls = $ob->nls; |
23
|
|
|
} |
24
|
|
|
return $nls.'_'.$name; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected static function getStore() { |
28
|
|
|
$ob = ar::context()->getObject(); |
29
|
|
|
return array( $ob, $ob->store->get_filestore("files") ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function ls( $nls=null ) { |
33
|
|
|
list($ob, $fstore) = static::getStore(); |
34
|
|
|
$files = $fstore->ls($ob->id); |
35
|
|
|
if ( !$files ) { |
36
|
|
|
$files = array(); |
37
|
|
|
} |
38
|
|
|
$files = array_map( array('self','parseName'), $files ); |
39
|
|
|
if ( isset($nls) ) { |
40
|
|
|
$files = array_filter( $files, function($f) use($nls) { |
41
|
|
|
return ( $f['nls'] == $nls ); |
42
|
|
|
} ); |
43
|
|
|
} |
44
|
|
|
return $files; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function get( $name, $nls=null ) { |
48
|
|
|
$info = static::exists($name, $nls); |
49
|
|
|
if ( !$info ) { |
50
|
|
|
return ar_error::raiseError('File not found: '.$name.' - '.$nls, 404); |
51
|
|
|
} |
52
|
|
|
list( $ob, $fstore ) = static::getStore(); |
53
|
|
|
$fname = static::compileName($info['name'], $info['nls']); |
54
|
|
|
$stream = $fstore->get_stream($ob->id, $fname); |
55
|
|
|
return new ar_content_filesFile( $stream ); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function save( $name, $contents, $nls=null ) { |
59
|
|
|
list( $ob, $fstore ) = static::getStore(); |
60
|
|
|
$fname = static::compileName($name, $nls); |
61
|
|
|
if( $contents instanceof ar_content_filesFile ){ |
62
|
|
|
// FIXME: this should be more efficient then coping the whole contents of the file in memory |
63
|
|
|
// should be fixed with a copyFrom/copyTo function call on ar_content_filesFile |
64
|
|
|
$contents = $contents->getContents(); |
65
|
|
|
} |
66
|
|
|
if ( is_resource($contents) && get_resource_type($contents)==='stream' ) { |
67
|
|
|
return $fstore->copy_stream_to_store( $contents, $ob->id, $fname ); |
68
|
|
|
} else { |
69
|
|
|
return $fstore->write( (string) $contents, $ob->id, $fname ); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function delete( $name, $nls=null ) { |
74
|
|
|
list( $ob, $fstore ) = static::getStore(); |
75
|
|
|
$fname = static::compileName($name, $nls); |
76
|
|
|
if ( $fstore->exists( $ob->id, $fname ) ) { |
77
|
|
|
return $fstore->remove( $ob->id, $fname ); |
78
|
|
|
} |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function touch( $name, $time=null, $nls=null ) { |
83
|
|
|
list( $ob, $fstore ) = static::getStore(); |
84
|
|
|
$fname = static::compileName($name, $nls); |
85
|
|
|
return $fstore->touch( $ob->id, $fname, $time ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public static function temp( $contents=null ) { |
89
|
|
|
list( $ob, $fstore ) = static::getStore(); |
|
|
|
|
90
|
|
|
$tmpsrc = tempnam($ob->store->get_config("files")."temp", "tmp"); |
91
|
|
|
if ( !$tmpsrc ) { |
92
|
|
|
// FIXME: nlsstrings |
93
|
|
|
return ar_error::raiseError( 'Could not create temp file', 501 ); |
94
|
|
|
} |
95
|
|
|
$fp = fopen( $tmpsrc, 'w+' ); |
96
|
|
|
if (is_resource($fp)) { |
97
|
|
|
$res = new ar_content_filesFile($fp); |
98
|
|
|
if( $contents instanceof ar_content_filesFile ){ |
99
|
|
|
// FIXME: create an more efficient way for copying with a stream_copy_to_stream call or wrapping t in an CopyFrom/CopyTo api |
100
|
|
|
// or even an temp function on a ar_content_filesFile object which creates an temp file from the ar_content_filesFile |
101
|
|
|
$contents = $contents->getContents(); |
102
|
|
|
fwrite($fp, $contents); |
103
|
|
|
rewind($fp); |
104
|
|
|
} else if ( is_resource($contents) && get_resource_type($contents)==='stream' ) { |
105
|
|
|
stream_copy_to_stream( $contents, $fp ); |
106
|
|
|
rewind($fp); |
107
|
|
|
} else if (isset($contents)) { |
108
|
|
|
fwrite($fp, $contents); |
109
|
|
|
rewind($fp); |
110
|
|
|
} |
111
|
|
|
return $res; |
112
|
|
|
} else { |
113
|
|
|
// FIXME: nlsstrings |
114
|
|
|
return ar_error::raiseError('Could not create temp file', 502); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public static function exists( $name, $nls=null ) { |
119
|
|
|
list( $ob, $fstore ) = static::getStore(); |
120
|
|
|
$fname = static::compileName($name, $nls); |
121
|
|
|
if ( !$fstore->exists($ob->id, $fname) && !isset($nls) ) { |
122
|
|
|
$nls = $ob->data->nls->default; |
123
|
|
|
$fname = static::compileName($name, $nls); |
124
|
|
|
} |
125
|
|
|
if ( !$fstore->exists($ob->id, $fname ) ) { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
return static::parseName($fname); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.