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