<?php
namespace Moto\Application\FileManager\Filter; use Moto; use Moto\Filter\RelativePath; use Moto\Filter\Transliterate; use Zend\Filter\File; class RenameUpload extends File\RenameUpload { public function setTarget($target) { if (is_string($target)) { $target = trim($target); if (substr($target, -1) === '/' && !is_dir($target)) { $_target = rtrim($target, '/'); if (is_file($_target)) { @unlink($_target); } Moto\Util::createDir($target); } } return parent::setTarget($target); } public function getFinalTarget($uploadData) { $uploadData['original_name'] = $uploadData['name']; $transliterateFilter = new Transliterate(); $uploadData['name'] = $transliterateFilter->filter($uploadData['name']); $uploadData['name'] = strtolower($uploadData['name']); $uploadData['name'] = preg_replace('/[^a-z0-9_\-\.]/', '', $uploadData['name']); $uploadData['name'] = preg_replace('/^\./', '', $uploadData['name']); $uploadData['name'] = preg_replace('/([_\-\.])(\\1+)/', '$1', $uploadData['name']); $filename = pathinfo($uploadData['name'], PATHINFO_FILENAME); if (empty($filename)) { $filename = 'file'; if (!empty($uploadData['type']) && preg_match('/^([a-z0-9]+)/', $uploadData['type'], $match)) { if (!empty($match[1])) { $filename = $match[1]; } } $uploadData['name'] = $filename . $uploadData['name']; } $pathFilter = new RelativePath(); $uploadData['name'] = $pathFilter->filter($uploadData['name']); $targetFile = parent::getFinalTarget($uploadData); return $targetFile; } protected function applyRandomToFilename($source, $filename) { $target = $this->getTarget(); if (!is_file($target . '/' . $filename)) { return $filename; } return parent::applyRandomToFilename($source, $filename); } } 