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