actions_setmoduleoptions.inc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /* Copyright (C) 2014-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/actions_setmoduleoptions.inc.php
  20. * \brief Code for actions on setting notes of object page
  21. */
  22. // $error must have been initialized to 0
  23. // $action must be defined
  24. // $arrayofparameters must be set for action 'update'
  25. // $nomessageinupdate can be set to 1
  26. // $nomessageinsetmoduleoptions can be set to 1
  27. // $formSetup may be defined
  28. if ($action == 'update' && !empty($formSetup) && is_object($formSetup) && !empty($user->admin)) {
  29. $formSetup->saveConfFromPost();
  30. return;
  31. }
  32. if ($action == 'update' && is_array($arrayofparameters) && !empty($user->admin)) {
  33. $db->begin();
  34. foreach ($arrayofparameters as $key => $val) {
  35. // Modify constant only if key was posted (avoid resetting key to the null value)
  36. if (GETPOSTISSET($key)) {
  37. if (preg_match('/category:/', $val['type'])) {
  38. if (GETPOST($key, 'int') == '-1') {
  39. $val_const = '';
  40. } else {
  41. $val_const = GETPOST($key, 'int');
  42. }
  43. // Added by MMI Mathieu Moulin iProspective
  44. // Action:setmoduleoption:update Permit html data in module parameter
  45. } elseif ($val['type']=='html') {
  46. $val_const = GETPOST($key, 'html');
  47. } else {
  48. $val_const = GETPOST($key, 'alpha');
  49. }
  50. $result = dolibarr_set_const($db, $key, $val_const, 'chaine', 0, '', $conf->entity);
  51. if ($result < 0) {
  52. $error++;
  53. break;
  54. }
  55. }
  56. }
  57. if (!$error) {
  58. $db->commit();
  59. if (empty($nomessageinupdate)) {
  60. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  61. }
  62. } else {
  63. $db->rollback();
  64. if (empty($nomessageinupdate)) {
  65. setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
  66. }
  67. }
  68. }
  69. if ($action == 'deletefile' && $modulepart == 'doctemplates' && !empty($user->admin)) {
  70. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  71. $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
  72. $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
  73. foreach ($listofdir as $key => $tmpdir) {
  74. $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
  75. if (!$tmpdir) {
  76. unset($listofdir[$key]);
  77. continue;
  78. }
  79. $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
  80. if (!is_dir($tmpdir)) {
  81. if (empty($nomessageinsetmoduleoptions)) {
  82. setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
  83. }
  84. } else {
  85. $upload_dir = $tmpdir;
  86. break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
  87. }
  88. }
  89. $filetodelete = $tmpdir.'/'.GETPOST('file');
  90. $result = dol_delete_file($filetodelete);
  91. if ($result > 0) {
  92. setEventMessages($langs->trans("FileWasRemoved", GETPOST('file')), null, 'mesgs');
  93. }
  94. }
  95. // Define constants for submodules that contains parameters (forms with param1, param2, ... and value1, value2, ...)
  96. if ($action == 'setModuleOptions' && !empty($user->admin)) {
  97. $db->begin();
  98. // Process common param fields
  99. if (is_array($_POST)) {
  100. foreach ($_POST as $key => $val) {
  101. $reg = array();
  102. if (preg_match('/^param(\d*)$/', $key, $reg)) { // Works for POST['param'], POST['param1'], POST['param2'], ...
  103. $param = GETPOST("param".$reg[1], 'alpha');
  104. $value = GETPOST("value".$reg[1], 'alpha');
  105. if ($param) {
  106. $res = dolibarr_set_const($db, $param, $value, 'chaine', 0, '', $conf->entity);
  107. if (!($res > 0)) {
  108. $error++;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. // Process upload fields
  115. if (GETPOST('upload', 'alpha') && GETPOST('keyforuploaddir', 'aZ09')) {
  116. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  117. $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
  118. $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
  119. foreach ($listofdir as $key => $tmpdir) {
  120. $tmpdir = trim($tmpdir);
  121. $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
  122. if (!$tmpdir) {
  123. unset($listofdir[$key]);
  124. continue;
  125. }
  126. $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
  127. if (!is_dir($tmpdir)) {
  128. if (empty($nomessageinsetmoduleoptions)) {
  129. setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
  130. }
  131. } else {
  132. $upload_dir = $tmpdir;
  133. break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
  134. }
  135. }
  136. if ($upload_dir) {
  137. $result = dol_add_file_process($upload_dir, 1, 1, 'uploadfile', '');
  138. if ($result <= 0) {
  139. $error++;
  140. }
  141. }
  142. }
  143. if (!$error) {
  144. $db->commit();
  145. if (empty($nomessageinsetmoduleoptions)) {
  146. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  147. }
  148. } else {
  149. $db->rollback();
  150. if (empty($nomessageinsetmoduleoptions)) {
  151. setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
  152. }
  153. }
  154. }