dolibarr 23.0.3
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';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
38require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
39require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
40
41// Load translation files required by the page
42$langs->loadLangs(array('users', 'admin', 'other'));
43
44$action = GETPOST('action', 'aZ09');
45$sortfield = GETPOST('sortfield', 'aZ09');
46$sortorder = GETPOST('sortorder', 'aZ09');
47if (empty($sortfield)) {
48 $sortfield = 'date';
49}
50if (empty($sortorder)) {
51 $sortorder = 'desc';
52}
53
54$upload_dir = $conf->admin->dir_temp;
55
56if (!$user->admin) {
58}
59
60$error = 0;
61
62
63/*
64 * Actions
65 */
66
67if (GETPOST('sendit') && getDolGlobalString('MAIN_UPLOAD_DOC')) {
68 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
69
70 dol_add_file_process($upload_dir, 1, 0, 'userfile');
71}
72
73if ($action == 'updateform') {
74 $antivircommand = GETPOST('MAIN_ANTIVIRUS_COMMAND', 'restricthtml'); // Use GETPOST restricthtml because we must accept ". Example c:\Progra~1\ClamWin\bin\clamscan.exe
75 $antivirparam = GETPOST('MAIN_ANTIVIRUS_PARAM', 'restricthtml'); // Use GETPOST restricthtml because we must accept ". Example --database="C:\Program Files (x86)\ClamWin\lib"
76 $antivircommand = dol_string_nospecial($antivircommand, '', array("|", ";", "<", ">", "&", "+")); // Sanitize command
77 $antivirparam = dol_string_nospecial($antivirparam, '', array("|", ";", "<", ">", "&", "+")); // Sanitize params
78
79 if ($antivircommand && !empty($dolibarr_main_restrict_os_commands)) {
80 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
81 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
82 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$antivircommand);
83 $basenamecmddump = basename(str_replace('\\', '/', $antivircommand));
84 if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
85 $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
86 setEventMessages($errormsg, null, 'errors');
87 $error++;
88 }
89 }
90
91 if (!$error) {
92 $tmpumask = GETPOST('MAIN_UMASK', 'alpha');
93 $tmpumask = (octdec($tmpumask) & 0666);
94 $tmpumask = decoct($tmpumask);
95 if (!preg_match('/^0/', $tmpumask)) {
96 $tmpumask = '0'.$tmpumask;
97 }
98 if (empty($tmpumask)) { // Also matches '0'
99 $tmpumask = '0664';
100 }
101
102 $res3 = dolibarr_set_const($db, 'MAIN_UPLOAD_DOC', GETPOST('MAIN_UPLOAD_DOC', 'alpha'), 'chaine', 0, '', $conf->entity);
103 $res4 = dolibarr_set_const($db, "MAIN_UMASK", $tmpumask, 'chaine', 0, '', $conf->entity);
104 $res5 = dolibarr_set_const($db, "MAIN_ANTIVIRUS_COMMAND", trim($antivircommand), 'chaine', 0, '', $conf->entity);
105 $res6 = dolibarr_set_const($db, "MAIN_ANTIVIRUS_PARAM", trim($antivirparam), 'chaine', 0, '', $conf->entity);
106 $res7 = dolibarr_set_const($db, "MAIN_FILE_EXTENSION_UPLOAD_RESTRICTION", GETPOST('MAIN_FILE_EXTENSION_UPLOAD_RESTRICTION', 'alpha'), 'chaine', 0, '', $conf->entity);
107
108 $res8 = dolibarr_set_const($db, "MAIN_SECURITY_MAXFILESIZE_DOWNLOADED", GETPOST('MAIN_SECURITY_MAXFILESIZE_DOWNLOADED', 'alpha'), 'chaine', 0, '', $conf->entity);
109
110 if ($res3 && $res4 && $res5 && $res6 && $res7 && $res8) {
111 setEventMessages($langs->trans("RecordModifiedSuccessfully"), null, 'mesgs');
112 }
113 }
114} elseif ($action == 'deletefile') {
115 // Delete file
116 $langs->load("other");
117 $file = $conf->admin->dir_temp.'/'.GETPOST('urlfile', 'alpha');
118 $ret = dol_delete_file($file);
119 if ($ret) {
120 setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile', 'alpha')), null, 'mesgs');
121 } else {
122 setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
123 }
124}
125
126
127/*
128 * View
129 */
130
131$form = new Form($db);
132
133$wikihelp = 'EN:Setup_Security|FR:Paramétrage_Sécurité|ES:Configuración_Seguridad';
134llxHeader('', $langs->trans("Files"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-security_file');
135
136print load_fiche_titre($langs->trans("SecuritySetup"), '', 'title_setup');
137
138print '<span class="opacitymedium">'.$langs->trans("SecurityFilesDesc")."</span><br>\n";
139print "<br>\n";
140
141
142print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
143print '<input type="hidden" name="token" value="'.newToken().'">';
144print '<input type="hidden" name="action" value="updateform">';
145
146$head = security_prepare_head();
147
148print dol_get_fiche_head($head, 'file', '', -1);
149
150print '<br>';
151
152// Download options
153
154print '<div class="div-table-responsive-no-min">';
155print '<table class="noborder centpercent nomarginbottom">';
156print '<tr class="liste_titre">';
157print '<td>'.img_picto('', 'download', 'class="pictofixedwidth"').$langs->trans("Download").'</td>';
158print '<td></td>';
159print '</tr>';
160
161print '<tr class="oddeven">';
162print '<td>'.$langs->trans("MAIN_SECURITY_MAXFILESIZE_DOWNLOADED").'<br>';
163//print '<span class="opacitymedium">'.$langs->trans("MAIN_SECURITY_MAXFILESIZE_DOWNLOADED").'</span>';
164print '</td>';
165print '<td>';
166print '<input type="text" name="MAIN_SECURITY_MAXFILESIZE_DOWNLOADED" class="width100 right" spellcheck="false" value="'.getDolGlobalString('MAIN_SECURITY_MAXFILESIZE_DOWNLOADED').'"> '.$langs->trans("Kb");
167print "</td>";
168print '</tr>';
169
170print '</table>';
171print '</div>';
172
173
174print '<br>';
175print '<br>';
176
177
178// Upload options
179
180print '<div class="div-table-responsive-no-min">';
181print '<table class="noborder centpercent nomarginbottom">';
182print '<tr class="liste_titre">';
183print '<td>'.img_picto('', 'upload', 'class="pictofixedwidth"').$langs->trans("UploadName").'</td>';
184print '<td></td>';
185print '</tr>';
186
187print '<tr class="oddeven">';
188print '<td>'.$langs->trans("MaxSizeForUploadedFiles").'.';
189$max = @ini_get('upload_max_filesize');
190if (isset($max)) {
191 print '<br><span class="opacitymedium">'.$langs->trans("MustBeLowerThanPHPLimit", ((int) $max) * 1024, $langs->trans("Kb")).'.</span>';
192} else {
193 print ' '.$langs->trans("NoMaxSizeByPHPLimit").'.';
194}
195print '</td>';
196print '<td class="nowrap">';
197print '<input class="flat width75 right" name="MAIN_UPLOAD_DOC" type="text" spellcheck="false" value="'.dol_escape_htmltag(getDolGlobalString('MAIN_UPLOAD_DOC')).'"> '.$langs->trans("Kb");
198print '</td>';
199print '</tr>';
200
201print '<tr class="oddeven">';
202print '<td>';
203print $form->textwithpicto($langs->trans("UMask"), $langs->trans("UMaskExplanation"));
204print '</td>';
205print '<td class="nowrap">';
206print '<input class="flat width75 right" name="MAIN_UMASK" type="text" spellcheck="false" value="'.dol_escape_htmltag(getDolGlobalString('MAIN_UMASK')).'">';
207print '</td>';
208print '</tr>';
209
210print '<tr class="oddeven">';
211print '<td>'.$langs->trans("UploadExtensionRestriction").'<br>';
212print '<span class="opacitymedium">'.$langs->trans("UploadExtensionRestrictionExemple").'</span>';
213print '</td>';
214print '<td>';
215print '<input type="text" name="MAIN_FILE_EXTENSION_UPLOAD_RESTRICTION" class="minwidth500imp" spellcheck="false" value="'.getDolGlobalString('MAIN_FILE_EXTENSION_UPLOAD_RESTRICTION', implode(',', getExecutableContent())).'">';
216print "</td>";
217print '</tr>';
218
219
220// Use anti virus
221
222
223// Enable advanced perms
224print '<tr class="oddeven">';
225print '<td>'.$langs->trans("UseAntivirusOnUploadedFile").'</td>';
226print '<td class="">';
227if (defined('MAIN_ANTIVIRUS_UPLOAD_ON') && constant('MAIN_ANTIVIRUS_UPLOAD_ON')) {
228 print img_picto($langs->trans("Enabled")." - Can't be disabled (MAIN_ANTIVIRUS_UPLOAD_ON is set)", 'switch_on', '', 0, 0, 0, '', 'opacitymedium');
229} else {
230 if (!empty($conf->use_javascript_ajax)) {
231 print ajax_constantonoff('MAIN_ANTIVIRUS_UPLOAD_ON', array(), null, 0, 0, 1);
232 } else {
233 if (!getDolGlobalString('MAIN_ANTIVIRUS_UPLOAD_ON')) {
234 print '<a href="'.$_SERVER['PHP_SELF'].'?action=set_MAIN_ANTIVIRUS_UPLOAD_ON&token='.newToken().'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
235 } else {
236 print '<a href="'.$_SERVER['PHP_SELF'].'?action=del_MAIN_ANTIVIRUS_UPLOAD_ON&token='.newToken().'">'.img_picto($langs->trans("Enabled"), 'on').'</a>';
237 }
238 }
239}
240print "</td></tr>";
241
242if (getDolGlobalString('MAIN_ANTIVIRUS_UPLOAD_ON')) {
243 print '<tr class="oddeven">';
244 print '<td><span class="fieldrequired">'.$langs->trans("AntiVirusCommand").'</span><br>';
245 print '<span class="opacitymedium">'.$langs->trans("AntiVirusCommandExample").'</span>';
246 print '</td>';
247 print '<td>';
248 // Check that command is inside safe_mode
249 if (ini_get('safe_mode') && getDolGlobalString('MAIN_ANTIVIRUS_COMMAND')) {
250 $langs->load("errors");
251 $basedir = preg_replace('/"/', '', dirname($conf->global->MAIN_ANTIVIRUS_COMMAND));
252 $listdir = explode(';', ini_get('safe_mode_exec_dir'));
253 if (!in_array($basedir, $listdir)) {
254 print img_warning($langs->trans('WarningSafeModeOnCheckExecDir'));
255 dol_syslog("safe_mode is on, basedir is ".$basedir.", safe_mode_exec_dir is ".ini_get('safe_mode_exec_dir'), LOG_WARNING);
256 }
257 }
258 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')).'">';
259 if (defined('MAIN_ANTIVIRUS_COMMAND') && !defined('MAIN_ANTIVIRUS_BYPASS_COMMAND_AND_PARAM')) {
260 print '<br><span class="opacitymedium">'.$langs->trans("ValueIsForcedBySystem").'</span>';
261 }
262 print "</td>";
263 print '</tr>';
264
265 // Anti virus param
266 print '<tr class="oddeven">';
267 print '<td>'.$langs->trans("AntiVirusParam").'<br>';
268 print '<span class="opacitymedium">'.$langs->trans("AntiVirusParamExample").'</span>';
269 print '</td>';
270 print '<td>';
271 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')) : '').'">';
272 if (defined('MAIN_ANTIVIRUS_PARAM')) {
273 print '<br><span class="opacitymedium">'.$langs->trans("ValueIsForcedBySystem").'</span>';
274 }
275 print "</td>";
276 print '</tr>';
277}
278
279print '</table>';
280print '</div>';
281
282
283print dol_get_fiche_end();
284
285print $form->buttonsSaveCancel("Modify", '');
286
287print '</form>';
288
289
290// Form to test upload
291print '<br>';
292$formfile = new FormFile($db);
293$formfile->form_attach_new_file($_SERVER['PHP_SELF'], $langs->trans("FormToTestFileUploadForm"), 0, 0, 1, 50, null, '', 1, '', 0);
294
295// List of document
296$filearray = dol_dir_list($upload_dir, "files", 0, '', '', $sortfield, $sortorder == 'desc' ? SORT_DESC : SORT_ASC, 1);
297if (count($filearray) > 0) {
298 $formfile->list_of_documents($filearray, null, 'admin_temp', '');
299}
300
301// End of page
302llxFooter();
303$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.
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)
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.
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.