From 391bd558ea4348b000bdd9ad2d8380a1c4f6c622 Mon Sep 17 00:00:00 2001
From: Cristiano Magro <cristiano.magro@vola.it>
Date: Mon, 18 Dec 2017 19:04:38 +0100
Subject: [PATCH] classi di gestione upload file

---
 src/UploadFile/SetupUpload.class.php |  167 +++++++++++++++++++++++
 src/UploadFile/FileUpload.class.php  |  149 +++++++++++++++++++++
 src/Autoloader.php                   |   55 +++++++
 3 files changed, 371 insertions(+), 0 deletions(-)

diff --git a/src/Autoloader.php b/src/Autoloader.php
new file mode 100644
index 0000000..de2f7ed
--- /dev/null
+++ b/src/Autoloader.php
@@ -0,0 +1,55 @@
+<?php
+
+/*
+ * Autoloader per la gestione delle classi Vodafone BE sul progetto backoffice
+ */
+
+spl_autoload_register(array('Autoloader', 'autoload'));
+
+/**
+ * Autoloader
+ * Autoloader delle classi
+ *
+ * @author  Cristiano Magro
+ * @package
+ */
+class Autoloader {
+
+    /**
+     * Maps classnames to files containing the class.
+     */
+    private static $classes = array(
+        'FileUpload' => 'include/class/UploadFile/FileUpload.class.php',
+        'SetupUpload' => 'include/class/UploadFile/SetupUpload.class.php',
+    );
+
+    /**
+     * Esecuzione dell'autoloader delle classi secondo la mappa statica
+     *
+     * @param string $nomeClasse richiesta
+     *
+     * @return boolean la classe richiesta e' stata correttamente caricata?
+     */
+    public static function autoload($nomeClasse) {
+        $pathbase = dirname(__FILE__) . '/../../';
+
+        if (isset(self::$classes[$nomeClasse])) {
+            require_once($pathbase . self::$classes[$nomeClasse]);
+        }
+        // Transform the class name into a path
+        $file = str_replace('_', '/', $nomeClasse);
+
+        $path = $pathbase . 'include/class/' . $file . '.php';
+        if (file_exists($path)) {
+            // Load the class file
+            require $path;
+
+            // Class has been found
+            return true;
+        }
+
+        // Class is not in the filesystem
+        return false;
+    }
+
+}
diff --git a/src/UploadFile/FileUpload.class.php b/src/UploadFile/FileUpload.class.php
new file mode 100644
index 0000000..60b4774
--- /dev/null
+++ b/src/UploadFile/FileUpload.class.php
@@ -0,0 +1,149 @@
+<?php
+
+/**
+ * Alcune funzioni comuni da utilizzare per eserguire l'upload dei file
+ *
+ * @author Cristiano Magro
+ */
+class FileUpload {
+
+    const SLUG_CHAR = "_";
+
+    private static $cp1252_map = array(
+        "\xc2\x80" => "\xe2\x82\xac", /* EURO SIGN */
+        "\xc2\x82" => "\xe2\x80\x9a", /* SINGLE LOW-9 QUOTATION MARK */
+        "\xc2\x83" => "\xc6\x92", /* LATIN SMALL LETTER F WITH HOOK */
+        "\xc2\x84" => "\xe2\x80\x9e", /* DOUBLE LOW-9 QUOTATION MARK */
+        "\xc2\x85" => "\xe2\x80\xa6", /* HORIZONTAL ELLIPSIS */
+        "\xc2\x86" => "\xe2\x80\xa0", /* DAGGER */
+        "\xc2\x87" => "\xe2\x80\xa1", /* DOUBLE DAGGER */
+        "\xc2\x88" => "\xcb\x86", /* MODIFIER LETTER CIRCUMFLEX ACCENT */
+        "\xc2\x89" => "\xe2\x80\xb0", /* PER MILLE SIGN */
+        "\xc2\x8a" => "\xc5\xa0", /* LATIN CAPITAL LETTER S WITH CARON */
+        "\xc2\x8b" => "\xe2\x80\xb9", /* SINGLE LEFT-POINTING ANGLE QUOTATION */
+        "\xc2\x8c" => "\xc5\x92", /* LATIN CAPITAL LIGATURE OE */
+        "\xc2\x8e" => "\xc5\xbd", /* LATIN CAPITAL LETTER Z WITH CARON */
+        "\xc2\x91" => "\xe2\x80\x98", /* LEFT SINGLE QUOTATION MARK */
+        "\xc2\x92" => "\xe2\x80\x99", /* RIGHT SINGLE QUOTATION MARK */
+        "\xc2\x93" => "\xe2\x80\x9c", /* LEFT DOUBLE QUOTATION MARK */
+        "\xc2\x94" => "\xe2\x80\x9d", /* RIGHT DOUBLE QUOTATION MARK */
+        "\xc2\x95" => "\xe2\x80\xa2", /* BULLET */
+        "\xc2\x96" => "\xe2\x80\x93", /* EN DASH */
+        "\xc2\x97" => "\xe2\x80\x94", /* EM DASH */
+        "\xc2\x98" => "\xcb\x9c", /* SMALL TILDE */
+        "\xc2\x99" => "\xe2\x84\xa2", /* TRADE MARK SIGN */
+        "\xc2\x9a" => "\xc5\xa1", /* LATIN SMALL LETTER S WITH CARON */
+        "\xc2\x9b" => "\xe2\x80\xba", /* SINGLE RIGHT-POINTING ANGLE QUOTATION */
+        "\xc2\x9c" => "\xc5\x93", /* LATIN SMALL LIGATURE OE */
+        "\xc2\x9e" => "\xc5\xbe", /* LATIN SMALL LETTER Z WITH CARON */
+        "\xc2\x9f" => "\xc5\xb8" /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
+    );
+
+    /**
+     * Genero un numero unico.<br>
+     * un intero composto da un progressivo generato su base data, unito con un randomico.<br>
+     * Il progressivo su base data dovrebbe evitare collisioni sul lungo periodo. (27 anni)<br>
+     * !!!<b>Assicurarsi di memorizzare il dato in database come string, altrimenti eventuali zeri iniziali
+     * vengono persi in un campo integer</b>!!!
+     *
+     * @return string
+     */
+    public static function getProgressivoFile() {
+        $now = time();
+        $data_inizio_conteggio = strtotime("2014-06-01");
+        $datediff = $now - $data_inizio_conteggio;
+        $gg_trascorsi = floor($datediff / (60 * 60 * 24));
+        $numero_unico = str_pad($gg_trascorsi, 4, "0", STR_PAD_LEFT) . rand(10000, 99999);
+        return $numero_unico;
+    }
+
+    public static function utf8_iso8859($txt) {
+        return utf8_decode(strtr($txt, array_flip(self::$cp1252_map)));
+    }
+
+    public static function iso8859_utf8($txt) {
+        return utf8_encode(strtr($txt, self::$cp1252_map));
+    }
+
+    /**
+     * ripulisco la stringa dai caratteri accentati, sostituendoli opportunamente con un carattere
+     * specifico o con nulla.
+     *
+     * @param  string $string da ripulire
+     * @param  mixed  $slug   carattere sostitutivo
+     * @return string stringa ripulita dalle lettere accentate converita in minuscolo
+     */
+    public static function rimuoviAccenti($string, $slug = false) {
+        $string = strtolower($string);
+
+        // codice ASCII delle vocali
+        $ascii['a'] = range(224, 230);
+        $ascii['e'] = range(232, 235);
+        $ascii['i'] = range(236, 239);
+        $ascii['o'] = array_merge(range(242, 246), array(240, 248));
+        $ascii['u'] = range(249, 252);
+
+        // codice ASCII degli altri caratteri
+        $ascii['b'] = array(223);
+        $ascii['c'] = array(231);
+        $ascii['d'] = array(208);
+        $ascii['n'] = array(241);
+        $ascii['y'] = array(253, 255);
+
+        foreach ($ascii as $key => $item) {
+            $acentos = '';
+            foreach ($item as $codigo) {
+                $acentos .= chr($codigo); }
+            $troca[$key] = '/[' . $acentos . ']/i';
+        }
+
+        $string = preg_replace(array_values($troca), array_keys($troca), $string);
+
+        $slug = self::getSlugValido($slug);
+        if ($slug) {
+            // Scambio tutto ciò che non è una lettera o un numero per carattere
+            $string = preg_replace('/[^a-z0-9_.]/i', $slug, $string);
+            // Strip dei caratteri ripetuti
+            // $string = preg_replace('/' . $slug . '{2,}/i', $slug, $string);
+            // $string = trim($string, $slug);
+        }
+
+        return $string;
+    }
+
+    /**
+     * Verifico che il collante SLUG utilizzato sia un carattere accetabile:
+     * tra: a-z0-9_.
+     * Altrimenti restituisco quello di default
+     *
+     * @param  char $collante carattere da utilizzare come collante
+     * @return mixed false nessun collante|carattere valido
+     */
+    private static function getSlugValido($collante) {
+        if (!$collante) {
+            return false;
+        }
+
+        if ($collante === true) {
+            return self::SLUG_CHAR;
+        }
+
+        return preg_replace('/[^a-z0-9_.]/i', self::SLUG_CHAR, $collante);
+    }
+
+
+    /**
+     * Ripulisco il nome di un file da lettere accentate e caratteri speciali.
+     *
+     * @param  stringa $nomeFile nome del file originale
+     * @return string nome file valido per il sistema ripulito da accenti e charatteri strani
+     */
+    public static function clearNomeFile($nomeFile = "") {
+        if ($nomeFile) {
+            $file_name = self::utf8_iso8859($nomeFile);
+            return self::rimuoviAccenti($file_name, "_");
+        }
+        return $nomeFile;
+    }
+
+}
diff --git a/src/UploadFile/SetupUpload.class.php b/src/UploadFile/SetupUpload.class.php
new file mode 100644
index 0000000..75b32d0
--- /dev/null
+++ b/src/UploadFile/SetupUpload.class.php
@@ -0,0 +1,167 @@
+<?php
+
+/**
+ * Bean dati di upload
+ *
+ * Raccolgo i dati necessari per effettuare l'upload dei file e configurare la <br>
+ * pagina in modo dinamico, da passare a Smarty per inizializzare il codice <br>
+ * javascript di gestione del plupload
+ *
+ * @author Cristiano Magro
+ */
+class SetupUpload {
+
+    private $queueLimitUpload;
+    private $totLimitUpload;
+    private $sizeLimitFileUpload;
+    private $totLimitUploadTest;
+    private $fileFilterExt;
+    private $random;
+    private $pathUploadKey;
+    private $numMaxFiles = '1';
+    private $sizeMaxFilesByte;
+    private $sizeMaxFilesDesc;
+    private $debugPageActive = false;
+
+    /**
+     * Builder statico
+     *
+     * @return \SetupUpload
+     */
+    public static function create() {
+        $instance = new self;
+        return $instance;
+    }
+
+    public function getQueueLimit() {
+        return $this->queueLimitUpload;
+    }
+
+    public function setQueueLimit($x) {
+        $this->queueLimitUpload = $x;
+        return $this;
+    }
+
+    public function getTotalLimit() {
+        return $this->totLimitUpload;
+    }
+
+    public function setTotalLimit($x) {
+        $this->totLimitUpload = $x;
+        return $this;
+    }
+
+    public function getSizeLimitFile() {
+        return $this->sizeLimitFileUpload;
+    }
+
+    public function setSizeLimitFile($x) {
+        $this->sizeLimitFileUpload = $x;
+        return $this;
+    }
+
+    public function getLimitText() {
+        return $this->totLimitUploadTest;
+    }
+
+    public function setLimitText($x) {
+        $this->totLimitUploadTest = $x;
+        return $this;
+    }
+
+    public function getFilesFilter() {
+        return $this->fileFilterExt;
+    }
+
+    public function setFilesFilter($x) {
+        $this->fileFilterExt = $x;
+        return $this;
+    }
+
+    public function getRandomValue() {
+        return $this->random;
+    }
+
+    public function setRandomValue($x) {
+        $this->random = $x;
+        return $this;
+    }
+
+    public function getPathUploadKey() {
+        return $this->pathUploadKey;
+    }
+
+    /**
+     * Chiave per decodificare la directory di salvataggio dei file
+     *
+     * La chiave viene utilizzata per ricavare dalla mappatura delle directory, il
+     * path in cui salvare il file. Da utilizzare per offuscare il filesystem del
+     * server. La directory non viene mai passata nei parametri.
+     *
+     * @param string $pathKey necessaria per accedere alla mappatura
+     *
+     * @return \SetupUpload
+     */
+    public function setPathUploadKey($pathKey) {
+        $this->pathUploadKey = $pathKey;
+        return $this;
+    }
+
+    public function getNumMaxFiles() {
+        return $this->numMaxFiles;
+    }
+
+    public function setNumMaxFiles($x) {
+        $this->numMaxFiles = $x;
+        return $this;
+    }
+
+    public function getSizeMaxFilesByte() {
+        return $this->sizeMaxFilesByte;
+    }
+
+    public function setSizeMaxFilesByte($x) {
+        $this->sizeMaxFilesByte = $x;
+        return $this;
+    }
+
+    public function getSizeMaxFilesDesc() {
+        return $this->sizeMaxFilesDesc;
+    }
+
+    public function setSizeMaxFilesDesc($x) {
+        $this->sizeMaxFilesDesc = $x;
+        return $this;
+    }
+
+    public function getDebugActive() {
+        return $this->debugPageActive;
+    }
+
+    public function setDebugActive($x) {
+        $this->debugPageActive = $x;
+        return $this;
+    }
+
+    /**
+     * Creo JSON con i dati per inizializzare la pagina
+     *
+     * Per inizializzare l'oggetto JS che fa da schiavo a pluploader per la
+     * configurazione della pagina, creo un json string con i dati di configurazione
+     * che verranno passati a Smarty.
+     *
+     * @return string json
+     */
+    public function createJsonSetup() {
+        $dati = array(
+            'QUEUE_LIMIT_UPLOAD' => $this->getQueueLimit(),
+            'TOT_LIMIT_UPLOAD' => $this->getSizeMaxFilesByte(),
+            'SIZE_LIMIT_UPLOAD' => $this->getSizeLimitFile(),
+            'TOT_LIMIT_UPLOAD_TXT' => $this->getSizeMaxFilesDesc(),
+            'NUM_MAX_FILES' => $this->getNumMaxFiles(),
+        );
+
+        return json_encode($dati);
+    }
+
+}

--
Gitblit v1.8.0