actions_linkedfiles.inc.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /* Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
  3. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  4. * Copyright (C) 2015 Ferran Marcet <fmarcet@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. * or see https://www.gnu.org/
  19. */
  20. // Variable $upload_dir must be defined when entering here.
  21. // Variable $upload_dirold may also exists.
  22. // Variable $confirm must be defined.
  23. // If variable $permissiontoadd is defined, we check it is true. Note: A test on permission should already have been done into the restrictedArea() method called by parent page.
  24. //var_dump($upload_dir);
  25. //var_dump($upload_dirold);
  26. // Protection to understand what happen when submitting files larger than post_max_size
  27. if (GETPOST('uploadform', 'int') && empty($_POST) && empty($_FILES)) {
  28. dol_syslog("The PHP parameter 'post_max_size' is too low. All POST parameters and FILES were set to empty.");
  29. $langs->loadLangs(array("errors", "install"));
  30. print $langs->trans("ErrorFileSizeTooLarge").' ';
  31. print $langs->trans("ErrorGoBackAndCorrectParameters");
  32. die;
  33. }
  34. if ((GETPOST('sendit', 'alpha')
  35. || GETPOST('linkit', 'restricthtml')
  36. || ($action == 'confirm_deletefile' && $confirm == 'yes')
  37. || ($action == 'confirm_updateline' && GETPOST('save', 'alpha') && GETPOST('link', 'alpha'))
  38. || ($action == 'renamefile' && GETPOST('renamefilesave', 'alpha'))) && empty($permissiontoadd)) {
  39. dol_syslog('The file actions_linkedfiles.inc.php was included but parameter $permissiontoadd was not set before.');
  40. print 'The file actions_linkedfiles.inc.php was included but parameter $permissiontoadd was not set before.';
  41. die;
  42. }
  43. // Submit file/link
  44. if (GETPOST('sendit', 'alpha') && !empty($conf->global->MAIN_UPLOAD_DOC) && !empty($permissiontoadd)) {
  45. if (!empty($_FILES)) {
  46. if (is_array($_FILES['userfile']['tmp_name'])) {
  47. $userfiles = $_FILES['userfile']['tmp_name'];
  48. } else {
  49. $userfiles = array($_FILES['userfile']['tmp_name']);
  50. }
  51. foreach ($userfiles as $key => $userfile) {
  52. if (empty($_FILES['userfile']['tmp_name'][$key])) {
  53. $error++;
  54. if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2) {
  55. setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
  56. } else {
  57. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("File")), null, 'errors');
  58. }
  59. }
  60. if (preg_match('/__.*__/', $_FILES['userfile']['name'][$key])) {
  61. $error++;
  62. setEventMessages($langs->trans('ErrorWrongFileName'), null, 'errors');
  63. }
  64. }
  65. if (!$error) {
  66. // Define if we have to generate thumbs or not
  67. $generatethumbs = 1;
  68. if (GETPOST('section_dir', 'alpha')) {
  69. $generatethumbs = 0;
  70. }
  71. $allowoverwrite = (GETPOST('overwritefile', 'int') ? 1 : 0);
  72. if (!empty($upload_dirold) && getDolGlobalInt('PRODUCT_USE_OLD_PATH_FOR_PHOTO')) {
  73. $result = dol_add_file_process($upload_dirold, $allowoverwrite, 1, 'userfile', GETPOST('savingdocmask', 'alpha'), null, '', $generatethumbs, $object);
  74. } elseif (!empty($upload_dir)) {
  75. $result = dol_add_file_process($upload_dir, $allowoverwrite, 1, 'userfile', GETPOST('savingdocmask', 'alpha'), null, '', $generatethumbs, $object);
  76. }
  77. }
  78. }
  79. } elseif (GETPOST('linkit', 'restricthtml') && !empty($conf->global->MAIN_UPLOAD_DOC) && !empty($permissiontoadd)) {
  80. $link = GETPOST('link', 'alpha');
  81. if ($link) {
  82. if (substr($link, 0, 7) != 'http://' && substr($link, 0, 8) != 'https://' && substr($link, 0, 7) != 'file://' && substr($link, 0, 7) != 'davs://') {
  83. $link = 'http://'.$link;
  84. }
  85. // Parse $newUrl
  86. $newUrlArray = parse_url($link);
  87. // Check URL is external
  88. if (!getDolGlobalString('MAIN_ALLOW_SVG_FILES_AS_EXTERNAL_LINKS')) {
  89. if (!empty($newUrlArray['path']) && preg_match('/\.svg$/i', $newUrlArray['path'])) {
  90. $error++;
  91. $langs->load("errors");
  92. setEventMessages($langs->trans('ErrorSVGFilesNotAllowedAsLinksWithout', 'MAIN_ALLOW_SVG_FILES_AS_EXTERNAL_LINKS'), null, 'errors');
  93. }
  94. }
  95. // Alow external links to svg ?
  96. if (!getDolGlobalString('MAIN_ALLOW_LOCAL_LINKS_AS_EXTERNAL_LINKS')) {
  97. // Test $newUrlAray['host'] to check link is external using isIPAllowed()
  98. // TODO
  99. }
  100. if (!$error) {
  101. dol_add_file_process($upload_dir, 0, 1, 'userfile', null, $link, '', 0);
  102. }
  103. }
  104. }
  105. // Delete file/link
  106. if ($action == 'confirm_deletefile' && $confirm == 'yes' && !empty($permissiontoadd)) {
  107. $urlfile = GETPOST('urlfile', 'alpha', 0, null, null, 1); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  108. if (GETPOST('section', 'alpha')) {
  109. // For a delete from the ECM module, upload_dir is ECM root dir and urlfile contains relative path from upload_dir
  110. $file = $upload_dir.(preg_match('/\/$/', $upload_dir) ? '' : '/').$urlfile;
  111. } else // For a delete from the file manager into another module, or from documents pages, upload_dir contains already path to file from module dir, so we clean path into urlfile.
  112. {
  113. $urlfile = basename($urlfile);
  114. $file = $upload_dir.(preg_match('/\/$/', $upload_dir) ? '' : '/').$urlfile;
  115. if (!empty($upload_dirold)) {
  116. $fileold = $upload_dirold."/".$urlfile;
  117. }
  118. }
  119. $linkid = GETPOST('linkid', 'int');
  120. if ($urlfile) {
  121. // delete of a file
  122. $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine
  123. $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette (if file is an image)
  124. $ret = dol_delete_file($file, 0, 0, 0, (is_object($object) ? $object : null));
  125. if (!empty($fileold)) {
  126. dol_delete_file($fileold, 0, 0, 0, (is_object($object) ? $object : null)); // Delete file using old path
  127. }
  128. if ($ret) {
  129. // If it exists, remove thumb.
  130. $regs = array();
  131. if (preg_match('/(\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff)$/i', $file, $regs)) {
  132. $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_small'.$regs[0]);
  133. if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
  134. dol_delete_file($dirthumb.$photo_vignette);
  135. }
  136. $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_mini'.$regs[0]);
  137. if (file_exists(dol_osencode($dirthumb.$photo_vignette))) {
  138. dol_delete_file($dirthumb.$photo_vignette);
  139. }
  140. }
  141. setEventMessages($langs->trans("FileWasRemoved", $urlfile), null, 'mesgs');
  142. } else {
  143. setEventMessages($langs->trans("ErrorFailToDeleteFile", $urlfile), null, 'errors');
  144. }
  145. } elseif ($linkid) { // delete of external link
  146. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  147. $link = new Link($db);
  148. $link->fetch($linkid);
  149. $res = $link->delete($user);
  150. $langs->load('link');
  151. if ($res > 0) {
  152. setEventMessages($langs->trans("LinkRemoved", $link->label), null, 'mesgs');
  153. } else {
  154. if (count($link->errors)) {
  155. setEventMessages('', $link->errors, 'errors');
  156. } else {
  157. setEventMessages($langs->trans("ErrorFailedToDeleteLink", $link->label), null, 'errors');
  158. }
  159. }
  160. }
  161. if (is_object($object) && $object->id > 0) {
  162. if (!empty($backtopage)) {
  163. header('Location: '.$backtopage);
  164. exit;
  165. } else {
  166. $tmpurl = $_SERVER["PHP_SELF"].'?id='.$object->id.(GETPOST('section_dir', 'alpha') ? '&section_dir='.urlencode(GETPOST('section_dir', 'alpha')) : '').(!empty($withproject) ? '&withproject=1' : '');
  167. header('Location: '.$tmpurl);
  168. exit;
  169. }
  170. }
  171. } elseif ($action == 'confirm_updateline' && GETPOST('save', 'alpha') && GETPOST('link', 'alpha') && !empty($permissiontoadd)) {
  172. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  173. $langs->load('link');
  174. $link = new Link($db);
  175. $f = $link->fetch(GETPOST('linkid', 'int'));
  176. if ($f) {
  177. $link->url = GETPOST('link', 'alpha');
  178. if (substr($link->url, 0, 7) != 'http://' && substr($link->url, 0, 8) != 'https://' && substr($link->url, 0, 7) != 'file://') {
  179. $link->url = 'http://'.$link->url;
  180. }
  181. $link->label = GETPOST('label', 'alphanohtml');
  182. $res = $link->update($user);
  183. if (!$res) {
  184. setEventMessages($langs->trans("ErrorFailedToUpdateLink", $link->label), null, 'mesgs');
  185. }
  186. } else {
  187. //error fetching
  188. }
  189. } elseif ($action == 'renamefile' && GETPOST('renamefilesave', 'alpha') && !empty($permissiontoadd)) {
  190. // For documents pages, upload_dir contains already the path to the file from module dir
  191. if (!empty($upload_dir)) {
  192. $filenamefrom = dol_sanitizeFileName(GETPOST('renamefilefrom', 'alpha'), '_', 0); // Do not remove accents
  193. $filenameto = dol_sanitizeFileName(GETPOST('renamefileto', 'alpha'), '_', 0); // Do not remove accents
  194. // We apply dol_string_nohtmltag also to clean file names (this remove duplicate spaces) because
  195. // this function is also applied when we upload and when we make try to download file (by the GETPOST(filename, 'alphanohtml') call).
  196. $filenameto = dol_string_nohtmltag($filenameto);
  197. if (preg_match('/__.*__/', $filenameto)) {
  198. $error++;
  199. setEventMessages($langs->trans('ErrorWrongFileName'), null, 'errors');
  200. }
  201. // Check that filename is not the one of a reserved allowed CLI command
  202. if (empty($error)) {
  203. global $dolibarr_main_restrict_os_commands;
  204. if (!empty($dolibarr_main_restrict_os_commands)) {
  205. $arrayofallowedcommand = explode(',', $dolibarr_main_restrict_os_commands);
  206. $arrayofallowedcommand = array_map('trim', $arrayofallowedcommand);
  207. if (in_array(basename($filenameto), $arrayofallowedcommand)) {
  208. $error++;
  209. $langs->load("errors"); // key must be loaded because we can't rely on loading during output, we need var substitution to be done now.
  210. setEventMessages($langs->trans("ErrorFilenameReserved", basename($filenameto)), null, 'errors');
  211. }
  212. }
  213. }
  214. if (empty($error) && $filenamefrom != $filenameto) {
  215. // Security:
  216. // Disallow file with some extensions. We rename them.
  217. // Because if we put the documents directory into a directory inside web root (very bad), this allows to execute on demand arbitrary code.
  218. if (isAFileWithExecutableContent($filenameto) && empty($conf->global->MAIN_DOCUMENT_IS_OUTSIDE_WEBROOT_SO_NOEXE_NOT_REQUIRED)) {
  219. // $upload_dir ends with a slash, so be must be sure the medias dir to compare to ends with slash too.
  220. $publicmediasdirwithslash = $conf->medias->multidir_output[$conf->entity];
  221. if (!preg_match('/\/$/', $publicmediasdirwithslash)) {
  222. $publicmediasdirwithslash .= '/';
  223. }
  224. if (strpos($upload_dir, $publicmediasdirwithslash) !== 0) { // We never add .noexe on files into media directory
  225. $filenameto .= '.noexe';
  226. }
  227. }
  228. if ($filenamefrom && $filenameto) {
  229. $srcpath = $upload_dir.'/'.$filenamefrom;
  230. $destpath = $upload_dir.'/'.$filenameto;
  231. $reshook = $hookmanager->initHooks(array('actionlinkedfiles'));
  232. $parameters = array('filenamefrom' => $filenamefrom, 'filenameto' => $filenameto, 'upload_dir' => $upload_dir);
  233. $reshook = $hookmanager->executeHooks('renameUploadedFile', $parameters, $object);
  234. if (empty($reshook)) {
  235. if (preg_match('/^\./', $filenameto)) {
  236. $langs->load("errors"); // lang must be loaded because we can't rely on loading during output, we need var substitution to be done now.
  237. setEventMessages($langs->trans("ErrorFilenameCantStartWithDot", $filenameto), null, 'errors');
  238. } elseif (!file_exists($destpath)) {
  239. $result = dol_move($srcpath, $destpath);
  240. if ($result) {
  241. // Define if we have to generate thumbs or not
  242. $generatethumbs = 1;
  243. // When we rename a file from the file manager in ecm, we must not regenerate thumbs (not a problem, we do pass here)
  244. // When we rename a file from the website module, we must not regenerate thumbs (module = medias in such a case)
  245. // but when we rename from a tab "Documents", we must regenerate thumbs
  246. if (GETPOST('modulepart', 'aZ09') == 'medias') {
  247. $generatethumbs = 0;
  248. }
  249. if ($generatethumbs) {
  250. if ($object->id > 0) {
  251. // Create thumbs for the new file
  252. $object->addThumbs($destpath);
  253. // Delete thumb files with old name
  254. $object->delThumbs($srcpath);
  255. }
  256. }
  257. setEventMessages($langs->trans("FileRenamed"), null);
  258. } else {
  259. $langs->load("errors"); // lang must be loaded because we can't rely on loading during output, we need var substitution to be done now.
  260. setEventMessages($langs->trans("ErrorFailToRenameFile", $filenamefrom, $filenameto), null, 'errors');
  261. }
  262. } else {
  263. $langs->load("errors"); // lang must be loaded because we can't rely on loading during output, we need var substitution to be done now.
  264. setEventMessages($langs->trans("ErrorDestinationAlreadyExists", $filenameto), null, 'errors');
  265. }
  266. }
  267. }
  268. }
  269. }
  270. // Update properties in ECM table
  271. if (GETPOST('ecmfileid', 'int') > 0) {
  272. $shareenabled = GETPOST('shareenabled', 'alpha');
  273. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  274. $ecmfile = new EcmFiles($db);
  275. $result = $ecmfile->fetch(GETPOST('ecmfileid', 'int'));
  276. if ($result > 0) {
  277. if ($shareenabled) {
  278. if (empty($ecmfile->share)) {
  279. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  280. $ecmfile->share = getRandomPassword(true);
  281. }
  282. } else {
  283. $ecmfile->share = '';
  284. }
  285. $result = $ecmfile->update($user);
  286. if ($result < 0) {
  287. setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
  288. }
  289. }
  290. }
  291. }