actions_linkedfiles.inc.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. //var_dump($upload_dir);
  23. //var_dump($upload_dirold);
  24. // Submit file/link
  25. if (GETPOST('sendit') && ! empty($conf->global->MAIN_UPLOAD_DOC))
  26. {
  27. if ($object->id)
  28. {
  29. if (! empty($upload_dirold) && ! empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO))
  30. dol_add_file_process($upload_dirold, 0, 1, 'userfile', GETPOST('savingdocmask'));
  31. else
  32. dol_add_file_process($upload_dir, 0, 1, 'userfile', GETPOST('savingdocmask'));
  33. }
  34. }
  35. elseif (GETPOST('linkit') && ! empty($conf->global->MAIN_UPLOAD_DOC))
  36. {
  37. if ($object->id)
  38. {
  39. $link = GETPOST('link', 'alpha');
  40. if ($link)
  41. {
  42. if (substr($link, 0, 7) != 'http://' && substr($link, 0, 8) != 'https://' && substr($link, 0, 7) != 'file://') {
  43. $link = 'http://' . $link;
  44. }
  45. dol_add_file_process($upload_dir, 0, 1, 'userfile', null, $link);
  46. }
  47. }
  48. }
  49. // Delete file/link
  50. if ($action == 'confirm_deletefile' && $confirm == 'yes')
  51. {
  52. if ($object->id)
  53. {
  54. $urlfile = GETPOST('urlfile', 'alpha'); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  55. if (GETPOST('section')) $file = $upload_dir . "/" . $urlfile; // For a delete of GED module urlfile contains full path from upload_dir
  56. else // For documents pages, upload_dir contains already path to file from module dir, so we clean path into urlfile.
  57. {
  58. $urlfile=basename($urlfile);
  59. $file = $upload_dir . "/" . $urlfile;
  60. if (! empty($upload_dirold)) $fileold = $upload_dirold . "/" . $urlfile;
  61. }
  62. $linkid = GETPOST('linkid', 'int'); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP).
  63. if ($urlfile)
  64. {
  65. $dir = dirname($file).'/'; // Chemin du dossier contenant l'image d'origine
  66. $dirthumb = $dir.'/thumbs/'; // Chemin du dossier contenant la vignette
  67. $ret = dol_delete_file($file, 0, 0, 0, $object);
  68. if (! empty($fileold)) dol_delete_file($fileold, 0, 0, 0, $object); // Delete file using old path
  69. // Si elle existe, on efface la vignette
  70. if (preg_match('/(\.jpg|\.jpeg|\.bmp|\.gif|\.png|\.tiff)$/i',$file,$regs))
  71. {
  72. $photo_vignette=basename(preg_replace('/'.$regs[0].'/i','',$file).'_small'.$regs[0]);
  73. if (file_exists(dol_osencode($dirthumb.$photo_vignette)))
  74. {
  75. dol_delete_file($dirthumb.$photo_vignette);
  76. }
  77. $photo_vignette=basename(preg_replace('/'.$regs[0].'/i','',$file).'_mini'.$regs[0]);
  78. if (file_exists(dol_osencode($dirthumb.$photo_vignette)))
  79. {
  80. dol_delete_file($dirthumb.$photo_vignette);
  81. }
  82. }
  83. if ($ret) setEventMessages($langs->trans("FileWasRemoved", $urlfile), null, 'mesgs');
  84. else setEventMessages($langs->trans("ErrorFailToDeleteFile", $urlfile), null, 'errors');
  85. }
  86. elseif ($linkid)
  87. {
  88. require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php';
  89. $link = new Link($db);
  90. $link->id = $linkid;
  91. $link->fetch();
  92. $res = $link->delete($user);
  93. $langs->load('link');
  94. if ($res > 0) {
  95. setEventMessages($langs->trans("LinkRemoved", $link->label), null, 'mesgs');
  96. } else {
  97. if (count($link->errors)) {
  98. setEventMessages('', $link->errors, 'errors');
  99. } else {
  100. setEventMessages($langs->trans("ErrorFailedToDeleteLink", $link->label), null, 'errors');
  101. }
  102. }
  103. }
  104. header('Location: ' . $_SERVER["PHP_SELF"] . '?id=' . $object->id.(!empty($withproject)?'&withproject=1':''));
  105. exit;
  106. }
  107. }
  108. elseif ($action == 'confirm_updateline' && GETPOST('save') && GETPOST('link', 'alpha'))
  109. {
  110. require_once DOL_DOCUMENT_ROOT . '/core/class/link.class.php';
  111. $langs->load('link');
  112. $link = new Link($db);
  113. $link->id = GETPOST('linkid', 'int');
  114. $f = $link->fetch();
  115. if ($f)
  116. {
  117. $link->url = GETPOST('link', 'alpha');
  118. if (substr($link->url, 0, 7) != 'http://' && substr($link->url, 0, 8) != 'https://' && substr($link->url, 0, 7) != 'file://')
  119. {
  120. $link->url = 'http://' . $link->url;
  121. }
  122. $link->label = GETPOST('label', 'alpha');
  123. $res = $link->update($user);
  124. if (!$res)
  125. {
  126. setEventMessages($langs->trans("ErrorFailedToUpdateLink", $link->label), null, 'mesgs');
  127. }
  128. }
  129. else
  130. {
  131. //error fetching
  132. }
  133. }
  134. elseif ($action == 'renamefile' && GETPOST('renamefilesave'))
  135. {
  136. if ($object->id)
  137. {
  138. // For documents pages, upload_dir contains already path to file from module dir, so we clean path into urlfile.
  139. //var_dump($upload_dir);exit;
  140. if (! empty($upload_dir))
  141. {
  142. $filenamefrom=dol_sanitizeFileName(GETPOST('renamefilefrom'));
  143. $filenameto=dol_sanitizeFileName(GETPOST('renamefileto'));
  144. if ($filenamefrom && $filenameto)
  145. {
  146. $srcpath = $upload_dir.'/'.$filenamefrom;
  147. $destpath = $upload_dir.'/'.$filenameto;
  148. $result = dol_move($srcpath, $destpath);
  149. if ($result)
  150. {
  151. $object->addThumbs($destpath);
  152. // TODO Add revert function of addThumbs
  153. //$object->delThumbs($srcpath);
  154. setEventMessages($langs->trans("FileRenamed"), null);
  155. }
  156. else
  157. {
  158. $langs->load("errors"); // key must be loaded because we can't rely on loading during output, we need var substitution to be done now.
  159. setEventMessages($langs->trans("ErrorFailToRenameFile", $filenamefrom, $filenameto), null, 'errors');
  160. }
  161. }
  162. }
  163. }
  164. }