2021-10-15 16:58:59 +02:00
< ? php
2024-03-12 16:16:35 +01:00
/* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2024-04-27 12:45:50 +02:00
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2024-08-27 07:01:03 +02:00
* Copyright ( C ) 2024 William Mead < william . mead @ manchenumerique . fr >
2024-04-18 00:01:22 +02:00
*
2021-10-15 16:58:59 +02:00
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*/
/**
2023-05-03 23:59:45 +02:00
* \file / htdocs / core / ajax / onlineSign . php
2024-04-18 00:01:22 +02:00
* \brief File to make Ajax action to add the signature of a document
2021-10-15 16:58:59 +02:00
*/
if ( ! defined ( 'NOTOKENRENEWAL' )) {
define ( 'NOTOKENRENEWAL' , '1' ); // Disables token renewal
}
if ( ! defined ( 'NOREQUIREHTML' )) {
define ( 'NOREQUIREHTML' , '1' );
}
if ( ! defined ( 'NOREQUIREAJAX' )) {
define ( 'NOREQUIREAJAX' , '1' );
}
2022-10-06 16:03:50 +02:00
// Needed to create other objects with workflow
2022-10-03 08:22:27 +02:00
/* if ( ! defined ( 'NOREQUIRESOC' )) {
2021-10-15 16:58:59 +02:00
define ( 'NOREQUIRESOC' , '1' );
2022-10-03 08:22:27 +02:00
} */
2021-10-15 16:58:59 +02:00
// Do not check anti CSRF attack test
if ( ! defined ( 'NOREQUIREMENU' )) {
define ( 'NOREQUIREMENU' , '1' );
}
// If there is no need to load and show top and left menu
if ( ! defined ( " NOLOGIN " )) {
define ( " NOLOGIN " , '1' );
}
if ( ! defined ( 'NOIPCHECK' )) {
define ( 'NOIPCHECK' , '1' ); // Do not check IP defined into conf $dolibarr_main_restrict_ip
}
if ( ! defined ( 'NOBROWSERNOTIF' )) {
define ( 'NOBROWSERNOTIF' , '1' );
}
2024-03-28 17:10:22 +01:00
$entity = ( ! empty ( $_GET [ 'entity' ]) ? ( int ) $_GET [ 'entity' ] : ( ! empty ( $_POST [ 'entity' ]) ? ( int ) $_POST [ 'entity' ] : 1 )); // Keep $_GET and $_POST here. GETPOST not yet defined.
2022-04-11 18:01:25 +02:00
if ( is_numeric ( $entity )) {
define ( " DOLENTITY " , $entity );
}
2021-10-15 16:58:59 +02:00
include '../../main.inc.php' ;
2023-05-03 23:59:45 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2024-11-04 23:53:20 +01:00
/**
* @ var Conf $conf
* @ var DoliDB $db
* @ var HookManager $hookmanager
* @ var Translate $langs
* @ var User $user
*/
2021-10-15 16:58:59 +02:00
$action = GETPOST ( 'action' , 'aZ09' );
2021-12-25 17:08:40 +01:00
2021-10-15 16:58:59 +02:00
$signature = GETPOST ( 'signaturebase64' );
$ref = GETPOST ( 'ref' , 'aZ09' );
2023-05-03 23:59:45 +02:00
$mode = GETPOST ( 'mode' , 'aZ09' ); // 'proposal', ...
2021-12-25 17:08:40 +01:00
$SECUREKEY = GETPOST ( " securekey " ); // Secure key
2024-03-28 21:29:02 +01:00
$online_sign_name = GETPOST ( " onlinesignname " );
2021-12-25 17:08:40 +01:00
2021-10-15 16:58:59 +02:00
$error = 0 ;
$response = " " ;
2021-12-25 17:08:40 +01:00
$type = $mode ;
2023-03-27 11:52:56 +02:00
// Security check
2022-01-04 12:56:08 +01:00
$securekeyseed = '' ;
if ( $type == 'proposal' ) {
2022-08-14 19:00:50 +02:00
$securekeyseed = getDolGlobalString ( 'PROPOSAL_ONLINE_SIGNATURE_SECURITY_TOKEN' );
2023-12-18 16:41:40 +01:00
} elseif ( $type == 'contract' ) {
$securekeyseed = getDolGlobalString ( 'CONTRACT_ONLINE_SIGNATURE_SECURITY_TOKEN' );
} elseif ( $type == 'fichinter' ) {
$securekeyseed = getDolGlobalString ( 'FICHINTER_ONLINE_SIGNATURE_SECURITY_TOKEN' );
} else {
$securekeyseed = getDolGlobalString ( strtoupper ( $type ) . '_ONLINE_SIGNATURE_SECURITY_TOKEN' );
2022-01-04 12:56:08 +01:00
}
2023-05-03 23:59:45 +02:00
if ( empty ( $SECUREKEY ) || ! dol_verifyHash ( $securekeyseed . $type . $ref . ( ! isModEnabled ( 'multicompany' ) ? '' : $entity ), $SECUREKEY , '0' )) {
httponly_accessforbidden ( 'Bad value for securitykey. Value provided ' . dol_escape_htmltag ( $SECUREKEY ) . ' does not match expected value for ref=' . dol_escape_htmltag ( $ref ), 403 );
2021-12-25 17:08:40 +01:00
}
2024-06-30 21:02:50 +02:00
// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
2024-06-03 17:31:21 +02:00
$hookmanager -> initHooks ( array ( 'ajaxonlinesign' ));
2021-12-25 17:08:40 +01:00
2021-10-15 16:58:59 +02:00
/*
* Actions
*/
// None
/*
* View
*/
2022-09-03 20:08:13 +02:00
top_httphead ();
2021-10-15 16:58:59 +02:00
if ( $action == " importSignature " ) {
2023-03-01 23:50:02 +01:00
$issignatureok = ( ! empty ( $signature ) && $signature [ 0 ] == " image/png;base64 " );
if ( $issignatureok ) {
2021-10-15 16:58:59 +02:00
$signature = $signature [ 1 ];
$data = base64_decode ( $signature );
2021-12-25 17:08:40 +01:00
if ( $mode == " propale " || $mode == 'proposal' ) {
2023-05-03 23:59:45 +02:00
require_once DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/pdf.lib.php' ;
2021-12-25 17:08:40 +01:00
$object = new Propal ( $db );
$object -> fetch ( 0 , $ref );
2023-05-03 23:59:45 +02:00
$upload_dir = ! empty ( $conf -> propal -> multidir_output [ $object -> entity ]) ? $conf -> propal -> multidir_output [ $object -> entity ] : $conf -> propal -> dir_output ;
$upload_dir .= '/' . dol_sanitizeFileName ( $object -> ref ) . '/' ;
2021-12-25 17:08:40 +01:00
2023-05-03 23:59:45 +02:00
$default_font_size = pdf_getPDFFontSize ( $langs ); // Must be after pdf_getInstance
$default_font = pdf_getPDFFont ( $langs ); // Must be after pdf_getInstance
2023-02-25 01:54:13 +01:00
$langs -> loadLangs ( array ( " main " , " companies " ));
2021-12-25 17:08:40 +01:00
$date = dol_print_date ( dol_now (), " %Y%m%d%H%M%S " );
2023-05-03 23:59:45 +02:00
$filename = " signatures/ " . $date . " _signature.png " ;
if ( ! is_dir ( $upload_dir . " signatures/ " )) {
if ( ! dol_mkdir ( $upload_dir . " signatures/ " )) {
$response = " Error mkdir. Failed to create dir " . $upload_dir . " signatures/ " ;
2021-12-25 17:08:40 +01:00
$error ++ ;
}
2021-10-15 16:58:59 +02:00
}
2021-12-25 17:08:40 +01:00
if ( ! $error ) {
2023-05-03 23:59:45 +02:00
$return = file_put_contents ( $upload_dir . $filename , $data );
2021-12-25 17:08:40 +01:00
if ( $return == false ) {
$error ++ ;
2022-01-04 12:56:08 +01:00
$response = 'Error file_put_content: failed to create signature file.' ;
2021-12-25 17:08:40 +01:00
}
}
if ( ! $error ) {
2022-07-10 18:06:41 +02:00
// Defined modele of doc
2022-08-14 20:45:04 +02:00
$last_main_doc_file = $object -> last_main_doc ;
2023-05-03 23:59:45 +02:00
$directdownloadlink = $object -> getLastMainDocLink ( 'proposal' ); // url to download the $object->last_main_doc
2022-07-10 18:06:41 +02:00
2022-08-14 20:45:04 +02:00
if ( preg_match ( '/\.pdf/i' , $last_main_doc_file )) {
// TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
2023-05-03 23:59:45 +02:00
$newpdffilename = $upload_dir . $ref . " _signed- " . $date . " .pdf " ;
$sourcefile = $upload_dir . $ref . " .pdf " ;
2022-07-10 18:06:41 +02:00
if ( dol_is_file ( $sourcefile )) {
2024-06-03 17:37:42 +02:00
$parameters = array ( 'sourcefile' => $sourcefile , 'newpdffilename' => $newpdffilename );
2024-06-03 17:31:21 +02:00
$reshook = $hookmanager -> executeHooks ( 'AddSignature' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
if ( $reshook < 0 ) {
setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
2022-08-14 21:31:53 +02:00
}
2024-06-03 17:31:21 +02:00
if ( empty ( $reshook )) {
// We build the new PDF
$pdf = pdf_getInstance ();
if ( class_exists ( 'TCPDF' )) {
$pdf -> setPrintHeader ( false );
$pdf -> setPrintFooter ( false );
}
$pdf -> SetFont ( pdf_getPDFFont ( $langs ));
2022-08-14 21:31:53 +02:00
2024-06-03 17:31:21 +02:00
if ( getDolGlobalString ( 'MAIN_DISABLE_PDF_COMPRESSION' )) {
$pdf -> SetCompression ( false );
}
2022-07-10 18:06:41 +02:00
2024-06-03 17:31:21 +02:00
//$pdf->Open();
$pagecount = $pdf -> setSourceFile ( $sourcefile ); // original PDF
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
$param = array ();
$param [ 'online_sign_name' ] = $online_sign_name ;
$param [ 'pathtoimage' ] = $upload_dir . $filename ;
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
$s = array (); // Array with size of each page. Example array(w'=>210, 'h'=>297);
for ( $i = 1 ; $i < ( $pagecount + 1 ); $i ++ ) {
try {
$tppl = $pdf -> importPage ( $i );
$s = $pdf -> getTemplatesize ( $tppl );
$pdf -> AddPage ( $s [ 'h' ] > $s [ 'w' ] ? 'P' : 'L' );
$pdf -> useTemplate ( $tppl );
2024-07-04 21:34:43 +02:00
$propalsignonspecificpage = getDolGlobalInt ( " PROPAL_SIGNATURE_ON_SPECIFIC_PAGE " );
if ( $propalsignonspecificpage < 0 ) {
$propalsignonspecificpage = $pagecount - abs ( $propalsignonspecificpage );
}
2023-12-18 16:41:40 +01:00
2024-07-04 21:34:43 +02:00
if ( getDolGlobalString ( " PROPAL_SIGNATURE_ON_ALL_PAGES " ) || $propalsignonspecificpage == $i ) {
2024-06-03 17:31:21 +02:00
// A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
// TODO Get position of box from PDF template
if ( getDolGlobalString ( " PROPAL_SIGNATURE_XFORIMGSTART " )) {
$param [ 'xforimgstart' ] = getDolGlobalString ( " PROPAL_SIGNATURE_XFORIMGSTART " );
} else {
$param [ 'xforimgstart' ] = ( empty ( $s [ 'w' ]) ? 120 : round ( $s [ 'w' ] / 2 ) + 15 );
}
if ( getDolGlobalString ( " PROPAL_SIGNATURE_YFORIMGSTART " )) {
$param [ 'yforimgstart' ] = getDolGlobalString ( " PROPAL_SIGNATURE_YFORIMGSTART " );
} else {
$param [ 'yforimgstart' ] = ( empty ( $s [ 'h' ]) ? 240 : $s [ 'h' ] - 60 );
}
if ( getDolGlobalString ( " PROPAL_SIGNATURE_WFORIMG " )) {
$param [ 'wforimg' ] = getDolGlobalString ( " PROPAL_SIGNATURE_WFORIMG " );
} else {
$param [ 'wforimg' ] = $s [ 'w' ] - 20 - $param [ 'xforimgstart' ];
}
dolPrintSignatureImage ( $pdf , $langs , $param );
2024-05-15 15:12:26 +02:00
}
2024-06-03 17:31:21 +02:00
} catch ( Exception $e ) {
dol_syslog ( " Error when manipulating the PDF " . $sourcefile . " by onlineSign: " . $e -> getMessage (), LOG_ERR );
$response = $e -> getMessage ();
$error ++ ;
2023-12-18 16:41:40 +01:00
}
2022-08-14 21:31:53 +02:00
}
2022-07-10 18:06:41 +02:00
2024-06-03 17:31:21 +02:00
if ( ! getDolGlobalString ( " PROPAL_SIGNATURE_ON_ALL_PAGES " ) && ! getDolGlobalInt ( " PROPAL_SIGNATURE_ON_SPECIFIC_PAGE " )) {
// A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
// TODO Get position of box from PDF template
2022-08-14 21:31:53 +02:00
2024-06-24 22:31:39 +02:00
if ( getDolGlobalString ( " PROPAL_SIGNATURE_XFORIMGSTART " )) {
2024-11-11 14:48:18 +01:00
$param [ 'xforimgstart' ] = getDolGlobalString ( " PROPAL_SIGNATURE_XFORIMGSTART " );
2024-06-24 22:31:39 +02:00
} else {
$param [ 'xforimgstart' ] = ( empty ( $s [ 'w' ]) ? 120 : round ( $s [ 'w' ] / 2 ) + 15 );
}
if ( getDolGlobalString ( " PROPAL_SIGNATURE_YFORIMGSTART " )) {
$param [ 'yforimgstart' ] = getDolGlobalString ( " PROPAL_SIGNATURE_YFORIMGSTART " );
} else {
$param [ 'yforimgstart' ] = ( empty ( $s [ 'h' ]) ? 240 : $s [ 'h' ] - 60 );
}
if ( getDolGlobalString ( " PROPAL_SIGNATURE_WFORIMG " )) {
$param [ 'wforimg' ] = getDolGlobalString ( " PROPAL_SIGNATURE_WFORIMG " );
} else {
$param [ 'wforimg' ] = $s [ 'w' ] - 20 - $param [ 'xforimgstart' ];
}
2023-02-25 01:54:13 +01:00
2024-06-03 17:31:21 +02:00
dolPrintSignatureImage ( $pdf , $langs , $param );
}
2023-02-25 01:54:13 +01:00
2024-06-03 17:31:21 +02:00
//$pdf->Close();
$pdf -> Output ( $newpdffilename , " F " );
2022-07-10 18:06:41 +02:00
2024-06-03 17:31:21 +02:00
// Index the new file and update the last_main_doc property of object.
$object -> indexFile ( $newpdffilename , 1 );
}
2022-07-10 18:06:41 +02:00
}
2022-08-14 20:45:04 +02:00
} elseif ( preg_match ( '/\.odt/i' , $last_main_doc_file )) {
2022-08-14 20:03:13 +02:00
// Adding signature on .ODT not yet supported
// TODO
2022-07-10 18:06:41 +02:00
} else {
2022-08-14 20:03:13 +02:00
// Document format not supported to insert online signature.
// We should just create an image file with the signature.
2022-05-21 15:30:52 +02:00
}
2022-07-10 18:06:41 +02:00
}
2022-05-21 15:30:52 +02:00
2022-07-10 18:06:41 +02:00
if ( ! $error ) {
2022-01-04 13:27:26 +01:00
$db -> begin ();
2022-01-04 13:25:02 +01:00
$online_sign_ip = getUserRemoteIP ();
2023-05-03 23:59:45 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " propal " ;
$sql .= " SET fk_statut = " . (( int ) $object :: STATUS_SIGNED ) . " , note_private = ' " . $db -> escape ( $object -> note_private ) . " ', " ;
$sql .= " date_signature = ' " . $db -> idate ( dol_now ()) . " ', " ;
$sql .= " online_sign_ip = ' " . $db -> escape ( $online_sign_ip ) . " ' " ;
2022-01-04 13:25:02 +01:00
if ( $online_sign_name ) {
2023-05-03 23:59:45 +02:00
$sql .= " , online_sign_name = ' " . $db -> escape ( $online_sign_name ) . " ' " ;
2022-01-04 13:25:02 +01:00
}
2023-05-03 23:59:45 +02:00
$sql .= " WHERE rowid = " . (( int ) $object -> id );
2021-12-25 17:08:40 +01:00
2023-10-20 11:20:02 +02:00
dol_syslog ( __FILE__ , LOG_DEBUG );
2021-12-25 17:08:40 +01:00
$resql = $db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
} else {
$num = $db -> affected_rows ( $resql );
}
if ( ! $error ) {
2022-08-29 15:57:13 +02:00
if ( method_exists ( $object , 'call_trigger' )) {
//customer is not a user !?! so could we use same user as validation ?
$user = new User ( $db );
2023-10-17 11:10:25 +02:00
$user -> fetch ( $object -> user_validation_id );
2022-11-14 22:38:39 +01:00
$object -> context = array ( 'closedfromonlinesignature' => 'closedfromonlinesignature' );
2022-08-29 15:57:13 +02:00
$result = $object -> call_trigger ( 'PROPAL_CLOSE_SIGNED' , $user );
if ( $result < 0 ) {
$error ++ ;
2023-05-03 23:59:45 +02:00
$response = " error in trigger " . $object -> error ;
2023-03-01 23:50:02 +01:00
} else {
2024-10-09 01:06:23 +02:00
$soc = new Societe ( $db );
$soc -> id = $object -> socid ;
$result = $soc -> setAsCustomer ();
if ( $result < 0 ) {
$error ++ ;
$response = $db -> lasterror ();
} else {
$response = " success " ;
}
2022-08-29 15:57:13 +02:00
}
2023-03-01 23:50:02 +01:00
} else {
$response = " success " ;
2022-08-29 15:57:13 +02:00
}
2021-12-25 17:08:40 +01:00
} else {
$error ++ ;
$response = " error sql " ;
2021-10-15 16:58:59 +02:00
}
2023-03-01 23:50:02 +01:00
if ( ! $error ) {
$db -> commit ();
$response = " success " ;
setEventMessages ( " PropalSigned " , null , 'warnings' );
} else {
$db -> rollback ();
}
2021-10-15 16:58:59 +02:00
}
2022-09-29 14:28:58 +02:00
} elseif ( $mode == 'contract' ) {
2023-05-03 23:59:45 +02:00
require_once DOL_DOCUMENT_ROOT . '/contrat/class/contrat.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/pdf.lib.php' ;
2022-09-29 14:28:58 +02:00
$object = new Contrat ( $db );
$object -> fetch ( 0 , $ref );
2023-05-03 23:59:45 +02:00
$upload_dir = ! empty ( $conf -> contrat -> multidir_output [ $object -> entity ]) ? $conf -> contrat -> multidir_output [ $object -> entity ] : $conf -> contrat -> dir_output ;
$upload_dir .= '/' . dol_sanitizeFileName ( $object -> ref ) . '/' ;
2022-09-29 14:28:58 +02:00
$date = dol_print_date ( dol_now (), " %Y%m%d%H%M%S " );
2023-05-03 23:59:45 +02:00
$filename = " signatures/ " . $date . " _signature.png " ;
if ( ! is_dir ( $upload_dir . " signatures/ " )) {
if ( ! dol_mkdir ( $upload_dir . " signatures/ " )) {
$response = " Error mkdir. Failed to create dir " . $upload_dir . " signatures/ " ;
2022-09-29 14:28:58 +02:00
$error ++ ;
}
}
if ( ! $error ) {
2023-05-03 23:59:45 +02:00
$return = file_put_contents ( $upload_dir . $filename , $data );
2022-09-29 14:28:58 +02:00
if ( $return == false ) {
$error ++ ;
$response = 'Error file_put_content: failed to create signature file.' ;
}
}
if ( ! $error ) {
// Defined modele of doc
$last_main_doc_file = $object -> last_main_doc ;
2023-05-03 23:59:45 +02:00
$directdownloadlink = $object -> getLastMainDocLink ( 'contrat' ); // url to download the $object->last_main_doc
2024-06-03 17:37:42 +02:00
2022-09-29 14:28:58 +02:00
if ( preg_match ( '/\.pdf/i' , $last_main_doc_file )) {
// TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
2023-05-03 23:59:45 +02:00
$newpdffilename = $upload_dir . $ref . " _signed- " . $date . " .pdf " ;
$sourcefile = $upload_dir . $ref . " .pdf " ;
2022-09-29 14:28:58 +02:00
if ( dol_is_file ( $sourcefile )) {
2024-06-03 17:37:42 +02:00
$parameters = array ( 'sourcefile' => $sourcefile , 'newpdffilename' => $newpdffilename );
2024-06-03 17:31:21 +02:00
$reshook = $hookmanager -> executeHooks ( 'AddSignature' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
if ( $reshook < 0 ) {
setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
2022-09-29 14:28:58 +02:00
}
2024-06-03 17:31:21 +02:00
if ( empty ( $reshook )) {
// We build the new PDF
$pdf = pdf_getInstance ();
if ( class_exists ( 'TCPDF' )) {
$pdf -> setPrintHeader ( false );
$pdf -> setPrintFooter ( false );
}
$pdf -> SetFont ( pdf_getPDFFont ( $langs ));
2022-09-29 14:28:58 +02:00
2024-06-03 17:31:21 +02:00
if ( getDolGlobalString ( 'MAIN_DISABLE_PDF_COMPRESSION' )) {
$pdf -> SetCompression ( false );
}
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
//$pdf->Open();
$pagecount = $pdf -> setSourceFile ( $sourcefile ); // original PDF
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
$param = array ();
$param [ 'online_sign_name' ] = $online_sign_name ;
$param [ 'pathtoimage' ] = $upload_dir . $filename ;
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
$s = array (); // Array with size of each page. Example array(w'=>210, 'h'=>297);
for ( $i = 1 ; $i < ( $pagecount + 1 ); $i ++ ) {
try {
$tppl = $pdf -> importPage ( $i );
$s = $pdf -> getTemplatesize ( $tppl );
$pdf -> AddPage ( $s [ 'h' ] > $s [ 'w' ] ? 'P' : 'L' );
$pdf -> useTemplate ( $tppl );
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
if ( getDolGlobalString ( " CONTRACT_SIGNATURE_ON_ALL_PAGES " )) {
// A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
// TODO Get position of box from PDF template
if ( getDolGlobalString ( " CONTRACT_SIGNATURE_XFORIMGSTART " )) {
$param [ 'xforimgstart' ] = getDolGlobalString ( " CONTRACT_SIGNATURE_XFORIMGSTART " );
} else {
2024-09-01 14:41:16 +02:00
$param [ 'xforimgstart' ] = ( empty ( $s [ 'w' ]) ? 110 : $s [ 'w' ] / 2 - 0 );
2024-06-03 17:31:21 +02:00
}
if ( getDolGlobalString ( " CONTRACT_SIGNATURE_YFORIMGSTART " )) {
$param [ 'yforimgstart' ] = getDolGlobalString ( " CONTRACT_SIGNATURE_YFORIMGSTART " );
} else {
2024-09-01 14:41:16 +02:00
$param [ 'yforimgstart' ] = ( empty ( $s [ 'h' ]) ? 250 : $s [ 'h' ] - 62 );
2024-06-03 17:31:21 +02:00
}
if ( getDolGlobalString ( " CONTRACT_SIGNATURE_WFORIMG " )) {
$param [ 'wforimg' ] = getDolGlobalString ( " CONTRACT_SIGNATURE_WFORIMG " );
} else {
2024-09-01 14:41:16 +02:00
$param [ 'wforimg' ] = $s [ 'w' ] - ( $param [ 'xforimgstart' ] + 16 );
2024-06-03 17:31:21 +02:00
}
dolPrintSignatureImage ( $pdf , $langs , $param );
2024-05-15 15:12:26 +02:00
}
2024-06-03 17:31:21 +02:00
} catch ( Exception $e ) {
dol_syslog ( " Error when manipulating some PDF by onlineSign: " . $e -> getMessage (), LOG_ERR );
$response = $e -> getMessage ();
$error ++ ;
2023-12-18 16:41:40 +01:00
}
2022-09-29 14:28:58 +02:00
}
2024-06-03 17:31:21 +02:00
if ( ! getDolGlobalString ( " CONTRACT_SIGNATURE_ON_ALL_PAGES " )) {
// A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
// TODO Get position of box from PDF template
2023-12-18 16:41:40 +01:00
2024-09-01 14:41:16 +02:00
$param [ 'xforimgstart' ] = ( empty ( $s [ 'w' ]) ? 110 : $s [ 'w' ] / 2 - 0 );
$param [ 'yforimgstart' ] = ( empty ( $s [ 'h' ]) ? 250 : $s [ 'h' ] - 62 );
$param [ 'wforimg' ] = $s [ 'w' ] - ( $param [ 'xforimgstart' ] + 16 );
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
dolPrintSignatureImage ( $pdf , $langs , $param );
}
2022-09-29 14:28:58 +02:00
2024-06-03 17:31:21 +02:00
//$pdf->Close();
$pdf -> Output ( $newpdffilename , " F " );
2022-09-29 14:28:58 +02:00
2024-06-03 17:31:21 +02:00
// Index the new file and update the last_main_doc property of object.
$object -> indexFile ( $newpdffilename , 1 );
}
2022-09-29 14:28:58 +02:00
}
if ( ! $error ) {
$response = " success " ;
}
} elseif ( preg_match ( '/\.odt/i' , $last_main_doc_file )) {
// Adding signature on .ODT not yet supported
// TODO
} else {
// Document format not supported to insert online signature.
// We should just create an image file with the signature.
}
2024-09-10 03:04:49 +02:00
$user = new User ( $db );
2024-09-13 17:45:05 +02:00
$object -> setSignedStatus ( $user , Contrat :: $SIGNED_STATUSES [ 'STATUS_SIGNED_RECEIVER_ONLINE' ], 0 , 'CONTRACT_MODIFY' );
2022-10-20 18:54:45 +02:00
}
2022-10-20 16:52:57 +02:00
} elseif ( $mode == 'fichinter' ) {
2023-05-03 23:59:45 +02:00
require_once DOL_DOCUMENT_ROOT . '/fichinter/class/fichinter.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/pdf.lib.php' ;
2022-10-20 16:52:57 +02:00
$object = new Fichinter ( $db );
$object -> fetch ( 0 , $ref );
2023-05-03 23:59:45 +02:00
$upload_dir = ! empty ( $conf -> ficheinter -> multidir_output [ $object -> entity ]) ? $conf -> ficheinter -> multidir_output [ $object -> entity ] : $conf -> ficheinter -> dir_output ;
2023-12-02 14:52:36 +01:00
$upload_dir .= '/' . dol_sanitizeFileName ( $object -> ref ) . '/' ;
2023-12-01 19:50:09 +01:00
$langs -> loadLangs ( array ( " main " , " companies " ));
$default_font_size = pdf_getPDFFontSize ( $langs ); // Must be after pdf_getInstance
$default_font = pdf_getPDFFont ( $langs ); // Must be
2022-10-20 16:52:57 +02:00
$date = dol_print_date ( dol_now (), " %Y%m%d%H%M%S " );
2023-05-03 23:59:45 +02:00
$filename = " signatures/ " . $date . " _signature.png " ;
if ( ! is_dir ( $upload_dir . " signatures/ " )) {
if ( ! dol_mkdir ( $upload_dir . " signatures/ " )) {
$response = " Error mkdir. Failed to create dir " . $upload_dir . " signatures/ " ;
2022-10-20 16:52:57 +02:00
$error ++ ;
}
}
if ( ! $error ) {
2023-05-03 23:59:45 +02:00
$return = file_put_contents ( $upload_dir . $filename , $data );
2022-10-20 16:52:57 +02:00
if ( $return == false ) {
$error ++ ;
$response = 'Error file_put_content: failed to create signature file.' ;
}
}
if ( ! $error ) {
// Defined modele of doc
$last_main_doc_file = $object -> last_main_doc ;
2023-05-03 23:59:45 +02:00
$directdownloadlink = $object -> getLastMainDocLink ( 'fichinter' ); // url to download the $object->last_main_doc
2024-06-03 17:37:42 +02:00
2022-10-20 16:52:57 +02:00
if ( preg_match ( '/\.pdf/i' , $last_main_doc_file )) {
// TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
2023-05-03 23:59:45 +02:00
$newpdffilename = $upload_dir . $ref . " _signed- " . $date . " .pdf " ;
$sourcefile = $upload_dir . $ref . " .pdf " ;
2022-10-20 16:52:57 +02:00
if ( dol_is_file ( $sourcefile )) {
2024-06-03 17:37:42 +02:00
$parameters = array ( 'sourcefile' => $sourcefile , 'newpdffilename' => $newpdffilename );
2024-06-03 17:31:21 +02:00
$reshook = $hookmanager -> executeHooks ( 'AddSignature' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
if ( $reshook < 0 ) {
setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
2022-10-20 16:52:57 +02:00
}
2024-06-03 17:31:21 +02:00
if ( empty ( $reshook )) {
// We build the new PDF
$pdf = pdf_getInstance ();
if ( class_exists ( 'TCPDF' )) {
$pdf -> setPrintHeader ( false );
$pdf -> setPrintFooter ( false );
}
$pdf -> SetFont ( pdf_getPDFFont ( $langs ));
2022-10-20 16:52:57 +02:00
2024-06-03 17:31:21 +02:00
if ( getDolGlobalString ( 'MAIN_DISABLE_PDF_COMPRESSION' )) {
$pdf -> SetCompression ( false );
}
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
//$pdf->Open();
$pagecount = $pdf -> setSourceFile ( $sourcefile ); // original PDF
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
$param = array ();
$param [ 'online_sign_name' ] = $online_sign_name ;
$param [ 'pathtoimage' ] = $upload_dir . $filename ;
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
$s = array (); // Array with size of each page. Example array(w'=>210, 'h'=>297);
for ( $i = 1 ; $i < ( $pagecount + 1 ); $i ++ ) {
try {
$tppl = $pdf -> importPage ( $i );
$s = $pdf -> getTemplatesize ( $tppl );
$pdf -> AddPage ( $s [ 'h' ] > $s [ 'w' ] ? 'P' : 'L' );
$pdf -> useTemplate ( $tppl );
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
if ( getDolGlobalString ( " FICHINTER_SIGNATURE_ON_ALL_PAGES " )) {
// A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
// TODO Get position of box from PDF template
if ( getDolGlobalString ( " FICHINTER_SIGNATURE_XFORIMGSTART " )) {
$param [ 'xforimgstart' ] = getDolGlobalString ( " FICHINTER_SIGNATURE_XFORIMGSTART " );
} else {
2024-09-13 03:11:10 +02:00
$param [ 'xforimgstart' ] = ( empty ( $s [ 'w' ]) ? 110 : $s [ 'w' ] / 2 - 2 );
2024-06-03 17:31:21 +02:00
}
if ( getDolGlobalString ( " FICHINTER_SIGNATURE_YFORIMGSTART " )) {
$param [ 'yforimgstart' ] = getDolGlobalString ( " FICHINTER_SIGNATURE_YFORIMGSTART " );
} else {
2024-09-13 03:11:10 +02:00
$param [ 'yforimgstart' ] = ( empty ( $s [ 'h' ]) ? 250 : $s [ 'h' ] - 38 );
2024-06-03 17:31:21 +02:00
}
if ( getDolGlobalString ( " FICHINTER_SIGNATURE_WFORIMG " )) {
$param [ 'wforimg' ] = getDolGlobalString ( " FICHINTER_SIGNATURE_WFORIMG " );
} else {
2024-09-13 03:11:10 +02:00
$param [ 'wforimg' ] = $s [ 'w' ] - ( $param [ 'xforimgstart' ] + 20 );
2024-06-03 17:31:21 +02:00
}
dolPrintSignatureImage ( $pdf , $langs , $param );
2024-05-15 15:12:26 +02:00
}
2024-06-03 17:31:21 +02:00
} catch ( Exception $e ) {
dol_syslog ( " Error when manipulating some PDF by onlineSign: " . $e -> getMessage (), LOG_ERR );
$response = $e -> getMessage ();
$error ++ ;
2023-12-18 16:41:40 +01:00
}
2022-10-20 16:52:57 +02:00
}
2024-06-03 17:31:21 +02:00
if ( ! getDolGlobalString ( " FICHINTER_SIGNATURE_ON_ALL_PAGES " )) {
// A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
// TODO Get position of box from PDF template
2023-12-01 19:50:09 +01:00
2024-09-13 03:11:10 +02:00
$param [ 'xforimgstart' ] = ( empty ( $s [ 'w' ]) ? 110 : $s [ 'w' ] / 2 - 2 );
$param [ 'yforimgstart' ] = ( empty ( $s [ 'h' ]) ? 250 : $s [ 'h' ] - 38 );
$param [ 'wforimg' ] = $s [ 'w' ] - ( $param [ 'xforimgstart' ] + 20 );
2023-12-01 19:50:09 +01:00
2024-06-03 17:31:21 +02:00
dolPrintSignatureImage ( $pdf , $langs , $param );
}
2023-12-01 19:50:09 +01:00
2024-06-03 17:31:21 +02:00
//$pdf->Close();
$pdf -> Output ( $newpdffilename , " F " );
2022-10-20 16:52:57 +02:00
2024-06-03 17:31:21 +02:00
// Index the new file and update the last_main_doc property of object.
$object -> indexFile ( $newpdffilename , 1 );
}
2022-10-20 16:52:57 +02:00
}
if ( ! $error ) {
$response = " success " ;
}
} elseif ( preg_match ( '/\.odt/i' , $last_main_doc_file )) {
// Adding signature on .ODT not yet supported
// TODO
} else {
// Document format not supported to insert online signature.
// We should just create an image file with the signature.
}
2024-08-27 07:01:03 +02:00
$user = new User ( $db );
2024-09-13 17:45:05 +02:00
$object -> setSignedStatus ( $user , Fichinter :: $SIGNED_STATUSES [ 'STATUS_SIGNED_RECEIVER_ONLINE' ], 0 , 'FICHINTER_MODIFY' );
2022-09-29 14:28:58 +02:00
}
2023-05-03 22:11:49 +02:00
} elseif ( $mode == " societe_rib " ) {
2023-05-03 23:59:45 +02:00
$langs -> load ( 'withdrawals' );
require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/pdf.lib.php' ;
2023-05-05 11:48:24 +02:00
$modelpath = " core/modules/bank/doc/ " ;
2023-05-03 22:11:49 +02:00
$object = new CompanyBankAccount ( $db );
2024-04-27 12:45:50 +02:00
$object -> fetch ( 0 , $ref );
2023-05-03 22:11:49 +02:00
if ( ! empty ( $object -> id )) {
$object -> fetch_thirdparty ();
2024-09-05 16:05:37 +02:00
$upload_dir = $conf -> societe -> multidir_output [ $object -> thirdparty -> entity ] . '/' . dol_sanitizeFileName (( string ) $object -> thirdparty -> id ) . '/' ;
2023-05-03 22:11:49 +02:00
2023-05-03 23:59:45 +02:00
$default_font_size = pdf_getPDFFontSize ( $langs ); // Must be after pdf_getInstance
$default_font = pdf_getPDFFont ( $langs ); // Must be after pdf_getInstance
$langs -> loadLangs ( array ( " main " , " companies " ));
$date = dol_print_date ( dol_now (), " %Y%m%d%H%M%S " );
$filename = " signatures/ " . $date . " _signature.png " ;
2024-05-31 13:30:05 +02:00
if ( ! dol_is_dir ( $upload_dir . " signatures/ " )) {
2023-05-03 23:59:45 +02:00
if ( ! dol_mkdir ( $upload_dir . " signatures/ " )) {
$response = " Error mkdir. Failed to create dir " . $upload_dir . " signatures/ " ;
$error ++ ;
}
2023-05-03 22:11:49 +02:00
}
2024-05-31 13:30:05 +02:00
if ( ! dol_is_writable ( $upload_dir . " signatures/ " )) {
$response = " Error directory " . $upload_dir . " signatures/ is not writable " ;
$error ++ ;
}
if ( ! dol_is_writable ( DOL_DATA_ROOT . '/admin/temp/' )) { // This is used by TCPDF as working directory
$response = " Error directory " . DOL_DATA_ROOT . " /admin/temp/ is not writable " ;
$error ++ ;
}
2023-05-03 22:11:49 +02:00
2023-05-03 23:59:45 +02:00
if ( ! $error ) {
$return = file_put_contents ( $upload_dir . $filename , $data );
if ( $return == false ) {
$error ++ ;
$response = 'Error file_put_content: failed to create signature file.' ;
}
2023-05-03 22:11:49 +02:00
}
2023-05-03 23:59:45 +02:00
if ( ! $error ) {
// Defined modele of doc
$last_main_doc_file = $object -> last_main_doc ;
2023-05-05 11:48:24 +02:00
$last_modelpdf = $object -> model_pdf ;
2023-05-03 23:59:45 +02:00
$directdownloadlink = $object -> getLastMainDocLink ( 'company' ); // url to download the $object->last_main_doc
if ( preg_match ( '/\.pdf/i' , $last_main_doc_file )) {
2023-05-05 11:48:24 +02:00
$sourcefile = '' ;
$newpdffilename = '' ;
if ( $last_modelpdf == 'sepamandate' ) {
$newpdffilename = $upload_dir . $langs -> transnoentitiesnoconv ( " SepaMandateShort " ) . ' ' . dol_sanitizeFileName ( $object -> ref ) . " - " . dol_sanitizeFileName ( $object -> rum ) . " _signed- " . $date . " .pdf " ;
$sourcefile = $upload_dir . $langs -> transnoentitiesnoconv ( " SepaMandateShort " ) . ' ' . dol_sanitizeFileName ( $object -> ref ) . " - " . dol_sanitizeFileName ( $object -> rum ) . " .pdf " ;
}
2023-05-03 23:59:45 +02:00
if ( dol_is_file ( $sourcefile )) {
2024-06-03 17:37:42 +02:00
$parameters = array ( 'sourcefile' => $sourcefile , 'newpdffilename' => $newpdffilename );
2024-06-03 17:31:21 +02:00
$reshook = $hookmanager -> executeHooks ( 'AddSignature' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
if ( $reshook < 0 ) {
setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
2023-05-03 23:59:45 +02:00
}
2023-05-03 22:11:49 +02:00
2024-06-03 17:31:21 +02:00
if ( empty ( $reshook )) {
// We build the new PDF
$pdf = pdf_getInstance ();
if ( class_exists ( 'TCPDF' )) {
$pdf -> setPrintHeader ( false );
$pdf -> setPrintFooter ( false );
}
$pdf -> SetFont ( pdf_getPDFFont ( $langs ));
2023-05-03 22:11:49 +02:00
2024-06-03 17:31:21 +02:00
if ( getDolGlobalString ( 'MAIN_DISABLE_PDF_COMPRESSION' )) {
$pdf -> SetCompression ( false );
}
2023-05-03 23:59:45 +02:00
2024-06-03 17:31:21 +02:00
//$pdf->Open();
$pagecount = $pdf -> setSourceFile ( $sourcefile ); // original PDF
$s = array (); // Array with size of each page. Example array(w'=>210, 'h'=>297);
for ( $i = 1 ; $i < ( $pagecount + 1 ); $i ++ ) {
try {
$tppl = $pdf -> importPage ( $i );
$s = $pdf -> getTemplatesize ( $tppl );
$pdf -> AddPage ( $s [ 'h' ] > $s [ 'w' ] ? 'P' : 'L' );
$pdf -> useTemplate ( $tppl );
} catch ( Exception $e ) {
dol_syslog ( " Error when manipulating the PDF " . $sourcefile . " by onlineSign: " . $e -> getMessage (), LOG_ERR );
$response = $e -> getMessage ();
$error ++ ;
}
2023-05-03 23:59:45 +02:00
}
2023-05-03 22:11:49 +02:00
2024-06-03 17:31:21 +02:00
// Get position of box from PDF template
$file = '' ;
$classname = '' ;
$filefound = '' ;
$dirmodels = array ( '/' );
if ( is_array ( $conf -> modules_parts [ 'models' ])) {
$dirmodels = array_merge ( $dirmodels , $conf -> modules_parts [ 'models' ]);
}
foreach ( $dirmodels as $reldir ) {
$file = " pdf_ " . $last_modelpdf . " .modules.php " ;
// On vérifie l'emplacement du modele
$file = dol_buildpath ( $reldir . $modelpath . $file , 0 );
if ( file_exists ( $file )) {
$filefound = $file ;
$classname = 'pdf_' . $last_modelpdf ;
break ;
}
2023-05-05 11:48:24 +02:00
}
2024-06-03 17:31:21 +02:00
if ( $filefound === '' ) {
$response = $langs -> trans ( " Error " ) . ' Failed to load doc generator with modelpaths=' . $modelpath . ' - modele=' . $last_modelpdf ;
dol_syslog ( $response , LOG_ERR );
$error ++ ;
}
2023-05-05 11:48:24 +02:00
2024-06-03 17:31:21 +02:00
if ( ! $error && $classname !== '' ) {
// If PDF template class was found
require_once $file ;
2023-05-05 11:48:24 +02:00
2024-06-03 17:31:21 +02:00
$objPDF = new $classname ( $db );
2023-05-03 22:11:49 +02:00
2024-06-03 17:31:21 +02:00
$pdf -> SetFont ( $default_font , '' , $default_font_size - 1 );
2023-05-03 22:11:49 +02:00
2024-06-03 17:31:21 +02:00
$xForDate = $objPDF -> marge_gauche ;
$yForDate = $objPDF -> page_hauteur - $objPDF -> heightforinfotot - $objPDF -> heightforfreetext - $objPDF -> heightforfooter + 10 ;
$pdf -> SetXY ( $xForDate , $yForDate );
$pdf -> MultiCell ( 100 , 4 , dol_print_date ( dol_now (), " daytext " , false , $langs , true ), 0 , 'L' );
2023-05-03 22:11:49 +02:00
2024-06-03 17:31:21 +02:00
$xforimgstart = $objPDF -> xPosSignArea ;
$yforimgstart = $yForDate - 5 ;
$wforimg = $s [ 'w' ] - 20 - $xforimgstart ;
2023-05-03 22:11:49 +02:00
2024-06-03 17:31:21 +02:00
$param = array ();
$param [ 'online_sign_name' ] = $online_sign_name ;
$param [ 'pathtoimage' ] = $upload_dir . $filename ;
2023-05-03 22:11:49 +02:00
2024-06-03 17:31:21 +02:00
// A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
// TODO Get position of box from PDF template
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
$param [ 'xforimgstart' ] = $xforimgstart ;
$param [ 'yforimgstart' ] = $yforimgstart ;
$param [ 'wforimg' ] = $wforimg ;
2023-12-18 16:41:40 +01:00
2024-06-03 17:31:21 +02:00
dolPrintSignatureImage ( $pdf , $langs , $param );
}
//$pdf->Close();
$pdf -> Output ( $newpdffilename , " F " );
2023-05-03 22:11:49 +02:00
2024-06-03 17:31:21 +02:00
// Index the new file and update the last_main_doc property of object.
$object -> indexFile ( $newpdffilename , 1 );
}
2023-05-03 23:59:45 +02:00
}
} elseif ( preg_match ( '/\.odt/i' , $last_main_doc_file )) {
// Adding signature on .ODT not yet supported
// TODO
} else {
// Document format not supported to insert online signature.
// We should just create an image file with the signature.
2023-05-03 22:11:49 +02:00
}
}
2023-05-03 23:59:45 +02:00
} else {
$error ++ ;
2024-04-18 00:01:22 +02:00
$response = " cannot find BAN/RIB " ;
2023-05-03 22:11:49 +02:00
}
if ( ! $error ) {
$db -> begin ();
$online_sign_ip = getUserRemoteIP ();
2023-05-03 23:59:45 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . $object -> table_element ;
2023-05-03 22:11:49 +02:00
$sql .= " SET " ;
2023-05-03 23:59:45 +02:00
$sql .= " date_signature = ' " . $db -> idate ( dol_now ()) . " ', " ;
$sql .= " online_sign_ip = ' " . $db -> escape ( $online_sign_ip ) . " ' " ;
2023-05-03 22:11:49 +02:00
if ( $online_sign_name ) {
2023-05-03 23:59:45 +02:00
$sql .= " , online_sign_name = ' " . $db -> escape ( $online_sign_name ) . " ' " ;
2023-05-03 22:11:49 +02:00
}
2023-05-05 11:48:24 +02:00
//$sql .= ", last_main_doc = '" . $db->escape($object->element'..') . "'";
2023-05-03 23:59:45 +02:00
$sql .= " WHERE rowid = " . (( int ) $object -> id );
2023-05-03 22:11:49 +02:00
2023-10-23 19:19:10 +02:00
dol_syslog ( __FILE__ , LOG_DEBUG );
2023-05-03 22:11:49 +02:00
$resql = $db -> query ( $sql );
if ( ! $resql ) {
$error ++ ;
} else {
$num = $db -> affected_rows ( $resql );
}
if ( ! $error ) {
2023-05-03 23:59:45 +02:00
$response = " success " ;
2023-05-03 22:11:49 +02:00
} else {
$error ++ ;
$response = " error sql " ;
}
if ( ! $error ) {
$db -> commit ();
$response = " success " ;
2023-05-05 11:48:24 +02:00
setEventMessages ( dol_ucfirst ( $mode ) . " Signed " , null , 'warnings' );
2023-05-03 22:11:49 +02:00
} else {
$db -> rollback ();
}
}
2024-05-05 02:48:02 +02:00
} elseif ( $mode == 'expedition' ) {
require_once DOL_DOCUMENT_ROOT . '/expedition/class/expedition.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/pdf.lib.php' ;
$object = new Expedition ( $db );
$object -> fetch ( 0 , $ref );
$upload_dir = $conf -> expedition -> dir_output . " /sending/ " ;
$upload_dir .= '/' . dol_sanitizeFileName ( $object -> ref ) . '/' ;
$langs -> loadLangs ( array ( " main " , " companies " ));
$default_font_size = pdf_getPDFFontSize ( $langs ); // Must be after pdf_getInstance
$default_font = pdf_getPDFFont ( $langs ); // Must be
$date = dol_print_date ( dol_now (), " %Y%m%d%H%M%S " );
$filename = " signatures/ " . $date . " _signature.png " ;
if ( ! is_dir ( $upload_dir . " signatures/ " )) {
if ( ! dol_mkdir ( $upload_dir . " signatures/ " )) {
$response = " Error mkdir. Failed to create dir " . $upload_dir . " signatures/ " ;
$error ++ ;
}
}
if ( ! $error ) {
$return = file_put_contents ( $upload_dir . $filename , $data );
if ( $return == false ) {
$error ++ ;
$response = 'Error file_put_content: failed to create signature file.' ;
}
}
if ( ! $error ) {
$last_main_doc_file = $object -> last_main_doc ;
// Defined modele of doc
if ( empty ( $last_main_doc_file ) || ! dol_is_file ( DOL_DATA_ROOT . '/' . $object -> last_main_doc )) {
// It seems document has never been generated, or was generated and then deleted.
// So we try to regenerate it with its default template.
$defaulttemplate = '' ; // We force the use an empty string instead of $object->model_pdf to be sure to use a "main" default template and not the last one used.
$object -> generateDocument ( $defaulttemplate , $langs );
}
$last_main_doc_file = $object -> last_main_doc ;
$directdownloadlink = $object -> getLastMainDocLink ( 'expedition' ); // url to download the $object->last_main_doc
2024-06-03 17:37:42 +02:00
2024-05-05 02:48:02 +02:00
if ( preg_match ( '/\.pdf/i' , $last_main_doc_file )) {
// TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
$newpdffilename = $upload_dir . $ref . " _signed- " . $date . " .pdf " ;
$sourcefile = $upload_dir . $ref . " .pdf " ;
if ( dol_is_file ( $sourcefile )) {
2024-06-03 17:37:42 +02:00
$parameters = array ( 'sourcefile' => $sourcefile , 'newpdffilename' => $newpdffilename );
2024-06-03 17:31:21 +02:00
$reshook = $hookmanager -> executeHooks ( 'AddSignature' , $parameters , $object , $action ); // Note that $action and $object may have been modified by hook
if ( $reshook < 0 ) {
setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
2024-05-05 02:48:02 +02:00
}
2024-06-03 17:31:21 +02:00
if ( empty ( $reshook )) {
// We build the new PDF
$pdf = pdf_getInstance ();
if ( class_exists ( 'TCPDF' )) {
$pdf -> setPrintHeader ( false );
$pdf -> setPrintFooter ( false );
}
$pdf -> SetFont ( pdf_getPDFFont ( $langs ));
2024-05-05 02:48:02 +02:00
2024-06-03 17:31:21 +02:00
if ( getDolGlobalString ( 'MAIN_DISABLE_PDF_COMPRESSION' )) {
$pdf -> SetCompression ( false );
}
2024-05-05 02:48:02 +02:00
2024-06-03 17:31:21 +02:00
//$pdf->Open();
$pagecount = $pdf -> setSourceFile ( $sourcefile ); // original PDF
2024-05-05 02:48:02 +02:00
2024-06-03 17:31:21 +02:00
$param = array ();
$param [ 'online_sign_name' ] = $online_sign_name ;
$param [ 'pathtoimage' ] = $upload_dir . $filename ;
2024-05-05 02:48:02 +02:00
2024-06-03 17:31:21 +02:00
$s = array (); // Array with size of each page. Example array(w'=>210, 'h'=>297);
for ( $i = 1 ; $i < ( $pagecount + 1 ); $i ++ ) {
try {
$tppl = $pdf -> importPage ( $i );
$s = $pdf -> getTemplatesize ( $tppl );
$pdf -> AddPage ( $s [ 'h' ] > $s [ 'w' ] ? 'P' : 'L' );
$pdf -> useTemplate ( $tppl );
2024-05-05 02:48:02 +02:00
2024-06-03 17:31:21 +02:00
if ( getDolGlobalString ( " SHIPMENT_SIGNATURE_ON_ALL_PAGES " )) {
// A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
// TODO Get position of box from PDF template
2024-05-05 02:48:02 +02:00
2024-06-03 17:31:21 +02:00
$param [ 'xforimgstart' ] = 111 ;
$param [ 'yforimgstart' ] = ( empty ( $s [ 'h' ]) ? 250 : $s [ 'h' ] - 60 );
$param [ 'wforimg' ] = $s [ 'w' ] - ( $param [ 'xforimgstart' ] + 16 );
dolPrintSignatureImage ( $pdf , $langs , $param );
}
} catch ( Exception $e ) {
dol_syslog ( " Error when manipulating some PDF by onlineSign: " . $e -> getMessage (), LOG_ERR );
$response = $e -> getMessage ();
$error ++ ;
2024-05-05 02:48:02 +02:00
}
}
2024-06-03 17:31:21 +02:00
if ( ! getDolGlobalString ( " SHIPMENT_SIGNATURE_ON_ALL_PAGES " )) {
// A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
// TODO Get position of box from PDF template
2024-05-05 02:48:02 +02:00
2024-06-03 17:31:21 +02:00
$param [ 'xforimgstart' ] = 111 ;
$param [ 'yforimgstart' ] = ( empty ( $s [ 'h' ]) ? 250 : $s [ 'h' ] - 60 );
$param [ 'wforimg' ] = $s [ 'w' ] - ( $param [ 'xforimgstart' ] + 16 );
2024-05-05 02:48:02 +02:00
2024-06-03 17:31:21 +02:00
dolPrintSignatureImage ( $pdf , $langs , $param );
}
2024-05-05 02:48:02 +02:00
2024-06-03 17:31:21 +02:00
//$pdf->Close();
$pdf -> Output ( $newpdffilename , " F " );
2024-05-05 02:48:02 +02:00
2024-06-03 17:31:21 +02:00
// Index the new file and update the last_main_doc property of object.
$object -> indexFile ( $newpdffilename , 1 );
}
2024-05-05 02:48:02 +02:00
}
if ( ! $error ) {
$response = " success " ;
}
} elseif ( preg_match ( '/\.odt/i' , $last_main_doc_file )) {
// Adding signature on .ODT not yet supported
// TODO
} else {
// Document format not supported to insert online signature.
// We should just create an image file with the signature.
}
}
2024-09-13 03:09:53 +02:00
$user = new User ( $db );
$object -> setSignedStatus ( $user , Expedition :: $SIGNED_STATUSES [ 'STATUS_SIGNED_RECEIVER_ONLINE' ], 0 , 'SHIPPING_MODIFY' );
2021-10-15 16:58:59 +02:00
}
} else {
2021-12-25 17:08:40 +01:00
$error ++ ;
2021-10-15 16:58:59 +02:00
$response = 'error signature_not_found' ;
}
}
2021-12-25 17:08:40 +01:00
if ( $error ) {
http_response_code ( 501 );
}
2021-10-15 16:58:59 +02:00
echo $response ;
2023-12-18 16:41:40 +01:00
/**
2024-04-18 00:01:22 +02:00
* Output the signature file into the PDF object .
2023-12-18 16:41:40 +01:00
*
* @ param TCPDF $pdf PDF handler
* @ param Translate $langs Language
2024-11-11 14:48:18 +01:00
* @ param array < string , int | float | string | mixed [] > $params Array of params
2023-12-18 16:41:40 +01:00
* @ return void
*/
function dolPrintSignatureImage ( TCPDF $pdf , $langs , $params )
{
$default_font_size = pdf_getPDFFontSize ( $langs ); // Must be after pdf_getInstance
$default_font = pdf_getPDFFont ( $langs ); // Must be
$xforimgstart = $params [ 'xforimgstart' ];
$yforimgstart = $params [ 'yforimgstart' ];
$wforimg = $params [ 'wforimg' ];
$pdf -> SetXY ( $xforimgstart , $yforimgstart + round ( $wforimg / 4 ) - 4 );
$pdf -> SetFont ( $default_font , '' , $default_font_size - 1 );
$pdf -> SetTextColor ( 80 , 80 , 80 );
$pdf -> MultiCell ( $wforimg , 4 , $langs -> trans ( " Signature " ) . ': ' . dol_print_date ( dol_now (), " day " , false , $langs , true ) . ' - ' . $params [ 'online_sign_name' ], 0 , 'L' );
//$pdf->SetXY($xforimgstart, $yforimgstart + round($wforimg / 4));
//$pdf->MultiCell($wforimg, 4, $langs->trans("Lastname") . ': ' . $online_sign_name, 0, 'L');
$pdf -> Image ( $params [ 'pathtoimage' ], $xforimgstart , $yforimgstart , $wforimg , round ( $wforimg / 4 ));
return ;
}