Loading

[SOLVED] Files in exported folder not decrypted

Get help with problems, or report & discuss bugs in Collabtive

[SOLVED] Files in exported folder not decrypted

Postby Remiguel » 23.10.2014, 12:08

With the new encryption feature, files before their compression while a folder is exported, need to be decrypted first.
In Collabtive 2.0, it seems to not be yet implemented. All files in compressed folders are still encrypted.
How could we implement this feature quickly (which template or class should be modified)?

By clicking on the little disk icon the entire folder will be compressed and download on your desktop.
Folder_exp.png
Folder_exp.png (2.7 KiB) Viewed 4440 times
Last edited by Remiguel on 02.11.2014, 00:56, edited 2 times in total.
Remiguel
 
Posts: 96
Joined: 10.06.2014, 09:34
Location: Spain / France

Re: Files in exported folder not decrypted

Postby Remiguel » 26.10.2014, 21:26

By clicking on the small disk icon, the folderzip function is called.
This function basically compress all files, which names are contained in the array $thefolder
Code: Select all
$create = $zip->create(CL_ROOT . "/files/" . CL_CONFIG . "/$id/$thefolder[name]/", PCLZIP_OPT_REMOVE_ALL_PATH);


The quick solution would be, to decrypt all the files contained in this array in a temporary folder "tmp".
Compress the tmp file like this:
Code: Select all
$create = $zip->create(CL_ROOT . "/files/" . CL_CONFIG . "/$id/tmp/$thefolder[name]/", PCLZIP_OPT_REMOVE_ALL_PATH);

Download the zip tmp file.
Delete the tmp folder.

I don't know if we could achieved it with a more simple flow. If you have something better, please post the idea. In the meanwhile I will give a try.
Remiguel
 
Posts: 96
Joined: 10.06.2014, 09:34
Location: Spain / France

Re: Files in exported folder not decrypted

Postby Remiguel » 31.10.2014, 14:21

I have a functional version of my code. First I will go through step by step and at the end of this post you will find the full code.
This function is defined in managefile.php file

Function folderexport

Create a tmp directory
Code: Select all
 $thepath = substr(($thefolder["abspath"]),0,-(strlen($thefolder["name"])));
    $tmpfolder = CL_ROOT . "/files/" . CL_CONFIG . "/$id" . $thepath . "tmp";
   
    if (!is_dir($tmpfolder)) {
       mkdir($tmpfolder , 0777, true); // create tmp folder
    }


Query the db to get all files from the project which the name of the selected folder is contained into the file path
Code: Select all
    $filesList = $conn->query("SELECT * FROM `files` WHERE `datei` LIKE '%" . $thefolder["name"]. "%' AND `project` =" . $id)->fetchAll();


Cycle in the array to test and decrypt the files
Code: Select all
    foreach($filesList as $file){


Test whether both paths are matching
This test allows to keep all subdirectories and only subdirectories of the parent folder and of course their corresponding files.

Code: Select all
$chemin = substr(($file["datei"]),strlen("/files/" . CL_CONFIG . "/$id"),-(strlen($file["name"]))); // clean the path
   if (strpos("/" . $chemin, $thepath) === false) continue; // skip files out of the initial path
   $chemin = $tmpfolder . "/" . $chemin;


Create directories and subdirectories in the tmp folder
Code: Select all
$newfile = $chemin. "/" . $file["name"];
   if (!is_dir($chemin)) {
       mkdir($chemin, 0777, true); // create folders if they don't exist
       }


Decrypt the file
Code: Select all
$plaintext = $myfile->decryptFile($tmpFile , $settings["filePass"]);


Save the decrypted file in the tmp folder
Code: Select all
file_put_contents($newfile,$plaintext);


Create zip file and erase it after download
Code: Select all
    if (file_exists($topfad)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($topfad));
    flush();
    readfile($topfad);
    unlink ($topfad); // erase the file
    }


Delete tmp folder, subfolders and associated files
Code: Select all
  if (file_exists($tmpfolder )) {
       delete_directory($tmpfolder);
   }


Full code
Code: Select all
} elseif ($action == "folderexport") {
    $thefolder = $myfile->getFolder($thisfile);
 
    $thepath = substr(($thefolder["abspath"]),0,-(strlen($thefolder["name"])));
    $tmpfolder = CL_ROOT . "/files/" . CL_CONFIG . "/$id" . $thepath . "tmp";
   
    if (!is_dir($tmpfolder)) {
       mkdir($tmpfolder , 0777, true); // create tmp folder
    }
   
    $filesList = $conn->query("SELECT * FROM `files` WHERE `datei` LIKE '%" . $thefolder["name"]. "%' AND `project` =" . $id)->fetchAll(); // save all data from files, which the folder name is contained in file path and from the project of the selected folder
   
    foreach($filesList as $file){

   $chemin = substr(($file["datei"]),strlen("/files/" . CL_CONFIG . "/$id"),-(strlen($file["name"]))); // remove the file name and root path from the path
   if (strpos("/" . $chemin, $thepath) === false) continue; // skip files out of the initial path
   $chemin = $tmpfolder . "/" . $chemin;
   
   $newfile = $chemin. "/" . $file["name"];
   if (!is_dir($chemin)) {
       mkdir($chemin, 0777, true); // create folders if they don't exist
       }

   $tmpFile = CL_ROOT . "/" . $file["datei"];

   $plaintext = $myfile->decryptFile($tmpFile , $settings["filePass"]); // decrypt the file

   //no plaintext means file was not encrypted or not decrypted. however copy the file in tmp folder
   if(!$plaintext) {
      copy ($tmpFile, $newfile);
   } else {
   file_put_contents($newfile,$plaintext);
   }

}

    $topfad = CL_ROOT . "/files/" . CL_CONFIG . "/$id" . "/folder" . $thefolder["ID"] . ".zip";
    $zip = new PclZip($topfad);
   
    if (file_exists($topfad)) {
        if (unlink($topfad)) {
            $create = $zip->create($tmpfolder . "/", PCLZIP_OPT_REMOVE_ALL_PATH);
        }
    } else {
        $create = $zip->create($tmpfolder . "/", PCLZIP_OPT_REMOVE_ALL_PATH);
    }
   
    // read zip file and send it to standard output. allow to delete zip file unlink
    if (file_exists($topfad)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($topfad));
    flush();
    readfile($topfad);
    unlink ($topfad); // erase the file
    }
   
   // delete tmp folder and all files and subfolders
   if (file_exists($tmpfolder )) {
       delete_directory($tmpfolder);
   }
Remiguel
 
Posts: 96
Joined: 10.06.2014, 09:34
Location: Spain / France


Return to Problems and Bugs

Who is online

Users browsing this forum: Bing [Bot]