actions_setmoduleoptions.inc.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 (!empty($val['type']) && preg_match('/category:/', $val['type'])) {
  38. if (GETPOST($key, 'int') == '-1') {
  39. $val_const = '';
  40. } else {
  41. $val_const = GETPOST($key, 'int');
  42. }
  43. } else {
  44. $val_const = GETPOST($key, 'alpha');
  45. }
  46. $result = dolibarr_set_const($db, $key, $val_const, 'chaine', 0, '', $conf->entity);
  47. if ($result < 0) {
  48. $error++;
  49. break;
  50. }
  51. }
  52. }
  53. if (!$error) {
  54. $db->commit();
  55. if (empty($nomessageinupdate)) {
  56. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  57. }
  58. } else {
  59. $db->rollback();
  60. if (empty($nomessageinupdate)) {
  61. setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
  62. }
  63. }
  64. }
  65. if ($action == 'deletefile' && $modulepart == 'doctemplates' && !empty($user->admin)) {
  66. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  67. $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
  68. $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
  69. foreach ($listofdir as $key => $tmpdir) {
  70. $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
  71. if (!$tmpdir) {
  72. unset($listofdir[$key]);
  73. continue;
  74. }
  75. $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
  76. if (!is_dir($tmpdir)) {
  77. if (empty($nomessageinsetmoduleoptions)) {
  78. setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
  79. }
  80. } else {
  81. $upload_dir = $tmpdir;
  82. break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
  83. }
  84. }
  85. $filetodelete = $tmpdir.'/'.GETPOST('file');
  86. $result = dol_delete_file($filetodelete);
  87. if ($result > 0) {
  88. setEventMessages($langs->trans("FileWasRemoved", GETPOST('file')), null, 'mesgs');
  89. }
  90. }
  91. // Define constants for submodules that contains parameters (forms with param1, param2, ... and value1, value2, ...)
  92. if ($action == 'setModuleOptions' && !empty($user->admin)) {
  93. $db->begin();
  94. // Process common param fields
  95. if (is_array($_POST)) {
  96. foreach ($_POST as $key => $val) {
  97. $reg = array();
  98. if (preg_match('/^param(\d*)$/', $key, $reg)) { // Works for POST['param'], POST['param1'], POST['param2'], ...
  99. $param = GETPOST("param".$reg[1], 'alpha');
  100. $value = GETPOST("value".$reg[1], 'alpha');
  101. if ($param) {
  102. $res = dolibarr_set_const($db, $param, $value, 'chaine', 0, '', $conf->entity);
  103. if (!($res > 0)) {
  104. $error++;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. // Process upload fields
  111. if (GETPOST('upload', 'alpha') && GETPOST('keyforuploaddir', 'aZ09')) {
  112. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  113. $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
  114. $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
  115. foreach ($listofdir as $key => $tmpdir) {
  116. $tmpdir = trim($tmpdir);
  117. $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
  118. if (!$tmpdir) {
  119. unset($listofdir[$key]);
  120. continue;
  121. }
  122. $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
  123. if (!is_dir($tmpdir)) {
  124. if (empty($nomessageinsetmoduleoptions)) {
  125. setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
  126. }
  127. } else {
  128. $upload_dir = $tmpdir;
  129. break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
  130. }
  131. }
  132. if ($upload_dir) {
  133. $result = dol_add_file_process($upload_dir, 1, 1, 'uploadfile', '');
  134. if ($result <= 0) {
  135. $error++;
  136. }
  137. }
  138. }
  139. if (!$error) {
  140. $db->commit();
  141. if (empty($nomessageinsetmoduleoptions)) {
  142. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  143. }
  144. } else {
  145. $db->rollback();
  146. if (empty($nomessageinsetmoduleoptions)) {
  147. setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
  148. }
  149. }
  150. }