onlineSign.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /*
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * \file /htdocs/core/ajax/onlineSign.php
  18. * \brief File to make Ajax action on Knowledge Management
  19. */
  20. if (!defined('NOTOKENRENEWAL')) {
  21. define('NOTOKENRENEWAL', '1'); // Disables token renewal
  22. }
  23. if (!defined('NOREQUIREHTML')) {
  24. define('NOREQUIREHTML', '1');
  25. }
  26. if (!defined('NOREQUIREAJAX')) {
  27. define('NOREQUIREAJAX', '1');
  28. }
  29. if (!defined('NOREQUIRESOC')) {
  30. define('NOREQUIRESOC', '1');
  31. }
  32. if (!defined('NOCSRFCHECK')) {
  33. define('NOCSRFCHECK', '1');
  34. }
  35. // Do not check anti CSRF attack test
  36. if (!defined('NOREQUIREMENU')) {
  37. define('NOREQUIREMENU', '1');
  38. }
  39. // If there is no need to load and show top and left menu
  40. if (!defined("NOLOGIN")) {
  41. define("NOLOGIN", '1');
  42. }
  43. if (!defined('NOIPCHECK')) {
  44. define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  45. }
  46. if (!defined('NOBROWSERNOTIF')) {
  47. define('NOBROWSERNOTIF', '1');
  48. }
  49. $entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  50. if (is_numeric($entity)) {
  51. define("DOLENTITY", $entity);
  52. }
  53. include '../../main.inc.php';
  54. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  55. $action = GETPOST('action', 'aZ09');
  56. $signature = GETPOST('signaturebase64');
  57. $ref = GETPOST('ref', 'aZ09');
  58. $mode = GETPOST('mode', 'aZ09'); // 'proposal', ...
  59. $SECUREKEY = GETPOST("securekey"); // Secure key
  60. $error = 0;
  61. $response = "";
  62. $type = $mode;
  63. // Check securitykey
  64. $securekeyseed = '';
  65. if ($type == 'proposal') {
  66. $securekeyseed = getDolGlobalString('PROPOSAL_ONLINE_SIGNATURE_SECURITY_TOKEN');
  67. }
  68. if (empty($SECUREKEY) || !dol_verifyHash($securekeyseed.$type.$ref.(!isModEnabled('multicompany') ? '' : $entity), $SECUREKEY, '0')) {
  69. httponly_accessforbidden('Bad value for securitykey. Value provided '.dol_escape_htmltag($SECUREKEY).' does not match expected value for ref='.dol_escape_htmltag($ref), 403);
  70. }
  71. /*
  72. * Actions
  73. */
  74. // None
  75. /*
  76. * View
  77. */
  78. top_httphead();
  79. if ($action == "importSignature") {
  80. if (!empty($signature) && $signature[0] == "image/png;base64") {
  81. $signature = $signature[1];
  82. $data = base64_decode($signature);
  83. if ($mode == "propale" || $mode == 'proposal') {
  84. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  85. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  86. $object = new Propal($db);
  87. $object->fetch(0, $ref);
  88. $upload_dir = !empty($conf->propal->multidir_output[$object->entity])?$conf->propal->multidir_output[$object->entity]:$conf->propal->dir_output;
  89. $upload_dir .= '/'.dol_sanitizeFileName($object->ref).'/';
  90. $date = dol_print_date(dol_now(), "%Y%m%d%H%M%S");
  91. $filename = "signatures/".$date."_signature.png";
  92. if (!is_dir($upload_dir."signatures/")) {
  93. if (!dol_mkdir($upload_dir."signatures/")) {
  94. $response ="Error mkdir. Failed to create dir ".$upload_dir."signatures/";
  95. $error++;
  96. }
  97. }
  98. if (!$error) {
  99. $return = file_put_contents($upload_dir.$filename, $data);
  100. if ($return == false) {
  101. $error++;
  102. $response = 'Error file_put_content: failed to create signature file.';
  103. }
  104. }
  105. if (!$error) {
  106. // Defined modele of doc
  107. $last_main_doc_file = $object->last_main_doc;
  108. $directdownloadlink = $object->getLastMainDocLink('proposal'); // url to download the $object->last_main_doc
  109. if (preg_match('/\.pdf/i', $last_main_doc_file)) {
  110. // TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
  111. $newpdffilename = $upload_dir.$ref."_signed-".$date.".pdf";
  112. $sourcefile = $upload_dir.$ref.".pdf";
  113. if (dol_is_file($sourcefile)) {
  114. // We build the new PDF
  115. $pdf = pdf_getInstance();
  116. if (class_exists('TCPDF')) {
  117. $pdf->setPrintHeader(false);
  118. $pdf->setPrintFooter(false);
  119. }
  120. $pdf->SetFont(pdf_getPDFFont($langs));
  121. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  122. $pdf->SetCompression(false);
  123. }
  124. //$pdf->Open();
  125. $pagecount = $pdf->setSourceFile($sourcefile); // original PDF
  126. $s = array(); // Array with size of each page. Exemple array(w'=>210, 'h'=>297);
  127. for ($i=1; $i<($pagecount+1); $i++) {
  128. try {
  129. $tppl = $pdf->importPage($i);
  130. $s = $pdf->getTemplatesize($tppl);
  131. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  132. $pdf->useTemplate($tppl);
  133. } catch (Exception $e) {
  134. dol_syslog("Error when manipulating some PDF by onlineSign: ".$e->getMessage(), LOG_ERR);
  135. $response = $e->getMessage();
  136. $error++;
  137. }
  138. }
  139. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  140. // TODO Get position of box from PDF template
  141. $xforimgstart = (empty($s['w']) ? 120 : round($s['w'] / 2) + 15);
  142. $yforimgstart = (empty($s['h']) ? 240 : $s['h'] - 60);
  143. $wforimg = $s['w'] - 20 - $xforimgstart;
  144. $pdf->Image($upload_dir.$filename, $xforimgstart, $yforimgstart, $wforimg, round($wforimg / 4));
  145. //$pdf->Close();
  146. $pdf->Output($newpdffilename, "F");
  147. // Index the new file and update the last_main_doc property of object.
  148. $object->indexFile($newpdffilename, 1);
  149. }
  150. } elseif (preg_match('/\.odt/i', $last_main_doc_file)) {
  151. // Adding signature on .ODT not yet supported
  152. // TODO
  153. } else {
  154. // Document format not supported to insert online signature.
  155. // We should just create an image file with the signature.
  156. }
  157. }
  158. if (!$error) {
  159. $db->begin();
  160. $online_sign_ip = getUserRemoteIP();
  161. $online_sign_name = ''; // TODO Ask name on form to sign
  162. $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
  163. $sql .= " SET fk_statut = ".((int) $object::STATUS_SIGNED).", note_private = '".$db->escape($object->note_private)."',";
  164. $sql .= " date_signature = '".$db->idate(dol_now())."',";
  165. $sql .= " online_sign_ip = '".$db->escape($online_sign_ip)."'";
  166. if ($online_sign_name) {
  167. $sql .= ", online_sign_name = '".$db->escape($online_sign_name)."'";
  168. }
  169. $sql .= " WHERE rowid = ".((int) $object->id);
  170. dol_syslog(__METHOD__, LOG_DEBUG);
  171. $resql = $db->query($sql);
  172. if (!$resql) {
  173. $error++;
  174. } else {
  175. $num = $db->affected_rows($resql);
  176. }
  177. if (!$error) {
  178. $db->commit();
  179. $response = "success";
  180. setEventMessages("PropalSigned", null, 'warnings');
  181. if (method_exists($object, 'call_trigger')) {
  182. //customer is not a user !?! so could we use same user as validation ?
  183. $user = new User($db);
  184. $user->fetch($object->user_valid_id);
  185. $result = $object->call_trigger('PROPAL_CLOSE_SIGNED', $user);
  186. if ($result < 0) {
  187. $error++;
  188. }
  189. }
  190. } else {
  191. $db->rollback();
  192. $error++;
  193. $response = "error sql";
  194. }
  195. }
  196. }
  197. } else {
  198. $error++;
  199. $response = 'error signature_not_found';
  200. }
  201. }
  202. if ($error) {
  203. http_response_code(501);
  204. }
  205. echo $response;