file_card.php 12 KB

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