1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Icybee package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Icybee\Modules\Files; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\ErrorCollection; |
15
|
|
|
|
16
|
|
|
class Module extends \Icybee\Modules\Nodes\Module |
17
|
|
|
{ |
18
|
|
|
const OPERATION_UPLOAD = 'upload'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Creates following directories, with their .htaccess file. |
22
|
|
|
* |
23
|
|
|
* - "/repository/tmp", allow from all |
24
|
|
|
* - "/repository/files", allow from all |
25
|
|
|
* - "/repository/files-index", deny from all |
26
|
|
|
* |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function install(ErrorCollection $errors) |
30
|
|
|
{ |
31
|
|
|
$repository = \ICanBoogie\REPOSITORY; |
32
|
|
|
|
33
|
|
|
# |
34
|
|
|
# $repository/tmp |
35
|
|
|
# |
36
|
|
|
|
37
|
|
|
$path = $repository . 'tmp'; |
38
|
|
|
|
39
|
|
View Code Duplication |
if (!file_exists($path)) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$parent = dirname($path); |
42
|
|
|
|
43
|
|
|
if (is_writable($parent)) |
44
|
|
|
{ |
45
|
|
|
mkdir($path); |
46
|
|
|
|
47
|
|
|
file_put_contents($path . DIRECTORY_SEPARATOR . '.htaccess', 'Deny from all'); |
48
|
|
|
} |
49
|
|
|
else |
50
|
|
|
{ |
51
|
|
|
$errors->add($this->id, "Unable to create %directory directory, its parent is not writable.", [ '%directory' => $path ]); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
# |
56
|
|
|
# $repository/files |
57
|
|
|
# |
58
|
|
|
|
59
|
|
|
$path = $repository . 'files'; |
60
|
|
|
|
61
|
|
View Code Duplication |
if (!file_exists($path)) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$parent = dirname($path); |
64
|
|
|
|
65
|
|
|
if (is_writable($parent)) |
66
|
|
|
{ |
67
|
|
|
mkdir($path); |
68
|
|
|
|
69
|
|
|
file_put_contents($path . DIRECTORY_SEPARATOR . '.htaccess', 'Allow from all'); |
70
|
|
|
} |
71
|
|
|
else |
72
|
|
|
{ |
73
|
|
|
$errors->add($this->id, "Unable to create %directory directory, its parent is not writable", [ '%directory' => $path ]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
# |
78
|
|
|
# $repository/files-index |
79
|
|
|
# |
80
|
|
|
|
81
|
|
|
$path = $repository . 'files-index'; |
82
|
|
|
|
83
|
|
View Code Duplication |
if (!file_exists($path)) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$parent = dirname($path); |
86
|
|
|
|
87
|
|
|
if (is_writable($parent)) |
88
|
|
|
{ |
89
|
|
|
mkdir($path); |
90
|
|
|
|
91
|
|
|
file_put_contents($path . DIRECTORY_SEPARATOR . '.htaccess', 'Deny from all'); |
92
|
|
|
} |
93
|
|
|
else |
94
|
|
|
{ |
95
|
|
|
$errors->add($this->id, 'Unable to create %directory directory, its parent is not writable', [ '%directory' => $path ]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
# |
100
|
|
|
# config: max_file_size |
101
|
|
|
# |
102
|
|
|
|
103
|
|
|
$this->app->registry["{$this->flat_id}.max_file_size"] = 16000; |
104
|
|
|
|
105
|
|
|
return parent::install($errors); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Checks that the "tmp" and "files" directories exist in the repository. |
110
|
|
|
* |
111
|
|
|
* @inheritdoc |
112
|
|
|
*/ |
113
|
|
|
public function is_installed(ErrorCollection $errors) |
114
|
|
|
{ |
115
|
|
|
$repository = \ICanBoogie\REPOSITORY; |
116
|
|
|
|
117
|
|
|
# |
118
|
|
|
# $repository/tmp |
119
|
|
|
# |
120
|
|
|
|
121
|
|
|
$path = $repository . 'tmp'; |
122
|
|
|
|
123
|
|
|
if (!is_dir($path)) |
124
|
|
|
{ |
125
|
|
|
$errors->add($this->id, "The %directory directory is missing.", [ '%directory' => $path ]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
# |
129
|
|
|
# $repository/files |
130
|
|
|
# |
131
|
|
|
|
132
|
|
|
$path = $repository . 'files'; |
133
|
|
|
|
134
|
|
|
if (!is_dir($path)) |
135
|
|
|
{ |
136
|
|
|
$errors->add($this->id, "The %directory directory is missing.", [ '%directory' => $path ]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return parent::is_installed($errors); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function clean_temporary_files($lifetime = 3600) |
143
|
|
|
{ |
144
|
|
|
$path = \ICanBoogie\REPOSITORY . 'tmp'; |
145
|
|
|
|
146
|
|
|
if (!is_dir($path)) |
147
|
|
|
{ |
148
|
|
|
\ICanBoogie\log_error('The directory %directory does not exists', [ '%directory' => $path ]); |
149
|
|
|
|
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
if (!is_writable($path)) |
154
|
|
|
{ |
155
|
|
|
\ICanBoogie\log_error('The directory %directory is not writable', [ '%directory' => $path ]); |
156
|
|
|
|
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$dh = opendir($path); |
161
|
|
|
|
162
|
|
|
if (!$dh) |
163
|
|
|
{ |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$now = time(); |
168
|
|
|
$location = getcwd(); |
169
|
|
|
|
170
|
|
|
chdir($path); |
171
|
|
|
|
172
|
|
|
while ($file = readdir($dh)) |
173
|
|
|
{ |
174
|
|
|
if ($file{0} == '.') |
175
|
|
|
{ |
176
|
|
|
continue; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$stat = stat($file); |
180
|
|
|
|
181
|
|
|
if ($now - $stat['ctime'] > $lifetime) |
182
|
|
|
{ |
183
|
|
|
unlink($file); |
184
|
|
|
|
185
|
|
|
\ICanBoogie\log('The temporary file %file has been deleted form the repository %directory', [ |
186
|
|
|
|
187
|
|
|
'%file' => $file, |
188
|
|
|
'%directory' => $path |
189
|
|
|
|
190
|
|
|
]); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
chdir($location); |
195
|
|
|
|
196
|
|
|
closedir($dh); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.