Skip to content

Commit

Permalink
Fix ZIP file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Jun 3, 2018
1 parent 4b45032 commit bf357b3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 53 deletions.
2 changes: 1 addition & 1 deletion class/EsterenChar.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public function export_to_img($img_type = 'original', $printer_friendly = false,
*
* @param string $sheet_style Détermine le type de l'image, cela va lancer la fonction éponyme
* @param boolean $printer_friendly Détermine si l'on crée une image printer friendly ou pas
* @return boolean True si réussi, False sinon
* @return tFPDF|boolean True si réussi, False sinon
*/
public function export_to_pdf($sheet_style = 'original', $printer_friendly = false) {
$method = '_make_pdf_from_'.$sheet_style;
Expand Down
91 changes: 73 additions & 18 deletions functions/func_createZip.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
<?php

/**
* Fonction de gestion zip récupérée sur http://php.net/
*
* @param array $files Contient la liste des fichiers et du chemin de ceux-ci
* @param string $destination Destination du fichier zip
* @param boolean $overwrite Si false, on ne réécrit pas par-dessus. Sinon, on remplace le fichier zip
*/
function create_zip($files,$destination, $destination_names = array(), $overwrite = true, $debug = false) {

if(file_exists($destination) && !$overwrite) {//if the zip file already exists and overwrite is false, return false
if ($debug === true && P_LOGGED === true) { echo 'Le fichier existe déjà'; }
return false;
function create_zip(array $files, $destination, array $destination_names = array(), $overwrite = true, $debug = false)
{
if(!$overwrite && file_exists($destination)) {
// If file exists and we don't need to recreate it, it's ok
return true;
}

$valid_files = $invalid_files = array();//vars
Expand All @@ -27,8 +24,8 @@ function create_zip($files,$destination, $destination_names = array(), $overwrit

if(count($valid_files)) {//if we have good files...
$zip = new ZipArchive();//create the archive
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {//try opening zip file
return false;
if(true !== $zipReturn = $zip->open($destination,ZIPARCHIVE::OVERWRITE | ZIPARCHIVE::CREATE)) {//try opening zip file
throw new \RuntimeException('Zip error: '.zipErrorMessage($zipReturn));
}

foreach($valid_files as $i => $file) {
Expand All @@ -46,18 +43,76 @@ function create_zip($files,$destination, $destination_names = array(), $overwrit

$zip->close();//close the zip -- done!

if (count($invalid_files) && $debug === true && P_LOGGED === true) {
if (P_LOGGED === true && $debug === true && count($invalid_files)) {
echo 'Fichiers invalides : ';
foreach($invalid_files as $file) {
echo '<br />'.$file;
}
}

return file_exists($destination);//check to make sure the file exists
} else {
if ($debug === true && P_LOGGED === true) {
echo 'Aucun fichier correct';
}
return false;
}
}

if ($debug === true && P_LOGGED === true) {
echo 'Aucun fichier correct';
}

return false;
}

function zipErrorMessage($code)
{
switch (true)
{
case $code === 0:
return 'No error';
case $code === 1:
return 'Multi-disk zip archives not supported';
case $code === 2:
return 'Renaming temporary file failed';
case $code === 3:
return 'Closing zip archive failed';
case $code === 4:
return 'Seek error';
case $code === 5:
return 'Read error';
case $code === 6:
return 'Write error';
case $code === 7:
return 'CRC error';
case $code === 8:
return 'Containing zip archive was closed';
case $code === 9:
return 'No such file';
case $code === 10:
return 'File already exists';
case $code === 11:
return 'Can\'t open file';
case $code === 12:
return 'Failure to create temporary file';
case $code === 13:
return 'Zlib error';
case $code === 14:
return 'Malloc failure';
case $code === 15:
return 'Entry has been changed';
case $code === 16:
return 'Compression method not supported';
case $code === 17:
return 'Premature EOF';
case $code === 18:
return 'Invalid argument';
case $code === 19:
return 'Not a zip archive';
case $code === 20:
return 'Internal error';
case $code === 21:
return 'Zip archive inconsistent';
case $code === 22:
return 'Can\'t remove file';
case $code === 23:
return 'Entry has been deleted';
default:
return 'An unknown error has occurred('.$code.')';
}
}
4 changes: 2 additions & 2 deletions functions/func_pDump.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ function pr($val, $return = false) {
* @return string contenant un dump plus agréable de la variable entrée en paramètre
* @author Pierstoval 26/12/2012
*/
function p_dump(...$args) {
dump(...$args);
function p_dump() {
pr(func_get_args());
}

function show_globals($glob = array(), $return = false) {
Expand Down
1 change: 0 additions & 1 deletion modules_see_character/mod_character_diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
}
foreach ($list as $id => $val) {
if (isset($val['val'])) {
// dump($referenceDomains, $id, $finalDomains);
$finalDomains[$referenceDomains[$id]['domain_name']][$type] = $val['val'];
}
if (isset($val['disciplines'])) {
Expand Down
64 changes: 33 additions & 31 deletions modules_sheet/mod_zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,69 @@
use App\FileAndDir;
use App\Session;

$char_name = $character->get('details_personnage.name');
$char_name = clean_word($char_name);
$zip_dest_name = CHAR_EXPORT.DS.$char_id.DS.$char_name.'_'.P_LANG.'.zip';

$files_to_load = array(
CHAR_EXPORT.DS.$character->id().DS.$char_name.'_original1-print_'.P_LANG.'.jpg' => $char_name.'-original-page1-print_'.P_LANG.'.jpg',
CHAR_EXPORT.DS.$character->id().DS.$char_name.'_original2-print_'.P_LANG.'.jpg' => $char_name.'-original-page2-print_'.P_LANG.'.jpg',
CHAR_EXPORT.DS.$character->id().DS.$char_name.'_original3-print_'.P_LANG.'.jpg' => $char_name.'-original-page3-print_'.P_LANG.'.jpg',
CHAR_EXPORT.DS.$character->id().DS.$char_name.'_original1_'.P_LANG.'.jpg' => $char_name.'-original-page1_'.P_LANG.'.jpg',
CHAR_EXPORT.DS.$character->id().DS.$char_name.'_original2_'.P_LANG.'.jpg' => $char_name.'-original-page2_'.P_LANG.'.jpg',
CHAR_EXPORT.DS.$character->id().DS.$char_name.'_original3_'.P_LANG.'.jpg' => $char_name.'-original-page3_'.P_LANG.'.jpg',
CHAR_EXPORT.DS.$character->id().DS.$char_name.'-original_'.P_LANG.'.pdf' => $char_name.'-original_'.P_LANG.'.pdf',
CHAR_EXPORT.DS.$character->id().DS.$char_name.'-original-print_'.P_LANG.'.pdf' => $char_name.'-original-print_'.P_LANG.'.pdf',
$char_name = clean_word($character->get('details_personnage.name'));

$charOutputDirectory = CHAR_EXPORT.DS.$char_id;

$zip_dest_name = $charOutputDirectory.DS.$char_name.'_'.P_LANG.'.zip';

$files_to_check = array(
$charOutputDirectory.DS.$char_name.'_original1-print_'.P_LANG.'.jpg',
$charOutputDirectory.DS.$char_name.'_original2-print_'.P_LANG.'.jpg',
$charOutputDirectory.DS.$char_name.'_original3-print_'.P_LANG.'.jpg',
$charOutputDirectory.DS.$char_name.'_original1_'.P_LANG.'.jpg',
$charOutputDirectory.DS.$char_name.'_original2_'.P_LANG.'.jpg',
$charOutputDirectory.DS.$char_name.'_original3_'.P_LANG.'.jpg',
$pdfNonPrintFriendly = CHAR_EXPORT.DS.$char_id.DS.$char_name.'-original_'.P_LANG.'.pdf',
$pdfPrintFriendly = CHAR_EXPORT.DS.$char_id.DS.$char_name.'-original-print_'.P_LANG.'.pdf',
);

$imgs = array();

if (!FileAndDir::fexists($zip_dest_name)) {
$generate_all = false;
foreach ($files_to_load as $file => $name) {
foreach ($files_to_check as $file) {
if (!FileAndDir::fexists($file)) {//Si l'un des éléments n'existe pas, on va générer le tout pour remettre à jour le cache personnages du site
$generate_all = true;
break;
}
}
$generate_all = true;

if ($generate_all === true) {//Export de toutes les feuilles de personnage si les fichiers n'existent pas
$imgs_norm = $character->export_to_img('original', false);//Export des jpg
$imgs_print = $character->export_to_img('original', true);//Export des jpg

$pdf_names = array(
CHAR_EXPORT.DS.$character->id().DS.$char_name.'-original.pdf',
CHAR_EXPORT.DS.$character->id().DS.$char_name.'-original-print.pdf',
);
$pdf_norm = $character->export_to_pdf('original', false);//Création du pdf
$pdf_norm->Output($pdf_names[0]);//Export du pdf
if (!$pdf_norm) {
throw new RuntimeException('Failed to create non-printer friendly PDF');
}
$pdf_norm->Output($pdfNonPrintFriendly, 'F');//Export du pdf

$pdf_norm = $character->export_to_pdf('original', true);//Création du pdf
$pdf_norm->Output($pdf_names[1]);//Export du pdf
if (!$pdf_norm) {
throw new RuntimeException('Failed to create non-printer friendly PDF');
}
$pdf_norm->Output($pdfPrintFriendly, 'F');//Export du pdf

$imgs = array_merge($imgs_print, $imgs_norm, $pdf_names);//Fusion de tous les tableaux
$finalFiles = array_merge($imgs_norm, $imgs_print, [$pdfNonPrintFriendly, $pdfPrintFriendly]);

foreach ($imgs as $k => $v) {
unset($imgs[$k]);
if (isset($files_to_load[$v])) {
$imgs[$v] = $files_to_load[$v];
}
}
foreach ($imgs as $v => $k) {
foreach ($finalFiles as $v) {
if (!FileAndDir::fexists($v)) {
Session::setFlash('Une erreur est survenue lors de l\'affichage de la feuille de personnage/'.$v, 'error');
return;
}
}
$create_zip = create_zip(array_keys($imgs), $zip_dest_name, array_values($imgs), true, true);

$create_zip = create_zip($finalFiles, $zip_dest_name, array_map('basename', $finalFiles), true, true);

if (false === $create_zip) {
throw new RuntimeException('Failed to create the ZIP file');
}
} else {
$imgs = $files_to_load;
$create_zip = true;
}
} else {
$imgs = $files_to_load;
$create_zip = true;
}

Expand Down

0 comments on commit bf357b3

Please sign in to comment.