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