ソースを参照

Version 1.1 : Ajout de la vérification par checksum qui permet de ne pas stocker 2 fois le même fichier, celui ci est juste 'mis en lien'

David 2 年 前
コミット
971cc1ac09
8 ファイル変更56 行追加26 行削除
  1. 1 0
      .gitignore
  2. 9 0
      README.md
  3. 3 0
      config.yaml.dist
  4. BIN
      files/checksum.db
  5. 11 5
      index.php
  6. 3 3
      lib/Checksum.php
  7. 20 15
      lib/UploadHandler.php
  8. 9 3
      lib/functions.php

+ 1 - 0
.gitignore

@@ -1,6 +1,7 @@
 files/*/*
 devToProd.sh
 files/.*
+files/*.db
 config.yaml
 stats
 header-page.php

+ 9 - 0
README.md

@@ -9,6 +9,7 @@ PHP file sharing service [free of rights](https://en.wikipedia.org/wiki/Open_sou
  - PHP 5.6 minimum
  - PHP GD lib 
     - ImageMagick convert binary (option)
+ - PHP PDO sqlite (no requir if check_checksum:false)
  - No nessecary database
 
 ## Installation
@@ -46,6 +47,7 @@ https://github.com/blueimp/jQuery-File-Upload/blob/master/SECURITY.md#secure-fil
 ## Changelog
 
  - futur
+ 	- jQuery-File-Upload is deprecated
 	- serveur disque use limit (check % space and disable upload if < X%)
 	- drag and drop for upload
 	- limit by ip+cookies (sqlite) (limit abuse)
@@ -53,6 +55,13 @@ https://github.com/blueimp/jQuery-File-Upload/blob/master/SECURITY.md#secure-fil
 	- captcha / anti bot
 	- "signaler un fichier"
 	- mimeTypes restriction
+ - 1.1
+	- No upload file if exist on server, just link
+		- required PDO sqlite
+```
+check_checksum: true
+checksumDb: files/checksum.db
+```
  - 1.0
     - jQuery-File-Upload implemented : https://github.com/blueimp/jQuery-File-Upload/
     - Resize image befor upload

+ 3 - 0
config.yaml.dist

@@ -51,3 +51,6 @@ similarServicesLink:
 htmlPages:                               # This page is search in dir "htmlPages/$name.html"  clear if you wan't disable
     cgu: CGU                             # Print page "htmlPages/cgu.html"
 checkUpdate: 86400                       # in seconds or "false" for disable
+# For not upload identique file, just link
+check_checksum: true
+checksumDb: files/checksum.db

BIN
files/checksum.db


+ 11 - 5
index.php

@@ -1,5 +1,5 @@
 <?php 
-define('VERSION', '1.0');
+define('VERSION', '1.1');
 if (!is_readable('./config.yaml')) {
     exit('Error: The configuration file is not present, move config.yaml.default to config.yaml');
 }
@@ -7,6 +7,9 @@ if (($config = yaml_parse_file('./config.yaml')) == false) {
     exit('config.yaml syntax error, check with : http://www.yamllint.com/');
 } 
 include('./lib/functions.php');
+if ($config['check_checksum'] === true) {
+    $checksum = new Checksum;
+}
 
 if (isset($_GET['id'])){
     $id = $_GET['id'];
@@ -164,8 +167,11 @@ if ($passwordForm == false) {
             // Checksum
             $checksum = new Checksum();
             $deleteFile = $checksum->deleteFile($config['uploadDir'].'/'.$id.'/'.$_GET['file']);
-            error_log('checksum>deleteFile : '.$config['uploadDir'].'/'.$id.'/'.$_GET['file']);
-            error_log('checksum>deleteFile return    : '.json_encode($deleteFile));
+            //error_log('checksum>deleteFile : '.$config['uploadDir'].'/'.$id.'/'.$_GET['file']);
+            //error_log('checksum>deleteFile return    : '.json_encode($deleteFile));
+            if ($deleteFile != true) {
+                exit("deleteFile : ".json_encode($deleteFile));
+            }
             // Si c'est le dernire, on supprime le tout
             $uploadDirId = $config['uploadDir'].'/'.$id.'/';
             $nbFile=0;
@@ -178,11 +184,11 @@ if ($passwordForm == false) {
                 }
             }
             if ($nbFile == 0) {
-                rrmdir($config['uploadDir'].'/'.$id.'/');
+                rrmdir($config['uploadDir'].'/'.$id);
             }
         } else {
         // Si c'est le dossier
-            rrmdir($config['uploadDir'].'/'.$id.'/');
+            rrmdir($config['uploadDir'].'/'.$id);
             $nbFile = 0;
         }
         @include_once('./header.php'); 

+ 3 - 3
lib/Checksum.php

@@ -22,12 +22,12 @@ class Checksum{
             $create = $this->dbco->query("
                 CREATE TABLE IF NOT EXISTS checksum (
                 id INTEGER PRIMARY KEY,
-                file_path CHAR(250) NOT NULL,
-                file_md5sum CHAR(150) NOT NULL UNIQUE,
+                file_path CHAR(250) NOT NULL UNIQUE,
+                file_md5sum CHAR(150) NOT NULL,
                 dateExpir INTEGER NOT NULL);
             ");
         } catch ( PDOException $e ) {
-            echo "Error initializing checksum tables";
+            echo "Error initializing checksum tables : ".$e;
             die();
         }
 	}

+ 20 - 15
lib/UploadHandler.php

@@ -1142,6 +1142,7 @@ class UploadHandler
 
     protected function handle_file_upload($uploaded_file, $name, $size, $type, $error,
         $index = null, $content_range = null) {
+        global $config;
         $file = new \stdClass();
         $file->name = $this->enleverCaracteresSpeciaux($this->get_file_name($uploaded_file, $name, $size, $type, $error,
             $index, $content_range));
@@ -1166,18 +1167,24 @@ class UploadHandler
                         FILE_APPEND
                     );
                 } else {
-                    //Checksum
-                    //error_log('move uploaded file');
-                    //error_log("file_path : ".$file_path);
-                    //error_log("uploaded_file : ".$uploaded_file);
-                    $md5sum_uploaded_file = md5_file($uploaded_file);
-                    //error_log("md5sum_uploaded_file : ".$md5sum_uploaded_file);
-                    $checksum = new Checksum();
-                    $checkChecksum = $checksum->checkChecksum($md5sum_uploaded_file);
-                    //error_log("checkChecksum : ".json_encode($checkChecksum));
-                    if ($checkChecksum == false) {
-                        //error_log("Le checksum n'existe pas, on upload normalement");
-                        move_uploaded_file($uploaded_file, $file_path);
+                    if ($config['check_checksum'] === true) {
+                        # Checksum
+                        //error_log('move uploaded file');
+                        //error_log("file_path : ".$file_path);
+                        //error_log("uploaded_file : ".$uploaded_file);
+                        $md5sum_uploaded_file = md5_file($uploaded_file);
+                        //error_log("md5sum_uploaded_file : ".$md5sum_uploaded_file);
+                        $checksum = new Checksum();
+                        $checkChecksum = $checksum->checkChecksum($md5sum_uploaded_file);
+                        //error_log("checkChecksum : ".json_encode($checkChecksum));
+                        if ($checkChecksum == false) {
+                            //error_log("Le checksum n'existe pas, on upload normalement");
+                            move_uploaded_file($uploaded_file, $file_path);
+                        } else {
+                            //error_log("Le checksum existe ! Création d'un lien : ".$checkChecksum['file_path']." vers ".$file_path);
+                            link($checkChecksum['file_path'], $file_path);
+                        }
+                        # Enregistrement dans la base
                         //error_log("id : ".$this->options['id']);
                         $id_split = explode("-", $this->options['id']);
                         $dateExpir = $id_split[0];
@@ -1187,11 +1194,9 @@ class UploadHandler
                             error_log("checksumAddFile : ".json_encode($checksumAddFile));
                         }
                     } else {
-                        //error_log("Le checksum existe ! Création d'un lien : ".$checkChecksum['file_path']." vers ".$file_path);
-                        link($checkChecksum['file_path'], $file_path);
+                        move_uploaded_file($uploaded_file, $file_path);
                     }
                     
-                    
                 }
             } else {
                 // Non-multipart uploads (PUT method support)

+ 9 - 3
lib/functions.php

@@ -1,6 +1,7 @@
 <?php 
 
-require('lib/Checksum.php');
+require_once('Checksum.php');
+require_once('lib/Checksum.php');
 
 //							CODE			LOCALE (locale -a)
 $langueEtLocalDispo=array(	'fr'		=> 'fr_FR', 
@@ -72,7 +73,7 @@ function genZip($id) {
 }
 
 function rrmdir($dir) { 
-	$checksum = new Checksum();
+	$checksum = new Checksum;
 	if (is_dir($dir)) { 
 		$objects = scandir($dir); 
 		foreach ($objects as $object) { 
@@ -81,7 +82,12 @@ function rrmdir($dir) {
 					rrmdir($dir."/".$object);
 				} else {
 					unlink($dir."/".$object); 
-					$checksum->deleteByFile($dir."/".$object);
+					//error_log("deleteFile : ".json_encode($dir."/".$object));
+					$checksumDeleteFile = $checksum->deleteFile($dir."/".$object);
+					//error_log("checksumDeleteFile : ".json_encode($checksumDeleteFile));
+					if ($checksumDeleteFile != true) {
+						exit("checksumDeleteFile : ".json_encode($checksumDeleteFile));
+					}
 				}
 			} 
 		}