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