actions_linkedfiles.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <http://www.gnu.org/licenses/>.
  18. * or see http://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')) $generatethumbs=0;
  50. if (! empty($upload_dirold) && ! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO))
  51. {
  52. $result = dol_add_file_process($upload_dirold, 0, 1, 'userfile', GETPOST('savingdocmask', 'alpha'), null, '', $generatethumbs);
  53. }
  54. elseif (! empty($upload_dir))
  55. {
  56. $result = dol_add_file_process($upload_dir, 0, 1, 'userfile', GETPOST('savingdocmask', 'alpha'), null, '', $generatethumbs);
  57. }
  58. }
  59. }
  60. }
  61. elseif (GETPOST('linkit','none') && ! empty($conf->global->MAIN_UPLOAD_DOC))
  62. {
  63. $link = GETPOST('link', 'alpha');
  64. if ($link)
  65. {
  66. if (substr($link, 0, 7) != 'http://' && substr($link, 0, 8) != 'https://' && substr($link, 0, 7) != 'file://') {
  67. $link = 'http://' . $link;
  68. }
  69. dol_add_file_process($upload_dir, 0, 1, 'userfile', null, $link, '', 0);
  70. }
  71. }
  72. // Delete file/link
  73. if ($action == 'confirm_deletefile' && $confirm == 'yes')
  74. {
  75. $urlfile = GETPOST('urlfile', 'alpha', 0, null, null, 1); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  76. if (GETPOST('section', 'alpha')) // For a delete from the ECM module, upload_dir is ECM root dir and urlfile contains relative path from upload_dir
  77. {
  78. $file = $upload_dir . (preg_match('/\/$/', $upload_dir) ? '' : '/') . $urlfile;
  79. }
  80. 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.
  81. {
  82. $urlfile=basename($urlfile);
  83. $file = $upload_dir . (preg_match('/\/$/', $upload_dir) ? '' : '/') . $urlfile;
  84. if (! empty($upload_dirold)) $fileold = $upload_dirold . "/" . $urlfile;
  85. }
  86. $linkid = GETPOST('linkid', 'int');
  87. if ($urlfile) // delete of a file
  88. {
  89. $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine
  90. $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette (if file is an image)
  91. $ret = dol_delete_file($file, 0, 0, 0, (is_object($object)?$object:null));
  92. if (! empty($fileold)) dol_delete_file($fileold, 0, 0, 0, (is_object($object)?$object:null)); // Delete file using old path
  93. // Si elle existe, on efface la vignette
  94. if (preg_match('/(\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff)$/i',$file,$regs))
  95. {
  96. $photo_vignette=basename(preg_replace('/'.$regs[0].'/i','',$file).'_small'.$regs[0]);
  97. if (file_exists(dol_osencode($dirthumb.$photo_vignette)))
  98. {
  99. dol_delete_file($dirthumb.$photo_vignette);
  100. }
  101. $photo_vignette=basename(preg_replace('/'.$regs[0].'/i','',$file).'_mini'.$regs[0]);
  102. if (file_exists(dol_osencode($dirthumb.$photo_vignette)))
  103. {
  104. dol_delete_file($dirthumb.$photo_vignette);
  105. }
  106. }
  107. if ($ret) setEventMessages($langs->trans("FileWasRemoved", $urlfile), null, 'mesgs');
  108. else setEventMessages($langs->trans("ErrorFailToDeleteFile", $urlfile), null, 'errors');
  109. }
  110. elseif ($linkid) // delete of external link
  111. {
  112. require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php';
  113. $link = new Link($db);
  114. $link->id = $linkid;
  115. $link->fetch();
  116. $res = $link->delete($user);
  117. $langs->load('link');
  118. if ($res > 0) {
  119. setEventMessages($langs->trans("LinkRemoved", $link->label), null, 'mesgs');
  120. } else {
  121. if (count($link->errors)) {
  122. setEventMessages('', $link->errors, 'errors');
  123. } else {
  124. setEventMessages($langs->trans("ErrorFailedToDeleteLink", $link->label), null, 'errors');
  125. }
  126. }
  127. }
  128. if (is_object($object) && $object->id > 0)
  129. {
  130. if ($backtopage)
  131. {
  132. header('Location: ' . $backtopage);
  133. exit;
  134. }
  135. else
  136. {
  137. header('Location: '.$_SERVER["PHP_SELF"].'?id='.$object->id.(GETPOST('section_dir','alpha')?'&section_dir='.urlencode(GETPOST('section_dir','alpha')):'').(!empty($withproject)?'&withproject=1':''));
  138. exit;
  139. }
  140. }
  141. }
  142. elseif ($action == 'confirm_updateline' && GETPOST('save','alpha') && GETPOST('link', 'alpha'))
  143. {
  144. require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php';
  145. $langs->load('link');
  146. $link = new Link($db);
  147. $link->id = GETPOST('linkid', 'int');
  148. $f = $link->fetch();
  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', 'alpha');
  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. if ($filenamefrom != $filenameto)
  176. {
  177. // Security:
  178. // Disallow file with some extensions. We rename them.
  179. // Because if we put the documents directory into a directory inside web root (very bad), this allows to execute on demand arbitrary code.
  180. if (preg_match('/\.htm|\.html|\.php|\.pl|\.cgi$/i',$filenameto) && empty($conf->global->MAIN_DOCUMENT_IS_OUTSIDE_WEBROOT_SO_NOEXE_NOT_REQUIRED))
  181. {
  182. $filenameto.= '.noexe';
  183. }
  184. if ($filenamefrom && $filenameto)
  185. {
  186. $srcpath = $upload_dir.'/'.$filenamefrom;
  187. $destpath = $upload_dir.'/'.$filenameto;
  188. $reshook=$hookmanager->initHooks(array('actionlinkedfiles'));
  189. $parameters=array('filenamefrom' => $filenamefrom, 'filenameto' => $filenameto, 'upload_dir' => $upload_dir);
  190. $reshook=$hookmanager->executeHooks('renameUploadedFile', $parameters, $object);
  191. if (empty($reshook))
  192. {
  193. if (! file_exists($destpath))
  194. {
  195. $result = dol_move($srcpath, $destpath);
  196. if ($result)
  197. {
  198. // Define if we have to generate thumbs or not
  199. $generatethumbs = 1;
  200. // When we rename a file from the file manager in ecm, we must not regenerate thumbs (not a problem, we do pass here)
  201. // When we rename a file from the website module, we must not regenerate thumbs (module = medias in such a case)
  202. // but when we rename from a tab "Documents", we must regenerate thumbs
  203. if (GETPOST('modulepart') == 'medias') $generatethumbs=0;
  204. if ($generatethumbs)
  205. {
  206. if ($object->id)
  207. {
  208. $object->addThumbs($destpath);
  209. }
  210. // TODO Add revert function of addThumbs to remove thumbs with old name
  211. //$object->delThumbs($srcpath);
  212. }
  213. setEventMessages($langs->trans("FileRenamed"), null);
  214. }
  215. else
  216. {
  217. $langs->load("errors"); // key must be loaded because we can't rely on loading during output, we need var substitution to be done now.
  218. setEventMessages($langs->trans("ErrorFailToRenameFile", $filenamefrom, $filenameto), null, 'errors');
  219. }
  220. }
  221. else
  222. {
  223. $langs->load("errors"); // key must be loaded because we can't rely on loading during output, we need var substitution to be done now.
  224. setEventMessages($langs->trans("ErrorDestinationAlreadyExists", $filenameto), null, 'errors');
  225. }
  226. }
  227. }
  228. }
  229. }
  230. // Update properties in ECM table
  231. if (GETPOST('ecmfileid', 'int') > 0)
  232. {
  233. $shareenabled = GETPOST('shareenabled', 'alpha');
  234. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  235. $ecmfile=new EcmFiles($db);
  236. $result = $ecmfile->fetch(GETPOST('ecmfileid', 'int'));
  237. if ($result > 0)
  238. {
  239. if ($shareenabled)
  240. {
  241. if (empty($ecmfile->share))
  242. {
  243. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  244. $ecmfile->share = getRandomPassword(true);
  245. }
  246. }
  247. else
  248. {
  249. $ecmfile->share = '';
  250. }
  251. $result = $ecmfile->update($user);
  252. if ($result < 0)
  253. {
  254. setEventMessages($ecmfile->error, $ecmfile->errors, 'warnings');
  255. }
  256. }
  257. }
  258. }