dolibarr  9.0.0
index.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2008-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2008-2010 Regis Houssin <regis.houssin@inodbox.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * You can call this page with param module=medias to get a filemanager for medias.
19  */
20 
27 require '../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
33 
34 // Load translation files required by the page
35 $langs->loadLangs(array("ecm","companies","other","users","orders","propal","bills","contracts"));
36 
37 // Security check
38 if ($user->societe_id) $socid=$user->societe_id;
39 $result = restrictedArea($user, 'ecm', 0);
40 
41 // Get parameters
42 $socid=GETPOST('socid','int');
43 $action=GETPOST('action','aZ09');
44 $section=GETPOST('section','int')?GETPOST('section','int'):GETPOST('section_id','int');
45 if (! $section) $section=0;
46 $section_dir=GETPOST('section_dir','alpha');
47 
48 $sortfield = GETPOST("sortfield",'alpha');
49 $sortorder = GETPOST("sortorder",'alpha');
50 $page = GETPOST("page",'int');
51 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
52 $offset = $conf->liste_limit * $page;
53 $pageprev = $page - 1;
54 $pagenext = $page + 1;
55 if (! $sortorder) $sortorder="ASC";
56 if (! $sortfield) $sortfield="fullname";
57 
58 $ecmdir = new EcmDirectory($db);
59 if ($section)
60 {
61  $result=$ecmdir->fetch($section);
62  if (! $result > 0)
63  {
64  dol_print_error($db,$ecmdir->error);
65  exit;
66  }
67 }
68 
69 $form=new Form($db);
70 $ecmdirstatic = new EcmDirectory($db);
71 $userstatic = new User($db);
72 
73 $error=0;
74 
75 
76 /*
77  * Actions
78  */
79 
80 // TODO Replace sendit and confirm_deletefile with
81 //$backtopage=$_SERVER["PHP_SELF"].'?file_manager=1&website='.$websitekey.'&pageid='.$pageid; // used after a confirm_deletefile into actions_linkedfiles.inc.php
82 //include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
83 
84 // Upload file (code similar but different than actions_linkedfiles.inc.php)
85 if (GETPOST("sendit",'none') && ! empty($conf->global->MAIN_UPLOAD_DOC))
86 {
87  // Define relativepath and upload_dir
88  $relativepath='';
89  if ($ecmdir->id) $relativepath=$ecmdir->getRelativePath();
90  else $relativepath=$section_dir;
91  $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
92 
93  if (is_array($_FILES['userfile']['tmp_name'])) $userfiles=$_FILES['userfile']['tmp_name'];
94  else $userfiles=array($_FILES['userfile']['tmp_name']);
95 
96  foreach($userfiles as $key => $userfile)
97  {
98  if (empty($_FILES['userfile']['tmp_name'][$key]))
99  {
100  $error++;
101  if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2){
102  setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
103  }
104  else {
105  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("File")), null, 'errors');
106  }
107  }
108  }
109 
110  if (! $error)
111  {
112  $generatethumbs = 0;
113  $res = dol_add_file_process($upload_dir, 0, 1, 'userfile', '', null, '', $generatethumbs);
114  if ($res > 0)
115  {
116  $result=$ecmdir->changeNbOfFiles('+');
117  }
118  }
119 }
120 
121 // Remove file (code similar but different than actions_linkedfiles.inc.php)
122 if ($action == 'confirm_deletefile')
123 {
124  if (GETPOST('confirm') == 'yes')
125  {
126  // GETPOST('urlfile','alpha') is full relative URL from ecm root dir. Contains path of all sections.
127  //var_dump(GETPOST('urlfile'));exit;
128 
129  $upload_dir = $conf->ecm->dir_output.($relativepath?'/'.$relativepath:'');
130  $file = $upload_dir . "/" . GETPOST('urlfile','alpha');
131 
132  $ret=dol_delete_file($file); // This include also the delete from file index in database.
133  if ($ret)
134  {
135  setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile','alpha')), null, 'mesgs');
136  $result=$ecmdir->changeNbOfFiles('-');
137  }
138  else
139  {
140  setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile','alpha')), null, 'errors');
141  }
142 
143  clearstatcache();
144  }
145  $action='file_manager';
146 }
147 
148 // Add directory
149 if ($action == 'add' && $user->rights->ecm->setup)
150 {
151  $ecmdir->ref = 'NOTUSEDYET';
152  $ecmdir->label = GETPOST("label");
153  $ecmdir->description = GETPOST("desc");
154 
155  $id = $ecmdir->create($user);
156  if ($id > 0)
157  {
158  header("Location: ".$_SERVER["PHP_SELF"]);
159  exit;
160  }
161  else
162  {
163  setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
164  $action = "create";
165  }
166 
167  clearstatcache();
168 }
169 
170 // Remove directory
171 if ($action == 'confirm_deletesection' && GETPOST('confirm') == 'yes')
172 {
173  $result=$ecmdir->delete($user);
174  setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
175 
176  clearstatcache();
177 }
178 
179 // Refresh directory view
180 // This refresh list of dirs, not list of files (for preformance reason). List of files is refresh only if dir was not synchronized.
181 // To refresh content of dir with cache, just open the dir in edit mode.
182 if ($action == 'refreshmanual')
183 {
184  $ecmdirtmp = new EcmDirectory($db);
185 
186  // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
187  clearstatcache();
188 
189  $diroutputslash=str_replace('\\','/',$conf->ecm->dir_output);
190  $diroutputslash.='/';
191 
192  // Scan directory tree on disk
193  $disktree=dol_dir_list($conf->ecm->dir_output,'directories',1,'','^temp$','','',0);
194 
195  // Scan directory tree in database
196  $sqltree=$ecmdirstatic->get_full_arbo(0);
197 
198  $adirwascreated=0;
199 
200  // Now we compare both trees to complete missing trees into database
201  //var_dump($disktree);
202  //var_dump($sqltree);
203  foreach($disktree as $dirdesc) // Loop on tree onto disk
204  {
205  $dirisindatabase=0;
206  foreach($sqltree as $dirsqldesc)
207  {
208  if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname'])
209  {
210  $dirisindatabase=1;
211  break;
212  }
213  }
214 
215  if (! $dirisindatabase)
216  {
217  $txt="Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
218  dol_syslog($txt);
219  //print $txt."<br>\n";
220 
221  // We must first find the fk_parent of directory to create $dirdesc['fullname']
222  $fk_parent=-1;
223  $relativepathmissing=str_replace($diroutputslash,'',$dirdesc['fullname']);
224  $relativepathtosearchparent=$relativepathmissing;
225  //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
226  if (preg_match('/\//',$relativepathtosearchparent))
227  //while (preg_match('/\//',$relativepathtosearchparent))
228  {
229  $relativepathtosearchparent=preg_replace('/\/[^\/]*$/','',$relativepathtosearchparent);
230  $txt="Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
231  dol_syslog($txt);
232  //print $txt." -> ";
233  $parentdirisindatabase=0;
234  foreach($sqltree as $dirsqldesc)
235  {
236  if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent)
237  {
238  $parentdirisindatabase=$dirsqldesc['id'];
239  break;
240  }
241  }
242  if ($parentdirisindatabase > 0)
243  {
244  dol_syslog("Yes with id ".$parentdirisindatabase);
245  //print "Yes with id ".$parentdirisindatabase."<br>\n";
246  $fk_parent=$parentdirisindatabase;
247  //break; // We found parent, we can stop the while loop
248  }
249  else
250  {
251  dol_syslog("No");
252  //print "No<br>\n";
253  }
254  }
255  else
256  {
257  dol_syslog("Parent is root");
258  $fk_parent=0; // Parent is root
259  }
260 
261  if ($fk_parent >= 0)
262  {
263  $ecmdirtmp->ref = 'NOTUSEDYET';
264  $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
265  $ecmdirtmp->description = '';
266  $ecmdirtmp->fk_parent = $fk_parent;
267 
268  $txt="We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
269  dol_syslog($txt);
270  //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
271  $id = $ecmdirtmp->create($user);
272  if ($id > 0)
273  {
274  $newdirsql=array('id'=>$id,
275  'id_mere'=>$ecmdirtmp->fk_parent,
276  'label'=>$ecmdirtmp->label,
277  'description'=>$ecmdirtmp->description,
278  'fullrelativename'=>$relativepathmissing);
279  $sqltree[]=$newdirsql; // We complete fulltree for following loops
280  //var_dump($sqltree);
281  $adirwascreated=1;
282  }
283  else
284  {
285  dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
286  }
287  }
288  else {
289  $txt="Parent of ".$dirdesc['fullname']." not found";
290  dol_syslog($txt);
291  //print $txt."<br>\n";
292  }
293  }
294  }
295 
296  // Loop now on each sql tree to check if dir exists
297  foreach($sqltree as $dirdesc) // Loop on each sqltree to check dir is on disk
298  {
299  $dirtotest=$conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
300  if (! dol_is_dir($dirtotest))
301  {
302  $ecmdirtmp->id=$dirdesc['id'];
303  $ecmdirtmp->delete($user,'databaseonly');
304  //exit;
305  }
306  }
307 
308  $sql="UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cahce counting, we set to value -1 = "unknown"
309  dol_syslog("sql = ".$sql);
310  $db->query($sql);
311 
312  // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
313  // it to be sure that fulltree array is not used without reloading it.
314  if ($adirwascreated) $sqltree=null;
315 }
316 
317 
318 
319 /*
320  * View
321  */
322 
323 // Define height of file area (depends on $_SESSION["dol_screenheight"])
324 //print $_SESSION["dol_screenheight"];
325 $maxheightwin=(isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466)?($_SESSION["dol_screenheight"]-136):660; // Also into index_auto.php file
326 
327 $moreheadcss='';
328 $moreheadjs='';
329 
330 //$morejs=array();
331 $morejs=array('includes/jquery/plugins/blockUI/jquery.blockUI.js','core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
332 if (empty($conf->global->MAIN_ECM_DISABLE_JS)) $morejs[]="includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
333 
334 $moreheadjs.='<script type="text/javascript">'."\n";
335 $moreheadjs.='var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
336 $moreheadjs.='</script>'."\n";
337 
338 llxHeader($moreheadcss.$moreheadjs,$langs->trans("ECMArea"),'','','','',$morejs,'',0,0);
339 
340 $head = ecm_prepare_dasboard_head('');
341 dol_fiche_head($head, 'index', $langs->trans("ECMArea").' - '.$langs->trans("ECMFileManager"), -1, '');
342 
343 
344 // Add filemanager component
345 $module='ecm';
346 include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
347 
348 // End of page
349 dol_fiche_end();
350 
351 llxFooter();
352 
353 $db->close();
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
Class to manage Dolibarr users.
Definition: user.class.php:41
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:438
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.
dol_add_file_process($upload_dir, $allowoverwrite=0, $donotupdatesession=0, $varfiles='addedfile', $savingdocmask='', $link=null, $trackid='', $generatethumbs=1)
Get and save an upload file (for example after submitting a new file a mail form).
Definition: files.lib.php:1519
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
Definition: files.lib.php:35
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
Remove a file or several files with a mask.
Definition: files.lib.php:1139
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:59
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
Class to manage ECM directories.