dolibarr 25.0.0-alpha
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-2026 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';
38require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
39require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
40require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
41require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
42require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
43require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
44
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', 'aZ09arobase');
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
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 $paramscancel = array('urlfile' => $urlfile, 'section' => $section);
139 if ($module) {
140 $paramscancel['module'] = $module;
141 }
142 header('Location: '.dolBuildUrl($_SERVER["PHP_SELF"], $paramscancel));
143 exit;
144 }
145}
146
147// Rename file
148if ($action == 'update' && $permissiontoadd) {
149 $error = 0;
150
151 $oldlabel = GETPOST('urlfile', 'alpha');
152 $newlabel = dol_sanitizeFileName(GETPOST('label', 'alpha'), '_', 0);
153 $shareenabled = GETPOST('shareenabled', 'alpha');
154
155 //$db->begin();
156
157 $olddir = $ecmdir->getRelativePath(0); // Relative to ecm
158 $olddirrelativetodocument = 'ecm/'.$olddir; // Relative to document
159 $newdirrelativetodocument = 'ecm/'.$olddir;
160 $olddir = $conf->ecm->dir_output.'/'.$olddir;
161 $newdir = $olddir;
162
163 $oldfile = $olddir.$oldlabel;
164 $newfile = $newdir.$newlabel;
165 $newfileformove = $newfile;
166 // If old file end with .noexe, new file must also end with .noexe
167 if (preg_match('/\.noexe$/', $oldfile) && !preg_match('/\.noexe$/', $newfileformove)) {
168 $newfileformove .= '.noexe';
169 }
170 //var_dump($oldfile);var_dump($newfile);exit;
171
172 // Now we update index of file
173 $db->begin();
174 //print $oldfile.' - '.$newfile;
175 if ($newlabel != $oldlabel) {
176 $result = dol_move($oldfile, $newfileformove); // This include update of database
177 if (!$result) {
178 $langs->load('errors');
179 setEventMessages($langs->trans('ErrorFailToRenameFile', $oldfile, $newfile), null, 'errors');
180 $error++;
181 }
182
183 // Reload object after the move
184 $result = $object->fetch(0, '', $newdirrelativetodocument.$newlabel);
185 if ($result < 0) {
186 dol_print_error($db, $object->error, $object->errors);
187 exit;
188 }
189 }
190
191 if (!$error) {
192 if ($shareenabled) {
193 require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
194 $object->share = getRandomPassword(true);
195 } else {
196 $object->share = '';
197 }
198
199 if ($object->id > 0) {
200 $ret = $extrafields->setOptionalsFromPost(null, $object);
201 if ($ret < 0) {
202 $error++;
203 }
204 if (!$error) {
205 // Actions on extra fields
206 $result = $object->insertExtraFields();
207 if ($result < 0) {
208 setEventMessages($object->error, $object->errors, 'errors');
209 $error++;
210 }
211 }
212 // Call update to set the share key
213 $result = $object->update($user);
214 if ($result < 0) {
215 setEventMessages($object->error, $object->errors, 'warnings');
216 }
217 } else {
218 // Call create to insert record
219 $object->entity = $conf->entity;
220 $object->filepath = preg_replace('/[\\/]+$/', '', $newdirrelativetodocument);
221 $object->filename = $newlabel;
222 $object->label = md5_file(dol_osencode($newfileformove)); // hash of file content
223 $object->fullpath_orig = '';
224 $object->gen_or_uploaded = 'unknown';
225 $object->description = ''; // indexed content
226 $object->keywords = ''; // keyword content
227 $result = $object->create($user);
228 if ($result < 0) {
229 setEventMessages($object->error, $object->errors, 'warnings');
230 }
231 }
232 }
233
234 if (!$error) {
235 $db->commit();
236
237 $urlfile = $newlabel;
238 // If old file end with .noexe, new file must also end with .noexe
239 if (preg_match('/\.noexe$/', $newfileformove)) {
240 $urlfile .= '.noexe';
241 }
242
243 header('Location: '.dolBuildUrl($_SERVER["PHP_SELF"], array('urlfile' => $urlfile, 'section' => $section)));
244 exit;
245 } else {
246 $db->rollback();
247 }
248}
249
250
251
252/*
253 * View
254 */
255
256$form = new Form($db);
257
258llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-ecm page-file_card');
259
260$object->section_id = $ecmdir->id;
261$object->label = $urlfile;
262$head = ecm_file_prepare_head($object);
263
264if ($action == 'edit') {
265 print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
266 print '<input type="hidden" name="token" value="'.newToken().'">';
267 print '<input type="hidden" name="section" value="'.$section.'">';
268 print '<input type="hidden" name="urlfile" value="'.$urlfile.'">';
269 print '<input type="hidden" name="module" value="'.$module.'">';
270 print '<input type="hidden" name="action" value="update">';
271 print '<input type="hidden" name="id" value="'.$object->id.'">';
272}
273
274print dol_get_fiche_head($head, 'card', $langs->trans("File"), -1, 'generic');
275
276
277$s = '';
278$tmpecmdir = new EcmDirectory($db); // Need to create a new one
279$tmpecmdir->fetch($ecmdir->id);
280$result = 1;
281$i = 0;
282while ($tmpecmdir && $result > 0) {
283 $tmpecmdir->ref = $tmpecmdir->label;
284 $s = $tmpecmdir->getNomUrl(1).$s;
285 if ($tmpecmdir->fk_parent) {
286 $s = ' -> '.$s;
287 $result = $tmpecmdir->fetch($tmpecmdir->fk_parent);
288 } else {
289 $tmpecmdir = 0;
290 }
291 $i++;
292}
293
294$urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfile);
295
296$s = img_picto('', 'object_dir').' <a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> '.$s.' -> ';
297if ($action == 'edit') {
298 $s .= '<input type="text" name="label" class="quatrevingtpercent" value="'.$urlfiletoshow.'">';
299} else {
300 $s .= $urlfiletoshow;
301}
302
303$linkback = '';
304if ($backtopage) {
305 $linkback = '<a href="'.$backtopage.'">'.$langs->trans("BackToTree").'</a>';
306}
307
308$object->ref = ''; // Force to hide ref
309dol_banner_tab($object, '', $linkback, 0, '', '', $s);
310
311print '<div class="fichecenter">';
312
313print '<div class="underbanner clearboth"></div>';
314print '<table class="border centpercent tableforfield">';
315print '<tr><td class="titlefieldcreate">'.$langs->trans("ECMCreationDate").'</td><td>';
316print dol_print_date(dol_filemtime($fullpath), 'dayhour');
317print '</td></tr>';
318/*print '<tr><td>'.$langs->trans("ECMDirectoryForFiles").'</td><td>';
319print '/ecm/'.$relativepath;
320print '</td></tr>';
321print '<tr><td>'.$langs->trans("ECMNbOfDocs").'</td><td>';
322print count($filearray);
323print '</td></tr>';
324print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td>';
325print dol_print_size($totalsize);
326print '</td></tr>';
327*/
328
329// Hash of file content
330print '<tr><td>'.$langs->trans("HashOfFileContent").'</td><td>';
331$object = new EcmFiles($db);
332$object->fetch(0, '', $filepathtodocument);
333if (!empty($object->label)) {
334 print $object->label;
335} else {
336 print img_warning().' '.$langs->trans("FileNotYetIndexedInDatabase");
337}
338print '</td></tr>';
339
340// Define $urlwithroot
341$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
342$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
343//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
344
345// Link for internal download
346print '<tr><td>';
347print $form->textwithpicto($langs->trans("DirectDownloadInternalLink"), $langs->trans("PrivateDownloadLinkDesc"));
348print '</td><td>';
349$modulepart = 'ecm';
350$paramsrellink = array('modulepart' => $modulepart, 'attachment' => 1);
351if (!empty($object->entity)) {
352 $paramsrellink['entity'] = $object->entity;
353}
354$paramsrellink['file'] = $filepath;
355$rellink = dolBuildUrl('/document.php', $paramsrellink);
356$fulllink = $urlwithroot.$rellink;
357print img_picto('', 'globe').' ';
358if ($action != 'edit') {
359 print '<input type="text" class="maxquatrevingtpercent widthcentpercentminusxx small" id="downloadinternallink" name="downloadinternellink" value="'.dol_escape_htmltag($fulllink).'">';
360} else {
361 print $fulllink;
362}
363if ($action != 'edit') {
364 print ' <a href="'.$fulllink.'">'.img_picto($langs->trans("Download"), 'download', 'class="opacitymedium paddingrightonly"').'</a>'; // No target here.
365}
366print '</td></tr>';
367
368// Link for direct external download
369print '<tr><td>';
370if ($action != 'edit') {
371 print $form->textwithpicto($langs->trans("DirectDownloadLink"), $langs->trans("PublicDownloadLinkDesc"));
372} else {
373 print $form->textwithpicto($langs->trans("FileSharedViaALink"), $langs->trans("PublicDownloadLinkDesc"));
374}
375print '</td><td>';
376if (!empty($object->share)) {
377 if ($action != 'edit') {
378 $fulllink = $urlwithroot.'/document.php?hashp='.$object->share; // Hash for public share
379 //if (!empty($object->ref)) $fulllink.='&hashn='.$object->ref; // Hash of file path
380 //elseif (!empty($object->label)) $fulllink.='&hashc='.$object->label; // Hash of file content
381
382 print img_picto('', 'globe').' ';
383 if ($action != 'edit') {
384 print '<input type="text" class="maxquatrevingtpercent widthcentpercentminusxx nopadding small downloadexternallink" id="downloadlink" name="downloadexternallink" value="'.dol_escape_htmltag($fulllink).'" spellcheck="false">';
385 } else {
386 print $fulllink;
387 }
388 if ($action != 'edit') {
389 print ' <a href="'.$fulllink.'">'.img_picto($langs->trans("Download"), 'download', 'class="opacitymedium paddingrightonly"').'</a>'; // No target here
390 }
391 } else {
392 print '<input type="checkbox" name="shareenabled" checked="checked" /> ';
393 }
394} else {
395 if ($action != 'edit') {
396 print '<span class="opacitymedium">'.$langs->trans("FileNotShared").'</span>';
397 } else {
398 print '<input type="checkbox" name="shareenabled" /> ';
399 }
400}
401print '</td>';
402print '</tr>';
403print $object->showOptionals($extrafields, ($action == 'edit' ? 'edit' : 'view'));
404print '</table>';
405print '</div>';
406
407print ajax_autoselect('downloadinternallink');
408print ajax_autoselect('downloadlink');
409
410print dol_get_fiche_end();
411
412if ($action == 'edit') {
413 print $form->buttonsSaveCancel();
414
415 print '</form>';
416}
417
418
419// Confirm deletion of a file
420if ($action == 'deletefile') {
421 print $form->formconfirm(dolBuildUrl($_SERVER["PHP_SELF"], array('section' => $section)), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile', $urlfile), 'confirm_deletefile', '', 1, 1);
422}
423
424if ($action != 'edit') {
425 // Actions buttons
426 print '<div class="tabsAction">';
427
428 if ($user->hasRight('ecm', 'setup')) {
429 print '<a class="butAction" href="'.dolBuildUrl($_SERVER['PHP_SELF'], array('action' => 'edit', 'section' => $section, 'urlfile' => $urlfile)).'">'.$langs->trans('Edit').'</a>';
430 }
431
432 print '</div>';
433}
434
435
436// End of page
437llxFooter();
438$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 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
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dolBuildUrl($url, $params=[], $addtoken=false, $anchor='')
Return path of url.
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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).
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.
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.
Definition html.lib.php:519
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:717
ajax_autoselect($htmlname, $addlink='', $textonlink='Link')
Make content of an input box selected when we click into input field.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
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.