1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Files;
13
14 use ICanBoogie\Uploaded;
15
16 class Module extends \Icybee\Modules\Nodes\Module
17 {
18 const OPERATION_UPLOAD = 'upload';
19
20 21 22 23 24
25 public function install(\ICanBoogie\Errors $errors)
26 {
27 global $core;
28
29 $repository = \ICanBoogie\REPOSITORY;
30
31
32
33
34
35 $path = $repository . 'tmp';
36
37 if (!file_exists($path))
38 {
39 $parent = dirname($path);
40
41 if (is_writable($parent))
42 {
43 mkdir($path);
44
45 file_put_contents($path . DIRECTORY_SEPARATOR . '.htaccess', 'Deny from all');
46 }
47 else
48 {
49 $errors[$this->id] = $errors->format('Unable to create %directory directory, its parent is not writtable.', [ '%directory' => $path ]);
50 }
51 }
52
53
54
55
56
57 $path = $repository . 'files';
58
59 if (!file_exists($path))
60 {
61 $parent = dirname($path);
62
63 if (is_writable($parent))
64 {
65 mkdir($path);
66
67 file_put_contents($path . DIRECTORY_SEPARATOR . '.htaccess', 'Allow from all');
68 }
69 else
70 {
71 $errors[$this->id] = $errors->format('Unable to create %directory directory, its parent is not writtable', [ '%directory' => $path ]);
72 }
73 }
74
75
76
77
78
79 $core->registry["{$this->flat_id}.max_file_size"] = 16000;
80
81 return parent::install($errors);
82 }
83
84 85 86
87 public function is_installed(\ICanBoogie\Errors $errors)
88 {
89 global $core;
90
91 $repository = \ICanBoogie\REPOSITORY;
92
93
94
95
96
97 $path = $repository . 'tmp';
98
99 if (!is_dir($path))
100 {
101 $errors[$this->id] = $errors->format('The %directory directory is missing.', [ '%directory' => $path ]);
102 }
103
104
105
106
107
108 $path = $repository . 'files';
109
110 if (!is_dir($path))
111 {
112 $errors[$this->id] = $errors->format('The %directory directory is missing.', [ '%directory' => $path ]);
113 }
114
115 return parent::is_installed($errors);
116 }
117
118 public function clean_temporary_files($lifetime=3600)
119 {
120 $path = \ICanBoogie\REPOSITORY . 'tmp';
121
122 if (!is_dir($path))
123 {
124 \ICanBoogie\log_error('The directory %directory does not exists', [ '%directory' => $path ]);
125
126 return;
127 }
128
129 if (!is_writable($path))
130 {
131 \ICanBoogie\log_error('The directory %directory is not writtable', [ '%directory' => $path ]);
132
133 return;
134 }
135
136 $dh = opendir($path);
137
138 if (!$dh)
139 {
140 return;
141 }
142
143 $now = time();
144 $location = getcwd();
145
146 chdir($path);
147
148 while ($file = readdir($dh))
149 {
150 if ($file{0} == '.')
151 {
152 continue;
153 }
154
155 $stat = stat($file);
156
157 if ($now - $stat['ctime'] > $lifetime)
158 {
159 unlink($file);
160
161 \ICanBoogie\log('The temporary file %file has been deleted form the repository %directory', [
162
163 '%file' => $file,
164 '%directory' => $path
165
166 ]);
167 }
168 }
169
170 chdir($location);
171
172 closedir($dh);
173 }
174 }