actions_linkedfiles.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. //var_dump($upload_dir);
  24. //var_dump($upload_dirold);
  25. // Submit file/link
  26. if (GETPOST('sendit', 'alpha') && !empty($conf->global->MAIN_UPLOAD_DOC))
  27. {
  28. if (!empty($_FILES))
  29. {
  30. if (is_array($_FILES['userfile']['tmp_name'])) $userfiles = $_FILES['userfile']['tmp_name'];
  31. else $userfiles = array($_FILES['userfile']['tmp_name']);
  32. foreach ($userfiles as $key => $userfile)
  33. {
  34. if (empty($_FILES['userfile']['tmp_name'][$key]))
  35. {
  36. $error++;
  37. if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2) {
  38. setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
  39. }
  40. else {
  41. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("File")), null, 'errors');
  42. }
  43. }
  44. }
  45. if (!$error)
  46. {
  47. // Define if we have to generate thumbs or not
  48. $generatethumbs = 1;
  49. if (GETPOST('section_dir', 'alpha')) $generatethumbs = 0;
  50. $allowoverwrite = (GETPOST('overwritefile', 'int') ? 1 : 0);
  51. if (!empty($upload_dirold) && !empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO))
  52. {
  53. $result = dol_add_file_process($upload_dirold, $allowoverwrite, 1, 'userfile', GETPOST('savingdocmask', 'alpha'), null, '', $generatethumbs);
  54. }
  55. elseif (!empty($upload_dir))
  56. {
  57. $result = dol_add_file_process($upload_dir, $allowoverwrite, 1, 'userfile', GETPOST('savingdocmask', 'alpha'), null, '', $generatethumbs);
  58. }
  59. }
  60. }
  61. }
  62. elseif (GETPOST('linkit', 'none') && !empty($conf->global->MAIN_UPLOAD_DOC))
  63. {
  64. $link = GETPOST('link', 'alpha');
  65. if ($link)
  66. {
  67. if (substr($link, 0, 7) != 'http://' && substr($link, 0, 8) != 'https://' && substr($link, 0, 7) != 'file://') {
  68. $link = 'http://'.$link;
  69. }
  70. dol_add_file_process($upload_dir, 0, 1, 'userfile', null, $link, '', 0);
  71. }
  72. }
  73. // Delete file/link
  74. if ($action == 'confirm_deletefile' && $confirm == 'yes')
  75. {
  76. $urlfile = GETPOST('urlfile', 'alpha', 0, null, null, 1); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  77. if (GETPOST('section', 'alpha')) {
  78. // For a delete from the ECM module, upload_dir is ECM root dir and urlfile contains relative path from upload_dir
  79. $file = $upload_dir.(preg_match('/\/$/', $upload_dir) ? '' : '/').$urlfile;
  80. }
  81. 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.
  82. {
  83. $urlfile = basename($urlfile);
  84. $file = $upload_dir.(preg_match('/\/$/', $upload_dir) ? '' : '/').$urlfile;
  85. if (!empty($upload_dirold)) $fileold = $upload_dirold."/".$urlfile;
  86. }
  87. $linkid = GETPOST('linkid', 'int');
  88. if ($urlfile) {
  89. // delete of a file
  90. $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine
  91. $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette (if file is an image)
  92. $ret = dol_delete_file($file, 0, 0, 0, (is_object($object) ? $object : null));
  93. if (!empty($fileold)) dol_delete_file($fileold, 0, 0, 0, (is_object($object) ? $object : null)); // Delete file using old path
  94. // If it exists, remove thumb.
  95. $regs = array();
  96. if (preg_match('/(\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff)$/i', $file, $regs))
  97. {
  98. $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_small'.$regs[0]);
  99. if (file_exists(dol_osencode($dirthumb.$photo_vignette)))
  100. {
  101. dol_delete_file($dirthumb.$photo_vignette);
  102. }
  103. $photo_vignette = basename(preg_replace('/'.$regs[0].'/i', '', $file).'_mini'.$regs[0]);
  104. if (file_exists(dol_osencode($dirthumb.$photo_vignette)))
  105. {
  106. dol_delete_file($dirthumb.$photo_vignette);
  107. }
  108. }
  109. if ($ret) {
  110. setEventMessages($langs->trans("FileWasRemoved", $urlfile), null, 'mesgs');
  111. } else {
  112. setEventMessages($langs->trans("ErrorFailToDeleteFile", $urlfile), null, 'errors');
  113. }
  114. } elseif ($linkid) { // delete of external link
  115. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  116. $link = new Link($db);
  117. $link->fetch($linkid);
  118. $res = $link->delete($user);
  119. $langs->load('link');
  120. if ($res > 0) {
  121. setEventMessages($langs->trans("LinkRemoved", $link->label), null, 'mesgs');
  122. } else {
  123. if (count($link->errors)) {
  124. setEventMessages('', $link->errors, 'errors');
  125. } else {
  126. setEventMessages($langs->trans("ErrorFailedToDeleteLink", $link->label), null, 'errors');
  127. }
  128. }
  129. }
  130. if (is_object($object) && $object->id > 0) {
  131. if ($backtopage) {
  132. header('Location: '.$backtopage);
  133. exit;
  134. }
  135. else
  136. {
  137. $tmpurl = $_SERVER["PHP_SELF"].'?id='.$object->id.(GETPOST('section_dir', 'alpha') ? '&section_dir='.urlencode(GETPOST('section_dir', 'alpha')) : '').(!empty($withproject) ? '&withproject=1' : '');
  138. header('Location: '.$tmpurl);
  139. exit;
  140. }
  141. }
  142. }
  143. elseif ($action == 'confirm_updateline' && GETPOST('save', 'alpha') && GETPOST('link', 'alpha'))
  144. {
  145. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  146. $langs->load('link');
  147. $link = new Link($db);
  148. $f = $link->fetch(GETPOST('linkid', 'int'));
  149. if ($f)
  150. {
  151. $link->url = GETPOST('link', 'alpha');
  152. if (substr($link->url, 0, 7) != 'http://' && substr($link->url, 0, 8) != 'https://' && substr($link->url, 0, 7) != 'file://')
  153. {
  154. $link->url = 'http://'.$link->url;
  155. }
  156. $link->label = GETPOST('label', 'alphanohtml');
  157. $res = $link->update($user);
  158. if (!$res)
  159. {
  160. setEventMessages($langs->trans("ErrorFailedToUpdateLink", $link->label), null, 'mesgs');
  161. }
  162. }
  163. else
  164. {
  165. //error fetching
  166. }
  167. }
  168. elseif ($action == 'renamefile' && GETPOST('renamefilesave', 'alpha'))
  169. {
  170. // For documents pages, upload_dir contains already path to file from module dir, so we clean path into urlfile.
  171. if (!empty($upload_dir))
  172. {
  173. $filenamefrom = dol_sanitizeFileName(GETPOST('renamefilefrom', 'alpha'), '_', 0); // Do not remove accents
  174. $filenameto = dol_sanitizeFileName(GETPOST('renamefileto', 'alpha'), '_', 0); // Do not remove accents
  175. // We apply dol_string_nohtmltag also to clean file names (this remove duplicate spaces) because
  176. // this function is also applied when we upload and when we make try to download file (by the GETPOST(filename, 'alphanohtml') call).
  177. $filenameto = dol_string_nohtmltag($filenameto);
  178. if ($filenamefrom != $filenameto)
  179. {
  180. // Security:
  181. // Disallow file with some extensions. We rename them.
  182. // Because if we put the documents directory into a directory inside web root (very bad), this allows to execute on demand arbitrary code.
  183. if (isAFileWithExecutableContent($filenameto) && empty($conf->global->MAIN_DOCUMENT_IS_OUTSIDE_WEBROOT_SO_NOEXE_NOT_REQUIRED))
  184. {
  185. // $upload_dir ends with a slash, so be must be sure the medias dir to compare to ends with slash too.
  186. $publicmediasdirwithslash = $conf->medias->multidir_output[$conf->entity];
  187. if (! preg_match('/\/$/', $publicmediasdirwithslash)) $publicmediasdirwithslash.='/';
  188. if (strpos($upload_dir, $publicmediasdirwithslash) !== 0) { // We never add .noexe on files into media directory
  189. $filenameto .= '.noexe';
  190. }
  191. }
  192. if ($filenamefrom && $filenameto)
  193. {
  194. $srcpath = $upload_dir.'/'.$filenamefrom;
  195. $destpath = $upload_dir.'/'.$filenameto;
  196. $reshook = $hookmanager->initHooks(array('actionlinkedfiles'));
  197. $parameters = array('filenamefrom' => $filenamefrom, 'filenameto' => $filenameto, 'upload_dir' => $upload_dir);
  198. $reshook = $hookmanager->executeHooks('renameUploadedFile', $parameters, $object);
  199. if (empty($reshook))
  200. {
  201. if (preg_match('/^\./', $filenameto)) {
  202. $langs->load("errors"); // lang must be loaded because we can't rely on loading during output, we need var substitution to be done now.
  203. setEventMessages($langs->trans("ErrorFilenameCantStartWithDot", $filenameto), null, 'errors');
  204. } elseif (!file_exists($destpath)) {
  205. $result = dol_move($srcpath, $destpath);
  206. if ($result)
  207. {
  208. // Define if we have to generate thumbs or not
  209. $generatethumbs = 1;
  210. // When we rename a file from the file manager in ecm, we must not regenerate thumbs (not a problem, we do pass here)
  211. // When we rename a file from the website module, we must not regenerate thumbs (module = medias in such a case)
  212. // but when we rename from a tab "Documents", we must regenerate thumbs
  213. if (GETPOST('modulepart') == 'medias') $generatethumbs = 0;
  214. if ($generatethumbs)
  215. {
  216. if ($object->id)
  217. {
  218. $object->addThumbs($destpath);
  219. }
  220. // TODO Add revert function of addThumbs to remove thumbs with old name
  221. //$object->delThumbs($srcpath);
  222. }
  223. setEventMessages($langs->trans("FileRenamed"), null);
  224. }
  225. else
  226. {
  227. $langs->load("errors"); // lang must be loaded because we can't rely on loading during output, we need var substitution to be done now.
  228. setEventMessages($langs->trans("ErrorFailToRenameFile", $filenamefrom, $filenameto), null, 'errors');
  229. }
  230. }
  231. else
  232. {
  233. $langs->load("errors"); // lang must be loaded because we can't rely on loading during output, we need var substitution to be done now.
  234. setEventMessages($langs->trans("ErrorDestinationAlreadyExists", $filenameto), null, 'errors');
  235. }
  236. }
  237. }
  238. }
  239. }
  240. // Update properties in ECM table
  241. if (GETPOST('ecmfileid', 'int') > 0)
  242. {
  243. $shareenabled = GETPOST('shareenabled', 'alpha');
  244. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  245. $ecmfile = new EcmFiles($db);
  246. $result = $ecmfile->fetch(GETPOST('ecmfileid', 'int'));
  247. if ($result > 0)
  248. {
  249. if ($shareenabled)
  250. {
  251. if (empty($ecmfile->share))
  252. {
  253. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  254. $ecmfile->share = getRandomPassword(true);
  255. }
  256. }
  257. else
  258. {
  259. $ecmfile->share = '';
  260. }
  261. $result = $ecmfile->update($user);
  262. if ($result < 0)
  263. {
  264. setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
  265. }
  266. }
  267. }
  268. }