dolibarr  7.0.0-beta
ajaxdirtree.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
25 // This script is called with a POST method.
26 // Directory to scan (full path) is inside POST['dir'] and encode by js escape() if ajax is used or encoded by urlencode if mode=noajax
27 
28 if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL',1); // Disables token renewal
29 if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
30 if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
31 if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
32 
33 if (! isset($mode) || $mode != 'noajax') // For ajax call
34 {
35  $res=@include '../../main.inc.php';
36  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
37  include_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
38  include_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
39  include_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
40  include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
41 
42  $openeddir = GETPOST('openeddir');
43  $modulepart= GETPOST('modulepart');
44  $selecteddir = jsUnEscape(GETPOST('dir')); // relative path. We must decode using same encoding function used by javascript: escape()
45  if ($selecteddir != '/') $selecteddir = preg_replace('/\/$/','',$selecteddir); // We removed last '/' except if it is '/'
46 }
47 else // For no ajax call
48 {
49  $openeddir = GETPOST('openeddir');
50  $modulepart= GETPOST('modulepart');
51  $selecteddir = GETPOST('dir');
52  if ($selecteddir != '/') $selecteddir = preg_replace('/\/$/','',$selecteddir); // We removed last '/' except if it is '/'
53  if (empty($url)) $url=DOL_URL_ROOT.'/ecm/index.php';
54 }
55 
56 $langs->load("ecm");
57 
58 // Define fullpathselecteddir.
59 $fullpathselecteddir='<none>';
60 if ($modulepart == 'ecm') $fullpathselecteddir=$conf->ecm->dir_output.'/'.($selecteddir != '/' ? $selecteddir : '');
61 if ($modulepart == 'medias') $fullpathselecteddir=$dolibarr_main_data_root.'/medias/'.($selecteddir != '/' ? $selecteddir : '');
62 
63 
64 // Security:
65 // On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
66 if (preg_match('/\.\./',$fullpathselecteddir) || preg_match('/[<>|]/',$fullpathselecteddir))
67 {
68  dol_syslog("Refused to deliver file ".$original_file);
69  // Do no show plain path in shown error message
70  dol_print_error(0,$langs->trans("ErrorFileNameInvalid",GETPOST("file")));
71  exit;
72 }
73 
74 // Check permissions
75 if ($modulepart == 'ecm')
76 {
77  if (! $user->rights->ecm->read) accessforbidden();
78 }
79 if ($modulepart == 'medias')
80 {
81  // Always allowed
82 }
83 
84 
85 /*
86  * View
87  */
88 
89 if (! isset($mode) || $mode != 'noajax')
90 {
91  top_httphead();
92 }
93 
94 //print '<!-- selecteddir = '.$selecteddir.', openeddir = '.$openeddir.', modulepart='.$modulepart.' -->'."\n";
95 $userstatic=new User($db);
96 $form=new Form($db);
97 $ecmdirstatic = new EcmDirectory($db);
98 
99 // Load full tree from database. We will use it to define nbofsubdir and nboffilesinsubdir
100 if (empty($sqltree)) $sqltree=$ecmdirstatic->get_full_arbo(0);
101 
102 // Try to find key into $sqltree
103 $current_ecmdir_id=-1;
104 foreach($sqltree as $keycursor => $val)
105 {
106  //print $val['fullrelativename']." == ".$selecteddir;
107  if ($val['fullrelativename'] == $selecteddir)
108  {
109  $current_ecmdir_id = $keycursor;
110  }
111 }
112 
113 if (! empty($conf->use_javascript_ajax) && empty($conf->global->MAIN_ECM_DISABLE_JS))
114 {
115  if (file_exists($fullpathselecteddir))
116  {
117  $files = @scandir($fullpathselecteddir);
118 
119  if ($files)
120  {
121  natcasesort($files);
122  if (count($files) > 2) /* The 2 accounts for . and .. */
123  {
124  echo '<ul class="ecmjqft" style="display: none;">'."\n";
125 
126  // All dirs
127  foreach ($files as $file) // $file can be '.', '..', or 'My dir' or 'My file'
128  {
129  if ($file == 'temp') continue;
130 
131  $nbofsubdir=0;
132  $nboffilesinsubdir=0;
133 
134  $val=array();
135 
136  // Loop on all database entries (sqltree) to find the one matching the subdir found into dir to scan
137  foreach($sqltree as $key => $tmpval)
138  {
139  //print "-- key=".$key." - ".$tmpval['fullrelativename']." vs ".(($selecteddir != '/'?$selecteddir.'/':'').$file)."<br>\n";
140  if ($tmpval['fullrelativename'] == (($selecteddir != '/'?$selecteddir.'/':'').$file)) // We found equivalent record into database
141  {
142  $val=$tmpval;
143  $resarray=tree_showpad($sqltree,$key,1);
144 
145  // Refresh cache for this subdir
146  if (isset($val['cachenbofdoc']) && $val['cachenbofdoc'] < 0) // Cache is not up to date, so we update it for this directory t
147  {
148  $result=$ecmdirstatic->fetch($val['id']);
149  $ecmdirstatic->ref=$ecmdirstatic->label;
150 
151  $result=$ecmdirstatic->refreshcachenboffile(0);
152  $val['cachenbofdoc']=$result;
153  }
154 
155  $a=$resarray[0];
156  $nbofsubdir=$resarray[1];
157  $nboffilesinsubdir=$resarray[2];
158  break;
159  }
160  }
161 
162  //print 'modulepart='.$modulepart.' fullpathselecteddir='.$fullpathselecteddir.' - val[fullrelativename] (in database)='.$val['fullrelativename'].' - val[id]='.$val['id'].' - is_dir='.dol_is_dir($fullpathselecteddir . $file).' - file='.$file."\n";
163  if ($file != '.' && $file != '..' && ((! empty($val['fullrelativename']) && $val['id'] >= 0) || dol_is_dir($fullpathselecteddir . (preg_match('/\/$/',$fullpathselecteddir)?'':'/') . $file)))
164  {
165  if (empty($val['fullrelativename'])) // If we did not find entry into database, but found a directory (dol_is_dir was ok at previous test)
166  {
167  $val['fullrelativename']=(($selecteddir && $selecteddir != '/')?$selecteddir.'/':'').$file;
168  $val['id']=0;
169  $val['label']=$file;
170  $val['description']='';
171  $nboffilesinsubdir=$langs->trans("Unknown");
172  }
173 
174  print '<li class="directory collapsed">';
175 
176  print "<a class=\"fmdirlia jqft ecmjqft\" href=\"";
177  print "#";
178  print "\" rel=\"" . dol_escape_htmltag($val['fullrelativename'].'/') . "\" id=\"fmdirlia_id_".$val['id']."\"";
179  print " onClick=\"loadandshowpreview('".dol_escape_js($val['fullrelativename'])."',".$val['id'].")";
180  print "\">";
181  print dol_escape_htmltag($file);
182  print "</a>";
183 
184  print '<div class="ecmjqft">';
185 
186  print '<table class="nobordernopadding"><tr>';
187 
188  /*print '<td align="left">';
189  print dol_escape_htmltag($file);
190  print '</td>';*/
191 
192  // Nb of docs
193  print '<td align="right">';
194  print (isset($val['cachenbofdoc']) && $val['cachenbofdoc'] >= 0)?$val['cachenbofdoc']:'&nbsp;';
195  print '</td>';
196  print '<td align="left">';
197  if ($nbofsubdir > 0 && $nboffilesinsubdir > 0) print '<font color="#AAAAAA">+'.$nboffilesinsubdir.'</font> ';
198  print '</td>';
199 
200  // Edit link
201  print '<td align="right" width="18"><a href="';
202  print DOL_URL_ROOT.'/ecm/dir_card.php?module='.urlencode($modulepart).'&section='.$val['id'].'&relativedir='.urlencode($val['fullrelativename']);
203  print '&backtopage='.urlencode($_SERVER["PHP_SELF"].'?file_manager=1&website='.$website.'&pageid='.$pageid);
204  print '">'.img_view($langs->trans("Edit").' - '.$langs->trans("View"), 0, 'class="valignmiddle"').'</a></td>';
205 
206  // Add link
207  //print '<td align="right"><a href="'.DOL_URL_ROOT.'/ecm/dir_add_card.php?action=create&amp;catParent='.$val['id'].'">'.img_edit_add().'</a></td>';
208  //print '<td align="right" width="14">&nbsp;</td>';
209 
210  // Info
211  if ($modulepart == 'ecm')
212  {
213  print '<td align="right" width="18">';
214  $userstatic->id=isset($val['fk_user_c'])?$val['fk_user_c']:0;
215  $userstatic->lastname=isset($val['login_c'])?$val['login_c']:0;
216  $htmltooltip='<b>'.$langs->trans("ECMSection").'</b>: '.$val['label'].'<br>';
217  $htmltooltip='<b>'.$langs->trans("Type").'</b>: '.$langs->trans("ECMSectionManual").'<br>';
218  $htmltooltip.='<b>'.$langs->trans("ECMCreationUser").'</b>: '.$userstatic->getNomUrl(1, '', false, 1).'<br>';
219  $htmltooltip.='<b>'.$langs->trans("ECMCreationDate").'</b>: '.(isset($val['date_c'])?dol_print_date($val['date_c'],"dayhour"):$langs->trans("NeedRefresh")).'<br>';
220  $htmltooltip.='<b>'.$langs->trans("Description").'</b>: '.$val['description'].'<br>';
221  $htmltooltip.='<b>'.$langs->trans("ECMNbOfFilesInDir").'</b>: '.((isset($val['cachenbofdoc']) && $val['cachenbofdoc'] >= 0)?$val['cachenbofdoc']:$langs->trans("NeedRefresh")).'<br>';
222  if ($nboffilesinsubdir > 0) $htmltooltip.='<b>'.$langs->trans("ECMNbOfFilesInSubDir").'</b>: '.$nboffilesinsubdir;
223  else $htmltooltip.='<b>'.$langs->trans("ECMNbOfSubDir").'</b>: '.($nbofsubdir >= 0 ? $nbofsubdir : $langs->trans("NeedRefresh")).'<br>';
224  print $form->textwithpicto('',$htmltooltip,1,"info");
225  print "</td>";
226  }
227 
228  print "</tr></table>\n";
229  print '</div>';
230 
231  //print '<div>&nbsp;</div>';
232  print "</li>\n";
233  }
234  }
235 
236  // Enable jquery handlers on new generated HTML objects (same code than into lib_footer.js.php)
237  // Because the content is reloaded by ajax call, we must also reenable some jquery hooks
238  print "\n<!-- JS CODE TO ENABLE Tooltips on all object with class classfortooltip (reload into ajaxdirtree) -->\n";
239  print '<script type="text/javascript">
240  jQuery(document).ready(function () {
241  jQuery(".classfortooltip").tooltip({
242  show: { collision: "flipfit", effect:\'toggle\', delay:50 },
243  hide: { delay: 50 }, /* If I enable effect:\'toggle\' here, a bug appears: the tooltip is shown when collpasing a new dir if it was shown before */
244  tooltipClass: "mytooltip",
245  content: function () {
246  return $(this).prop(\'title\'); /* To force to get title as is */
247  }
248  });
249  });
250  </script>';
251 
252  echo "</ul>\n";
253 
254  }
255  }
256  else print "PermissionDenied";
257  }
258 
259  // This ajax service is called only when a directory $selecteddir is opened but not when closed.
260  //print '<script language="javascript">';
261  //print "loadandshowpreview('".dol_escape_js($selecteddir)."');";
262  //print '</script>';
263 }
264 
265 
266 if (empty($conf->use_javascript_ajax) || ! empty($conf->global->MAIN_ECM_DISABLE_JS))
267 {
268  print '<ul class="ecmjqft">';
269 
270  // Load full tree from database. We will use it to define nbofsubdir and nboffilesinsubdir
271  if (empty($sqltree)) $sqltree=$ecmdirstatic->get_full_arbo(0); // Slow
272 
273  // ----- This section will show a tree from a fulltree array -----
274  // $section must also be defined
275  // ----------------------------------------------------------------
276 
277  // Define fullpathselected ( _x_y_z ) of $section parameter (!! not into ajaxdirtree)
278  $fullpathselected='';
279  foreach($sqltree as $key => $val)
280  {
281  //print $val['id']."-".$section."<br>";
282  if ($val['id'] == $section)
283  {
284  $fullpathselected=$val['fullpath'];
285  break;
286  }
287  }
288  //print "fullpathselected=".$fullpathselected."<br>";
289 
290  // Update expandedsectionarray in session
291  $expandedsectionarray=array();
292  if (isset($_SESSION['dol_ecmexpandedsectionarray'])) $expandedsectionarray=explode(',',$_SESSION['dol_ecmexpandedsectionarray']);
293 
294  if ($section && GETPOST('sectionexpand') == 'true')
295  {
296  // We add all sections that are parent of opened section
297  $pathtosection=explode('_',$fullpathselected);
298  foreach($pathtosection as $idcursor)
299  {
300  if ($idcursor && ! in_array($idcursor,$expandedsectionarray)) // Not already in array
301  {
302  $expandedsectionarray[]=$idcursor;
303  }
304  }
305  $_SESSION['dol_ecmexpandedsectionarray']=join(',',$expandedsectionarray);
306  }
307  if ($section && GETPOST('sectionexpand') == 'false')
308  {
309  // We removed all expanded sections that are child of the closed section
310  $oldexpandedsectionarray=$expandedsectionarray;
311  $expandedsectionarray=array(); // Reset
312  foreach($oldexpandedsectionarray as $sectioncursor)
313  {
314  // TODO is_in_subtree(fulltree,sectionparent,sectionchild) does nox exists. Enable or remove this...
315  //if ($sectioncursor && ! is_in_subtree($sqltree,$section,$sectioncursor)) $expandedsectionarray[]=$sectioncursor;
316  }
317  $_SESSION['dol_ecmexpandedsectionarray']=join(',',$expandedsectionarray);
318  }
319  //print $_SESSION['dol_ecmexpandedsectionarray'].'<br>';
320 
321  $nbofentries=0;
322  $oldvallevel=0;
323  $var=true;
324  foreach($sqltree as $key => $val)
325  {
326  $var=false;
327 
328  $ecmdirstatic->id=$val['id'];
329  $ecmdirstatic->ref=$val['label'];
330 
331  // Refresh cache
332  if (preg_match('/refresh/i',$action))
333  {
334  $result=$ecmdirstatic->fetch($val['id']);
335  $ecmdirstatic->ref=$ecmdirstatic->label;
336 
337  $result=$ecmdirstatic->refreshcachenboffile(0);
338  $val['cachenbofdoc']=$result;
339  }
340 
341  //$fullpathparent=preg_replace('/(_[^_]+)$/i','',$val['fullpath']);
342 
343  // Define showline
344  $showline=0;
345 
346  // If directory is son of expanded directory, we show line
347  if (in_array($val['id_mere'],$expandedsectionarray)) $showline=4;
348  // If directory is brother of selected directory, we show line
349  elseif ($val['id'] != $section && $val['id_mere'] == $ecmdirstatic->motherof[$section]) $showline=3;
350  // If directory is parent of selected directory or is selected directory, we show line
351  elseif (preg_match('/'.$val['fullpath'].'_/i',$fullpathselected.'_')) $showline=2;
352  // If we are level one we show line
353  elseif ($val['level'] < 2) $showline=1;
354 
355  if ($showline)
356  {
357  if (in_array($val['id'],$expandedsectionarray)) $option='indexexpanded';
358  else $option='indexnotexpanded';
359  //print $option;
360 
361  print '<li class="directory collapsed">';
362 
363  // Show tree graph pictos
364  $cpt=1;
365  while ($cpt < $sqltree[$key]['level'])
366  {
367  print ' &nbsp; &nbsp;';
368  $cpt++;
369  }
370  $resarray=tree_showpad($sqltree,$key,1);
371  $a=$resarray[0];
372  $nbofsubdir=$resarray[1];
373  $nboffilesinsubdir=$resarray[2];
374 
375  // Show link
376  print $ecmdirstatic->getNomUrl(0,$option,32,'class="fmdirlia jqft ecmjqft"');
377 
378  print '<div class="ecmjqft">';
379 
380  // Nb of docs
381  print '<table class="nobordernopadding"><tr><td>';
382  print $val['cachenbofdoc'];
383  print '</td>';
384  print '<td align="left">';
385  if ($nbofsubdir && $nboffilesinsubdir) print '<font color="#AAAAAA">+'.$nboffilesinsubdir.'</font> ';
386  print '</td>';
387 
388  // Info
389  print '<td align="center">';
390  $userstatic->id=$val['fk_user_c'];
391  $userstatic->lastname=$val['login_c'];
392  $htmltooltip='<b>'.$langs->trans("ECMSection").'</b>: '.$val['label'].'<br>';
393  $htmltooltip='<b>'.$langs->trans("Type").'</b>: '.$langs->trans("ECMSectionManual").'<br>';
394  $htmltooltip.='<b>'.$langs->trans("ECMCreationUser").'</b>: '.$userstatic->getNomUrl(1, '', false, 1).'<br>';
395  $htmltooltip.='<b>'.$langs->trans("ECMCreationDate").'</b>: '.dol_print_date($val['date_c'],"dayhour").'<br>';
396  $htmltooltip.='<b>'.$langs->trans("Description").'</b>: '.$val['description'].'<br>';
397  $htmltooltip.='<b>'.$langs->trans("ECMNbOfFilesInDir").'</b>: '.$val['cachenbofdoc'].'<br>';
398  if ($nbofsubdir) $htmltooltip.='<b>'.$langs->trans("ECMNbOfFilesInSubDir").'</b>: '.$nboffilesinsubdir;
399  else $htmltooltip.='<b>'.$langs->trans("ECMNbOfSubDir").'</b>: '.$nbofsubdir.'<br>';
400  print $form->textwithpicto('', $htmltooltip, 1, 'info');
401  print "</td>";
402 
403  print '</tr></table>';
404 
405  print '</div>';
406 
407  print "</li>\n";
408  }
409 
410  $oldvallevel=$val['level'];
411  $nbofentries++;
412  }
413 
414  // If nothing to show
415  if ($nbofentries == 0)
416  {
417  print '<li class="directory collapsed">';
418  print '<div class="ecmjqft">';
419  print $langs->trans("ECMNoDirectoryYet");
420  print '</div>';
421  print "</li>\n";
422  }
423 
424  print '</ul>';
425 }
426 
427 
428 // Close db if mode is not noajax
429 if ((! isset($mode) || $mode != 'noajax') && is_object($db)) $db->close();
tree_showpad(&$fulltree, $key, $silent=0)
Show indent and picto of a tree line.
Class to manage Dolibarr users.
Definition: user.class.php:39
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:414
if(empty($reshook)) $form
View.
Definition: perms.php:103
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
jsUnEscape($source)
Same function than javascript unescape() function but in PHP.
dol_print_date($time, $format='', $tzoutput='tzserver', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
print
Draft customers invoices.
Definition: index.php:91
if(!defined('NOREQUIREMENU')) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html')
Show HTTP header.
Definition: main.inc.php:1052
Class to manage ECM directories.