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