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