|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* CKFinder |
|
5
|
|
|
* ======== |
|
6
|
|
|
* http://cksource.com/ckfinder |
|
7
|
|
|
* Copyright (C) 2007-2016, CKSource - Frederico Knabben. All rights reserved. |
|
8
|
|
|
* |
|
9
|
|
|
* The software, this file and its contents are subject to the CKFinder |
|
10
|
|
|
* License. Please read the license.txt file before using, installing, copying, |
|
11
|
|
|
* modifying or distribute this file or part of its contents. The contents of |
|
12
|
|
|
* this file is part of the Source Code of CKFinder. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace CKSource\CKFinder\Command; |
|
16
|
|
|
|
|
17
|
|
|
use CKSource\CKFinder\Acl\Acl; |
|
18
|
|
|
use CKSource\CKFinder\Acl\Permission; |
|
19
|
|
|
use CKSource\CKFinder\Config; |
|
20
|
|
|
use CKSource\CKFinder\Event\CKFinderEvent; |
|
21
|
|
|
use CKSource\CKFinder\Event\EditFileEvent; |
|
22
|
|
|
use CKSource\CKFinder\Exception\InvalidExtensionException; |
|
23
|
|
|
use CKSource\CKFinder\Exception\InvalidRequestException; |
|
24
|
|
|
use CKSource\CKFinder\Exception\InvalidUploadException; |
|
25
|
|
|
use CKSource\CKFinder\Exception\UnauthorizedException; |
|
26
|
|
|
use CKSource\CKFinder\Filesystem\File\EditedImage; |
|
27
|
|
|
use CKSource\CKFinder\Filesystem\Folder\WorkingFolder; |
|
28
|
|
|
use CKSource\CKFinder\Image; |
|
29
|
|
|
use CKSource\CKFinder\ResizedImage\ResizedImageRepository; |
|
30
|
|
|
use CKSource\CKFinder\Thumbnail\ThumbnailRepository; |
|
31
|
|
|
use CKSource\CKFinder\Utils; |
|
32
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
33
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The ImageEdit command class. |
|
37
|
|
|
* |
|
38
|
|
|
* This command performs basic image modifications: |
|
39
|
|
|
* - crop |
|
40
|
|
|
* - rotate |
|
41
|
|
|
* - resize |
|
42
|
|
|
* |
|
43
|
|
|
* @copyright 2016 CKSource - Frederico Knabben |
|
44
|
|
|
*/ |
|
45
|
|
|
class ImageEdit extends CommandAbstract |
|
46
|
|
|
{ |
|
47
|
|
|
const OPERATION_CROP = 'crop'; |
|
48
|
|
|
const OPERATION_ROTATE = 'rotate'; |
|
49
|
|
|
const OPERATION_RESIZE = 'resize'; |
|
50
|
|
|
|
|
51
|
|
|
protected $requestMethod = Request::METHOD_POST; |
|
52
|
|
|
|
|
53
|
|
|
protected $requires = array(Permission::FILE_CREATE); |
|
54
|
|
|
|
|
55
|
|
|
public function execute(Request $request, WorkingFolder $workingFolder, EventDispatcher $dispatcher, Acl $acl, ResizedImageRepository $resizedImageRepository, ThumbnailRepository $thumbnailRepository, Config $config) |
|
56
|
|
|
{ |
|
57
|
|
|
$fileName = (string) $request->get('fileName'); |
|
58
|
|
|
$newFileName = (string) $request->get('newFileName'); |
|
59
|
|
|
|
|
60
|
|
|
$editedImage = new EditedImage($fileName, $this->app, $newFileName); |
|
61
|
|
|
|
|
62
|
|
|
$resourceType = $workingFolder->getResourceType(); |
|
63
|
|
|
|
|
64
|
|
|
if (null === $newFileName) { |
|
|
|
|
|
|
65
|
|
|
$resourceTypeName = $resourceType->getName(); |
|
66
|
|
|
$path = $workingFolder->getClientCurrentFolder(); |
|
67
|
|
|
|
|
68
|
|
|
if (!$acl->isAllowed($resourceTypeName, $path, Permission::FILE_DELETE)) { |
|
69
|
|
|
throw new UnauthorizedException(sprintf('Unauthorized: no FILE_DELETE permission in %s:%s', $resourceTypeName, $path)); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (!Image::isSupportedExtension($editedImage->getExtension())) { |
|
74
|
|
|
throw new InvalidExtensionException('Unsupported image type or not image file'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$image = Image::create($editedImage->getContents()); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$actions = (array) $request->get('actions'); |
|
80
|
|
|
|
|
81
|
|
|
if (empty($actions)) { |
|
82
|
|
|
throw new InvalidRequestException(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
foreach ($actions as $actionInfo) { |
|
86
|
|
|
if (!isset($actionInfo['action'])) { |
|
87
|
|
|
throw new InvalidRequestException('ImageEdit: action name missing'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
switch ($actionInfo['action']) { |
|
91
|
|
|
case self::OPERATION_CROP: |
|
92
|
|
|
if (!Utils::arrayContainsKeys($actionInfo, array('x', 'y', 'width', 'height'))) { |
|
93
|
|
|
throw new InvalidRequestException(); |
|
94
|
|
|
} |
|
95
|
|
|
$x = $actionInfo['x']; |
|
96
|
|
|
$y = $actionInfo['y']; |
|
97
|
|
|
$width = $actionInfo['width']; |
|
98
|
|
|
$height = $actionInfo['height']; |
|
99
|
|
|
$image->crop($x, $y, $width, $height); |
|
100
|
|
|
break; |
|
101
|
|
|
|
|
102
|
|
|
case self::OPERATION_ROTATE: |
|
103
|
|
|
if (!isset($actionInfo['angle'])) { |
|
104
|
|
|
throw new InvalidRequestException(); |
|
105
|
|
|
} |
|
106
|
|
|
$degrees = $actionInfo['angle']; |
|
107
|
|
|
$bgcolor = isset($actionInfo['bgcolor']) ? $actionInfo['bgcolor'] : 0; |
|
108
|
|
|
$image->rotate($degrees, $bgcolor); |
|
109
|
|
|
break; |
|
110
|
|
|
|
|
111
|
|
|
case self::OPERATION_RESIZE: |
|
112
|
|
|
if (!Utils::arrayContainsKeys($actionInfo, array('width', 'height'))) { |
|
113
|
|
|
throw new InvalidRequestException(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$imagesConfig = $config->get('images'); |
|
117
|
|
|
|
|
118
|
|
|
$width = $imagesConfig['maxWidth'] && $actionInfo['width'] > $imagesConfig['maxWidth'] ? $imagesConfig['maxWidth'] : $actionInfo['width']; |
|
119
|
|
|
$height = $imagesConfig['maxHeight'] && $actionInfo['height'] > $imagesConfig['maxHeight'] ? $imagesConfig['maxHeight'] : $actionInfo['height']; |
|
120
|
|
|
$image->resize((int) $width, (int) $height, $imagesConfig['quality']); |
|
121
|
|
|
break; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$editFileEvent = new EditFileEvent($this->app, $editedImage); |
|
126
|
|
|
|
|
127
|
|
|
$editedImage->setNewContents($image->getData()); |
|
128
|
|
|
$editedImage->setNewDimensions($image->getWidth(), $image->getHeight()); |
|
129
|
|
|
|
|
130
|
|
|
if (!$editedImage->isValid()) { |
|
131
|
|
|
throw new InvalidUploadException('Invalid file provided'); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$dispatcher->dispatch(CKFinderEvent::EDIT_IMAGE, $editFileEvent); |
|
135
|
|
|
|
|
136
|
|
|
$saved = false; |
|
137
|
|
|
|
|
138
|
|
|
if (!$editFileEvent->isPropagationStopped()) { |
|
139
|
|
|
$saved = $editedImage->save($editFileEvent->getNewContents()); |
|
140
|
|
|
|
|
141
|
|
|
//Remove thumbnails and resized images in case if file is overwritten |
|
142
|
|
|
if ($newFileName === null && $saved) { |
|
|
|
|
|
|
143
|
|
|
$thumbnailRepository->deleteThumbnails($resourceType, $workingFolder->getClientCurrentFolder(), $fileName); |
|
144
|
|
|
$resizedImageRepository->deleteResizedImages($resourceType, $workingFolder->getClientCurrentFolder(), $fileName); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return array( |
|
149
|
|
|
'saved' => (int) $saved, |
|
150
|
|
|
'date' => Utils::formatDate(time()) |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|