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