file_card.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. /* Copyright (C) 2008-2020 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/ecm/file_card.php
  19. * \ingroup ecm
  20. * \brief Card of a file for ECM module
  21. */
  22. // Load Dolibarr environment
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  30. // Load translation files required by page
  31. $langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts', 'categories'));
  32. $action = GETPOST('action', 'aZ09');
  33. $cancel = GETPOST('cancel', 'alpha');
  34. $backtopage = GETPOST('backtopage', 'alpha');
  35. // Get parameters
  36. $socid = GETPOST("socid", "int");
  37. // Security check
  38. if ($user->socid > 0) {
  39. $action = '';
  40. $socid = $user->socid;
  41. }
  42. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  43. $sortfield = GETPOST('sortfield', 'aZ09comma');
  44. $sortorder = GETPOST('sortorder', 'aZ09comma');
  45. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  46. if (empty($page) || $page == -1) {
  47. $page = 0;
  48. } // If $page is not defined, or '' or -1
  49. $offset = $limit * $page;
  50. $pageprev = $page - 1;
  51. $pagenext = $page + 1;
  52. if (!$sortorder) {
  53. $sortorder = "ASC";
  54. }
  55. if (!$sortfield) {
  56. $sortfield = "label";
  57. }
  58. $section = GETPOST("section", 'alpha');
  59. if (!$section) {
  60. dol_print_error('', 'Error, section parameter missing');
  61. exit;
  62. }
  63. $urlfile = (string) dol_sanitizePathName(GETPOST("urlfile"), '_', 0);
  64. if (!$urlfile) {
  65. dol_print_error('', "ErrorParamNotDefined");
  66. exit;
  67. }
  68. // Load ecm object
  69. $ecmdir = new EcmDirectory($db);
  70. $result = $ecmdir->fetch(GETPOST("section", 'alpha'));
  71. if (!($result > 0)) {
  72. dol_print_error($db, $ecmdir->error);
  73. exit;
  74. }
  75. $relativepath = $ecmdir->getRelativePath();
  76. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  77. $fullpath = $conf->ecm->dir_output.'/'.$relativepath.$urlfile;
  78. $relativetodocument = 'ecm/'.$relativepath; // $relativepath is relative to ECM dir, we need relative to document
  79. $filepath = $relativepath.$urlfile;
  80. $filepathtodocument = $relativetodocument.$urlfile;
  81. // Try to load object from index
  82. $object = new EcmFiles($db);
  83. $extrafields = new ExtraFields($db);
  84. // fetch optionals attributes and labels
  85. $extrafields->fetch_name_optionals_label($object->table_element);
  86. $result = $object->fetch(0, '', $filepathtodocument);
  87. if ($result < 0) {
  88. dol_print_error($db, $object->error, $object->errors);
  89. exit;
  90. }
  91. // Permissions
  92. $permissiontoread = $user->rights->ecm->read;
  93. $permissiontoadd = $user->rights->ecm->setup;
  94. $permissiontoupload = $user->rights->ecm->upload;
  95. if (!$permissiontoread) {
  96. accessforbidden();
  97. }
  98. /*
  99. * Actions
  100. */
  101. if ($cancel) {
  102. $action = '';
  103. if ($backtopage) {
  104. header("Location: ".$backtopage);
  105. exit;
  106. } else {
  107. header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'&section='.urlencode($section).($module ? '&module='.urlencode($module) : ''));
  108. exit;
  109. }
  110. }
  111. // Rename file
  112. if ($action == 'update' && $permissiontoadd) {
  113. $error = 0;
  114. $oldlabel = GETPOST('urlfile', 'alpha');
  115. $newlabel = dol_sanitizeFileName(GETPOST('label', 'alpha'), '_', 0);
  116. $shareenabled = GETPOST('shareenabled', 'alpha');
  117. //$db->begin();
  118. $olddir = $ecmdir->getRelativePath(0); // Relative to ecm
  119. $olddirrelativetodocument = 'ecm/'.$olddir; // Relative to document
  120. $newdirrelativetodocument = 'ecm/'.$olddir;
  121. $olddir = $conf->ecm->dir_output.'/'.$olddir;
  122. $newdir = $olddir;
  123. $oldfile = $olddir.$oldlabel;
  124. $newfile = $newdir.$newlabel;
  125. $newfileformove = $newfile;
  126. // If old file end with .noexe, new file must also end with .noexe
  127. if (preg_match('/\.noexe$/', $oldfile) && !preg_match('/\.noexe$/', $newfileformove)) {
  128. $newfileformove .= '.noexe';
  129. }
  130. //var_dump($oldfile);var_dump($newfile);exit;
  131. // Now we update index of file
  132. $db->begin();
  133. //print $oldfile.' - '.$newfile;
  134. if ($newlabel != $oldlabel) {
  135. $result = dol_move($oldfile, $newfileformove); // This include update of database
  136. if (!$result) {
  137. $langs->load('errors');
  138. setEventMessages($langs->trans('ErrorFailToRenameFile', $oldfile, $newfile), null, 'errors');
  139. $error++;
  140. }
  141. // Reload object after the move
  142. $result = $object->fetch(0, '', $newdirrelativetodocument.$newlabel);
  143. if ($result < 0) {
  144. dol_print_error($db, $object->error, $object->errors);
  145. exit;
  146. }
  147. }
  148. if (!$error) {
  149. if ($shareenabled) {
  150. require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  151. $object->share = getRandomPassword(true);
  152. } else {
  153. $object->share = '';
  154. }
  155. if ($object->id > 0) {
  156. $ret = $extrafields->setOptionalsFromPost(null, $object);
  157. if ($ret < 0) {
  158. $error++;
  159. }
  160. if (!$error) {
  161. // Actions on extra fields
  162. $result = $object->insertExtraFields();
  163. if ($result < 0) {
  164. setEventMessages($object->error, $object->errors, 'errors');
  165. $error++;
  166. }
  167. }
  168. // Call update to set the share key
  169. $result = $object->update($user);
  170. if ($result < 0) {
  171. setEventMessages($object->error, $object->errors, 'warnings');
  172. }
  173. } else {
  174. // Call create to insert record
  175. $object->entity = $conf->entity;
  176. $object->filepath = preg_replace('/[\\/]+$/', '', $newdirrelativetodocument);
  177. $object->filename = $newlabel;
  178. $object->label = md5_file(dol_osencode($newfileformove)); // hash of file content
  179. $object->fullpath_orig = '';
  180. $object->gen_or_uploaded = 'unknown';
  181. $object->description = ''; // indexed content
  182. $object->keywords = ''; // keyword content
  183. $result = $object->create($user);
  184. if ($result < 0) {
  185. setEventMessages($object->error, $object->errors, 'warnings');
  186. }
  187. }
  188. }
  189. if (!$error) {
  190. $db->commit();
  191. $urlfile = $newlabel;
  192. // If old file end with .noexe, new file must also end with .noexe
  193. if (preg_match('/\.noexe$/', $newfileformove)) {
  194. $urlfile .= '.noexe';
  195. }
  196. header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'&section='.urlencode($section));
  197. exit;
  198. } else {
  199. $db->rollback();
  200. }
  201. }
  202. /*
  203. * View
  204. */
  205. $form = new Form($db);
  206. llxHeader();
  207. $object->section_id = $ecmdir->id;
  208. $object->label = $urlfile;
  209. $head = ecm_file_prepare_head($object);
  210. if ($action == 'edit') {
  211. print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  212. print '<input type="hidden" name="token" value="'.newToken().'">';
  213. print '<input type="hidden" name="section" value="'.$section.'">';
  214. print '<input type="hidden" name="urlfile" value="'.$urlfile.'">';
  215. print '<input type="hidden" name="module" value="'.$module.'">';
  216. print '<input type="hidden" name="action" value="update">';
  217. print '<input type="hidden" name="id" value="'.$object->id.'">';
  218. }
  219. print dol_get_fiche_head($head, 'card', $langs->trans("File"), -1, 'generic');
  220. $s = '';
  221. $tmpecmdir = new EcmDirectory($db); // Need to create a new one
  222. $tmpecmdir->fetch($ecmdir->id);
  223. $result = 1;
  224. $i = 0;
  225. while ($tmpecmdir && $result > 0) {
  226. $tmpecmdir->ref = $tmpecmdir->label;
  227. $s = $tmpecmdir->getNomUrl(1).$s;
  228. if ($tmpecmdir->fk_parent) {
  229. $s = ' -> '.$s;
  230. $result = $tmpecmdir->fetch($tmpecmdir->fk_parent);
  231. } else {
  232. $tmpecmdir = 0;
  233. }
  234. $i++;
  235. }
  236. $urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfile);
  237. $s = img_picto('', 'object_dir').' <a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> '.$s.' -> ';
  238. if ($action == 'edit') {
  239. $s .= '<input type="text" name="label" class="quatrevingtpercent" value="'.$urlfiletoshow.'">';
  240. } else {
  241. $s .= $urlfiletoshow;
  242. }
  243. $linkback = '';
  244. if ($backtopage) {
  245. $linkback = '<a href="'.$backtopage.'">'.$langs->trans("BackToTree").'</a>';
  246. }
  247. $object->ref = ''; // Force to hide ref
  248. dol_banner_tab($object, '', $linkback, 0, '', '', $s);
  249. print '<div class="fichecenter">';
  250. print '<div class="underbanner clearboth"></div>';
  251. print '<table class="border centpercent tableforfield">';
  252. print '<tr><td class="titlefieldcreate">'.$langs->trans("ECMCreationDate").'</td><td>';
  253. print dol_print_date(dol_filemtime($fullpath), 'dayhour');
  254. print '</td></tr>';
  255. /*print '<tr><td>'.$langs->trans("ECMDirectoryForFiles").'</td><td>';
  256. print '/ecm/'.$relativepath;
  257. print '</td></tr>';
  258. print '<tr><td>'.$langs->trans("ECMNbOfDocs").'</td><td>';
  259. print count($filearray);
  260. print '</td></tr>';
  261. print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td>';
  262. print dol_print_size($totalsize);
  263. print '</td></tr>';
  264. */
  265. // Hash of file content
  266. print '<tr><td>'.$langs->trans("HashOfFileContent").'</td><td>';
  267. $object = new EcmFiles($db);
  268. $object->fetch(0, '', $filepathtodocument);
  269. if (!empty($object->label)) {
  270. print $object->label;
  271. } else {
  272. print img_warning().' '.$langs->trans("FileNotYetIndexedInDatabase");
  273. }
  274. print '</td></tr>';
  275. // Define $urlwithroot
  276. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  277. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  278. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  279. // Link for internal download
  280. print '<tr><td>';
  281. print $form->textwithpicto($langs->trans("DirectDownloadInternalLink"), $langs->trans("PrivateDownloadLinkDesc"));
  282. print '</td><td>';
  283. $modulepart = 'ecm';
  284. $forcedownload = 1;
  285. $rellink = '/document.php?modulepart='.$modulepart;
  286. if ($forcedownload) {
  287. $rellink .= '&attachment=1';
  288. }
  289. if (!empty($object->entity)) {
  290. $rellink .= '&entity='.$object->entity;
  291. }
  292. $rellink .= '&file='.urlencode($filepath);
  293. $fulllink = $urlwithroot.$rellink;
  294. print img_picto('', 'globe').' ';
  295. if ($action != 'edit') {
  296. print '<input type="text" class="maxquatrevingtpercent widthcentpercentminusxx" id="downloadinternallink" name="downloadinternellink" value="'.dol_escape_htmltag($fulllink).'">';
  297. } else {
  298. print $fulllink;
  299. }
  300. if ($action != 'edit') {
  301. print ' <a href="'.$fulllink.'">'.img_picto($langs->trans("Download"), 'download', 'class="opacitymedium paddingrightonly"').'</a>'; // No target here.
  302. }
  303. print '</td></tr>';
  304. // Link for direct external download
  305. print '<tr><td>';
  306. if ($action != 'edit') {
  307. print $form->textwithpicto($langs->trans("DirectDownloadLink"), $langs->trans("PublicDownloadLinkDesc"));
  308. } else {
  309. print $form->textwithpicto($langs->trans("FileSharedViaALink"), $langs->trans("PublicDownloadLinkDesc"));
  310. }
  311. print '</td><td>';
  312. if (!empty($object->share)) {
  313. if ($action != 'edit') {
  314. $forcedownload = 0;
  315. $paramlink = '';
  316. if (!empty($object->share)) {
  317. $paramlink .= ($paramlink ? '&' : '').'hashp='.$object->share; // Hash for public share
  318. }
  319. if ($forcedownload) {
  320. $paramlink .= ($paramlink ? '&' : '').'attachment=1';
  321. }
  322. $fulllink = $urlwithroot.'/document.php'.($paramlink ? '?'.$paramlink : '');
  323. //if (!empty($object->ref)) $fulllink.='&hashn='.$object->ref; // Hash of file path
  324. //elseif (!empty($object->label)) $fulllink.='&hashc='.$object->label; // Hash of file content
  325. print img_picto('', 'globe').' ';
  326. if ($action != 'edit') {
  327. print '<input type="text" class="quatrevingtpercent nopadding small" id="downloadlink" name="downloadexternallink" value="'.dol_escape_htmltag($fulllink).'">';
  328. } else {
  329. print $fulllink;
  330. }
  331. if ($action != 'edit') {
  332. print ' <a href="'.$fulllink.'">'.$langs->trans("Download").'</a>'; // No target here
  333. }
  334. } else {
  335. print '<input type="checkbox" name="shareenabled"'.($object->share ? ' checked="checked"' : '').' /> ';
  336. }
  337. } else {
  338. if ($action != 'edit') {
  339. print '<span class="opacitymedium">'.$langs->trans("FileNotShared").'</span>';
  340. } else {
  341. print '<input type="checkbox" name="shareenabled"'.($object->share ? ' checked="checked"' : '').' /> ';
  342. }
  343. }
  344. print '</td>';
  345. print '</tr>';
  346. print $object->showOptionals($extrafields, ($action == 'edit' ? 'edit' : 'view'));
  347. print '</table>';
  348. print '</div>';
  349. print ajax_autoselect('downloadinternallink');
  350. print ajax_autoselect('downloadlink');
  351. print dol_get_fiche_end();
  352. if ($action == 'edit') {
  353. print $form->buttonsSaveCancel();
  354. print '</form>';
  355. }
  356. // Confirm deletion of a file
  357. if ($action == 'deletefile') {
  358. print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.urlencode($section), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile', $urlfile), 'confirm_deletefile', '', 1, 1);
  359. }
  360. if ($action != 'edit') {
  361. // Actions buttons
  362. print '<div class="tabsAction">';
  363. if ($user->hasRight('ecm', 'setup')) {
  364. print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&section='.urlencode($section).'&urlfile='.urlencode($urlfile).'">'.$langs->trans('Edit').'</a>';
  365. }
  366. //print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', $user->rights->ecm->setup);
  367. print '</div>';
  368. }
  369. // End of page
  370. llxFooter();
  371. $db->close();