1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\FormBundle\Helper; |
||
13 | |||
14 | use Mautic\CoreBundle\Exception\FileUploadException; |
||
15 | use Mautic\CoreBundle\Helper\CoreParametersHelper; |
||
16 | use Mautic\CoreBundle\Helper\FileUploader; |
||
17 | use Mautic\FormBundle\Crate\UploadFileCrate; |
||
18 | use Mautic\FormBundle\Entity\Field; |
||
19 | use Mautic\FormBundle\Entity\Form; |
||
20 | use Mautic\FormBundle\Entity\Submission; |
||
21 | |||
22 | class FormUploader |
||
23 | { |
||
24 | /** |
||
25 | * @var FileUploader |
||
26 | */ |
||
27 | private $fileUploader; |
||
28 | |||
29 | /** |
||
30 | * @var CoreParametersHelper |
||
31 | */ |
||
32 | private $coreParametersHelper; |
||
33 | |||
34 | public function __construct(FileUploader $fileUploader, CoreParametersHelper $coreParametersHelper) |
||
35 | { |
||
36 | $this->fileUploader = $fileUploader; |
||
37 | $this->coreParametersHelper = $coreParametersHelper; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @throws FileUploadException |
||
42 | */ |
||
43 | public function uploadFiles(UploadFileCrate $filesToUpload, Submission $submission) |
||
44 | { |
||
45 | $uploadedFiles = []; |
||
46 | $result = $submission->getResults(); |
||
47 | $alias = ''; //Only for IDE - will be overriden by foreach |
||
48 | |||
49 | try { |
||
50 | foreach ($filesToUpload as $fileFieldCrate) { |
||
51 | $field = $fileFieldCrate->getField(); |
||
52 | $alias = $field->getAlias(); |
||
53 | $uploadDir = $this->getUploadDir($field); |
||
54 | $fileName = $this->fileUploader->upload($uploadDir, $fileFieldCrate->getUploadedFile()); |
||
55 | $result[$alias] = $fileName; |
||
56 | $uploadedFile = $uploadDir.DIRECTORY_SEPARATOR.$fileName; |
||
57 | $this->fixRotationJPG($uploadedFile); |
||
58 | $uploadedFiles[] =$uploadedFile; |
||
59 | } |
||
60 | $submission->setResults($result); |
||
61 | } catch (FileUploadException $e) { |
||
62 | foreach ($uploadedFiles as $filePath) { |
||
63 | $this->fileUploader->delete($filePath); |
||
64 | } |
||
65 | throw new FileUploadException($alias); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $fileName |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function getCompleteFilePath(Field $field, $fileName) |
||
75 | { |
||
76 | $uploadDir = $this->getUploadDir($field); |
||
77 | |||
78 | return $uploadDir.DIRECTORY_SEPARATOR.$fileName; |
||
79 | } |
||
80 | |||
81 | public function deleteAllFilesOfFormField(Field $field) |
||
82 | { |
||
83 | if (!$field->isFileType()) { |
||
84 | return; |
||
85 | } |
||
86 | |||
87 | $uploadDir = $this->getUploadDir($field); |
||
88 | $this->fileUploader->delete($uploadDir); |
||
89 | } |
||
90 | |||
91 | public function deleteFilesOfForm(Form $form) |
||
92 | { |
||
93 | $formId = $form->getId() ?: $form->deletedId; |
||
94 | $formUploadDir = $this->getUploadDirOfForm($formId); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
95 | $this->fileUploader->delete($formUploadDir); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @todo Refactor code that result can be accessed normally and not only as a array of values |
||
100 | */ |
||
101 | public function deleteUploadedFiles(Submission $submission) |
||
102 | { |
||
103 | $fields = $submission->getForm()->getFields(); |
||
104 | foreach ($fields as $field) { |
||
105 | $this->deleteFileOfFormField($submission, $field); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | private function deleteFileOfFormField(Submission $submission, Field $field) |
||
110 | { |
||
111 | $alias = $field->getAlias(); |
||
112 | $results = $submission->getResults(); |
||
113 | |||
114 | if (!$field->isFileType() || empty($results[$alias])) { |
||
115 | return; |
||
116 | } |
||
117 | |||
118 | $fileName = $results[$alias]; |
||
119 | $filePath = $this->getCompleteFilePath($field, $fileName); |
||
120 | $this->fileUploader->delete($filePath); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | private function getUploadDir(Field $field) |
||
127 | { |
||
128 | $fieldId = $field->getId(); |
||
129 | $formUploadDir = $this->getUploadDirOfForm($field->getForm()->getId()); |
||
130 | |||
131 | return $formUploadDir.DIRECTORY_SEPARATOR.$fieldId; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return string |
||
136 | * |
||
137 | * @throws \LogicException If formId is null |
||
138 | */ |
||
139 | private function getUploadDirOfForm(int $formId) |
||
140 | { |
||
141 | $uploadDir = $this->coreParametersHelper->get('form_upload_dir'); |
||
142 | |||
143 | return $uploadDir.DIRECTORY_SEPARATOR.$formId; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Fix iOS picture orientation after upload PHP |
||
148 | * https://stackoverflow.com/questions/22308921/fix-ios-picture-orientation-after-upload-php. |
||
149 | * |
||
150 | * @param $filename |
||
151 | */ |
||
152 | private function fixRotationJPG($filename) |
||
153 | { |
||
154 | if (IMAGETYPE_JPEG != exif_imagetype($filename)) { |
||
155 | return; |
||
156 | } |
||
157 | $exif = exif_read_data($filename); |
||
158 | $ort = $exif['Orientation']; /*STORES ORIENTATION FROM IMAGE */ |
||
159 | $ort1 = $ort; |
||
160 | $exif = exif_read_data($filename, 0, true); |
||
161 | if (!empty($ort1)) { |
||
162 | $image = imagecreatefromjpeg($filename); |
||
163 | $ort = $ort1; |
||
164 | switch ($ort) { |
||
165 | case 3: |
||
166 | $image = imagerotate($image, 180, 0); |
||
167 | break; |
||
168 | |||
169 | case 6: |
||
170 | $image = imagerotate($image, -90, 0); |
||
171 | break; |
||
172 | |||
173 | case 8: |
||
174 | $image = imagerotate($image, 90, 0); |
||
175 | break; |
||
176 | } |
||
177 | } |
||
178 | imagejpeg($image, $filename, 90); |
||
179 | } |
||
180 | } |
||
181 |