dolibarr 25.0.0-alpha
security_file.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2005-2017 Regis Houssin <regis.houssin@inodbox.com>
4 * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
5 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28// Load Dolibarr environment
29require '../main.inc.php';
39require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
40require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
41require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
42
43// Load translation files required by the page
44$langs->loadLangs(array('users', 'admin', 'other'));
45
46$action = GETPOST('action', 'aZ09');
47$sortfield = GETPOST('sortfield', 'aZ09');
48$sortorder = GETPOST('sortorder', 'aZ09');
49if (empty($sortfield)) {
50 $sortfield = 'date';
51}
52if (empty($sortorder)) {
53 $sortorder = 'desc';
54}
55
56$upload_dir = $conf->admin->dir_temp;
57
58if (!$user->admin) {
60}
61
62$error = 0;
63
64
65/*
66 * Actions
67 */
68
69if (GETPOST('sendit') && getDolGlobalString('MAIN_UPLOAD_DOC')) {
70 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
71
72 dol_add_file_process($upload_dir, 1, 0, 'userfile');
73}
74
75if ($action == 'updateform') {
76 $antivircommand = GETPOST('MAIN_ANTIVIRUS_COMMAND', 'restricthtml'); // Use GETPOST restricthtml because we must accept ". Example c:\Progra~1\ClamWin\bin\clamscan.exe
77 $antivirparam = GETPOST('MAIN_ANTIVIRUS_PARAM', 'restricthtml'); // Use GETPOST restricthtml because we must accept ". Example --database="C:\Program Files (x86)\ClamWin\lib"
78 $antivircommand = dol_string_nospecial($antivircommand, '', array("|", ";", "<", ">", "&", "+")); // Sanitize command
79 $antivirparam = dol_string_nospecial($antivirparam, '', array("|", ";", "<", ">", "&", "+")); // Sanitize params
80
81 if ($antivircommand && !empty($dolibarr_main_restrict_os_commands)) {
82 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
83 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
84 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$antivircommand);
85 $basenamecmddump = basename(str_replace('\\', '/', $antivircommand));
86 if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
87 $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
88 setEventMessages($errormsg, null, 'errors');
89 $error++;
90 }
91 }
92
93 if (!$error) {
94 $tmpumask = GETPOST('MAIN_UMASK', 'alpha');
95 $tmpumask = (octdec($tmpumask) & 0666);
96 $tmpumask = decoct($tmpumask);
97 if (!preg_match('/^0/', $tmpumask)) {
98 $tmpumask = '0'.$tmpumask;
99 }
100 if (empty($tmpumask)) { // Also matches '0'
101 $tmpumask = '0664';
102 }
103
104 $res3 = dolibarr_set_const($db, 'MAIN_UPLOAD_DOC', GETPOST('MAIN_UPLOAD_DOC', 'alpha'), 'chaine', 0, '', $conf->entity);
105 $res4 = dolibarr_set_const($db, "MAIN_UMASK", $tmpumask, 'chaine', 0, '', $conf->entity);
106 $res5 = dolibarr_set_const($db, "MAIN_ANTIVIRUS_COMMAND", trim($antivircommand), 'chaine', 0, '', $conf->entity);
107 $res6 = dolibarr_set_const($db, "MAIN_ANTIVIRUS_PARAM", trim($antivirparam), 'chaine', 0, '', $conf->entity);
108 $res7 = dolibarr_set_const($db, "MAIN_FILE_EXTENSION_UPLOAD_RESTRICTION", GETPOST('MAIN_FILE_EXTENSION_UPLOAD_RESTRICTION', 'alpha'), 'chaine', 0, '', $conf->entity);
109
110 $res8 = dolibarr_set_const($db, "MAIN_SECURITY_MAXFILESIZE_DOWNLOADED", GETPOST('MAIN_SECURITY_MAXFILESIZE_DOWNLOADED', 'alpha'), 'chaine', 0, '', $conf->entity);
111
112 if ($res3 && $res4 && $res5 && $res6 && $res7 && $res8) {
113 setEventMessages($langs->trans("RecordModifiedSuccessfully"), null, 'mesgs');
114 }
115 }
116} elseif ($action == 'deletefile') {
117 // Delete file
118 $langs->load("other");
119 $file = $conf->admin->dir_temp.'/'.GETPOST('urlfile', 'alpha');
120 $ret = dol_delete_file($file, 1);
121 if ($ret) {
122 setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile', 'alpha')), null, 'mesgs');
123 } else {
124 setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
125 }
126}
127
128
129/*
130 * View
131 */
132
133$form = new Form($db);
134
135$wikihelp = 'EN:Setup_Security|FR:Paramétrage_Sécurité|ES:Configuración_Seguridad';
136llxHeader('', $langs->trans("Files"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-security_file');
137
138print load_fiche_titre($langs->trans("SecuritySetup"), '', 'title_setup');
139
140print '<span class="opacitymedium">'.$langs->trans("SecurityFilesDesc")."</span><br>\n";
141print "<br>\n";
142
143
144print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
145print '<input type="hidden" name="token" value="'.newToken().'">';
146print '<input type="hidden" name="action" value="updateform">';
147
148$head = security_prepare_head();
149
150print dol_get_fiche_head($head, 'file', '', -1);
151
152print '<br>';
153
154// Download options
155
156print '<div class="div-table-responsive-no-min">';
157print '<table class="noborder centpercent nomarginbottom">';
158print '<tr class="liste_titre">';
159print '<td>'.img_picto('', 'download', 'class="pictofixedwidth"').$langs->trans("Download").'</td>';
160print '<td></td>';
161print '</tr>';
162
163print '<tr class="oddeven">';
164print '<td>'.$langs->trans("MAIN_SECURITY_MAXFILESIZE_DOWNLOADED").'<br>';
165//print '<span class="opacitymedium">'.$langs->trans("MAIN_SECURITY_MAXFILESIZE_DOWNLOADED").'</span>';
166print '</td>';
167print '<td>';
168print '<input type="text" name="MAIN_SECURITY_MAXFILESIZE_DOWNLOADED" class="width100 right" spellcheck="false" value="'.getDolGlobalString('MAIN_SECURITY_MAXFILESIZE_DOWNLOADED').'"> '.$langs->trans("Kb");
169print "</td>";
170print '</tr>';
171
172print '</table>';
173print '</div>';
174
175
176print '<br>';
177print '<br>';
178
179
180// Upload options
181
182print '<div class="div-table-responsive-no-min">';
183print '<table class="noborder centpercent nomarginbottom">';
184print '<tr class="liste_titre">';
185print '<td>'.img_picto('', 'upload', 'class="pictofixedwidth"').$langs->trans("UploadName").'</td>';
186print '<td></td>';
187print '</tr>';
188
189print '<tr class="oddeven">';
190print '<td>'.$langs->trans("MaxSizeForUploadedFiles").'.';
191$max = @ini_get('upload_max_filesize');
192if (!empty($max)) {
193 print '<br><span class="opacitymedium">'.$langs->trans("MustBeLowerThanPHPLimit", ((int) $max) * 1024, $langs->trans("Kb")).'.</span>';
194} else {
195 print ' '.$langs->trans("NoMaxSizeByPHPLimit").'.';
196}
197print '</td>';
198print '<td class="nowrap">';
199print '<input class="flat width75 right" name="MAIN_UPLOAD_DOC" type="text" spellcheck="false" value="'.dol_escape_htmltag(getDolGlobalString('MAIN_UPLOAD_DOC')).'"> '.$langs->trans("Kb");
200print '</td>';
201print '</tr>';
202
203print '<tr class="oddeven">';
204print '<td>';
205print $form->textwithpicto($langs->trans("UMask"), $langs->trans("UMaskExplanation"));
206print '</td>';
207print '<td class="nowrap">';
208print '<input class="flat width75 right" name="MAIN_UMASK" type="text" spellcheck="false" value="'.dol_escape_htmltag(getDolGlobalString('MAIN_UMASK')).'">';
209print '</td>';
210print '</tr>';
211
212print '<tr class="oddeven">';
213print '<td>'.$langs->trans("UploadExtensionRestriction").'<br>';
214print '<span class="opacitymedium">'.$langs->trans("UploadExtensionRestrictionExemple").'</span>';
215print '</td>';
216print '<td>';
217print '<input type="text" name="MAIN_FILE_EXTENSION_UPLOAD_RESTRICTION" class="minwidth500imp" spellcheck="false" value="'.getDolGlobalString('MAIN_FILE_EXTENSION_UPLOAD_RESTRICTION', implode(',', getExecutableContent())).'">';
218print "</td>";
219print '</tr>';
220
221
222// Use anti virus
223
224
225// Enable advanced perms
226print '<tr class="oddeven">';
227print '<td>'.$langs->trans("UseAntivirusOnUploadedFile").'</td>';
228print '<td class="">';
229if (defined('MAIN_ANTIVIRUS_UPLOAD_ON') && constant('MAIN_ANTIVIRUS_UPLOAD_ON')) {
230 print img_picto($langs->trans("Enabled")." - Can't be disabled (PHP constant MAIN_ANTIVIRUS_UPLOAD_ON is set)", 'switch_on', '', 0, 0, 0, '', 'opacitymedium');
231} else {
232 if (!empty($conf->use_javascript_ajax)) {
233 print ajax_constantonoff('MAIN_ANTIVIRUS_UPLOAD_ON', array(), null, 0, 0, 1);
234 } else {
235 if (!getDolGlobalString('MAIN_ANTIVIRUS_UPLOAD_ON')) {
236 print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_MAIN_ANTIVIRUS_UPLOAD_ON&token='.newToken().'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
237 } else {
238 print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_MAIN_ANTIVIRUS_UPLOAD_ON&token='.newToken().'">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
239 }
240 }
241}
242print "</td></tr>";
243
244if (getDolGlobalString('MAIN_ANTIVIRUS_UPLOAD_ON')) {
245 print '<tr class="oddeven">';
246 print '<td><span class="fieldrequired">'.$langs->trans("AntiVirusCommand").'</span><br>';
247 print '<span class="opacitymedium">'.$langs->trans("AntiVirusCommandExample").'</span>';
248 print '</td>';
249 print '<td>';
250 // Check that command is inside safe_mode
251 if (ini_get('safe_mode') && getDolGlobalString('MAIN_ANTIVIRUS_COMMAND')) {
252 $langs->load("errors");
253 $basedir = preg_replace('/"/', '', dirname($conf->global->MAIN_ANTIVIRUS_COMMAND));
254 $listdir = explode(';', ini_get('safe_mode_exec_dir'));
255 if (!in_array($basedir, $listdir)) {
256 print img_warning($langs->trans('WarningSafeModeOnCheckExecDir'));
257 dol_syslog("safe_mode is on, basedir is ".$basedir.", safe_mode_exec_dir is ".ini_get('safe_mode_exec_dir'), LOG_WARNING);
258 }
259 }
260 print '<input type="text" '.((defined('MAIN_ANTIVIRUS_COMMAND') && !defined('MAIN_ANTIVIRUS_BYPASS_COMMAND_AND_PARAM')) ? 'disabled ' : '').'name="MAIN_ANTIVIRUS_COMMAND" class="minwidth500imp" spellcheck="false" value="'.dol_escape_htmltag(GETPOSTISSET('MAIN_ANTIVIRUS_COMMAND') ? GETPOST('MAIN_ANTIVIRUS_COMMAND') : getDolGlobalString('MAIN_ANTIVIRUS_COMMAND')).'">';
261 if (defined('MAIN_ANTIVIRUS_COMMAND') && !defined('MAIN_ANTIVIRUS_BYPASS_COMMAND_AND_PARAM')) {
262 print '<br><span class="opacitymedium">'.$langs->trans("ValueIsForcedBySystem").'</span>';
263 }
264 print "</td>";
265 print '</tr>';
266
267 // Anti virus param
268 print '<tr class="oddeven">';
269 print '<td>'.$langs->trans("AntiVirusParam").'<br>';
270 print '<span class="opacitymedium">'.$langs->trans("AntiVirusParamExample").'</span>';
271 print '</td>';
272 print '<td>';
273 print '<input type="text" '.(defined('MAIN_ANTIVIRUS_PARAM') ? 'disabled ' : '').'name="MAIN_ANTIVIRUS_PARAM" class="minwidth500imp" spellcheck="false" value="'.(getDolGlobalString('MAIN_ANTIVIRUS_PARAM') ? dol_escape_htmltag(getDolGlobalString('MAIN_ANTIVIRUS_PARAM')) : '').'">';
274 if (defined('MAIN_ANTIVIRUS_PARAM')) {
275 print '<br><span class="opacitymedium">'.$langs->trans("ValueIsForcedBySystem").'</span>';
276 }
277 print "</td>";
278 print '</tr>';
279}
280
281print '</table>';
282print '</div>';
283
284
285print dol_get_fiche_end();
286
287print $form->buttonsSaveCancel("Modify", '');
288
289print '</form>';
290
291
292// Form to test upload
293print '<br>';
294$formfile = new FormFile($db);
295$formfile->form_attach_new_file($_SERVER['PHP_SELF'], $langs->trans("FormToTestFileUploadForm"), 0, 0, 1, 50, null, '', 1, '', 0);
296
297// List of document
298$filearray = dol_dir_list($upload_dir, "files", 0, '', '', $sortfield, $sortorder == 'desc' ? SORT_DESC : SORT_ASC, 1);
299if (count($filearray) > 0) {
300 $formfile->list_of_documents($filearray, null, 'admin_temp', '');
301}
302
303// End of page
304llxFooter();
305$db->close();
security_prepare_head()
Prepare array with list of tabs.
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).
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
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.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:64
getExecutableContent()
Return array of extension for executable files of text files that can contains executable code.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
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_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.