Skip to content

Commit a0acc26

Browse files
authored
Merge pull request #136 from systeminc/development
Development
2 parents d6dc82a + 6bdf430 commit a0acc26

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

src/Http/Controllers/AdminController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function anyUploadTinyImage(Request $request)
100100

101101
foreach ($request->file('files') as $file) {
102102
if ($file->isValid() && in_array($file->getClientOriginalExtension(), $allowed) && strpos($directory, 'images/tiny') !== false) {
103-
$original = $file->getClientOriginalName();
103+
$original = $this->cleanSpecialChars($file->getClientOriginalName());
104104

105105
$original_image = Image::make($file)->encode();
106106

src/Http/Controllers/GalleriesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ private function uploadImages($images, $gallery_id, $image_id = false)
336336
private function handleFileElement($file)
337337
{
338338
if ($file && $file->isValid()) {
339-
$dirname = 'elements/'.$file->getClientOriginalName();
339+
$dirname = 'elements/'.$this->cleanSpecialChars($file->getClientOriginalName());
340340

341341
Storage::put('public/'.$dirname, file_get_contents($file));
342342

src/Http/Controllers/PagesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private function checkIfIsChangedElementPrefixAndUpdatePrefix($elements_prefix,
316316
private function handleFileElement($file, $elements_prefix)
317317
{
318318
if ($file && $file->isValid()) {
319-
$dirname = 'pages/'.$elements_prefix.'/'.$file->getClientOriginalName();
319+
$dirname = 'pages/'.$elements_prefix.'/'.$this->cleanSpecialChars($file->getClientOriginalName());
320320

321321
Storage::put('public/'.$dirname, file_get_contents($file));
322322

src/Traits/HelpersTrait.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,53 @@ public function consoleSignature()
175175
$this->line(' __________________________________________________________________________________________________ ');
176176
$this->line('');
177177
}
178+
179+
public function cleanSpecialChars($string)
180+
{
181+
$dict = [
182+
"I'm" => 'I am',
183+
'thier' => 'their',
184+
];
185+
186+
return strtolower(
187+
preg_replace(
188+
['#[\\s-]+#', '#[^A-Za-z0-9\. -]+#'],
189+
['-', ''],
190+
// the full cleanString() can be download from http://www.unexpectedit.com/php/php-clean-string-of-utf8-chars-convert-to-similar-ascii-char
191+
$this->cleanString(
192+
str_replace(// preg_replace to support more complicated replacements
193+
array_keys($dict),
194+
array_values($dict),
195+
urldecode($string)
196+
)
197+
)
198+
)
199+
);
200+
}
201+
202+
private function cleanString($text)
203+
{
204+
$utf8 = [
205+
'/[áàâãªä]/u' => 'a',
206+
'/[ÁÀÂÃÄ]/u' => 'A',
207+
'/[ÍÌÎÏ]/u' => 'I',
208+
'/[íìîï]/u' => 'i',
209+
'/[éèêë]/u' => 'e',
210+
'/[ÉÈÊË]/u' => 'E',
211+
'/[óòôõºö]/u' => 'o',
212+
'/[ÓÒÔÕÖ]/u' => 'O',
213+
'/[úùûü]/u' => 'u',
214+
'/[ÚÙÛÜ]/u' => 'U',
215+
'/ç/' => 'c',
216+
'/Ç/' => 'C',
217+
'/ñ/' => 'n',
218+
'/Ñ/' => 'N',
219+
'/–/' => '-', // UTF-8 hyphen to "normal" hyphen
220+
'/[’‘‹›‚]/u' => ' ', // Literally a single quote
221+
'/[“”«»„]/u' => ' ', // Double quote
222+
'/ /' => ' ', // nonbreaking space (equiv. to 0x160)
223+
];
224+
225+
return preg_replace(array_keys($utf8), array_values($utf8), $text);
226+
}
178227
}

0 commit comments

Comments
 (0)