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
42// Load translation files required by page
43$langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts', 'categories'));
44
45$action = GETPOST('action', 'aZ09');
46$cancel = GETPOST('cancel', 'alpha');
47$backtopage = GETPOST('backtopage', 'alpha');
48
49// Get parameters
50$socid = GETPOSTINT("socid");
51
52// Security check
53if ($user->socid > 0) {
54 $action = '';
55 $socid = $user->socid;
56}
57
58$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
59$sortfield = GETPOST('sortfield', 'aZ09comma');
60$sortorder = GETPOST('sortorder', 'aZ09comma');
61$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
62if (empty($page) || $page == -1) {
63 $page = 0;
64} // If $page is not defined, or '' or -1
65$offset = $limit * $page;
66$pageprev = $page - 1;
67$pagenext = $page + 1;
68if (!$sortorder) {
69 $sortorder = "ASC";
70}
71if (!$sortfield) {
72 $sortfield = "label";
73}
74
75$section = GETPOST("section", 'alpha');
76if (!$section) {
77 dol_print_error(null, 'Error, section parameter missing');
78 exit;
79}
80$urlfile = (string) dol_sanitizePathName(GETPOST("urlfile"), '_', 0);
81if (!$urlfile) {
82 dol_print_error(null, "ErrorParamNotDefined");
83 exit;
84}
85
86// Load ecm object
87$ecmdir = new EcmDirectory($db);
88$result = $ecmdir->fetch(GETPOST("section", 'alpha'));
89if (!($result > 0)) {
90 dol_print_error($db, $ecmdir->error);
91 exit;
92}
93$relativepath = $ecmdir->getRelativePath();
94$upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
95
96$fullpath = $conf->ecm->dir_output.'/'.$relativepath.$urlfile;
97
98$relativetodocument = 'ecm/'.$relativepath; // $relativepath is relative to ECM dir, we need relative to document
99$filepath = $relativepath.$urlfile;
100$filepathtodocument = $relativetodocument.$urlfile;
101
102// Try to load object from index
103$object = new EcmFiles($db);
104$extrafields = new ExtraFields($db);
105// fetch optionals attributes and labels
106$extrafields->fetch_name_optionals_label($object->table_element);
107
108$result = $object->fetch(0, '', $filepathtodocument);
109if ($result < 0) {
110 dol_print_error($db, $object->error, $object->errors);
111 exit;
112}
113
114// Permissions
115$permissiontoread = $user->hasRight('ecm', 'read');
116$permissiontoadd = $user->hasRight('ecm', 'setup');
117$permissiontoupload = $user->hasRight('ecm', 'upload');
118
119if (!$permissiontoread) {
121}
122
123
124/*
125 * Actions
126 */
127
128if ($cancel) {
129 $action = '';
130 if ($backtopage) {
131 header("Location: ".$backtopage);
132 exit;
133 } else {
134 header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'&section='.urlencode($section).($module ? '&module='.urlencode($module) : ''));
135 exit;
136 }
137}
138
139// Rename file
140if ($action == 'update' && $permissiontoadd) {
141 $error = 0;
142
143 $oldlabel = GETPOST('urlfile', 'alpha');
144 $newlabel = dol_sanitizeFileName(GETPOST('label', 'alpha'), '_', 0);
145 $shareenabled = GETPOST('shareenabled', 'alpha');
146
147 //$db->begin();
148
149 $olddir = $ecmdir->getRelativePath(0); // Relative to ecm
150 $olddirrelativetodocument = 'ecm/'.$olddir; // Relative to document
151 $newdirrelativetodocument = 'ecm/'.$olddir;
152 $olddir = $conf->ecm->dir_output.'/'.$olddir;
153 $newdir = $olddir;
154
155 $oldfile = $olddir.$oldlabel;
156 $newfile = $newdir.$newlabel;
157 $newfileformove = $newfile;
158 // If old file end with .noexe, new file must also end with .noexe
159 if (preg_match('/\.noexe$/', $oldfile) && !preg_match('/\.noexe$/', $newfileformove)) {
160 $newfileformove .= '.noexe';
161 }
162 //var_dump($oldfile);var_dump($newfile);exit;
163
164 // Now we update index of file
165 $db->begin();
166 //print $oldfile.' - '.$newfile;
167 if ($newlabel != $oldlabel) {
168 $result = dol_move($oldfile, $newfileformove); // This include update of database
169 if (!$result) {
170 $langs->load('errors');
171 setEventMessages($langs->trans('ErrorFailToRenameFile', $oldfile, $newfile), null, 'errors');
172 $error++;
173 }
174
175 // Reload object after the move
176 $result = $object->fetch(0, '', $newdirrelativetodocument.$newlabel);
177 if ($result < 0) {
178 dol_print_error($db, $object->error, $object->errors);
179 exit;
180 }
181 }
182
183 if (!$error) {
184 if ($shareenabled) {
185 require_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
186 $object->share = getRandomPassword(true);
187 } else {
188 $object->share = '';
189 }
190
191 if ($object->id > 0) {
192 $ret = $extrafields->setOptionalsFromPost(null, $object);
193 if ($ret < 0) {
194 $error++;
195 }
196 if (!$error) {
197 // Actions on extra fields
198 $result = $object->insertExtraFields();
199 if ($result < 0) {
200 setEventMessages($object->error, $object->errors, 'errors');
201 $error++;
202 }
203 }
204 // Call update to set the share key
205 $result = $object->update($user);
206 if ($result < 0) {
207 setEventMessages($object->error, $object->errors, 'warnings');
208 }
209 } else {
210 // Call create to insert record
211 $object->entity = $conf->entity;
212 $object->filepath = preg_replace('/[\\/]+$/', '', $newdirrelativetodocument);
213 $object->filename = $newlabel;
214 $object->label = md5_file(dol_osencode($newfileformove)); // hash of file content
215 $object->fullpath_orig = '';
216 $object->gen_or_uploaded = 'unknown';
217 $object->description = ''; // indexed content
218 $object->keywords = ''; // keyword content
219 $result = $object->create($user);
220 if ($result < 0) {
221 setEventMessages($object->error, $object->errors, 'warnings');
222 }
223 }
224 }
225
226 if (!$error) {
227 $db->commit();
228
229 $urlfile = $newlabel;
230 // If old file end with .noexe, new file must also end with .noexe
231 if (preg_match('/\.noexe$/', $newfileformove)) {
232 $urlfile .= '.noexe';
233 }
234
235 header('Location: '.$_SERVER["PHP_SELF"].'?urlfile='.urlencode($urlfile).'&section='.urlencode($section));
236 exit;
237 } else {
238 $db->rollback();
239 }
240}
241
242
243
244/*
245 * View
246 */
247
248$form = new Form($db);
249
250llxHeader('', '', '', '', 0, 0, '', '', '', 'mod-ecm page-file_card');
251
252$object->section_id = $ecmdir->id;
253$object->label = $urlfile;
254$head = ecm_file_prepare_head($object);
255
256if ($action == 'edit') {
257 print '<form name="update" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
258 print '<input type="hidden" name="token" value="'.newToken().'">';
259 print '<input type="hidden" name="section" value="'.$section.'">';
260 print '<input type="hidden" name="urlfile" value="'.$urlfile.'">';
261 print '<input type="hidden" name="module" value="'.$module.'">';
262 print '<input type="hidden" name="action" value="update">';
263 print '<input type="hidden" name="id" value="'.$object->id.'">';
264}
265
266print dol_get_fiche_head($head, 'card', $langs->trans("File"), -1, 'generic');
267
268
269$s = '';
270$tmpecmdir = new EcmDirectory($db); // Need to create a new one
271$tmpecmdir->fetch($ecmdir->id);
272$result = 1;
273$i = 0;
274while ($tmpecmdir && $result > 0) {
275 $tmpecmdir->ref = $tmpecmdir->label;
276 $s = $tmpecmdir->getNomUrl(1).$s;
277 if ($tmpecmdir->fk_parent) {
278 $s = ' -> '.$s;
279 $result = $tmpecmdir->fetch($tmpecmdir->fk_parent);
280 } else {
281 $tmpecmdir = 0;
282 }
283 $i++;
284}
285
286$urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfile);
287
288$s = img_picto('', 'object_dir').' <a href="'.DOL_URL_ROOT.'/ecm/index.php">'.$langs->trans("ECMRoot").'</a> -> '.$s.' -> ';
289if ($action == 'edit') {
290 $s .= '<input type="text" name="label" class="quatrevingtpercent" value="'.$urlfiletoshow.'">';
291} else {
292 $s .= $urlfiletoshow;
293}
294
295$linkback = '';
296if ($backtopage) {
297 $linkback = '<a href="'.$backtopage.'">'.$langs->trans("BackToTree").'</a>';
298}
299
300$object->ref = ''; // Force to hide ref
301dol_banner_tab($object, '', $linkback, 0, '', '', $s);
302
303print '<div class="fichecenter">';
304
305print '<div class="underbanner clearboth"></div>';
306print '<table class="border centpercent tableforfield">';
307print '<tr><td class="titlefieldcreate">'.$langs->trans("ECMCreationDate").'</td><td>';
308print dol_print_date(dol_filemtime($fullpath), 'dayhour');
309print '</td></tr>';
310/*print '<tr><td>'.$langs->trans("ECMDirectoryForFiles").'</td><td>';
311print '/ecm/'.$relativepath;
312print '</td></tr>';
313print '<tr><td>'.$langs->trans("ECMNbOfDocs").'</td><td>';
314print count($filearray);
315print '</td></tr>';
316print '<tr><td>'.$langs->trans("TotalSizeOfAttachedFiles").'</td><td>';
317print dol_print_size($totalsize);
318print '</td></tr>';
319*/
320
321// Hash of file content
322print '<tr><td>'.$langs->trans("HashOfFileContent").'</td><td>';
323$object = new EcmFiles($db);
324$object->fetch(0, '', $filepathtodocument);
325if (!empty($object->label)) {
326 print $object->label;
327} else {
328 print img_warning().' '.$langs->trans("FileNotYetIndexedInDatabase");
329}
330print '</td></tr>';
331
332// Define $urlwithroot
333$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
334$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
335//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
336
337// Link for internal download
338print '<tr><td>';
339print $form->textwithpicto($langs->trans("DirectDownloadInternalLink"), $langs->trans("PrivateDownloadLinkDesc"));
340print '</td><td>';
341$modulepart = 'ecm';
342$forcedownload = 1;
343$rellink = '/document.php?modulepart='.$modulepart;
344if ($forcedownload) {
345 $rellink .= '&attachment=1';
346}
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" 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 $forcedownload = 0;
374
375 $paramlink = '';
376 if (!empty($object->share)) {
377 $paramlink .= ($paramlink ? '&' : '').'hashp='.$object->share; // Hash for public share
378 }
379 if ($forcedownload) {
380 $paramlink .= ($paramlink ? '&' : '').'attachment=1';
381 }
382
383 $fulllink = $urlwithroot.'/document.php'.($paramlink ? '?'.$paramlink : '');
384 //if (!empty($object->ref)) $fulllink.='&hashn='.$object->ref; // Hash of file path
385 //elseif (!empty($object->label)) $fulllink.='&hashc='.$object->label; // Hash of file content
386
387 print img_picto('', 'globe').' ';
388 if ($action != 'edit') {
389 print '<input type="text" class="quatrevingtpercent nopadding small" id="downloadlink" name="downloadexternallink" value="'.dol_escape_htmltag($fulllink).'">';
390 } else {
391 print $fulllink;
392 }
393 if ($action != 'edit') {
394 print ' <a href="'.$fulllink.'">'.$langs->trans("Download").'</a>'; // No target here
395 }
396 } else {
397 print '<input type="checkbox" name="shareenabled"'.($object->share ? ' checked="checked"' : '').' /> ';
398 }
399} else {
400 if ($action != 'edit') {
401 print '<span class="opacitymedium">'.$langs->trans("FileNotShared").'</span>';
402 } else {
403 print '<input type="checkbox" name="shareenabled"'.($object->share ? ' checked="checked"' : '').' /> ';
404 }
405}
406print '</td>';
407print '</tr>';
408print $object->showOptionals($extrafields, ($action == 'edit' ? 'edit' : 'view'));
409print '</table>';
410print '</div>';
411
412print ajax_autoselect('downloadinternallink');
413print ajax_autoselect('downloadlink');
414
415print dol_get_fiche_end();
416
417if ($action == 'edit') {
418 print $form->buttonsSaveCancel();
419
420 print '</form>';
421}
422
423
424// Confirm deletion of a file
425if ($action == 'deletefile') {
426 print $form->formconfirm($_SERVER["PHP_SELF"].'?section='.urlencode($section), $langs->trans('DeleteFile'), $langs->trans('ConfirmDeleteFile', $urlfile), 'confirm_deletefile', '', 1, 1);
427}
428
429if ($action != 'edit') {
430 // Actions buttons
431 print '<div class="tabsAction">';
432
433 if ($user->hasRight('ecm', 'setup')) {
434 print '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=edit&section='.urlencode($section).'&urlfile='.urlencode($urlfile).'">'.$langs->trans('Edit').'</a>';
435 }
436
437 print '</div>';
438}
439
440
441// End of page
442llxFooter();
443$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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.
llxFooter()
Footer empty.
Definition document.php:107
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.