This repository has been archived by the owner on Feb 10, 2019. It is now read-only.
/
Filebrowser.php
654 lines (534 loc) · 14 KB
/
Filebrowser.php
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
<?php defined('SYSPATH') or die('No direct access allowed.');
class Kohana_Controller_Filebrowser extends Controller_Template {
/**
* View page template
*
* @var View
*/
public $template = 'wysiwyg/filebrowser/overall';
/**
* Config array
*
* @var array
*/
protected $_config = array();
/**
* Path
*
* @var string
*/
protected $_path;
/**
* Local directory that store static files
*
* @var string
*/
protected $_directory;
public function before()
{
parent::before();
$this->template
->set('global_config', array())
->set('title', '')
->set('content', '');
/**
* Load config
*/
$this->_config = Kohana::$config
->load('filebrowser')
->as_array();
$this->_directory = $this->_config['public_directory']
.DIRECTORY_SEPARATOR
.$this->_config['uploads_directory']
.DIRECTORY_SEPARATOR;
$this->_path = str_replace('/', DIRECTORY_SEPARATOR, $this->request->param('path'));
}
public function action_browse()
{
$this->template->title = __('Files :path', array(
':path' => '/'.$this->_path
));
$this->_filebrowser();
}
public function action_dirs()
{
$this->auto_render = FALSE;
$dirs = Filebrowser::list_dirs($this->_directory.$this->_path);
foreach($dirs as $key => $val)
{
$dirname = $this->_directory.$this->_path.DIRECTORY_SEPARATOR.$key;
$subdirs = Filebrowser::list_dirs($dirname);
$dirs[$key] = sizeof($subdirs);
}
return $this->response->json(array('dirs' => $dirs));
}
public function action_images()
{
$this->template->title = __('Images :path', array(
':path' => '/'.$this->_path
));
$this->_filebrowser();
}
public function action_flash()
{
$this->template->title = __('Flash :path', array(
':path' => '/'.$this->_path
));
$this->_filebrowser();
}
public function action_status()
{
$this->auto_render = FALSE;
//if ( ! $this->request->is_ajax())
// return;
$response = array();
if ($_POST)
{
$dir = DOCROOT.$this->_directory.$this->_path;
if ( ! is_dir($dir))
throw new HTTP_Exception_404;
$files = Arr::get($_POST, 'files');
if (empty($files))
return;
if ( ! is_array($files))
$files = array($files);
if (sizeof($files) > 0)
{
foreach($files as $file)
{
// TODO: Files filtering and XSS protection
$response[$file] = is_file($dir.$file);
}
}
$this
->response
->json($response);
}
}
protected function _filebrowser()
{
$filter = $this->_config['filters'][$this->request->action()];
$path = $this->_directory.$this->_path;
if ($this->request->is_ajax())
{
$this->auto_render = FALSE;
return $this->response->json(array(
'files' => Filebrowser::list_files($path, $filter)
));
}
else
{
if ( ! empty($this->_path))
{
return $this->request
->redirect(Route::get('wysiwyg/filebrowser')->uri(array(
'action' => $this->request->action()
)));
}
}
$dirs = Filebrowser::list_dirs($path);
$files = Filebrowser::list_files($path, $filter);
foreach($dirs as $key => $val)
{
$dirname = $this->_directory.$this->_path.DIRECTORY_SEPARATOR.$key;
$dirs[$key] = sizeof(Filebrowser::list_dirs($dirname));
}
$this->template->content = View::factory('wysiwyg/filebrowser/browse')
->bind('files', $files)
->set('dirs', $dirs);
}
public function action_upload()
{
$this->auto_render = FALSE;
if ($_FILES)
{
// Check if file already exists
if(is_file(DOCROOT.$this->_directory.$this->_path.$_FILES['Filedata']['name']))
{
return $this->response->json(array(
'error' => __('FIle :file already exists in :path', array(
':file' => $_FILES['Filedata']['name'],
':path' => $this->_path
))));
}
// TODO: check file type
try
{
Upload::save($_FILES['Filedata'], $_FILES['Filedata']['name'], DOCROOT.$this->_directory.$this->_path);
}
catch(Kohana_Exception $e)
{
return $this->response->json(array(
'error' => $e->getMessage()
));
}
$this->response->body('Ok');
}
}
public function action_move()
{
$this->auto_render = FALSE;
$response = array();
// TODO: file checking
if (isset($_POST['to']))
{
$from = DOCROOT.$this->_directory.$this->_path;
$to = DOCROOT.$this->_directory.$_POST['to'].DIRECTORY_SEPARATOR.pathinfo($this->_path, PATHINFO_BASENAME);
try
{
copy($from, $to);
unlink($from);
$response['result'] = 'ok';
}
catch(Exception $e)
{
$response['error'] = __('Something\'s wrong');
}
}
return $this
->response
->body(json_encode($response));
}
public function action_download()
{
$this->auto_render = FALSE;
$file = DOCROOT.$this->_directory.$this->_path;
if ( ! file_exists($file) OR ! is_file($file))
{
return $this->response
->status(404);
}
$this->response
->send_file($file);
}
/**
* File or directory rename action.
* There is processing POST array, validation and renaming.
*
* If everything's ok,
* returns JSON `{ok: true}`.
*
* If something's wrong,
* returns JSON `{errors: {filename: <error message>}}`.
*/
public function action_rename()
{
$this->auto_render = FALSE;
$path = rtrim(DOCROOT.$this->_directory
.pathinfo($this->_path, PATHINFO_DIRNAME), '.')
.DIRECTORY_SEPARATOR;
$extension = pathinfo($this->_path, PATHINFO_EXTENSION);
$_POST = Arr::extract($_POST, array('filename'));
$current_fname = DOCROOT.$this->_directory.$this->_path;
$new_fname = $path.DIRECTORY_SEPARATOR.$_POST['filename']
.( ! empty($extension) ? '.'.$extension : '');
$is_directory = is_dir($current_fname);
// This means that the user doesn't enter anything,
// and we just need to pretend that everything's OK
if ($current_fname == $new_fname)
return $this->response->ok();
// Then we need to check filename
$validation = $this->_files_validation($_POST, $path, $extension)
->label('filename', ($is_directory ? 'Directory name' : 'File name'));
if ( ! $validation->check())
{
return $this->response->json(array(
'errors' => $validation->errors('wysiwyg')
));
}
// If everything's ok
try
{
// Try to rename a file
rename($current_fname, $new_fname);
}
catch (Exception $e)
{
// If something's wrong,
// return error message
return $this->response->json(array(
'errors' => array(
'filename' => __('Server error. Message: :message', array(
':message' => $e->getMessage()
)))));
}
return $this->response->ok();
}
public function action_rotate_right()
{
return $this->_rotate(90);
}
public function action_rotate_left()
{
return $this->_rotate(-90);
}
public function action_crop()
{
$file = DOCROOT.$this->_directory.$this->_path;
if ( ! is_file($file) OR ! $dimentions = Filebrowser::is_image($file))
throw new HTTP_Exception_404;
if ($_POST)
{
$this->auto_render = FALSE;
$_POST = Arr::extract($_POST, array(
'filename',
'image_width',
'image_height',
'crop_width',
'crop_height',
'offset_x',
'offset_y'
));
$path = rtrim(DOCROOT.$this->_directory
.pathinfo($this->_path, PATHINFO_DIRNAME), '.')
.DIRECTORY_SEPARATOR;
$extension = pathinfo($this->_path, PATHINFO_EXTENSION);
// Validate fle
$validation = $this
->_files_validation($_POST, $path, $extension)
->label('filename', __('Filename'));
if ( ! $validation->check())
{
return $this->response->json(array(
'errors' => $validation->errors('wysiwyg')
));
}
// Validate dimentions
$validation = Validation::factory($_POST);
foreach(array_keys($_POST) as $field)
{
if ($field != 'filename')
{
$validation->rules($field, array(
array('not_empty'),
array('digit', array(':value', TRUE))
));
}
}
// If crop-parameters isn't valid
if ( ! $validation->check())
{
$message = array();
foreach($validation->errors('wysiwyg') as $row => $error)
$message[] = $row.': '.$error;
// Send message
throw new HTTP_Exception_400(implode("\n", $message));
}
try
{
// Crop and resize an image
Image::factory($file)
->resize($_POST['image_width'], $_POST['image_height'])
->crop($_POST['crop_width'], $_POST['crop_height'],
$_POST['offset_x'], $_POST['offset_y'])
->save($path.$_POST['filename'].'.'.$extension);
}
catch(Exception $e)
{
// If something's wrong,
// return error message
$message = explode(':', $e->getMessage());
return $this->response->json(array(
'errors' => array(
'filename' => __('Server error. Message: :message', array(
':message' => $message[sizeof($message) - 1]
)))));
}
return $this->response->ok();
}
$path = str_replace(DIRECTORY_SEPARATOR, '/', $this->_path);
$file = $this->_config['public_directory'].'/'
.$this->_config['uploads_directory'].'/'
.$path;
$this->template = View::factory('wysiwyg/filebrowser/crop')
->bind('image', $file)
->set('path', $path)
->bind('width', $dimentions[0])
->bind('height', $dimentions[1]);
}
/**
* Add new directory action.
* There is processing POST array, validation and renaming.
*
* If everything's ok,
* returns JSON `{ok: true}`.
*
* If something's wrong,
* returns JSON `{errors: {filename: <error message>}}`.
*/
public function action_add()
{
$this->auto_render = FALSE;
$directory = DOCROOT.$this->_directory.$this->_path;
if ( ! is_dir($directory))
return $this->response->status(404);
$path = rtrim(DOCROOT.$this->_directory
.pathinfo($this->_path, PATHINFO_DIRNAME), '.')
.DIRECTORY_SEPARATOR;
if ($_POST)
{
// Then we need to check filename
$validation = $this->
_files_validation($_POST, $path)
->label('filename', 'Directory name');
if ( ! $validation->check())
{
return $this->response->json(array(
'errors' => $validation->errors('wysiwyg')
));
}
// Ok, let's try to create a new directory
try
{
mkdir($directory.DIRECTORY_SEPARATOR.$_POST['filename']);
}
catch(Exception $e)
{
// If something's wrong,
// return error message
return $this->response->json(array(
'errors' => array(
'filename' => __('Server error. Message: :message', array(
':message' => $e->getMessage()
)))));
}
}
$this->response->ok();
}
public function action_resize()
{
}
protected function _rotate($degrees)
{
$this->auto_render = FALSE;
$file = DOCROOT.$this->_directory.$this->_path;
if ( ! is_file($file) OR ! Filebrowser::is_image($file))
{
return $this->response
->status(404);
}
Image::factory($file)
->rotate($degrees)
->save($file);
}
/**
* File delete action.
*
* If everything's ok,
* returns JSON `{ok: true}`.
*
* If something's wrong,
* returns JSON `{error: <error message>}`.
*/
public function action_delete()
{
$this->auto_render = FALSE;
if (Arr::get($_POST, 'agree'))
{
try
{
unlink(DOCROOT.$this->_directory.$this->_path);
}
catch(Exception $e)
{
$message = explode(':', $e->getMessage());
$message = trim($message[sizeof($message) - 1]);
return $this->response->json(array(
'error' => __('Server error. Message: :message', array(
':message' => $message
))));
}
}
$this->response->ok();
}
/**
* Generates a thumbnail of a image
*
* @return void
*/
public function action_thumb()
{
$this->auto_render = FALSE;
$config = $this->_config['thumbs'];
$image = DOCROOT.$this->_directory.$this->_path;
if ( ! is_file($image) OR ! ($dimentions = Filebrowser::is_image($image)))
{
// Return a 404 status
return $this->response
->status(404);
}
$lastmod = filemtime($image);
// Check if the browser sent an "if-none-match: <etag>" header,
// and tell if the file hasn't changed
$this->check_cache(sha1($this->request->uri()).$lastmod);
// If the image is smaller than the thumbnail, stretch, it is not necessary
if ($dimentions[0] <= $config['width'] AND $dimentions[1] <= $config['height'])
{
// Do nothing - return original image
$image = file_get_contents($image);
}
else
{
// Resize image
$image = Image::factory($image)
->resize($config['width'], $config['height'])
->render();
}
// Send headers
$this->response
->headers('content-type', File::mime_by_ext(pathinfo($this->_path, PATHINFO_EXTENSION)))
->headers('last-modified', date('r', $lastmod));
// Send thumbnail content
$this->response
->body($image);
}
/**
* Returns basic files validation
*
* @param array Array to use for validation
* @param type Path
* @param type Extension
* @return Validation
*/
protected function _files_validation(array $array, $path, $extension = NULL)
{
return Validation::factory($_POST)
->rules('filename', array(
array('not_empty'),
array('regex', array(':value', '/^[a-zA-Z_-]$/')),
array('Filebrowser::file_not_exists', array($path, ':value', $extension))
));
}
/**
* Optional GET params (like session_id etc)
*
* @var array
*/
protected $_optional_params = array();
public function after()
{
if ($this->auto_render)
{
$route = Route::get('wysiwyg/filebrowser');
$mime = array();
// TODO: make types array depended of opener dialog (for ex. only images, docs...).
// for provide this mime-types in config already selected to arrays by type of files
// Now using all allowed types
foreach($this->_config['mime_types'] as $m) {
$mime = array_merge($mime, $m);
}
$this->template->global_config = array
(
'root' => $this->_config['public_directory'].'/'.$this->_config['uploads_directory'],
'dirs_url' => $route->uri(array('action' => 'dirs')),
'files_url' => $route->uri(array('action' => $this->request->action())),
'move_url' => $route->uri(array('action' => 'move')),
'params' => $this->_optional_params,
'mime_types' => $mime,
'max_upload_size' => $this->_config['max_upload_size'],
'upload_notes' => $this->_config['upload_notes'],
);
}
parent::after();
}
} // End Kohana_Controller_Filebrowser