24
24
25
25
use function Vvveb \__ ;
26
26
use Vvveb \Controller \Base ;
27
+ use Vvveb \Sql \media_ContentSQL ;
27
28
use function Vvveb \fileUploadErrMessage ;
28
29
use function Vvveb \sanitizeFileName ;
29
30
@@ -60,23 +61,26 @@ function index() {
60
61
$ admin_path = \Vvveb \adminPath ();
61
62
$ controllerPath = $ admin_path . 'index.php?module=media/media ' ;
62
63
63
- $ this ->view ->scanUrl = "$ controllerPath&action=scan " ;
64
- $ this ->view ->uploadUrl = "$ controllerPath&action=upload " ;
65
- $ this ->view ->deleteUrl = "$ controllerPath&action=delete " ;
66
- $ this ->view ->renameUrl = "$ controllerPath&action=rename " ;
64
+ $ this ->view ->mediaUrl = $ controllerPath ;
65
+ $ this ->view ->scanUrl = "$ controllerPath&action=scan " ;
66
+ $ this ->view ->uploadUrl = "$ controllerPath&action=upload " ;
67
+ $ this ->view ->deleteUrl = "$ controllerPath&action=delete " ;
68
+ $ this ->view ->renameUrl = "$ controllerPath&action=rename " ;
69
+ $ this ->view ->mediaContentUrl = "$ controllerPath&action=mediaContent " ;
67
70
}
68
71
69
72
function upload () {
70
- $ path = sanitizeFileName ($ this ->request ->post ['mediaPath ' ]);
71
73
$ file = $ this ->request ->files ['file ' ] ?? [];
72
- $ fileName = sanitizeFileName ($ file ['name ' ]);
73
- $ path = preg_replace ('@^[ \\\/]public[ \\\/]media|^[ \\\/]media|^[ \\\/]public@ ' , '' , $ path );
74
- $ extension = strtolower (substr ($ fileName , strrpos ($ fileName , '. ' ) + 1 ));
75
74
$ success = false ;
76
75
$ return = '' ;
77
76
$ message = '' ;
78
77
79
78
if ($ file ) {
79
+ $ path = sanitizeFileName ($ this ->request ->post ['mediaPath ' ] ?? '' );
80
+ $ fileName = sanitizeFileName ($ file ['name ' ]);
81
+ $ path = preg_replace ('@^[ \\\/]public[ \\\/]media|^[ \\\/]media|^[ \\\/]public@ ' , '' , $ path );
82
+ $ extension = strtolower (substr ($ fileName , strrpos ($ fileName , '. ' ) + 1 ));
83
+
80
84
if ($ file ['error ' ] == UPLOAD_ERR_OK ) {
81
85
$ success = true ;
82
86
} else {
@@ -127,10 +131,8 @@ function upload() {
127
131
function delete () {
128
132
$ file = sanitizeFileName ($ this ->request ->post ['file ' ]);
129
133
$ themeFolder = DIR_MEDIA ;
130
- //echo $themeFolder . DS . $file;
131
- header ('Content-type: application/json; charset=utf-8 ' );
132
134
133
- if (unlink ($ themeFolder . DS . $ file )) {
135
+ if ($ file && @ unlink ($ themeFolder . DS . $ file )) {
134
136
$ message = ['success ' => true , 'message ' => __ ('File deleted! ' )];
135
137
} else {
136
138
$ message = ['success ' => false , 'message ' => __ ('Error deleting file! ' )];
@@ -144,11 +146,10 @@ function rename() {
144
146
$ file = sanitizeFileName ($ this ->request ->post ['file ' ]);
145
147
$ newfile = sanitizeFileName ($ this ->request ->post ['newfile ' ]);
146
148
$ duplicate = $ this ->request ->post ['duplicate ' ] ?? false ;
147
- $ themeFolder = DIR_MEDIA ;
149
+ $ dirMedia = DIR_MEDIA ;
148
150
149
- header ('Content-type: application/json; charset=utf-8 ' );
150
- $ currentFile = $ themeFolder . DS . $ file ;
151
- $ targetFile = $ themeFolder . DS . $ newfile ;
151
+ $ currentFile = $ dirMedia . DS . $ file ;
152
+ $ targetFile = $ dirMedia . DS . $ newfile ;
152
153
153
154
if ($ duplicate ) {
154
155
if (copy ($ currentFile , $ targetFile )) {
@@ -168,6 +169,64 @@ function rename() {
168
169
$ this ->response ->output ($ message );
169
170
}
170
171
172
+ function newFolder () {
173
+ $ folder = sanitizeFileName ($ this ->request ->post ['folder ' ]);
174
+ $ path = sanitizeFileName ($ this ->request ->post ['path ' ]);
175
+ $ success = false ;
176
+
177
+ $ dirMedia = DIR_MEDIA ;
178
+ if (is_dir ($ dirMedia . $ path )) {
179
+ if (@mkdir ($ dirMedia . $ path . DS . $ folder )) {
180
+ $ message = __ ('Folder created! ' );
181
+ $ success = true ;
182
+ } else {
183
+ $ message = __ ('Error creating folder! ' );
184
+ }
185
+ } else {
186
+ $ message = __ ('Path does not exist! ' );
187
+ }
188
+
189
+ $ message = ['success ' => $ success , 'message ' => $ message ];
190
+
191
+ $ this ->response ->setType ('json ' );
192
+ $ this ->response ->output ($ message );
193
+ }
194
+
195
+ function mediaContentSave () {
196
+ $ file = sanitizeFileName ($ this ->request ->post ['file ' ]);
197
+ $ content = ($ this ->request ->post ['media_content ' ]);
198
+
199
+ $ mediaContent = new media_ContentSQL ();
200
+ $ media = $ mediaContent ->get (['file ' => $ file ] + $ this ->global );
201
+ if ($ media ) {
202
+ //var_dump(['media_id' => $media['media_id'], 'media_content' => $content]);
203
+ $ result = $ mediaContent ->edit (['media_id ' => $ media ['media_id ' ], 'media_content ' => $ content , 'media ' => []]);
204
+ } else {
205
+ //var_dump(['media' => ['file' => $file], 'media_content' => $content]);
206
+ $ result = $ mediaContent ->add (['media ' => ['file ' => $ file ], 'media_content ' => $ content ]);
207
+ }
208
+
209
+ if ($ result ) {
210
+ $ message = ['success ' => true , 'message ' => __ ('Saved! ' )];
211
+ } else {
212
+ $ message = ['success ' => false , 'message ' => __ ('Error saving! ' )];
213
+ }
214
+
215
+ $ this ->response ->setType ('json ' );
216
+ $ this ->response ->output ($ message );
217
+ }
218
+
219
+ function mediaContent () {
220
+ $ file = sanitizeFileName ($ this ->request ->get ['file ' ]);
221
+ $ themeFolder = DIR_MEDIA ;
222
+
223
+ $ mediaContent = new media_ContentSQL ();
224
+ $ result = $ mediaContent ->getContent (['file ' => $ file ] + $ this ->global );
225
+
226
+ $ this ->response ->setType ('json ' );
227
+ $ this ->response ->output ($ result ?? []);
228
+ }
229
+
171
230
function scan () {
172
231
$ type = $ this ->request ->get ['type ' ] ?? 'public ' ;
173
232
$ scandir = $ this ->dirForType ($ type );
@@ -183,10 +242,10 @@ function scan() {
183
242
// Is there actually such a folder/file?
184
243
185
244
if (file_exists ($ dir )) {
186
- $ files = @scandir ($ dir );
245
+ $ listdir = @scandir ($ dir );
187
246
188
- if ($ files ) {
189
- foreach ($ files as $ f ) {
247
+ if ($ listdir ) {
248
+ foreach ($ listdir as $ f ) {
190
249
if (! $ f || $ f [0 ] == '. ' || $ f == 'node_modules ' || $ f == 'vendor ' ) {
191
250
continue ; // Ignore hidden files
192
251
}
0 commit comments