file_card.php 13 KB

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