2011-08-26 17:57:05 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-08-26 17:57:05 +02:00
|
|
|
* @subpackage filesystem
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter certain characters from file name, for nicer (more SEO-friendly) URLs
|
|
|
|
* as well as better filesystem compatibility. Can be used for files and folders.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-26 17:57:05 +02:00
|
|
|
* Caution: Does not take care of full filename sanitization in regards to directory traversal etc.,
|
|
|
|
* please use PHP's built-in basename() for this purpose.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-26 17:57:05 +02:00
|
|
|
* The default sanitizer is quite conservative regarding non-ASCII characters,
|
|
|
|
* in order to achieve maximum filesystem compatibility.
|
|
|
|
* In case your filesystem supports a wider character set,
|
|
|
|
* or is case sensitive, you might want to relax these rules
|
|
|
|
* via overriding {@link FileNameFilter_DefaultFilter::$default_replacements}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-26 17:57:05 +02:00
|
|
|
* To leave uploaded filenames as they are (being aware of filesystem restrictions),
|
2013-03-21 19:48:54 +01:00
|
|
|
* add the following code to your YAML config:
|
2011-08-26 17:57:05 +02:00
|
|
|
* <code>
|
2013-03-21 19:48:54 +01:00
|
|
|
* FileNameFilter:
|
|
|
|
* default_use_transliterator: false
|
|
|
|
* default_replacements:
|
2011-08-26 17:57:05 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-11-14 12:26:51 +01:00
|
|
|
* See {@link URLSegmentFilter} for a more generic implementation.
|
2011-08-26 17:57:05 +02:00
|
|
|
*/
|
2012-04-04 16:59:30 +02:00
|
|
|
class FileNameFilter extends Object {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2011-08-26 17:57:05 +02:00
|
|
|
* @var Boolean
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_use_transliterator = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
2011-08-26 17:57:05 +02:00
|
|
|
* @var Array See {@link setReplacements()}.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $default_replacements = array(
|
2011-08-26 17:57:05 +02:00
|
|
|
'/\s/' => '-', // remove whitespace
|
|
|
|
'/_/' => '-', // underscores to dashes
|
2012-12-19 17:30:28 +01:00
|
|
|
'/[^A-Za-z0-9+.\-]+/' => '', // remove non-ASCII chars, only allow alphanumeric plus dash and dot
|
2011-08-26 17:57:05 +02:00
|
|
|
'/[\-]{2,}/' => '-', // remove duplicate dashes
|
2012-10-29 05:07:58 +01:00
|
|
|
'/^[\.\-_]+/' => '', // Remove all leading dots, dashes or underscores
|
2011-08-26 17:57:05 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
|
|
|
* @var Array See {@link setReplacements()}
|
|
|
|
*/
|
|
|
|
public $replacements = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
|
|
|
* Depending on the applied replacement rules, this method
|
|
|
|
* might result in an empty string. In this case, {@link getDefaultName()}
|
|
|
|
* will be used to return a randomly generated file name, while retaining its extension.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-26 17:57:05 +02:00
|
|
|
* @param String Filename including extension (not path).
|
|
|
|
* @return String A filtered filename
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function filter($name) {
|
2011-08-26 17:57:05 +02:00
|
|
|
$ext = pathinfo($name, PATHINFO_EXTENSION);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
$transliterator = $this->getTransliterator();
|
|
|
|
if($transliterator) $name = $transliterator->toASCII($name);
|
|
|
|
foreach($this->getReplacements() as $regex => $replace) {
|
|
|
|
$name = preg_replace($regex, $replace, $name);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
// Safeguard against empty file names
|
|
|
|
$nameWithoutExt = pathinfo($name, PATHINFO_FILENAME);
|
|
|
|
if(empty($nameWithoutExt)) $name = $this->getDefaultName() . '.' . $ext;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
return $name;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
|
|
|
* Take care not to add replacements which might invalidate the file structure,
|
|
|
|
* e.g. removing dots will remove file extension information.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-26 17:57:05 +02:00
|
|
|
* @param Array Map of find/replace used for preg_replace().
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setReplacements($r) {
|
2011-08-26 17:57:05 +02:00
|
|
|
$this->replacements = $r;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
|
|
|
* @return Array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getReplacements() {
|
2013-03-21 19:48:54 +01:00
|
|
|
return ($this->replacements) ? $this->replacements : (array)$this->config()->default_replacements;
|
2011-08-26 17:57:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
2012-06-15 05:54:47 +02:00
|
|
|
* @var SS_Transliterator
|
2011-08-26 17:57:05 +02:00
|
|
|
*/
|
|
|
|
protected $transliterator;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
2012-06-15 05:54:47 +02:00
|
|
|
* @return SS_Transliterator|NULL
|
2011-08-26 17:57:05 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTransliterator() {
|
2013-03-21 19:48:54 +01:00
|
|
|
if($this->transliterator === null && $this->config()->default_use_transliterator) {
|
2012-06-15 05:54:47 +02:00
|
|
|
$this->transliterator = SS_Transliterator::create();
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2011-08-26 17:57:05 +02:00
|
|
|
return $this->transliterator;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
2012-06-15 05:54:47 +02:00
|
|
|
* @param SS_Transliterator|FALSE
|
2011-08-26 17:57:05 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setTransliterator($t) {
|
2011-08-26 17:57:05 +02:00
|
|
|
$this->transliterator = $t;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-26 17:57:05 +02:00
|
|
|
/**
|
|
|
|
* @return String File name without extension
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDefaultName() {
|
2011-08-26 17:57:05 +02:00
|
|
|
return (string)uniqid();
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|