Clean code to try to fix #16123

This commit is contained in:
Laurent Destailleur 2021-02-02 01:30:23 +01:00
parent a19eec7791
commit f5df3b1d69
4 changed files with 41 additions and 44 deletions

View File

@ -22,6 +22,11 @@
* \ingroup member
* \brief Page to print sheets with barcodes using the document templates into core/modules/printsheets
*/
if (!empty($_POST['mode']) && $_POST['mode'] === 'label') { // Page is called to build a PDF and output, we must ne renew the token.
if (!defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL', '1'); // Do not roll the Anti CSRF token (used if MAIN_SECURITY_CSRF_WITH_TOKEN is on)
}
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/format_cards.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
@ -237,16 +242,22 @@ if ($action == 'builddoc')
$outfile = $langs->trans("BarCode").'_sheets_'.dol_print_date(dol_now(), 'dayhourlog').'.pdf';
if (!$mesg) $result = doc_label_pdf_create($db, $arrayofrecords, $modellabel, $outputlangs, $diroutput, $template, dol_sanitizeFileName($outfile));
if (!$mesg) {
$outputlangs = $langs;
// This generates and send PDF to output
// TODO Move
$result = doc_label_pdf_create($db, $arrayofrecords, $modellabel, $outputlangs, $diroutput, $template, dol_sanitizeFileName($outfile));
}
}
if ($result <= 0)
{
dol_print_error('', $result);
}
if ($result <= 0 || $mesg) {
if (empty($mesg)) {
$mesg = 'Error '.$result;
}
if (!$mesg)
{
setEventMessages($mesg, null, 'errors');
} else {
$db->close();
exit;
}
@ -275,10 +286,10 @@ dol_htmloutput_errors($mesg);
//print img_picto('','puce').' '.$langs->trans("PrintsheetForOneBarCode").'<br>';
//print '<br>';
print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">'; // The target is for brothers that open the file instead of downloading it
print '<input type="hidden" name="mode" value="label">';
print '<input type="hidden" name="action" value="builddoc">';
print '<input type="hidden" name="token" value="'.newtoken().'">';
print '<input type="hidden" name="token" value="'.currentToken().'">'; // The page will not renew the token but force download of a file, so we must use here currentToken
print '<div class="tagtable">';

View File

@ -321,24 +321,6 @@ class pdf_standardlabel extends CommonStickerGenerator
if (!empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));
// Output to http stream
clearstatcache();
$attachment = true;
if (!empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment = false;
$type = dol_mimetype($filename);
//if ($encoding) header('Content-Encoding: '.$encoding);
if ($type) header('Content-Type: '.$type);
if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
else header('Content-Disposition: inline; filename="'.$filename.'"');
// Ajout directives pour resoudre bug IE
header('Cache-Control: Public, must-revalidate');
header('Pragma: public');
readfile($file);
$this->result = array('fullpath'=>$file);
return 1;

View File

@ -359,23 +359,6 @@ class pdf_tcpdflabel extends CommonStickerGenerator
if (!empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK));
// Output to http stream
clearstatcache();
$attachment = true;
if (!empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment = false;
$type = dol_mimetype($filename);
//if ($encoding) header('Content-Encoding: '.$encoding);
if ($type) header('Content-Type: '.$type);
if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
else header('Content-Disposition: inline; filename="'.$filename.'"');
// Ajout directives pour resoudre bug IE
header('Cache-Control: Public, must-revalidate');
header('Pragma: public');
readfile($file);
$this->result = array('fullpath'=>$file);

View File

@ -150,6 +150,27 @@ function doc_label_pdf_create($db, $arrayofrecords, $modele, $outputlangs, $outp
if ($obj->write_file($arrayofrecords, $outputlangs, $srctemplatepath, $outputdir, $filename) > 0)
{
$outputlangs->charset_output = $sav_charset_output;
$fullpath = $obj->result['fullpath'];
// Output to http stream
clearstatcache();
$attachment = true;
if (!empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS)) $attachment = false;
$type = dol_mimetype($filename);
//if ($encoding) header('Content-Encoding: '.$encoding);
if ($type) header('Content-Type: '.$type);
if ($attachment) header('Content-Disposition: attachment; filename="'.$filename.'"');
else header('Content-Disposition: inline; filename="'.$filename.'"');
// Ajout directives pour resoudre bug IE
header('Cache-Control: Public, must-revalidate');
header('Pragma: public');
readfile($fullpath);
return 1;
} else {
$outputlangs->charset_output = $sav_charset_output;