security_file.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. *
  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. */
  19. /**
  20. * \file htdocs/admin/security_file.php
  21. * \ingroup core
  22. * \brief Security options setup
  23. */
  24. // Load Dolibarr environment
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('users', 'admin', 'other'));
  31. $action = GETPOST('action', 'aZ09');
  32. $sortfield = GETPOST('sortfield', 'aZ09');
  33. $sortorder = GETPOST('sortorder', 'aZ09');
  34. if (empty($sortfield)) {
  35. $sortfield = 'date';
  36. }
  37. if (empty($sortorder)) {
  38. $sortorder = 'desc';
  39. }
  40. $upload_dir = $conf->admin->dir_temp;
  41. if (!$user->admin) {
  42. accessforbidden();
  43. }
  44. $error = 0;
  45. /*
  46. * Actions
  47. */
  48. if (GETPOST('sendit') && !empty($conf->global->MAIN_UPLOAD_DOC)) {
  49. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  50. dol_add_file_process($upload_dir, 1, 0, 'userfile');
  51. }
  52. if ($action == 'updateform') {
  53. $antivircommand = GETPOST('MAIN_ANTIVIRUS_COMMAND', 'restricthtml'); // Use GETPOST restricthtml because we must accept ". Example c:\Progra~1\ClamWin\bin\clamscan.exe
  54. $antivirparam = GETPOST('MAIN_ANTIVIRUS_PARAM', 'restricthtml'); // Use GETPOST restricthtml because we must accept ". Example --database="C:\Program Files (x86)\ClamWin\lib"
  55. $antivircommand = dol_string_nospecial($antivircommand, '', array("|", ";", "<", ">", "&", "+")); // Sanitize command
  56. $antivirparam = dol_string_nospecial($antivirparam, '', array("|", ";", "<", ">", "&", "+")); // Sanitize params
  57. if ($antivircommand && !empty($dolibarr_main_restrict_os_commands)) {
  58. $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
  59. $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
  60. dol_syslog("Command are restricted to ".$dolibarr_main_restrict_os_commands.". We check that one of this command is inside ".$antivircommand);
  61. $basenamecmddump = basename(str_replace('\\', '/', $antivircommand));
  62. if (!in_array($basenamecmddump, $arrayofallowedcommand)) { // the provided command $cmddump must be an allowed command
  63. $errormsg = $langs->trans('CommandIsNotInsideAllowedCommands');
  64. setEventMessages($errormsg, null, 'errors');
  65. $error++;
  66. }
  67. }
  68. if (!$error) {
  69. $tmpumask = GETPOST('MAIN_UMASK', 'alpha');
  70. $tmpumask = (octdec($tmpumask) & 0666);
  71. $tmpumask = decoct($tmpumask);
  72. if (!preg_match('/^0/', $tmpumask)) {
  73. $tmpumask = '0'.$tmpumask;
  74. }
  75. if (empty($tmpumask) || $tmpumask === '0') {
  76. $tmpumask = '0664';
  77. }
  78. $res3 = dolibarr_set_const($db, 'MAIN_UPLOAD_DOC', GETPOST('MAIN_UPLOAD_DOC', 'alpha'), 'chaine', 0, '', $conf->entity);
  79. $res4 = dolibarr_set_const($db, "MAIN_UMASK", $tmpumask, 'chaine', 0, '', $conf->entity);
  80. $res5 = dolibarr_set_const($db, "MAIN_ANTIVIRUS_COMMAND", trim($antivircommand), 'chaine', 0, '', $conf->entity);
  81. $res6 = dolibarr_set_const($db, "MAIN_ANTIVIRUS_PARAM", trim($antivirparam), 'chaine', 0, '', $conf->entity);
  82. if ($res3 && $res4 && $res5 && $res6) {
  83. setEventMessages($langs->trans("RecordModifiedSuccessfully"), null, 'mesgs');
  84. }
  85. }
  86. } elseif ($action == 'deletefile') {
  87. // Delete file
  88. $langs->load("other");
  89. $file = $conf->admin->dir_temp.'/'.GETPOST('urlfile', 'alpha');
  90. $ret = dol_delete_file($file);
  91. if ($ret) {
  92. setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile', 'alpha')), null, 'mesgs');
  93. } else {
  94. setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
  95. }
  96. }
  97. /*
  98. * View
  99. */
  100. $form = new Form($db);
  101. $wikihelp = 'EN:Setup_Security|FR:Paramétrage_Sécurité|ES:Configuración_Seguridad';
  102. llxHeader('', $langs->trans("Files"), $wikihelp);
  103. print load_fiche_titre($langs->trans("SecuritySetup"), '', 'title_setup');
  104. print '<span class="opacitymedium">'.$langs->trans("SecurityFilesDesc")."</span><br>\n";
  105. print "<br>\n";
  106. print '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  107. print '<input type="hidden" name="token" value="'.newToken().'">';
  108. print '<input type="hidden" name="action" value="updateform">';
  109. $head = security_prepare_head();
  110. print dol_get_fiche_head($head, 'file', '', -1);
  111. print '<br>';
  112. // Upload options
  113. print '<div class="div-table-responsive-no-min">';
  114. print '<table class="noborder centpercent nomarginbottom">';
  115. print '<tr class="liste_titre">';
  116. print '<td>'.$langs->trans("Parameters").'</td>';
  117. print '<td>'.$langs->trans("Value").'</td>';
  118. print '</tr>';
  119. print '<tr class="oddeven">';
  120. print '<td>'.$langs->trans("MaxSizeForUploadedFiles").'.';
  121. $max = @ini_get('upload_max_filesize');
  122. if (isset($max)) {
  123. print '<br><span class="opacitymedium">'.$langs->trans("MustBeLowerThanPHPLimit", ((int) $max) * 1024, $langs->trans("Kb")).'.</span>';
  124. } else {
  125. print ' '.$langs->trans("NoMaxSizeByPHPLimit").'.';
  126. }
  127. print '</td>';
  128. print '<td class="nowrap">';
  129. print '<input class="flat" name="MAIN_UPLOAD_DOC" type="text" size="6" value="'.dol_escape_htmltag($conf->global->MAIN_UPLOAD_DOC).'"> '.$langs->trans("Kb");
  130. print '</td>';
  131. print '</tr>';
  132. print '<tr class="oddeven">';
  133. print '<td>';
  134. print $form->textwithpicto($langs->trans("UMask"), $langs->trans("UMaskExplanation"));
  135. print '</td>';
  136. print '<td class="nowrap">';
  137. print '<input class="flat" name="MAIN_UMASK" type="text" size="6" value="'.dol_escape_htmltag($conf->global->MAIN_UMASK).'">';
  138. print '</td>';
  139. print '</tr>';
  140. // Use anti virus
  141. print '<tr class="oddeven">';
  142. print '<td>'.$langs->trans("AntiVirusCommand").'<br>';
  143. print '<span class="opacitymedium">'.$langs->trans("AntiVirusCommandExample").'</span>';
  144. // Check command in inside safe_mode
  145. print '</td>';
  146. print '<td>';
  147. if (ini_get('safe_mode') && !empty($conf->global->MAIN_ANTIVIRUS_COMMAND)) {
  148. $langs->load("errors");
  149. $basedir = preg_replace('/"/', '', dirname($conf->global->MAIN_ANTIVIRUS_COMMAND));
  150. $listdir = explode(';', ini_get('safe_mode_exec_dir'));
  151. if (!in_array($basedir, $listdir)) {
  152. print img_warning($langs->trans('WarningSafeModeOnCheckExecDir'));
  153. dol_syslog("safe_mode is on, basedir is ".$basedir.", safe_mode_exec_dir is ".ini_get('safe_mode_exec_dir'), LOG_WARNING);
  154. }
  155. }
  156. print '<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')).'">';
  157. if (defined('MAIN_ANTIVIRUS_COMMAND') && !defined('MAIN_ANTIVIRUS_BYPASS_COMMAND_AND_PARAM')) {
  158. print '<br><span class="opacitymedium">'.$langs->trans("ValueIsForcedBySystem").'</span>';
  159. }
  160. print "</td>";
  161. print '</tr>';
  162. // Use anti virus
  163. print '<tr class="oddeven">';
  164. print '<td>'.$langs->trans("AntiVirusParam").'<br>';
  165. print '<span class="opacitymedium">'.$langs->trans("AntiVirusParamExample").'</span>';
  166. print '</td>';
  167. print '<td>';
  168. print '<input type="text" '.(defined('MAIN_ANTIVIRUS_PARAM') ? 'disabled ' : '').'name="MAIN_ANTIVIRUS_PARAM" class="minwidth500imp" value="'.(!empty($conf->global->MAIN_ANTIVIRUS_PARAM) ?dol_escape_htmltag($conf->global->MAIN_ANTIVIRUS_PARAM) : '').'">';
  169. if (defined('MAIN_ANTIVIRUS_PARAM')) {
  170. print '<br><span class="opacitymedium">'.$langs->trans("ValueIsForcedBySystem").'</span>';
  171. }
  172. print "</td>";
  173. print '</tr>';
  174. print '</table>';
  175. print '</div>';
  176. print dol_get_fiche_end();
  177. print $form->buttonsSaveCancel("Modify", '');
  178. print '</form>';
  179. // Form to test upload
  180. print '<br>';
  181. $formfile = new FormFile($db);
  182. $formfile->form_attach_new_file($_SERVER['PHP_SELF'], $langs->trans("FormToTestFileUploadForm"), 0, 0, 1, 50, '', '', 1, '', 0);
  183. // List of document
  184. $filearray = dol_dir_list($upload_dir, "files", 0, '', '', $sortfield, $sortorder == 'desc' ? SORT_DESC : SORT_ASC, 1);
  185. if (count($filearray) > 0) {
  186. $formfile->list_of_documents($filearray, null, 'admin_temp', '');
  187. }
  188. // End of page
  189. llxFooter();
  190. $db->close();