onlineSign.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. // Needed to create other objects with workflow
  30. /*if (!defined('NOREQUIRESOC')) {
  31. define('NOREQUIRESOC', '1');
  32. }*/
  33. // Do not check anti CSRF attack test
  34. if (!defined('NOREQUIREMENU')) {
  35. define('NOREQUIREMENU', '1');
  36. }
  37. // If there is no need to load and show top and left menu
  38. if (!defined("NOLOGIN")) {
  39. define("NOLOGIN", '1');
  40. }
  41. if (!defined('NOIPCHECK')) {
  42. define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  43. }
  44. if (!defined('NOBROWSERNOTIF')) {
  45. define('NOBROWSERNOTIF', '1');
  46. }
  47. $entity = (!empty($_GET['entity']) ? (int) $_GET['entity'] : (!empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  48. if (is_numeric($entity)) {
  49. define("DOLENTITY", $entity);
  50. }
  51. include '../../main.inc.php';
  52. require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  53. $action = GETPOST('action', 'aZ09');
  54. $signature = GETPOST('signaturebase64');
  55. $ref = GETPOST('ref', 'aZ09');
  56. $mode = GETPOST('mode', 'aZ09'); // 'proposal', ...
  57. $SECUREKEY = GETPOST("securekey"); // Secure key
  58. $online_sign_name = GETPOST("onlinesignname") ? GETPOST("onlinesignname") : '';
  59. $error = 0;
  60. $response = "";
  61. $type = $mode;
  62. // Security check
  63. $securekeyseed = '';
  64. if ($type == 'proposal') {
  65. $securekeyseed = getDolGlobalString('PROPOSAL_ONLINE_SIGNATURE_SECURITY_TOKEN');
  66. } elseif ($type == 'contract') {
  67. $securekeyseed = getDolGlobalString('CONTRACT_ONLINE_SIGNATURE_SECURITY_TOKEN');
  68. } elseif ($type == 'fichinter') {
  69. $securekeyseed = getDolGlobalString('FICHINTER_ONLINE_SIGNATURE_SECURITY_TOKEN');
  70. } else {
  71. $securekeyseed = getDolGlobalString(strtoupper($type).'_ONLINE_SIGNATURE_SECURITY_TOKEN');
  72. }
  73. if (empty($SECUREKEY) || !dol_verifyHash($securekeyseed . $type . $ref . (!isModEnabled('multicompany') ? '' : $entity), $SECUREKEY, '0')) {
  74. httponly_accessforbidden('Bad value for securitykey. Value provided ' . dol_escape_htmltag($SECUREKEY) . ' does not match expected value for ref=' . dol_escape_htmltag($ref), 403);
  75. }
  76. /*
  77. * Actions
  78. */
  79. // None
  80. /*
  81. * View
  82. */
  83. top_httphead();
  84. if ($action == "importSignature") {
  85. $issignatureok = (!empty($signature) && $signature[0] == "image/png;base64");
  86. if ($issignatureok) {
  87. $signature = $signature[1];
  88. $data = base64_decode($signature);
  89. if ($mode == "propale" || $mode == 'proposal') {
  90. require_once DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php';
  91. require_once DOL_DOCUMENT_ROOT . '/core/lib/pdf.lib.php';
  92. $object = new Propal($db);
  93. $object->fetch(0, $ref);
  94. $upload_dir = !empty($conf->propal->multidir_output[$object->entity]) ? $conf->propal->multidir_output[$object->entity] : $conf->propal->dir_output;
  95. $upload_dir .= '/' . dol_sanitizeFileName($object->ref) . '/';
  96. $default_font_size = pdf_getPDFFontSize($langs); // Must be after pdf_getInstance
  97. $default_font = pdf_getPDFFont($langs); // Must be after pdf_getInstance
  98. $langs->loadLangs(array("main", "companies"));
  99. $date = dol_print_date(dol_now(), "%Y%m%d%H%M%S");
  100. $filename = "signatures/" . $date . "_signature.png";
  101. if (!is_dir($upload_dir . "signatures/")) {
  102. if (!dol_mkdir($upload_dir . "signatures/")) {
  103. $response = "Error mkdir. Failed to create dir " . $upload_dir . "signatures/";
  104. $error++;
  105. }
  106. }
  107. if (!$error) {
  108. $return = file_put_contents($upload_dir . $filename, $data);
  109. if ($return == false) {
  110. $error++;
  111. $response = 'Error file_put_content: failed to create signature file.';
  112. }
  113. }
  114. if (!$error) {
  115. // Defined modele of doc
  116. $last_main_doc_file = $object->last_main_doc;
  117. $directdownloadlink = $object->getLastMainDocLink('proposal'); // url to download the $object->last_main_doc
  118. if (preg_match('/\.pdf/i', $last_main_doc_file)) {
  119. // TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
  120. $newpdffilename = $upload_dir . $ref . "_signed-" . $date . ".pdf";
  121. $sourcefile = $upload_dir . $ref . ".pdf";
  122. if (dol_is_file($sourcefile)) {
  123. // We build the new PDF
  124. $pdf = pdf_getInstance();
  125. if (class_exists('TCPDF')) {
  126. $pdf->setPrintHeader(false);
  127. $pdf->setPrintFooter(false);
  128. }
  129. $pdf->SetFont(pdf_getPDFFont($langs));
  130. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  131. $pdf->SetCompression(false);
  132. }
  133. //$pdf->Open();
  134. $pagecount = $pdf->setSourceFile($sourcefile); // original PDF
  135. $param = array();
  136. $param['online_sign_name'] = $online_sign_name;
  137. $param['pathtoimage'] = $upload_dir . $filename;
  138. $s = array(); // Array with size of each page. Exemple array(w'=>210, 'h'=>297);
  139. for ($i = 1; $i < ($pagecount + 1); $i++) {
  140. try {
  141. $tppl = $pdf->importPage($i);
  142. $s = $pdf->getTemplatesize($tppl);
  143. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  144. $pdf->useTemplate($tppl);
  145. if (getDolGlobalString("PROPAL_SIGNATURE_ON_ALL_PAGES")) {
  146. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  147. // TODO Get position of box from PDF template
  148. $param['xforimgstart'] = (empty($s['w']) ? 120 : round($s['w'] / 2) + 15);
  149. $param['yforimgstart'] = (empty($s['h']) ? 240 : $s['h'] - 60);
  150. $param['wforimg'] = $s['w'] - 20 - $param['xforimgstart'];
  151. dolPrintSignatureImage($pdf, $langs, $param);
  152. }
  153. } catch (Exception $e) {
  154. dol_syslog("Error when manipulating the PDF " . $sourcefile . " by onlineSign: " . $e->getMessage(), LOG_ERR);
  155. $response = $e->getMessage();
  156. $error++;
  157. }
  158. }
  159. if (!getDolGlobalString("PROPAL_SIGNATURE_ON_ALL_PAGES")) {
  160. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  161. // TODO Get position of box from PDF template
  162. $param['xforimgstart'] = (empty($s['w']) ? 120 : round($s['w'] / 2) + 15);
  163. $param['yforimgstart'] = (empty($s['h']) ? 240 : $s['h'] - 60);
  164. $param['wforimg'] = $s['w'] - 20 - $param['xforimgstart'];
  165. dolPrintSignatureImage($pdf, $langs, $param);
  166. }
  167. //$pdf->Close();
  168. $pdf->Output($newpdffilename, "F");
  169. // Index the new file and update the last_main_doc property of object.
  170. $object->indexFile($newpdffilename, 1);
  171. }
  172. } elseif (preg_match('/\.odt/i', $last_main_doc_file)) {
  173. // Adding signature on .ODT not yet supported
  174. // TODO
  175. } else {
  176. // Document format not supported to insert online signature.
  177. // We should just create an image file with the signature.
  178. }
  179. }
  180. if (!$error) {
  181. $db->begin();
  182. $online_sign_ip = getUserRemoteIP();
  183. $sql = "UPDATE " . MAIN_DB_PREFIX . "propal";
  184. $sql .= " SET fk_statut = " . ((int) $object::STATUS_SIGNED) . ", note_private = '" . $db->escape($object->note_private) . "',";
  185. $sql .= " date_signature = '" . $db->idate(dol_now()) . "',";
  186. $sql .= " online_sign_ip = '" . $db->escape($online_sign_ip) . "'";
  187. if ($online_sign_name) {
  188. $sql .= ", online_sign_name = '" . $db->escape($online_sign_name) . "'";
  189. }
  190. $sql .= " WHERE rowid = " . ((int) $object->id);
  191. dol_syslog(__FILE__, LOG_DEBUG);
  192. $resql = $db->query($sql);
  193. if (!$resql) {
  194. $error++;
  195. } else {
  196. $num = $db->affected_rows($resql);
  197. }
  198. if (!$error) {
  199. if (method_exists($object, 'call_trigger')) {
  200. //customer is not a user !?! so could we use same user as validation ?
  201. $user = new User($db);
  202. $user->fetch($object->user_validation_id);
  203. $object->context = array('closedfromonlinesignature' => 'closedfromonlinesignature');
  204. $result = $object->call_trigger('PROPAL_CLOSE_SIGNED', $user);
  205. if ($result < 0) {
  206. $error++;
  207. $response = "error in trigger " . $object->error;
  208. } else {
  209. $response = "success";
  210. }
  211. } else {
  212. $response = "success";
  213. }
  214. } else {
  215. $error++;
  216. $response = "error sql";
  217. }
  218. if (!$error) {
  219. $db->commit();
  220. $response = "success";
  221. setEventMessages("PropalSigned", null, 'warnings');
  222. } else {
  223. $db->rollback();
  224. }
  225. }
  226. } elseif ($mode == 'contract') {
  227. require_once DOL_DOCUMENT_ROOT . '/contrat/class/contrat.class.php';
  228. require_once DOL_DOCUMENT_ROOT . '/core/lib/pdf.lib.php';
  229. $object = new Contrat($db);
  230. $object->fetch(0, $ref);
  231. $upload_dir = !empty($conf->contrat->multidir_output[$object->entity]) ? $conf->contrat->multidir_output[$object->entity] : $conf->contrat->dir_output;
  232. $upload_dir .= '/' . dol_sanitizeFileName($object->ref) . '/';
  233. $date = dol_print_date(dol_now(), "%Y%m%d%H%M%S");
  234. $filename = "signatures/" . $date . "_signature.png";
  235. if (!is_dir($upload_dir . "signatures/")) {
  236. if (!dol_mkdir($upload_dir . "signatures/")) {
  237. $response = "Error mkdir. Failed to create dir " . $upload_dir . "signatures/";
  238. $error++;
  239. }
  240. }
  241. if (!$error) {
  242. $return = file_put_contents($upload_dir . $filename, $data);
  243. if ($return == false) {
  244. $error++;
  245. $response = 'Error file_put_content: failed to create signature file.';
  246. }
  247. }
  248. if (!$error) {
  249. // Defined modele of doc
  250. $last_main_doc_file = $object->last_main_doc;
  251. $directdownloadlink = $object->getLastMainDocLink('contrat'); // url to download the $object->last_main_doc
  252. if (preg_match('/\.pdf/i', $last_main_doc_file)) {
  253. // TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
  254. $newpdffilename = $upload_dir . $ref . "_signed-" . $date . ".pdf";
  255. $sourcefile = $upload_dir . $ref . ".pdf";
  256. if (dol_is_file($sourcefile)) {
  257. // We build the new PDF
  258. $pdf = pdf_getInstance();
  259. if (class_exists('TCPDF')) {
  260. $pdf->setPrintHeader(false);
  261. $pdf->setPrintFooter(false);
  262. }
  263. $pdf->SetFont(pdf_getPDFFont($langs));
  264. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  265. $pdf->SetCompression(false);
  266. }
  267. //$pdf->Open();
  268. $pagecount = $pdf->setSourceFile($sourcefile); // original PDF
  269. $param = array();
  270. $param['online_sign_name'] = $online_sign_name;
  271. $param['pathtoimage'] = $upload_dir . $filename;
  272. $s = array(); // Array with size of each page. Exemple array(w'=>210, 'h'=>297);
  273. for ($i = 1; $i < ($pagecount + 1); $i++) {
  274. try {
  275. $tppl = $pdf->importPage($i);
  276. $s = $pdf->getTemplatesize($tppl);
  277. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  278. $pdf->useTemplate($tppl);
  279. if (getDolGlobalString("CONTRACT_SIGNATURE_ON_ALL_PAGES")) {
  280. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  281. // TODO Get position of box from PDF template
  282. $param['xforimgstart'] = 10;
  283. $param['yforimgstart'] = (empty($s['h']) ? 240 : $s['h'] - 65);
  284. $param['wforimg'] = $s['w'] / 2 - $param['xforimgstart'];
  285. dolPrintSignatureImage($pdf, $langs, $param);
  286. }
  287. } catch (Exception $e) {
  288. dol_syslog("Error when manipulating some PDF by onlineSign: " . $e->getMessage(), LOG_ERR);
  289. $response = $e->getMessage();
  290. $error++;
  291. }
  292. }
  293. if (!getDolGlobalString("CONTRACT_SIGNATURE_ON_ALL_PAGES")) {
  294. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  295. // TODO Get position of box from PDF template
  296. $param['xforimgstart'] = 10;
  297. $param['yforimgstart'] = (empty($s['h']) ? 240 : $s['h'] - 65);
  298. $param['wforimg'] = $s['w'] / 2 - $param['xforimgstart'];
  299. dolPrintSignatureImage($pdf, $langs, $param);
  300. }
  301. //$pdf->Close();
  302. $pdf->Output($newpdffilename, "F");
  303. // Index the new file and update the last_main_doc property of object.
  304. $object->indexFile($newpdffilename, 1);
  305. }
  306. if (!$error) {
  307. $response = "success";
  308. }
  309. } elseif (preg_match('/\.odt/i', $last_main_doc_file)) {
  310. // Adding signature on .ODT not yet supported
  311. // TODO
  312. } else {
  313. // Document format not supported to insert online signature.
  314. // We should just create an image file with the signature.
  315. }
  316. }
  317. } elseif ($mode == 'fichinter') {
  318. require_once DOL_DOCUMENT_ROOT . '/fichinter/class/fichinter.class.php';
  319. require_once DOL_DOCUMENT_ROOT . '/core/lib/pdf.lib.php';
  320. $object = new Fichinter($db);
  321. $object->fetch(0, $ref);
  322. $upload_dir = !empty($conf->ficheinter->multidir_output[$object->entity]) ? $conf->ficheinter->multidir_output[$object->entity] : $conf->ficheinter->dir_output;
  323. $upload_dir .= '/'.dol_sanitizeFileName($object->ref).'/';
  324. $langs->loadLangs(array("main", "companies"));
  325. $default_font_size = pdf_getPDFFontSize($langs); // Must be after pdf_getInstance
  326. $default_font = pdf_getPDFFont($langs); // Must be
  327. $date = dol_print_date(dol_now(), "%Y%m%d%H%M%S");
  328. $filename = "signatures/" . $date . "_signature.png";
  329. if (!is_dir($upload_dir . "signatures/")) {
  330. if (!dol_mkdir($upload_dir . "signatures/")) {
  331. $response = "Error mkdir. Failed to create dir " . $upload_dir . "signatures/";
  332. $error++;
  333. }
  334. }
  335. if (!$error) {
  336. $return = file_put_contents($upload_dir . $filename, $data);
  337. if ($return == false) {
  338. $error++;
  339. $response = 'Error file_put_content: failed to create signature file.';
  340. }
  341. }
  342. if (!$error) {
  343. // Defined modele of doc
  344. $last_main_doc_file = $object->last_main_doc;
  345. $directdownloadlink = $object->getLastMainDocLink('fichinter'); // url to download the $object->last_main_doc
  346. if (preg_match('/\.pdf/i', $last_main_doc_file)) {
  347. // TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
  348. $newpdffilename = $upload_dir . $ref . "_signed-" . $date . ".pdf";
  349. $sourcefile = $upload_dir . $ref . ".pdf";
  350. if (dol_is_file($sourcefile)) {
  351. // We build the new PDF
  352. $pdf = pdf_getInstance();
  353. if (class_exists('TCPDF')) {
  354. $pdf->setPrintHeader(false);
  355. $pdf->setPrintFooter(false);
  356. }
  357. $pdf->SetFont(pdf_getPDFFont($langs));
  358. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  359. $pdf->SetCompression(false);
  360. }
  361. //$pdf->Open();
  362. $pagecount = $pdf->setSourceFile($sourcefile); // original PDF
  363. $param = array();
  364. $param['online_sign_name'] = $online_sign_name;
  365. $param['pathtoimage'] = $upload_dir . $filename;
  366. $s = array(); // Array with size of each page. Exemple array(w'=>210, 'h'=>297);
  367. for ($i = 1; $i < ($pagecount + 1); $i++) {
  368. try {
  369. $tppl = $pdf->importPage($i);
  370. $s = $pdf->getTemplatesize($tppl);
  371. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  372. $pdf->useTemplate($tppl);
  373. if (getDolGlobalString("FICHINTER_SIGNATURE_ON_ALL_PAGES")) {
  374. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  375. // TODO Get position of box from PDF template
  376. $param['xforimgstart'] = 111;
  377. $param['yforimgstart'] = (empty($s['h']) ? 250 : $s['h'] - 60);
  378. $param['wforimg'] = $s['w'] - ($param['xforimgstart'] + 16);
  379. dolPrintSignatureImage($pdf, $langs, $param);
  380. }
  381. } catch (Exception $e) {
  382. dol_syslog("Error when manipulating some PDF by onlineSign: " . $e->getMessage(), LOG_ERR);
  383. $response = $e->getMessage();
  384. $error++;
  385. }
  386. }
  387. if (!getDolGlobalString("FICHINTER_SIGNATURE_ON_ALL_PAGES")) {
  388. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  389. // TODO Get position of box from PDF template
  390. $param['xforimgstart'] = 111;
  391. $param['yforimgstart'] = (empty($s['h']) ? 250 : $s['h'] - 60);
  392. $param['wforimg'] = $s['w'] - ($param['xforimgstart'] + 16);
  393. dolPrintSignatureImage($pdf, $langs, $param);
  394. }
  395. //$pdf->Close();
  396. $pdf->Output($newpdffilename, "F");
  397. // Index the new file and update the last_main_doc property of object.
  398. $object->indexFile($newpdffilename, 1);
  399. }
  400. if (!$error) {
  401. $response = "success";
  402. }
  403. } elseif (preg_match('/\.odt/i', $last_main_doc_file)) {
  404. // Adding signature on .ODT not yet supported
  405. // TODO
  406. } else {
  407. // Document format not supported to insert online signature.
  408. // We should just create an image file with the signature.
  409. }
  410. }
  411. } elseif ($mode == "societe_rib") {
  412. $langs->load('withdrawals');
  413. require_once DOL_DOCUMENT_ROOT . '/societe/class/companybankaccount.class.php';
  414. require_once DOL_DOCUMENT_ROOT . '/core/lib/pdf.lib.php';
  415. $modelpath = "core/modules/bank/doc/";
  416. $object = new CompanyBankAccount($db);
  417. $object->fetch($ref);
  418. if (!empty($object->id)) {
  419. $object->fetch_thirdparty();
  420. $upload_dir = $conf->societe->multidir_output[$object->thirdparty->entity] . '/' . dol_sanitizeFileName($object->thirdparty->id) . '/';
  421. $default_font_size = pdf_getPDFFontSize($langs); // Must be after pdf_getInstance
  422. $default_font = pdf_getPDFFont($langs); // Must be after pdf_getInstance
  423. $langs->loadLangs(array("main", "companies"));
  424. $date = dol_print_date(dol_now(), "%Y%m%d%H%M%S");
  425. $filename = "signatures/" . $date . "_signature.png";
  426. if (!is_dir($upload_dir . "signatures/")) {
  427. if (!dol_mkdir($upload_dir . "signatures/")) {
  428. $response = "Error mkdir. Failed to create dir " . $upload_dir . "signatures/";
  429. $error++;
  430. }
  431. }
  432. if (!$error) {
  433. $return = file_put_contents($upload_dir . $filename, $data);
  434. if ($return == false) {
  435. $error++;
  436. $response = 'Error file_put_content: failed to create signature file.';
  437. }
  438. }
  439. if (!$error) {
  440. // Defined modele of doc
  441. $last_main_doc_file = $object->last_main_doc;
  442. $last_modelpdf = $object->model_pdf;
  443. $directdownloadlink = $object->getLastMainDocLink('company'); // url to download the $object->last_main_doc
  444. if (preg_match('/\.pdf/i', $last_main_doc_file)) {
  445. $sourcefile = '';
  446. $newpdffilename = '';
  447. if ($last_modelpdf == 'sepamandate') {
  448. $newpdffilename = $upload_dir . $langs->transnoentitiesnoconv("SepaMandateShort") . ' ' . dol_sanitizeFileName($object->ref) . "-" . dol_sanitizeFileName($object->rum) . "_signed-" . $date . ".pdf";
  449. $sourcefile = $upload_dir . $langs->transnoentitiesnoconv("SepaMandateShort") . ' ' . dol_sanitizeFileName($object->ref) . "-" . dol_sanitizeFileName($object->rum) . ".pdf";
  450. }
  451. if (dol_is_file($sourcefile)) {
  452. // We build the new PDF
  453. $pdf = pdf_getInstance();
  454. if (class_exists('TCPDF')) {
  455. $pdf->setPrintHeader(false);
  456. $pdf->setPrintFooter(false);
  457. }
  458. $pdf->SetFont(pdf_getPDFFont($langs));
  459. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  460. $pdf->SetCompression(false);
  461. }
  462. //$pdf->Open();
  463. $pagecount = $pdf->setSourceFile($sourcefile); // original PDF
  464. $s = array(); // Array with size of each page. Exemple array(w'=>210, 'h'=>297);
  465. for ($i = 1; $i < ($pagecount + 1); $i++) {
  466. try {
  467. $tppl = $pdf->importPage($i);
  468. $s = $pdf->getTemplatesize($tppl);
  469. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  470. $pdf->useTemplate($tppl);
  471. } catch (Exception $e) {
  472. dol_syslog("Error when manipulating the PDF " . $sourcefile . " by onlineSign: " . $e->getMessage(), LOG_ERR);
  473. $response = $e->getMessage();
  474. $error++;
  475. }
  476. }
  477. // Get position of box from PDF template
  478. $file = '';
  479. $classname = '';
  480. $filefound = '';
  481. $dirmodels = array('/');
  482. if (is_array($conf->modules_parts['models'])) {
  483. $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
  484. }
  485. foreach ($dirmodels as $reldir) {
  486. $file = "pdf_" . $last_modelpdf . ".modules.php";
  487. // On vérifie l'emplacement du modele
  488. $file = dol_buildpath($reldir . $modelpath . $file, 0);
  489. if (file_exists($file)) {
  490. $filefound = $file;
  491. $classname = 'pdf_' . $last_modelpdf;
  492. break;
  493. }
  494. if ($filefound) {
  495. break;
  496. }
  497. }
  498. if (!$filefound) {
  499. $response = $langs->trans("Error") . ' Failed to load doc generator with modelpaths=' . $modelpath . ' - modele=' . $last_modelpdf;
  500. dol_syslog($response, LOG_ERR);
  501. $error++;
  502. }
  503. if (!$error) {
  504. // If PDF template class was found
  505. require_once $file;
  506. $objPDF = new $classname($db);
  507. $pdf->SetFont($default_font, '', $default_font_size - 1);
  508. $xForDate = $objPDF->marge_gauche;
  509. $yForDate = $objPDF->page_hauteur - $objPDF->heightforinfotot - $objPDF->heightforfreetext - $objPDF->heightforfooter + 10;
  510. $pdf->SetXY($xForDate, $yForDate);
  511. $pdf->MultiCell(100, 4, dol_print_date(dol_now(), "daytext", false, $langs, true), 0, 'L');
  512. $xforimgstart = $objPDF->xPosSignArea;
  513. $yforimgstart = $yForDate - 5;
  514. $wforimg = $s['w'] - 20 - $xforimgstart;
  515. $param = array();
  516. $param['online_sign_name'] = $online_sign_name;
  517. $param['pathtoimage'] = $upload_dir . $filename;
  518. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  519. // TODO Get position of box from PDF template
  520. $param['xforimgstart'] = $xforimgstart;
  521. $param['yforimgstart'] = $yforimgstart;
  522. $param['wforimg'] = $wforimg;
  523. dolPrintSignatureImage($pdf, $langs, $param);
  524. }
  525. //$pdf->Close();
  526. $pdf->Output($newpdffilename, "F");
  527. // Index the new file and update the last_main_doc property of object.
  528. $object->indexFile($newpdffilename, 1);
  529. }
  530. } elseif (preg_match('/\.odt/i', $last_main_doc_file)) {
  531. // Adding signature on .ODT not yet supported
  532. // TODO
  533. } else {
  534. // Document format not supported to insert online signature.
  535. // We should just create an image file with the signature.
  536. }
  537. }
  538. } else {
  539. $error++;
  540. $response = "cannot find Rib";
  541. }
  542. if (!$error) {
  543. $db->begin();
  544. $online_sign_ip = getUserRemoteIP();
  545. $sql = "UPDATE " . MAIN_DB_PREFIX . $object->table_element;
  546. $sql .= " SET ";
  547. $sql .= " date_signature = '" . $db->idate(dol_now()) . "',";
  548. $sql .= " online_sign_ip = '" . $db->escape($online_sign_ip) . "'";
  549. if ($online_sign_name) {
  550. $sql .= ", online_sign_name = '" . $db->escape($online_sign_name) . "'";
  551. }
  552. //$sql .= ", last_main_doc = '" . $db->escape($object->element'..') . "'";
  553. $sql .= " WHERE rowid = " . ((int) $object->id);
  554. dol_syslog(__FILE__, LOG_DEBUG);
  555. $resql = $db->query($sql);
  556. if (!$resql) {
  557. $error++;
  558. } else {
  559. $num = $db->affected_rows($resql);
  560. }
  561. if (!$error) {
  562. $response = "success";
  563. } else {
  564. $error++;
  565. $response = "error sql";
  566. }
  567. if (!$error) {
  568. $db->commit();
  569. $response = "success";
  570. setEventMessages(dol_ucfirst($mode)."Signed", null, 'warnings');
  571. } else {
  572. $db->rollback();
  573. }
  574. }
  575. }
  576. } else {
  577. $error++;
  578. $response = 'error signature_not_found';
  579. }
  580. }
  581. if ($error) {
  582. http_response_code(501);
  583. }
  584. echo $response;
  585. /**
  586. * Output the signature file
  587. *
  588. * @param TCPDF $pdf PDF handler
  589. * @param Translate $langs Language
  590. * @param array $params Array of params
  591. * @return void
  592. */
  593. function dolPrintSignatureImage(TCPDF $pdf, $langs, $params)
  594. {
  595. $default_font_size = pdf_getPDFFontSize($langs); // Must be after pdf_getInstance
  596. $default_font = pdf_getPDFFont($langs); // Must be
  597. $xforimgstart = $params['xforimgstart'];
  598. $yforimgstart = $params['yforimgstart'];
  599. $wforimg = $params['wforimg'];
  600. $pdf->SetXY($xforimgstart, $yforimgstart + round($wforimg / 4) - 4);
  601. $pdf->SetFont($default_font, '', $default_font_size - 1);
  602. $pdf->SetTextColor(80, 80, 80);
  603. $pdf->MultiCell($wforimg, 4, $langs->trans("Signature") . ': ' . dol_print_date(dol_now(), "day", false, $langs, true). ' - '.$params['online_sign_name'], 0, 'L');
  604. //$pdf->SetXY($xforimgstart, $yforimgstart + round($wforimg / 4));
  605. //$pdf->MultiCell($wforimg, 4, $langs->trans("Lastname") . ': ' . $online_sign_name, 0, 'L');
  606. $pdf->Image($params['pathtoimage'], $xforimgstart, $yforimgstart, $wforimg, round($wforimg / 4));
  607. return;
  608. }