onlineSign.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. $error = 0;
  59. $response = "";
  60. $type = $mode;
  61. // Check securitykey
  62. $securekeyseed = '';
  63. if ($type == 'proposal') {
  64. $securekeyseed = getDolGlobalString('PROPOSAL_ONLINE_SIGNATURE_SECURITY_TOKEN');
  65. }
  66. if (empty($SECUREKEY) || !dol_verifyHash($securekeyseed.$type.$ref.(!isModEnabled('multicompany') ? '' : $entity), $SECUREKEY, '0')) {
  67. httponly_accessforbidden('Bad value for securitykey. Value provided '.dol_escape_htmltag($SECUREKEY).' does not match expected value for ref='.dol_escape_htmltag($ref), 403);
  68. }
  69. /*
  70. * Actions
  71. */
  72. // None
  73. /*
  74. * View
  75. */
  76. top_httphead();
  77. if ($action == "importSignature") {
  78. $issignatureok = (!empty($signature) && $signature[0] == "image/png;base64");
  79. if ($issignatureok) {
  80. $signature = $signature[1];
  81. $data = base64_decode($signature);
  82. if ($mode == "propale" || $mode == 'proposal') {
  83. require_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
  84. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  85. $object = new Propal($db);
  86. $object->fetch(0, $ref);
  87. $upload_dir = !empty($conf->propal->multidir_output[$object->entity])?$conf->propal->multidir_output[$object->entity]:$conf->propal->dir_output;
  88. $upload_dir .= '/'.dol_sanitizeFileName($object->ref).'/';
  89. $date = dol_print_date(dol_now(), "%Y%m%d%H%M%S");
  90. $filename = "signatures/".$date."_signature.png";
  91. if (!is_dir($upload_dir."signatures/")) {
  92. if (!dol_mkdir($upload_dir."signatures/")) {
  93. $response ="Error mkdir. Failed to create dir ".$upload_dir."signatures/";
  94. $error++;
  95. }
  96. }
  97. if (!$error) {
  98. $return = file_put_contents($upload_dir.$filename, $data);
  99. if ($return == false) {
  100. $error++;
  101. $response = 'Error file_put_content: failed to create signature file.';
  102. }
  103. }
  104. if (!$error) {
  105. // Defined modele of doc
  106. $last_main_doc_file = $object->last_main_doc;
  107. $directdownloadlink = $object->getLastMainDocLink('proposal'); // url to download the $object->last_main_doc
  108. if (preg_match('/\.pdf/i', $last_main_doc_file)) {
  109. // TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
  110. $newpdffilename = $upload_dir.$ref."_signed-".$date.".pdf";
  111. $sourcefile = $upload_dir.$ref.".pdf";
  112. if (dol_is_file($sourcefile)) {
  113. // We build the new PDF
  114. $pdf = pdf_getInstance();
  115. if (class_exists('TCPDF')) {
  116. $pdf->setPrintHeader(false);
  117. $pdf->setPrintFooter(false);
  118. }
  119. $pdf->SetFont(pdf_getPDFFont($langs));
  120. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  121. $pdf->SetCompression(false);
  122. }
  123. //$pdf->Open();
  124. $pagecount = $pdf->setSourceFile($sourcefile); // original PDF
  125. $s = array(); // Array with size of each page. Exemple array(w'=>210, 'h'=>297);
  126. for ($i=1; $i<($pagecount+1); $i++) {
  127. try {
  128. $tppl = $pdf->importPage($i);
  129. $s = $pdf->getTemplatesize($tppl);
  130. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  131. $pdf->useTemplate($tppl);
  132. } catch (Exception $e) {
  133. dol_syslog("Error when manipulating the PDF ".$sourcefile." by onlineSign: ".$e->getMessage(), LOG_ERR);
  134. $response = $e->getMessage();
  135. $error++;
  136. }
  137. }
  138. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  139. // TODO Get position of box from PDF template
  140. $xforimgstart = (empty($s['w']) ? 120 : round($s['w'] / 2) + 15);
  141. $yforimgstart = (empty($s['h']) ? 240 : $s['h'] - 60);
  142. $wforimg = $s['w'] - 20 - $xforimgstart;
  143. $pdf->Image($upload_dir.$filename, $xforimgstart, $yforimgstart, $wforimg, round($wforimg / 4));
  144. //$pdf->Close();
  145. $pdf->Output($newpdffilename, "F");
  146. // Index the new file and update the last_main_doc property of object.
  147. $object->indexFile($newpdffilename, 1);
  148. }
  149. } elseif (preg_match('/\.odt/i', $last_main_doc_file)) {
  150. // Adding signature on .ODT not yet supported
  151. // TODO
  152. } else {
  153. // Document format not supported to insert online signature.
  154. // We should just create an image file with the signature.
  155. }
  156. }
  157. if (!$error) {
  158. $db->begin();
  159. $online_sign_ip = getUserRemoteIP();
  160. $online_sign_name = ''; // TODO Ask name on form to sign
  161. $sql = "UPDATE ".MAIN_DB_PREFIX."propal";
  162. $sql .= " SET fk_statut = ".((int) $object::STATUS_SIGNED).", note_private = '".$db->escape($object->note_private)."',";
  163. $sql .= " date_signature = '".$db->idate(dol_now())."',";
  164. $sql .= " online_sign_ip = '".$db->escape($online_sign_ip)."'";
  165. if ($online_sign_name) {
  166. $sql .= ", online_sign_name = '".$db->escape($online_sign_name)."'";
  167. }
  168. $sql .= " WHERE rowid = ".((int) $object->id);
  169. dol_syslog(__METHOD__, LOG_DEBUG);
  170. $resql = $db->query($sql);
  171. if (!$resql) {
  172. $error++;
  173. } else {
  174. $num = $db->affected_rows($resql);
  175. }
  176. if (!$error) {
  177. if (method_exists($object, 'call_trigger')) {
  178. //customer is not a user !?! so could we use same user as validation ?
  179. $user = new User($db);
  180. $user->fetch($object->user_valid_id);
  181. $object->context = array('closedfromonlinesignature' => 'closedfromonlinesignature');
  182. $result = $object->call_trigger('PROPAL_CLOSE_SIGNED', $user);
  183. if ($result < 0) {
  184. $error++;
  185. $response = "error in trigger ".$object->error;
  186. } else {
  187. $response = "success";
  188. }
  189. } else {
  190. $response = "success";
  191. }
  192. } else {
  193. $error++;
  194. $response = "error sql";
  195. }
  196. if (!$error) {
  197. $db->commit();
  198. $response = "success";
  199. setEventMessages("PropalSigned", null, 'warnings');
  200. } else {
  201. $db->rollback();
  202. }
  203. }
  204. } elseif ($mode == 'contract') {
  205. require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
  206. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  207. $object = new Contrat($db);
  208. $object->fetch(0, $ref);
  209. $upload_dir = !empty($conf->contrat->multidir_output[$object->entity])?$conf->contrat->multidir_output[$object->entity]:$conf->contrat->dir_output;
  210. $upload_dir .= '/'.dol_sanitizeFileName($object->ref).'/';
  211. $date = dol_print_date(dol_now(), "%Y%m%d%H%M%S");
  212. $filename = "signatures/".$date."_signature.png";
  213. if (!is_dir($upload_dir."signatures/")) {
  214. if (!dol_mkdir($upload_dir."signatures/")) {
  215. $response ="Error mkdir. Failed to create dir ".$upload_dir."signatures/";
  216. $error++;
  217. }
  218. }
  219. if (!$error) {
  220. $return = file_put_contents($upload_dir.$filename, $data);
  221. if ($return == false) {
  222. $error++;
  223. $response = 'Error file_put_content: failed to create signature file.';
  224. }
  225. }
  226. if (!$error) {
  227. // Defined modele of doc
  228. $last_main_doc_file = $object->last_main_doc;
  229. $directdownloadlink = $object->getLastMainDocLink('contrat'); // url to download the $object->last_main_doc
  230. if (preg_match('/\.pdf/i', $last_main_doc_file)) {
  231. // TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
  232. $newpdffilename = $upload_dir.$ref."_signed-".$date.".pdf";
  233. $sourcefile = $upload_dir.$ref.".pdf";
  234. if (dol_is_file($sourcefile)) {
  235. // We build the new PDF
  236. $pdf = pdf_getInstance();
  237. if (class_exists('TCPDF')) {
  238. $pdf->setPrintHeader(false);
  239. $pdf->setPrintFooter(false);
  240. }
  241. $pdf->SetFont(pdf_getPDFFont($langs));
  242. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  243. $pdf->SetCompression(false);
  244. }
  245. //$pdf->Open();
  246. $pagecount = $pdf->setSourceFile($sourcefile); // original PDF
  247. $s = array(); // Array with size of each page. Exemple array(w'=>210, 'h'=>297);
  248. for ($i=1; $i<($pagecount+1); $i++) {
  249. try {
  250. $tppl = $pdf->importPage($i);
  251. $s = $pdf->getTemplatesize($tppl);
  252. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  253. $pdf->useTemplate($tppl);
  254. } catch (Exception $e) {
  255. dol_syslog("Error when manipulating some PDF by onlineSign: ".$e->getMessage(), LOG_ERR);
  256. $response = $e->getMessage();
  257. $error++;
  258. }
  259. }
  260. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  261. // TODO Get position of box from PDF template
  262. $xforimgstart = 5;
  263. $yforimgstart = (empty($s['h']) ? 240 : $s['h'] - 65);
  264. $wforimg = $s['w']/2 - $xforimgstart;
  265. $pdf->Image($upload_dir.$filename, $xforimgstart, $yforimgstart, $wforimg, round($wforimg / 4));
  266. //$pdf->Close();
  267. $pdf->Output($newpdffilename, "F");
  268. // Index the new file and update the last_main_doc property of object.
  269. $object->indexFile($newpdffilename, 1);
  270. }
  271. if (!$error) {
  272. $response = "success";
  273. }
  274. } elseif (preg_match('/\.odt/i', $last_main_doc_file)) {
  275. // Adding signature on .ODT not yet supported
  276. // TODO
  277. } else {
  278. // Document format not supported to insert online signature.
  279. // We should just create an image file with the signature.
  280. }
  281. }
  282. } elseif ($mode == 'fichinter') {
  283. require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
  284. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  285. $object = new Fichinter($db);
  286. $object->fetch(0, $ref);
  287. $upload_dir = !empty($conf->ficheinter->multidir_output[$object->entity])?$conf->ficheinter->multidir_output[$object->entity]:$conf->ficheinter->dir_output;
  288. $upload_dir .= '/'.dol_sanitizeFileName($object->ref).'/';
  289. $date = dol_print_date(dol_now(), "%Y%m%d%H%M%S");
  290. $filename = "signatures/".$date."_signature.png";
  291. if (!is_dir($upload_dir."signatures/")) {
  292. if (!dol_mkdir($upload_dir."signatures/")) {
  293. $response ="Error mkdir. Failed to create dir ".$upload_dir."signatures/";
  294. $error++;
  295. }
  296. }
  297. if (!$error) {
  298. $return = file_put_contents($upload_dir.$filename, $data);
  299. if ($return == false) {
  300. $error++;
  301. $response = 'Error file_put_content: failed to create signature file.';
  302. }
  303. }
  304. if (!$error) {
  305. // Defined modele of doc
  306. $last_main_doc_file = $object->last_main_doc;
  307. $directdownloadlink = $object->getLastMainDocLink('fichinter'); // url to download the $object->last_main_doc
  308. if (preg_match('/\.pdf/i', $last_main_doc_file)) {
  309. // TODO Use the $last_main_doc_file to defined the $newpdffilename and $sourcefile
  310. $newpdffilename = $upload_dir.$ref."_signed-".$date.".pdf";
  311. $sourcefile = $upload_dir.$ref.".pdf";
  312. if (dol_is_file($sourcefile)) {
  313. // We build the new PDF
  314. $pdf = pdf_getInstance();
  315. if (class_exists('TCPDF')) {
  316. $pdf->setPrintHeader(false);
  317. $pdf->setPrintFooter(false);
  318. }
  319. $pdf->SetFont(pdf_getPDFFont($langs));
  320. if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
  321. $pdf->SetCompression(false);
  322. }
  323. //$pdf->Open();
  324. $pagecount = $pdf->setSourceFile($sourcefile); // original PDF
  325. $s = array(); // Array with size of each page. Exemple array(w'=>210, 'h'=>297);
  326. for ($i=1; $i<($pagecount+1); $i++) {
  327. try {
  328. $tppl = $pdf->importPage($i);
  329. $s = $pdf->getTemplatesize($tppl);
  330. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  331. $pdf->useTemplate($tppl);
  332. } catch (Exception $e) {
  333. dol_syslog("Error when manipulating some PDF by onlineSign: ".$e->getMessage(), LOG_ERR);
  334. $response = $e->getMessage();
  335. $error++;
  336. }
  337. }
  338. // A signature image file is 720 x 180 (ratio 1/4) but we use only the size into PDF
  339. // TODO Get position of box from PDF template
  340. $xforimgstart = 105;
  341. $yforimgstart = (empty($s['h']) ? 250 : $s['h'] - 57);
  342. $wforimg = $s['w']/1 - ($xforimgstart + 16);
  343. $pdf->Image($upload_dir.$filename, $xforimgstart, $yforimgstart, $wforimg, round($wforimg / 4));
  344. //$pdf->Close();
  345. $pdf->Output($newpdffilename, "F");
  346. // Index the new file and update the last_main_doc property of object.
  347. $object->indexFile($newpdffilename, 1);
  348. }
  349. if (!$error) {
  350. $response = "success";
  351. }
  352. } elseif (preg_match('/\.odt/i', $last_main_doc_file)) {
  353. // Adding signature on .ODT not yet supported
  354. // TODO
  355. } else {
  356. // Document format not supported to insert online signature.
  357. // We should just create an image file with the signature.
  358. }
  359. }
  360. }
  361. } else {
  362. $error++;
  363. $response = 'error signature_not_found';
  364. }
  365. }
  366. if ($error) {
  367. http_response_code(501);
  368. }
  369. echo $response;