dolibarr 25.0.0-alpha
actions_setmoduleoptions.inc.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2014-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 * or see https://www.gnu.org/
19 */
20
26// $error must have been initialized to 0
27// $action must be defined
28// $arrayofparameters must be set to list of parameters to update for action 'update' on constants
29// $nomessageinupdate can be set to 1
30// $nomessageinsetmoduleoptions can be set to 1
31// $formSetup may be defined
32
50'
51@phan-var-force FormSetup $formSetup
52@phan-var-force string $action
53@phan-var-force int $error
54@phan-var-force ?string $modulepart
55@phan-var-force ?string $websitetemplateconf
56@phan-var-force ?string $upload_dir
57';
58
59if (($action == 'update' || !empty($websitetemplateconf)) && !empty($formSetup) && is_object($formSetup) && !empty($user->admin)) {
60 $formSetup->saveConfFromPost();
61 return;
62}
63
64if (($action == 'update' || !empty($websitetemplateconf)) && !empty($arrayofparameters) && is_array($arrayofparameters) && !empty($user->admin)) {
65 $db->begin();
66
67 foreach ($arrayofparameters as $key => $val) {
68 // Modify constant only if key was posted (avoid resetting key to the null value)
69 if (GETPOSTISSET($key)) {
70 if (isset($val['type']) && preg_match('/category:/', $val['type'])) {
71 if (GETPOSTINT($key) == '-1') {
72 $val_const = '';
73 } else {
74 $val_const = GETPOSTINT($key);
75 }
76 } elseif (isset($val['type']) && $val['type'] == 'html') {
77 $val_const = GETPOST($key, 'restricthtml');
78 } else {
79 $val_const = GETPOST($key, 'alpha');
80 }
81
82 $result = dolibarr_set_const($db, $key, $val_const, 'chaine', 0, '', $conf->entity);
83 if ($result < 0) {
84 $error++;
85 break;
86 }
87 }
88 }
89
90 if (!$error) {
91 $db->commit();
92 if (empty($nomessageinupdate)) {
93 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
94 }
95 } else {
96 $db->rollback();
97 if (empty($nomessageinupdate)) {
98 setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
99 }
100 }
101}
102
103if ($action == 'deletefile' && $modulepart == 'doctemplates' && !empty($user->admin)) {
104 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
105 $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
106 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
107
108 foreach ($listofdir as $key => $tmpdir) {
109 $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
110 if (!$tmpdir) {
111 unset($listofdir[$key]);
112 continue;
113 }
114 $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
115 if (!is_dir($tmpdir)) {
116 if (empty($nomessageinsetmoduleoptions)) {
117 setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
118 }
119 } else {
120 $upload_dir = $tmpdir;
121 break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
122 }
123 }
124
125 if ($upload_dir) {
126 $filetodelete = $upload_dir.'/'.GETPOST('file');
127 $result = dol_delete_file($filetodelete, 1);
128 if ($result > 0) {
129 setEventMessages($langs->trans("FileWasRemoved", GETPOST('file')), null, 'mesgs');
130 }
131 }
132}
133
134// Define constants for submodules that contains parameters (forms with param1, param2, ... and value1, value2, ...)
135if ($action == 'setModuleOptions' && !empty($user->admin)) {
136 $db->begin();
137
138 // Process common param fields
139 if (is_array($_POST)) {
140 foreach ($_POST as $key => $val) {
141 $reg = array();
142 if (preg_match('/^param(\d*)$/', $key, $reg)) { // Works for POST['param'], POST['param1'], POST['param2'], ...
143 $param = GETPOST("param".$reg[1], 'aZ09');
144 $value = GETPOST("value".$reg[1], 'alpha');
145 if ($param) {
146 $res = dolibarr_set_const($db, $param, $value, 'chaine', 0, '', $conf->entity);
147 if (!($res > 0)) {
148 $error++;
149 }
150
151 // Case we modify the list of directories for ODT templases, we want to be sure directory exists
152 if (preg_match('/_ADDON_PDF_ODT_PATH$/', $param) && preg_match('/^DOL_DATA_ROOT/', $value)) {
153 $tmpdir = preg_replace('/^DOL_DATA_ROOT/', DOL_DATA_ROOT, $value);
154 dol_mkdir($tmpdir, DOL_DATA_ROOT);
155 }
156 }
157 }
158 }
159 }
160
161 // Process upload fields
162 if (GETPOST('upload', 'alpha') && GETPOST('keyforuploaddir', 'aZ09')) {
163 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
164 $keyforuploaddir = GETPOST('keyforuploaddir', 'aZ09');
165 $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim(getDolGlobalString($keyforuploaddir))));
166
167 foreach ($listofdir as $key => $tmpdir) {
168 $tmpdir = trim($tmpdir);
169 $tmpdir = preg_replace('/DOL_DATA_ROOT\/*/', '', $tmpdir); // Clean string if we found a hardcoded DOL_DATA_ROOT
170 if (!$tmpdir) {
171 unset($listofdir[$key]);
172 continue;
173 }
174 $tmpdir = DOL_DATA_ROOT.'/'.$tmpdir; // Complete with DOL_DATA_ROOT. Only files into DOL_DATA_ROOT can be reach/set
175 if (!is_dir($tmpdir)) {
176 if (empty($nomessageinsetmoduleoptions)) {
177 setEventMessages($langs->trans("ErrorDirNotFound", $tmpdir), null, 'warnings');
178 }
179 } else {
180 $upload_dir = $tmpdir;
181 break; // So we take the first directory found into setup $conf->global->$keyforuploaddir
182 }
183 }
184
185 // Restrict uploads for ODT template setup pages.
186 if (preg_match('/_ADDON_PDF_ODT_PATH$/', $keyforuploaddir) && !empty($_FILES['uploadfile'])) {
187 $allowedext = array('odt', 'ods');
188 $allowedmimes = array(
189 'application/vnd.oasis.opendocument.text',
190 'application/vnd.oasis.opendocument.spreadsheet',
191 'application/zip',
192 'application/x-zip-compressed',
193 'application/octet-stream'
194 );
195 $files = $_FILES['uploadfile'];
196 if (!is_array($files['name'])) {
197 foreach ($files as $key => &$val) {
198 $val = array($val);
199 }
200 unset($val);
201 }
202 foreach ($files['name'] as $idx => $filename) {
203 if (empty($filename)) {
204 continue;
205 }
206
207 $extension = strtolower(pathinfo((string) $filename, PATHINFO_EXTENSION));
208 if (!in_array($extension, $allowedext, true)) {
209 $error++;
210 setEventMessages($langs->trans("ErrorFileNotUploaded").' (Only .odt/.ods templates are allowed)', null, 'errors');
211 dol_syslog(__FILE__." ODT template upload refused on extension filename=".$filename, LOG_WARNING);
212 break;
213 }
214
215 $detectedmime = '';
216 if (!empty($files['tmp_name'][$idx]) && function_exists('finfo_open')) {
217 $finfo = finfo_open(FILEINFO_MIME_TYPE);
218 if ($finfo) {
219 $detectedmime = (string) finfo_file($finfo, $files['tmp_name'][$idx]);
220 finfo_close($finfo);
221 }
222 }
223 if (!empty($detectedmime) && !in_array(strtolower($detectedmime), $allowedmimes, true)) {
224 $error++;
225 setEventMessages($langs->trans("ErrorFileNotUploaded").' (Invalid MIME type for ODT/ODS template)', null, 'errors');
226 dol_syslog(__FILE__." ODT template upload refused on mime filename=".$filename." mime=".$detectedmime, LOG_WARNING);
227 break;
228 }
229 }
230 }
231
232 if ($upload_dir && !$error) {
233 $result = dol_add_file_process($upload_dir, 1, 1, 'uploadfile', '');
234 if ($result <= 0) {
235 $error++;
236 }
237 }
238 }
239
240 if (!$error) {
241 $db->commit();
242 if (empty($nomessageinsetmoduleoptions)) {
243 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
244 }
245 } else {
246 $db->rollback();
247 if (empty($nomessageinsetmoduleoptions)) {
248 setEventMessages($langs->trans("SetupNotSaved"), null, 'errors');
249 }
250 }
251}
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_add_file_process($upload_dir, $allowoverwrite=0, $updatesessionordb=0, $keyforsourcefile='addedfile', $savingdocmask='', $link=null, $trackid='', $generatethumbs=1, $object=null, $forceFullTextIndexation='', $mode=0)
Get and save an upload file (for example after submitting a new file in a mail form).
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string