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