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