dolibarr  16.0.5
file_card.php
Go to the documentation of this file.
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 
24 require '../main.inc.php';
25 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
26 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
31 
32 // Load translation files required by page
33 $langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts', 'categories'));
34 
35 $action = GETPOST('action', 'aZ09');
36 $cancel = GETPOST('cancel', 'alpha');
37 $backtopage = GETPOST('backtopage', 'alpha');
38 
39 // Get parameters
40 $socid = GETPOST("socid", "int");
41 
42 // Security check
43 if ($user->socid > 0) {
44  $action = '';
45  $socid = $user->socid;
46 }
47 
48 $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
49 $sortfield = GETPOST('sortfield', 'aZ09comma');
50 $sortorder = GETPOST('sortorder', 'aZ09comma');
51 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
52 if (empty($page) || $page == -1) {
53  $page = 0;
54 } // If $page is not defined, or '' or -1
55 $offset = $limit * $page;
56 $pageprev = $page - 1;
57 $pagenext = $page + 1;
58 if (!$sortorder) {
59  $sortorder = "ASC";
60 }
61 if (!$sortfield) {
62  $sortfield = "label";
63 }
64 
65 $section = GETPOST("section", 'alpha');
66 if (!$section) {
67  dol_print_error('', 'Error, section parameter missing');
68  exit;
69 }
70 $urlfile = (string) dol_sanitizePathName(GETPOST("urlfile"), '_', 0);
71 if (!$urlfile) {
72  dol_print_error('', "ErrorParamNotDefined");
73  exit;
74 }
75 
76 // Load ecm object
77 $ecmdir = new EcmDirectory($db);
78 $result = $ecmdir->fetch(GETPOST("section", 'alpha'));
79 if (!($result > 0)) {
80  dol_print_error($db, $ecmdir->error);
81  exit;
82 }
83 $relativepath = $ecmdir->getRelativePath();
84 $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
85 
86 $fullpath = $conf->ecm->dir_output.'/'.$relativepath.$urlfile;
87 
88 $relativetodocument = 'ecm/'.$relativepath; // $relativepath is relative to ECM dir, we need relative to document
89 $filepath = $relativepath.$urlfile;
90 $filepathtodocument = $relativetodocument.$urlfile;
91 
92 // Try to load object from index
93 $object = new ECMFiles($db);
94 $extrafields = new ExtraFields($db);
95 // fetch optionals attributes and labels
96 $extrafields->fetch_name_optionals_label($object->table_element);
97 
98 $result = $object->fetch(0, '', $filepathtodocument);
99 if ($result < 0) {
100  dol_print_error($db, $object->error, $object->errors);
101  exit;
102 }
103 
104 // Permissions
105 $permtoread = $user->rights->ecm->read;
106 $permtoadd = $user->rights->ecm->setup;
107 $permtoupload = $user->rights->ecm->upload;
108 
109 if (!$permtoread) {
110  accessforbidden();
111 }
112 
113 
114 /*
115  * Actions
116  */
117 
118 if ($cancel) {
119  $action = '';
120  if ($backtopage) {
121  header("Location: ".$backtopage);
122  exit;
123  } else {
124  header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'&section='.urlencode($section).($module ? '&module='.urlencode($module) : ''));
125  exit;
126  }
127 }
128 
129 // Rename file
130 if ($action == 'update' && $permtoadd) {
131  $error = 0;
132 
133  $oldlabel = GETPOST('urlfile', 'alpha');
134  $newlabel = dol_sanitizeFileName(GETPOST('label', 'alpha'), '_', 0);
135  $shareenabled = GETPOST('shareenabled', 'alpha');
136 
137  //$db->begin();
138 
139  $olddir = $ecmdir->getRelativePath(0); // Relative to ecm
140  $olddirrelativetodocument = 'ecm/'.$olddir; // Relative to document
141  $newdirrelativetodocument = 'ecm/'.$olddir;
142  $olddir = $conf->ecm->dir_output.'/'.$olddir;
143  $newdir = $olddir;
144 
145  $oldfile = $olddir.$oldlabel;
146  $newfile = $newdir.$newlabel;
147  $newfileformove = $newfile;
148  // If old file end with .noexe, new file must also end with .noexe
149  if (preg_match('/\.noexe$/', $oldfile) && !preg_match('/\.noexe$/', $newfileformove)) {
150  $newfileformove .= '.noexe';
151  }
152  //var_dump($oldfile);var_dump($newfile);exit;
153 
154  // Now we update index of file
155  $db->begin();
156  //print $oldfile.' - '.$newfile;
157  if ($newlabel != $oldlabel) {
158  $result = dol_move($oldfile, $newfileformove); // This include update of database
159  if (!$result) {
160  $langs->load('errors');
161  setEventMessages($langs->trans('ErrorFailToRenameFile', $oldfile, $newfile), null, 'errors');
162  $error++;
163  }
164 
165  // Reload object after the move
166  $result = $object->fetch(0, '', $newdirrelativetodocument.$newlabel);
167  if ($result < 0) {
168  dol_print_error($db, $object->error, $object->errors);
169  exit;
170  }
171  }
172 
173  if (!$error) {
174  if ($shareenabled) {
175  require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
176  $object->share = getRandomPassword(true);
177  } else {
178  $object->share = '';
179  }
180 
181  if ($object->id > 0) {
182  $ret = $extrafields->setOptionalsFromPost(null, $object);
183  if ($ret < 0) {
184  $error++;
185  }
186  if (!$error) {
187  // Actions on extra fields
188  $result = $object->insertExtraFields();
189  if ($result < 0) {
190  setEventMessages($object->error, $object->errors, 'errors');
191  $error++;
192  }
193  }
194  // Call update to set the share key
195  $result = $object->update($user);
196  if ($result < 0) {
197  setEventMessages($object->error, $object->errors, 'warnings');
198  }
199  } else {
200  // Call create to insert record
201  $object->entity = $conf->entity;
202  $object->filepath = preg_replace('/[\\/]+$/', '', $newdirrelativetodocument);
203  $object->filename = $newlabel;
204  $object->label = md5_file(dol_osencode($newfileformove)); // hash of file content
205  $object->fullpath_orig = '';
206  $object->gen_or_uploaded = 'unknown';
207  $object->description = ''; // indexed content
208  $object->keywords = ''; // keyword content
209  $result = $object->create($user);
210  if ($result < 0) {
211  setEventMessages($object->error, $object->errors, 'warnings');
212  }
213  }
214  }
215 
216  if (!$error) {
217  $db->commit();
218 
219  $urlfile = $newlabel;
220  // If old file end with .noexe, new file must also end with .noexe
221  if (preg_match('/\.noexe$/', $newfileformove)) {
222  $urlfile .= '.noexe';
223  }
224 
225  header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'&section='.urlencode($section));
226  exit;
227  } else {
228  $db->rollback();
229  }
230 }
231 
232 
233 
234 /*
235  * View
236  */
237 
238 $form = new Form($db);
239 
240 llxHeader();
241 
242 $object->section_id = $ecmdir->id;
243 $object->label = $urlfile;
244 $head = ecm_file_prepare_head($object);
245 
246 if ($action == 'edit') {
247  print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
248  print '<input type="hidden" name="token" value="'.newToken().'">';
249  print '<input type="hidden" name="section" value="'.$section.'">';
250  print '<input type="hidden" name="urlfile" value="'.$urlfile.'">';
251  print '<input type="hidden" name="module" value="'.$module.'">';
252  print '<input type="hidden" name="action" value="update">';
253  print '<input type="hidden" name="id" value="'.$object->id.'">';
254 }
255 
256 print dol_get_fiche_head($head, 'card', $langs->trans("File"), -1, 'generic');
257 
258 
259 $s = '';
260 $tmpecmdir = new EcmDirectory($db); // Need to create a new one
261 $tmpecmdir->fetch($ecmdir->id);
262 $result = 1;
263 $i = 0;
264 while ($tmpecmdir && $result > 0) {
265  $tmpecmdir->ref = $tmpecmdir->label;
266  $s = $tmpecmdir->getNomUrl(1).$s;
267  if ($tmpecmdir->fk_parent) {
268  $s = ' -> '.$s;
269  $result = $tmpecmdir->fetch($tmpecmdir->fk_parent);
270  } else {
271  $tmpecmdir = 0;
272  }
273  $i++;
274 }
275 
276 $urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfile);
277 
278 $s = img_picto('', 'object_dir').' <a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> '.$s.' -> ';
279 if ($action == 'edit') {
280  $s .= '<input type="text" name="label" class="quatrevingtpercent" value="'.$urlfiletoshow.'">';
281 } else {
282  $s .= $urlfiletoshow;
283 }
284 
285 $linkback = '';
286 if ($backtopage) {
287  $linkback = '<a href="'.$backtopage.'">'.$langs->trans("BackToTree").'</a>';
288 }
289 
290 $object->ref = ''; // Force to hide ref
291 dol_banner_tab($object, '', $linkback, 0, '', '', $s);
292 
293 print '<div class="fichecenter">';
294 
295 print '<div class="underbanner clearboth"></div>';
296 print '<table class="border centpercent tableforfield">';
297 print '<tr><td class="titlefieldcreate">'.$langs->trans("ECMCreationDate").'</td><td>';
298 print dol_print_date(dol_filemtime($fullpath), 'dayhour');
299 print '</td></tr>';
300 /*print '<tr><td>'.$langs->trans("ECMDirectoryForFiles").'</td><td>';
301 print '/ecm/'.$relativepath;
302 print '</td></tr>';
303 print '<tr><td>'.$langs->trans("ECMNbOfDocs").'</td><td>';
304 print count($filearray);
305 print '</td></tr>';
306 print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td>';
307 print dol_print_size($totalsize);
308 print '</td></tr>';
309 */
310 
311 // Hash of file content
312 print '<tr><td>'.$langs->trans("HashOfFileContent").'</td><td>';
313 $object = new EcmFiles($db);
314 $object->fetch(0, '', $filepathtodocument);
315 if (!empty($object->label)) {
316  print $object->label;
317 } else {
318  print img_warning().' '.$langs->trans("FileNotYetIndexedInDatabase");
319 }
320 print '</td></tr>';
321 
322 // Define $urlwithroot
323 $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
324 $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
325 //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
326 
327 // Link for internal download
328 print '<tr><td>';
329 print $form->textwithpicto($langs->trans("DirectDownloadInternalLink"), $langs->trans("PrivateDownloadLinkDesc"));
330 print '</td><td>';
331 $modulepart = 'ecm';
332 $forcedownload = 1;
333 $rellink = '/document.php?modulepart='.$modulepart;
334 if ($forcedownload) {
335  $rellink .= '&attachment=1';
336 }
337 if (!empty($object->entity)) {
338  $rellink .= '&entity='.$object->entity;
339 }
340 $rellink .= '&file='.urlencode($filepath);
341 $fulllink = $urlwithroot.$rellink;
342 print img_picto('', 'globe').' ';
343 if ($action != 'edit') {
344  print '<input type="text" class="quatrevingtpercent" id="downloadinternallink" name="downloadinternellink" value="'.dol_escape_htmltag($fulllink).'">';
345 } else {
346  print $fulllink;
347 }
348 if ($action != 'edit') {
349  print ' <a href="'.$fulllink.'">'.img_picto($langs->trans("Download"), 'download', 'class="opacitymedium paddingrightonly"').'</a>'; // No target here.
350 }
351 print '</td></tr>';
352 
353 // Link for direct external download
354 print '<tr><td>';
355 if ($action != 'edit') {
356  print $form->textwithpicto($langs->trans("DirectDownloadLink"), $langs->trans("PublicDownloadLinkDesc"));
357 } else {
358  print $form->textwithpicto($langs->trans("FileSharedViaALink"), $langs->trans("PublicDownloadLinkDesc"));
359 }
360 print '</td><td>';
361 if (!empty($object->share)) {
362  if ($action != 'edit') {
363  $forcedownload = 0;
364 
365  $paramlink = '';
366  if (!empty($object->share)) {
367  $paramlink .= ($paramlink ? '&' : '').'hashp='.$object->share; // Hash for public share
368  }
369  if ($forcedownload) {
370  $paramlink .= ($paramlink ? '&' : '').'attachment=1';
371  }
372 
373  $fulllink = $urlwithroot.'/document.php'.($paramlink ? '?'.$paramlink : '');
374  //if (! empty($object->ref)) $fulllink.='&hashn='.$object->ref; // Hash of file path
375  //elseif (! empty($object->label)) $fulllink.='&hashc='.$object->label; // Hash of file content
376 
377  print img_picto('', 'globe').' ';
378  if ($action != 'edit') {
379  print '<input type="text" class="quatrevingtpercent nopadding small" id="downloadlink" name="downloadexternallink" value="'.dol_escape_htmltag($fulllink).'">';
380  } else {
381  print $fulllink;
382  }
383  if ($action != 'edit') {
384  print ' <a href="'.$fulllink.'">'.$langs->trans("Download").'</a>'; // No target here
385  }
386  } else {
387  print '<input type="checkbox" name="shareenabled"'.($object->share ? ' checked="checked"' : '').' /> ';
388  }
389 } else {
390  if ($action != 'edit') {
391  print '<span class="opacitymedium">'.$langs->trans("FileNotShared").'</span>';
392  } else {
393  print '<input type="checkbox" name="shareenabled"'.($object->share ? ' checked="checked"' : '').' /> ';
394  }
395 }
396 print '</td>';
397 print '</tr>';
398 print $object->showOptionals($extrafields, ($action == 'edit' ? 'edit' : 'view'));
399 print '</table>';
400 print '</div>';
401 
402 print ajax_autoselect('downloadinternallink');
403 print ajax_autoselect('downloadlink');
404 
405 print dol_get_fiche_end();
406 
407 if ($action == 'edit') {
408  print $form->buttonsSaveCancel();
409 
410  print '</form>';
411 }
412 
413 
414 // Confirm deletion of a file
415 if ($action == 'deletefile') {
416  print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.urlencode($section), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile', $urlfile), 'confirm_deletefile', '', 1, 1);
417 }
418 
419 if ($action != 'edit') {
420  // Actions buttons
421  print '<div class="tabsAction">';
422 
423  if ($user->rights->ecm->setup) {
424  print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&section='.urlencode($section).'&urlfile='.urlencode($urlfile).'">'.$langs->trans('Edit').'</a>';
425  }
426  /*
427  if ($user->rights->ecm->setup)
428  {
429  print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=deletefile&token='.newToken().'&section='.$section.'&urlfile='.urlencode($urlfile).'">'.$langs->trans('Delete').'</a>';
430  }
431  else
432  {
433  print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans("NotAllowed").'">'.$langs->trans('Delete').'</a>';
434  }
435  */
436  print '</div>';
437 }
438 
439 
440 // End of page
441 llxFooter();
442 $db->close();
dol_sanitizePathName
dol_sanitizePathName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a path name.
Definition: functions.lib.php:1251
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
dol_osencode
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
Definition: functions.lib.php:8499
dol_filemtime
dol_filemtime($pathoffile)
Return time of a file.
Definition: files.lib.php:593
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
img_warning
img_warning($titlealt='default', $moreatt='', $morecss='pictowarning')
Show warning logo.
Definition: functions.lib.php:4521
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
dol_banner_tab
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
Definition: functions.lib.php:2046
getRandomPassword
getRandomPassword($generic=false, $replaceambiguouschars=null, $length=32)
Return a generated password using default module.
Definition: security2.lib.php:454
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
ecm_file_prepare_head
ecm_file_prepare_head($object)
Prepare array with list of tabs.
Definition: ecm.lib.php:101
EcmDirectory
Class to manage ECM directories.
Definition: ecmdirectory.class.php:28
EcmFiles
Class to manage ECM files.
Definition: ecmfiles.class.php:35
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
ExtraFields
Class to manage standard extra fields.
Definition: extrafields.class.php:39
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
ajax_autoselect
ajax_autoselect($htmlname, $addlink='', $textonlink='Link')
Make content of an input box selected when we click into input field.
Definition: functions.lib.php:9681
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
dol_move
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
Definition: files.lib.php:855