dolibarr 20.0.5
ajaxdirpreview.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005 Simon Tosser <simon@kornog-computing.com>
5 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
6 * Copyright (C) 2010 Pierre Morin <pierre.morin@auguria.net>
7 * Copyright (C) 2013 Marcos GarcĂ­a <marcosgdf@gmail.com>
8 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
9 * Copyright (C) 2025 Joachim Kueter <git-jk@bloxera.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
32if (!defined('NOTOKENRENEWAL')) {
33 define('NOTOKENRENEWAL', 1); // Disables token renewal
34}
35if (!defined('NOREQUIREMENU')) {
36 define('NOREQUIREMENU', '1');
37}
38if (!defined('NOREQUIREHTML')) {
39 define('NOREQUIREHTML', '1');
40}
41if (!defined('NOREQUIREAJAX')) {
42 define('NOREQUIREAJAX', '1');
43}
44
45if (!isset($mode) || $mode != 'noajax') { // For ajax call
46 require_once '../../main.inc.php';
47 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
48 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
49 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
50
51 $action = GETPOST('action', 'aZ09');
52 $file = urldecode(GETPOST('file', 'alpha'));
53 $section = GETPOST("section", 'alpha');
54 $module = GETPOST("module", 'alpha');
55 $urlsource = GETPOST("urlsource", 'alpha');
56 $search_doc_ref = GETPOST('search_doc_ref', 'alpha');
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");
62 if (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;
68 if (!$sortorder) {
69 $sortorder = "ASC";
70 }
71 if (!$sortfield) {
72 $sortfield = "name";
73 }
74
75 $rootdirfordoc = $conf->ecm->dir_output;
76
77 $upload_dir = dirname(str_replace("../", "/", $rootdirfordoc.'/'.$file));
78
79 $ecmdir = new EcmDirectory($db);
80 if ($section > 0) {
81 $result = $ecmdir->fetch($section);
82 if (!($result > 0)) {
83 //dol_print_error($db,$ecmdir->error);
84 //exit;
85 }
86 }
87} else {
88 // For no ajax call
89 $rootdirfordoc = $conf->ecm->dir_output;
90
91 $ecmdir = new EcmDirectory($db);
92 $relativepath = '';
93 if ($section > 0) {
94 $result = $ecmdir->fetch($section);
95 if (!($result > 0)) {
96 dol_print_error($db, $ecmdir->error);
97 exit;
98 }
99
100 $relativepath = $ecmdir->getRelativePath(); // Example 'mydir/'
101 } elseif (GETPOST('section_dir')) {
102 $relativepath = GETPOST('section_dir');
103 }
104 //var_dump($section.'-'.GETPOST('section_dir').'-'.$relativepath);
105
106 $upload_dir = $rootdirfordoc.'/'.$relativepath;
107}
108
109if (empty($url)) { // autoset $url but it is better to have it defined before into filemanager.tpl.php (not possible when in auto tree)
110 if (!empty($module) && $module == 'medias' && !GETPOST('website')) {
111 $url = DOL_URL_ROOT.'/ecm/index_medias.php';
112 } elseif (GETPOSTISSET('website')) {
113 $url = DOL_URL_ROOT.'/website/index.php';
114 } else {
115 $url = DOL_URL_ROOT.'/ecm/index.php';
116 }
117}
118
119// Load translation files required by the page
120$langs->loadLangs(array("ecm", "companies", "other"));
121
122if (empty($modulepart)) {
123 $modulepart = $module;
124}
125
126// Security check
127if ($user->socid > 0) {
128 $socid = $user->socid;
129}
130// On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
131if (preg_match('/\.\./', $upload_dir) || preg_match('/[<>|]/', $upload_dir)) {
132 dol_syslog("Refused to deliver file ".$upload_dir);
133 // Do no show plain path in shown error message
134 dol_print_error(null, $langs->trans("ErrorFileNameInvalid", $upload_dir));
135 exit;
136}
137// Check permissions
138if ($modulepart == 'ecm') {
139 if (!$user->hasRight('ecm', 'read')) {
141 }
142} elseif ($modulepart == 'medias' || $modulepart == 'website') {
143 // Always allowed
144} else {
146}
147
148
149/*
150 * Action
151 */
152
153// None
154
155
156
157/*
158 * View
159 */
160
161if (!isset($mode) || $mode != 'noajax') {
162 // Ajout directives pour resoudre bug IE
163 header('Cache-Control: Public, must-revalidate');
164 header('Pragma: public');
165
166 top_httphead();
167}
168
169$type = 'directory';
170
171// This test if file exists should be useless. We keep it to find bug more easily
172if (!dol_is_dir($upload_dir)) {
173 //dol_mkdir($upload_dir);
174 /*$langs->load("install");
175 dol_print_error(0,$langs->trans("ErrorDirDoesNotExists",$upload_dir));
176 exit;*/
177}
178
179print '<!-- ajaxdirpreview type='.$type.' module='.$module.' modulepart='.$modulepart.'-->'."\n";
180//print '<!-- Page called with mode='.dol_escape_htmltag(isset($mode)?$mode:'').' type='.dol_escape_htmltag($type).' module='.dol_escape_htmltag($module).' url='.dol_escape_htmltag($url).' '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
181
182$param = ($sortfield ? '&sortfield='.urlencode($sortfield) : '').($sortorder ? '&sortorder='.urlencode($sortorder) : '');
183if (!empty($websitekey)) {
184 $param .= '&website='.urlencode($websitekey);
185}
186if (!empty($pageid)) {
187 $param .= '&pageid='.((int) $pageid);
188}
189
190
191// Dir scan
192if ($type == 'directory') {
193 $formfile = new FormFile($db);
194
195 $maxlengthname = 40;
196 $excludefiles = array('^SPECIMEN\.pdf$', '^\.', '(\.meta|_preview.*\.png)$', '^temp$', '^payments$', '^CVS$', '^thumbs$');
197 $sorting = (strtolower($sortorder) == 'desc' ? SORT_DESC : SORT_ASC);
198
199 // Right area. If module is defined here, we are in automatic ecm.
200 $automodules = array(
201 'company',
202 'invoice',
203 'invoice_supplier',
204 'propal',
205 'supplier_proposal',
206 'order',
207 'order_supplier',
208 'contract',
209 'product',
210 'tax',
211 'tax-vat',
212 'salaries',
213 'project',
214 'project_task',
215 'fichinter',
216 'user',
217 'expensereport',
218 'holiday',
219 'recruitment-recruitmentcandidature',
220 'banque',
221 'chequereceipt',
222 'mrp-mo'
223 );
224
225 $parameters = array('modulepart' => $module);
226 $reshook = $hookmanager->executeHooks('addSectionECMAuto', $parameters);
227 if ($reshook > 0 && is_array($hookmanager->resArray) && count($hookmanager->resArray) > 0) {
228 if (is_array($hookmanager->resArray['module'])) {
229 $automodules = array_merge($automodules, $hookmanager->resArray['module']);
230 } else {
231 $automodules[] = $hookmanager->resArray['module'];
232 }
233 }
234
235 // TODO change for multicompany sharing
236 if ($module == 'company') {
237 $upload_dir = $conf->societe->dir_output;
238 $excludefiles[] = '^contact$'; // The subdir 'contact' contains files of contacts.
239 } elseif ($module == 'invoice') {
240 $upload_dir = $conf->facture->dir_output;
241 } elseif ($module == 'invoice_supplier') {
242 $upload_dir = $conf->fournisseur->facture->dir_output;
243 } elseif ($module == 'propal') {
244 $upload_dir = $conf->propal->dir_output;
245 } elseif ($module == 'supplier_proposal') {
246 $upload_dir = $conf->supplier_proposal->dir_output;
247 } elseif ($module == 'order') {
248 $upload_dir = $conf->commande->dir_output;
249 } elseif ($module == 'order_supplier') {
250 $upload_dir = $conf->fournisseur->commande->dir_output;
251 } elseif ($module == 'contract') {
252 $upload_dir = $conf->contrat->dir_output;
253 } elseif ($module == 'product') {
254 $upload_dir = $conf->product->dir_output;
255 } elseif ($module == 'tax') {
256 $upload_dir = $conf->tax->dir_output;
257 $excludefiles[] = '^vat$'; // The subdir 'vat' contains files of vats.
258 } elseif ($module == 'tax-vat') {
259 $upload_dir = $conf->tax->dir_output.'/vat';
260 } elseif ($module == 'salaries') {
261 $upload_dir = $conf->salaries->dir_output;
262 } elseif ($module == 'project') {
263 $upload_dir = $conf->project->dir_output;
264 } elseif ($module == 'project_task') {
265 $upload_dir = $conf->project->dir_output;
266 } elseif ($module == 'fichinter') {
267 $upload_dir = $conf->ficheinter->dir_output;
268 } elseif ($module == 'user') {
269 $upload_dir = $conf->user->dir_output;
270 } elseif ($module == 'expensereport') {
271 $upload_dir = $conf->expensereport->dir_output;
272 } elseif ($module == 'holiday') {
273 $upload_dir = $conf->holiday->dir_output;
274 } elseif ($module == 'recruitment-recruitmentcandidature') {
275 $upload_dir = $conf->recruitment->dir_output.'/recruitmentcandidature';
276 } elseif ($module == 'banque') {
277 $upload_dir = $conf->bank->dir_output;
278 } elseif ($module == 'chequereceipt') {
279 $upload_dir = $conf->bank->dir_output.'/checkdeposits';
280 } elseif ($module == 'mrp-mo') {
281 $upload_dir = $conf->mrp->dir_output;
282 } else {
283 $parameters = array('modulepart' => $module);
284 $reshook = $hookmanager->executeHooks('addSectionECMAuto', $parameters);
285 if ($reshook > 0 && is_array($hookmanager->resArray) && count($hookmanager->resArray) > 0) {
286 $upload_dir = $hookmanager->resArray['directory'];
287 }
288 }
289
290 // Automatic list
291 if (in_array($module, $automodules)) {
292 $param .= '&module='.$module;
293 if (isset($search_doc_ref) && $search_doc_ref != '') {
294 $param .= '&search_doc_ref='.urlencode($search_doc_ref);
295 }
296
297 $textifempty = ($section ? $langs->trans("NoFileFound") : ($showonrightsize == 'featurenotyetavailable' ? $langs->trans("FeatureNotYetAvailable") : $langs->trans("NoFileFound")));
298
299 $filter = preg_quote($search_doc_ref, '/');
300 $filearray = dol_dir_list($upload_dir, "files", 1, $filter, $excludefiles, $sortfield, $sorting, 1);
301
302 $perm = $user->hasRight('ecm', 'upload');
303
304 $formfile->list_of_autoecmfiles($upload_dir, $filearray, $module, $param, 1, '', $perm, 1, $textifempty, $maxlengthname, $url, 1);
305 } else {
306 // Manual list
307 if ($module == 'medias') {
308 /*
309 $_POST is array like
310 'token' => string '062380e11b7dcd009d07318b57b71750' (length=32)
311 'action' => string 'file_manager' (length=12)
312 'website' => string 'template' (length=8)
313 'pageid' => string '124' (length=3)
314 'section_dir' => string 'mydir/' (length=3)
315 'section_id' => string '0' (length=1)
316 'max_file_size' => string '2097152' (length=7)
317 'sendit' => string 'Envoyer fichier' (length=15)
318 */
319 $relativepath = GETPOST('file', 'alpha') ? GETPOST('file', 'alpha') : GETPOST('section_dir', 'alpha');
320 if ($relativepath && $relativepath != '/') {
321 $relativepath .= '/';
322 }
323 $upload_dir = $dolibarr_main_data_root.'/'.$module.'/'.$relativepath;
324 if (GETPOSTISSET('website') || GETPOSTISSET('file_manager')) {
325 $param .= '&file_manager=1';
326 if (!preg_match('/website=/', $param) && GETPOST('website', 'alpha')) {
327 $param .= '&website='.urlencode(GETPOST('website', 'alpha'));
328 }
329 if (!preg_match('/pageid=/', $param)) {
330 $param .= '&pageid='.GETPOSTINT('pageid');
331 }
332 //if (!preg_match('/backtopage=/',$param)) $param.='&backtopage='.urlencode($_SERVER["PHP_SELF"].'?file_manager=1&website='.$websitekey.'&pageid='.$pageid);
333 }
334 } else {
335 $relativepath = $ecmdir->getRelativePath();
336 $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
337 }
338
339 // If $section defined with value 0
340 if (($section === '0' || empty($section)) && ($module != 'medias')) {
341 $filearray = array();
342 } else {
343 $filearray = dol_dir_list($upload_dir, "files", 0, '', array('^\.', '(\.meta|_preview.*\.png)$', '^temp$', '^CVS$'), $sortfield, $sorting, 1);
344 }
345
346 if ($section) {
347 $param .= '&section='.$section;
348 if (isset($search_doc_ref) && $search_doc_ref != '') {
349 $param .= '&search_doc_ref='.urlencode($search_doc_ref);
350 }
351
352 $textifempty = $langs->trans('NoFileFound');
353 } elseif ($section === '0') {
354 if ($module == 'ecm') {
355 $textifempty = '<br><div class="center"><span class="warning">'.$langs->trans("DirNotSynchronizedSyncFirst").'</span></div><br>';
356 } else {
357 $textifempty = $langs->trans('NoFileFound');
358 }
359 } else {
360 $textifempty = ($showonrightsize == 'featurenotyetavailable' ? $langs->trans("FeatureNotYetAvailable") : $langs->trans("ECMSelectASection"));
361 }
362
363 if ($module == 'medias') {
364 $useinecm = 6;
365 $modulepart = 'medias';
366 $perm = ($user->hasRight("website", "write") || $user->hasRight("emailing", "creer"));
367 $title = 'none';
368 } elseif ($module == 'ecm') { // DMS/ECM -> manual structure
369 if ($user->hasRight("ecm", "read")) {
370 // Buttons: Preview
371 $useinecm = 2;
372 }
373
374 if ($user->hasRight("ecm", "upload")) {
375 // Buttons: Preview + Delete
376 $useinecm = 4;
377 }
378
379 if ($user->hasRight("ecm", "setup")) {
380 // Buttons: Preview + Delete + Edit
381 $useinecm = 5;
382 }
383
384 $perm = $user->hasRight("ecm", "upload");
385 $modulepart = 'ecm';
386 $title = ''; // Use default
387 } else {
388 $useinecm = 5;
389 $modulepart = 'ecm';
390 $perm = $user->hasRight("ecm", "upload");
391 $title = ''; // Use default
392 }
393
394 // When we show list of files for ECM files, $filearray contains file list, and directory is defined with modulepart + section into $param
395 // When we show list of files for a directory, $filearray ciontains file list, and directory is defined with modulepart + $relativepath
396 // var_dump("section=".$section." title=".$title." modulepart=".$modulepart." useinecm=".$useinecm." perm(permtoeditline)=".$perm." relativepath=".$relativepath." param=".$param." url=".$url);
397 $formfile->list_of_documents($filearray, '', $modulepart, $param, 1, $relativepath, $perm, $useinecm, $textifempty, $maxlengthname, $title, $url, 0, $perm, '', $sortfield, $sortorder);
398 }
399}
400
401
402
403// Bottom of page
404$useajax = 1;
405if (!empty($conf->dol_use_jmobile)) {
406 $useajax = 0;
407}
408if (empty($conf->use_javascript_ajax)) {
409 $useajax = 0;
410}
411if (getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
412 $useajax = 0;
413}
414
415//$param.=($param?'?':'').(preg_replace('/^&/','',$param));
416
417if ($useajax || $action == 'deletefile') {
418 $urlfile = '';
419 if ($action == 'deletefile') {
420 $urlfile = GETPOST('urlfile', 'alpha');
421 }
422
423 if (empty($section_dir)) {
424 $section_dir = GETPOST("file", "alpha");
425 }
426 $section_id = $section;
427
428 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
429
430 $form = new Form($db);
431 $formquestion = array();
432 $formquestion['urlfile'] = array('type' => 'hidden', 'value' => $urlfile, 'name' => 'urlfile'); // We must always put field, even if empty because it is filled by javascript later
433 $formquestion['section'] = array('type' => 'hidden', 'value' => $section, 'name' => 'section'); // We must always put field, even if empty because it is filled by javascript later
434 $formquestion['section_id'] = array('type' => 'hidden', 'value' => $section_id, 'name' => 'section_id'); // We must always put field, even if empty because it is filled by javascript later
435 $formquestion['section_dir'] = array('type' => 'hidden', 'value' => $section_dir, 'name' => 'section_dir'); // We must always put field, even if empty because it is filled by javascript later
436 $formquestion['sortfield'] = array('type' => 'hidden', 'value' => $sortfield, 'name' => 'sortfield'); // We must always put field, even if empty because it is filled by javascript later
437 $formquestion['sortorder'] = array('type' => 'hidden', 'value' => $sortorder, 'name' => 'sortorder'); // We must always put field, even if empty because it is filled by javascript later
438 if (!empty($action) && $action == 'file_manager') {
439 $formquestion['file_manager'] = array('type' => 'hidden', 'value' => 1, 'name' => 'file_manager');
440 }
441 if (!empty($websitekey)) {
442 $formquestion['website'] = array('type' => 'hidden', 'value' => $websitekey, 'name' => 'website');
443 }
444 if (!empty($pageid) && $pageid > 0) {
445 $formquestion['pageid'] = array('type' => 'hidden', 'value' => $pageid, 'name' => 'pageid');
446 }
447
448 print $form->formconfirm($url, $langs->trans("DeleteFile"), $langs->trans("ConfirmDeleteFile"), 'confirm_deletefile', $formquestion, "no", ($useajax ? 'deletefile' : 0));
449}
450
451if ($useajax) {
452 print '<!-- ajaxdirpreview.php: js to manage preview of doc -->'."\n";
453 print '<script nonce="'.getNonce().'" type="text/javascript">';
454
455 // Enable jquery handlers on new generated HTML objects (same code than into lib_footer.js.php)
456 // Because the content is reloaded by ajax call, we must also reenable some jquery hooks
457 // Wrapper to manage document_preview
458 if ($conf->browser->layout != 'phone') {
459 print "\n/* JS CODE TO ENABLE document_preview */\n";
460 print '
461 jQuery(document).ready(function () {
462 jQuery(".documentpreview").click(function () {
463 console.log("We click on preview for element with href="+$(this).attr(\'href\')+" mime="+$(this).attr(\'mime\'));
464 document_preview($(this).attr(\'href\'), $(this).attr(\'mime\'), \''.dol_escape_js($langs->transnoentities("Preview")).'\');
465 return false;
466 });
467 });
468 ' . "\n";
469 }
470
471 // Enable jquery handlers button to delete files
472 print 'jQuery(document).ready(function() {'."\n";
473 print ' jQuery(".deletefilelink").click(function(e) { '."\n";
474 print ' console.log("We click on button with class deletefilelink, param='.$param.', we set urlfile to "+jQuery(this).attr("rel"));'."\n";
475 print ' jQuery("#urlfile").val(jQuery(this).attr("rel"));'."\n";
476 //print ' jQuery("#section_dir").val(\'aaa\');'."\n";
477 print ' jQuery("#dialog-confirm-deletefile").dialog("open");'."\n";
478 print ' return false;'."\n";
479 print ' });'."\n";
480 print '});'."\n";
481 print '</script>'."\n";
482}
483
484// Close db if mode is not noajax
485if ((!isset($mode) || $mode != 'noajax') && is_object($db)) {
486 $db->close();
487}
Class to manage ECM directories.
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition files.lib.php:63
dol_is_dir($folder)
Test if filename is a directory.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.