dolibarr 21.0.0-beta
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';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
33
42// Load translation files required by the page
43$langs->loadLangs(array('users', 'admin', 'other'));
44
45$action = GETPOST('action', 'aZ09');
46$sortfield = GETPOST('sortfield', 'aZ09');
47$sortorder = GETPOST('sortorder', 'aZ09');
48if (empty($sortfield)) {
49 $sortfield = 'date';
50}
51if (empty($sortorder)) {
52 $sortorder = 'desc';
53}
54
55$upload_dir = $conf->admin->dir_temp;
56
57if (!$user->admin) {
59}
60
61$error = 0;
62
63
64/*
65 * Actions
66 */
67
68if (GETPOST('sendit') && getDolGlobalString('MAIN_UPLOAD_DOC')) {
69 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
70
71 dol_add_file_process($upload_dir, 1, 0, 'userfile');
72}
73
74if ($action == 'updateform') {
75 $antivircommand = GETPOST('MAIN_ANTIVIRUS_COMMAND', 'restricthtml'); // Use GETPOST restricthtml because we must accept ". Example c:\Progra~1\ClamWin\bin\clamscan.exe
76 $antivirparam = GETPOST('MAIN_ANTIVIRUS_PARAM', 'restricthtml'); // Use GETPOST restricthtml because we must accept ". Example --database="C:\Program Files (x86)\ClamWin\lib"
77 $antivircommand = dol_string_nospecial($antivircommand, '', array("|", ";", "<", ">", "&", "+")); // Sanitize command
78 $antivirparam = dol_string_nospecial($antivirparam, '', array("|", ";", "<", ">", "&", "+")); // Sanitize params
79
80 if ($antivircommand && !empty($dolibarr_main_restrict_os_commands)) {
81 $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
82 $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
83 dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$antivircommand);
84 $basenamecmddump = basename(str_replace('\\', '/', $antivircommand));
85 if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
86 $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
87 setEventMessages($errormsg, null, 'errors');
88 $error++;
89 }
90 }
91
92 if (!$error) {
93 $tmpumask = GETPOST('MAIN_UMASK', 'alpha');
94 $tmpumask = (octdec($tmpumask) & 0666);
95 $tmpumask = decoct($tmpumask);
96 if (!preg_match('/^0/', $tmpumask)) {
97 $tmpumask = '0'.$tmpumask;
98 }
99 if (empty($tmpumask)) { // Also matches '0'
100 $tmpumask = '0664';
101 }
102
103 $res3 = dolibarr_set_const($db, 'MAIN_UPLOAD_DOC', GETPOST('MAIN_UPLOAD_DOC', 'alpha'), 'chaine', 0, '', $conf->entity);
104 $res4 = dolibarr_set_const($db, "MAIN_UMASK", $tmpumask, 'chaine', 0, '', $conf->entity);
105 $res5 = dolibarr_set_const($db, "MAIN_ANTIVIRUS_COMMAND", trim($antivircommand), 'chaine', 0, '', $conf->entity);
106 $res6 = dolibarr_set_const($db, "MAIN_ANTIVIRUS_PARAM", trim($antivirparam), 'chaine', 0, '', $conf->entity);
107 if ($res3 && $res4 && $res5 && $res6) {
108 setEventMessages($langs->trans("RecordModifiedSuccessfully"), null, 'mesgs');
109 }
110 }
111} elseif ($action == 'deletefile') {
112 // Delete file
113 $langs->load("other");
114 $file = $conf->admin->dir_temp.'/'.GETPOST('urlfile', 'alpha');
115 $ret = dol_delete_file($file);
116 if ($ret) {
117 setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile', 'alpha')), null, 'mesgs');
118 } else {
119 setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
120 }
121}
122
123
124/*
125 * View
126 */
127
128$form = new Form($db);
129
130$wikihelp = 'EN:Setup_Security|FR:Paramétrage_Sécurité|ES:Configuración_Seguridad';
131llxHeader('', $langs->trans("Files"), $wikihelp, '', 0, 0, '', '', '', 'mod-admin page-security_file');
132
133print load_fiche_titre($langs->trans("SecuritySetup"), '', 'title_setup');
134
135print '<span class="opacitymedium">'.$langs->trans("SecurityFilesDesc")."</span><br>\n";
136print "<br>\n";
137
138
139print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
140print '<input type="hidden" name="token" value="'.newToken().'">';
141print '<input type="hidden" name="action" value="updateform">';
142
143$head = security_prepare_head();
144
145print dol_get_fiche_head($head, 'file', '', -1);
146
147print '<br>';
148
149// Upload options
150
151print '<div class="div-table-responsive-no-min">';
152print '<table class="noborder centpercent nomarginbottom">';
153print '<tr class="liste_titre">';
154print '<td>'.$langs->trans("Parameters").'</td>';
155print '<td></td>';
156print '</tr>';
157
158print '<tr class="oddeven">';
159print '<td>'.$langs->trans("MaxSizeForUploadedFiles").'.';
160$max = @ini_get('upload_max_filesize');
161if (isset($max)) {
162 print '<br><span class="opacitymedium">'.$langs->trans("MustBeLowerThanPHPLimit", ((int) $max) * 1024, $langs->trans("Kb")).'.</span>';
163} else {
164 print ' '.$langs->trans("NoMaxSizeByPHPLimit").'.';
165}
166print '</td>';
167print '<td class="nowrap">';
168print '<input class="flat" name="MAIN_UPLOAD_DOC" type="text" size="6" value="'.dol_escape_htmltag(getDolGlobalString('MAIN_UPLOAD_DOC')).'"> '.$langs->trans("Kb");
169print '</td>';
170print '</tr>';
171
172
173print '<tr class="oddeven">';
174print '<td>';
175print $form->textwithpicto($langs->trans("UMask"), $langs->trans("UMaskExplanation"));
176print '</td>';
177print '<td class="nowrap">';
178print '<input class="flat" name="MAIN_UMASK" type="text" size="6" value="'.dol_escape_htmltag(getDolGlobalString('MAIN_UMASK')).'">';
179print '</td>';
180print '</tr>';
181
182// Use anti virus
183
184print '<tr class="oddeven">';
185print '<td>'.$langs->trans("AntiVirusCommand").'<br>';
186print '<span class="opacitymedium">'.$langs->trans("AntiVirusCommandExample").'</span>';
187// Check command in inside safe_mode
188print '</td>';
189print '<td>';
190if (ini_get('safe_mode') && getDolGlobalString('MAIN_ANTIVIRUS_COMMAND')) {
191 $langs->load("errors");
192 $basedir = preg_replace('/"/', '', dirname($conf->global->MAIN_ANTIVIRUS_COMMAND));
193 $listdir = explode(';', ini_get('safe_mode_exec_dir'));
194 if (!in_array($basedir, $listdir)) {
195 print img_warning($langs->trans('WarningSafeModeOnCheckExecDir'));
196 dol_syslog("safe_mode is on, basedir is ".$basedir.", safe_mode_exec_dir is ".ini_get('safe_mode_exec_dir'), LOG_WARNING);
197 }
198}
199print '<input type="text" '.((defined('MAIN_ANTIVIRUS_COMMAND') && !defined('MAIN_ANTIVIRUS_BYPASS_COMMAND_AND_PARAM')) ? 'disabled ' : '').'name="MAIN_ANTIVIRUS_COMMAND" class="minwidth500imp" value="'.dol_escape_htmltag(GETPOSTISSET('MAIN_ANTIVIRUS_COMMAND') ? GETPOST('MAIN_ANTIVIRUS_COMMAND') : getDolGlobalString('MAIN_ANTIVIRUS_COMMAND')).'">';
200if (defined('MAIN_ANTIVIRUS_COMMAND') && !defined('MAIN_ANTIVIRUS_BYPASS_COMMAND_AND_PARAM')) {
201 print '<br><span class="opacitymedium">'.$langs->trans("ValueIsForcedBySystem").'</span>';
202}
203print "</td>";
204print '</tr>';
205
206// Use anti virus
207
208print '<tr class="oddeven">';
209print '<td>'.$langs->trans("AntiVirusParam").'<br>';
210print '<span class="opacitymedium">'.$langs->trans("AntiVirusParamExample").'</span>';
211print '</td>';
212print '<td>';
213print '<input type="text" '.(defined('MAIN_ANTIVIRUS_PARAM') ? 'disabled ' : '').'name="MAIN_ANTIVIRUS_PARAM" class="minwidth500imp" value="'.(getDolGlobalString('MAIN_ANTIVIRUS_PARAM') ? dol_escape_htmltag(getDolGlobalString('MAIN_ANTIVIRUS_PARAM')) : '').'">';
214if (defined('MAIN_ANTIVIRUS_PARAM')) {
215 print '<br><span class="opacitymedium">'.$langs->trans("ValueIsForcedBySystem").'</span>';
216}
217print "</td>";
218print '</tr>';
219
220print '</table>';
221print '</div>';
222
223print dol_get_fiche_end();
224
225print $form->buttonsSaveCancel("Modify", '');
226
227print '</form>';
228
229
230// Form to test upload
231print '<br>';
232$formfile = new FormFile($db);
233$formfile->form_attach_new_file($_SERVER['PHP_SELF'], $langs->trans("FormToTestFileUploadForm"), 0, 0, 1, 50, null, '', 1, '', 0);
234
235// List of document
236$filearray = dol_dir_list($upload_dir, "files", 0, '', '', $sortfield, $sortorder == 'desc' ? SORT_DESC : SORT_ASC, 1);
237if (count($filearray) > 0) {
238 $formfile->list_of_documents($filearray, null, 'admin_temp', '');
239}
240
241// End of page
242llxFooter();
243$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).
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:71
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
llxFooter()
Footer empty.
Definition document.php:107
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_add_file_process($upload_dir, $allowoverwrite=0, $updatesessionordb=0, $varfiles='addedfile', $savingdocmask='', $link=null, $trackid='', $generatethumbs=1, $object=null, $forceFullTestIndexation='')
Get and save an upload file (for example after submitting a new file a mail form).
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:63
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
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)
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.