Classi per la configurazione upload
Cristiano Magro
2019-08-26 3f6eaed0ab3a1ea0a06600561404d5da1df76c04
Classe per filtrare il nome file

fa pulizia di tutto quello che non può essere usato come file nome
2 files added
3 files modified
101 ■■■■■ changed files
README.md 7 ●●●●● patch | view | raw | blame | history
phpunit.xml 2 ●●● patch | view | raw | blame | history
src/Vola/UploadFile/Autoloader.php 1 ●●●● patch | view | raw | blame | history
src/Vola/UploadFile/Filename.php 70 ●●●●● patch | view | raw | blame | history
tests/Vola/UploadFile/FilenameTest.php 21 ●●●●● patch | view | raw | blame | history
README.md
@@ -5,4 +5,11 @@
#Esecuzione phpunit test
~~~php
$ phpunit.phar -c phpunit.xml
~~~
#Configusazione
Occorre installare le dipendenze con composer.
~~~
$ composer update
~~~
phpunit.xml
@@ -1,4 +1,4 @@
<phpunit bootstrap="src/Vola/UploadFile/autoloader.php"
<phpunit bootstrap="src/Vola/UploadFile/Autoloader.php"
         stopOnFailure="true">
    <testsuites>
        <testsuite name="FileUpload">
src/Vola/UploadFile/Autoloader.php
@@ -31,6 +31,7 @@
        'Upload_Exc_FileNotExist' => 'Exc/FileNotExist.php',
        'Upload_MgrFile' => 'MgrFile.php',
        'Upload_FileUp' => 'FileUp.php',
        'Upload_Filename' => 'Filename.php',
    );
    /**
src/Vola/UploadFile/Filename.php
New file
@@ -0,0 +1,70 @@
<?php
/**
 * Created by PhpStorm.
 * User: cristiano
 * Date: 26/08/19
 * Time: 23.20
 */
class Upload_Filename
{
    protected function __construct()
    {
    }
    /**
     * Ritorno un'istanza dell'oggetto
     * @return Upload_Filename
     */
    public function create()
    {
        $instance = new self();
        return $instance;
    }
    public function filter($filename, $beautify = true)
    {
        // sanitize filename
        $filename = preg_replace(
            '~
        [<>:"/\\|?*]|            # file system reserved https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
        [\x00-\x1F]|             # control characters http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
        [\x7F\xA0\xAD]|          # non-printing characters DEL, NO-BREAK SPACE, SOFT HYPHEN
        [#\[\]@!$&\'()+,;=]|     # URI reserved https://tools.ietf.org/html/rfc3986#section-2.2
        [{}^\~`]                 # URL unsafe characters https://www.ietf.org/rfc/rfc1738.txt
        ~x',
            '-', $filename);
        // avoids ".", ".." or ".hiddenFiles"
        $filename = ltrim($filename, '.-');
        // optional beautification
        if ($beautify) $filename = $this->beautify_filename($filename);
        // maximize filename length to 255 bytes http://serverfault.com/a/9548/44086
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        $filename = mb_strcut(pathinfo($filename, PATHINFO_FILENAME), 0, 255 - ($ext ? strlen($ext) + 1 : 0), mb_detect_encoding($filename)) . ($ext ? '.' . $ext : '');
        return $filename;
    }
    public function beautify_filename($filename) {
        // reduce consecutive characters
        $filename = preg_replace(array(
            // "file   name.zip" becomes "file-name.zip"
            '/ +/',
            // "file___name.zip" becomes "file-name.zip"
            '/_+/',
            // "file---name.zip" becomes "file-name.zip"
            '/-+/'
        ), '-', $filename);
        $filename = preg_replace(array(
            // "file--.--.-.--name.zip" becomes "file.name.zip"
            '/-*\.-*/',
            // "file...name..zip" becomes "file.name.zip"
            '/\.{2,}/'
        ), '.', $filename);
        // lowercase for windows/unix interoperability http://support.microsoft.com/kb/100625
        $filename = mb_strtolower($filename, mb_detect_encoding($filename));
        // ".file-name.-" becomes "file-name"
        $filename = trim($filename, '.-');
        return $filename;
    }
}
tests/Vola/UploadFile/FilenameTest.php
New file
@@ -0,0 +1,21 @@
<?php
/**
 * Created by PhpStorm.
 * User: cristiano
 * Date: 26/08/19
 * Time: 23.25
 */
class FilenameTest extends PHPUnit_Framework_TestCase
{
    public function testFilename()
    {
        $nome = '../ciCcio-- a.zip';
        $atteso = 'ciccio-a.zip';
        $esito = Upload_Filename::create()->filter($nome);
        $this->assertEquals($atteso, $esito);
    }
}