dolibarr  7.0.0-beta
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@capnetworks.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 traductions files
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 // Upload file (code similar but different than actions_linkedfiles.inc.php)
81 if (GETPOST("sendit",'none') && ! empty($conf->global->MAIN_UPLOAD_DOC))
82 {
83  // Define relativepath and upload_dir
84  $relativepath='';
85  if ($ecmdir->id) $relativepath=$ecmdir->getRelativePath();
86  else $relativepath=$section_dir;
87  $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
88 
89  if (is_array($_FILES['userfile']['tmp_name'])) $userfiles=$_FILES['userfile']['tmp_name'];
90  else $userfiles=array($_FILES['userfile']['tmp_name']);
91 
92  foreach($userfiles as $key => $userfile)
93  {
94  if (empty($_FILES['userfile']['tmp_name'][$key]))
95  {
96  $error++;
97  if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2){
98  setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
99  }
100  else {
101  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("File")), null, 'errors');
102  }
103  }
104  }
105 
106  if (! $error)
107  {
108  $res = dol_add_file_process($upload_dir, 0, 1, 'userfile', '', '', '', 0);
109  if ($res > 0)
110  {
111  $result=$ecmdir->changeNbOfFiles('+');
112  }
113  }
114 }
115 
116 // Add directory
117 if ($action == 'add' && $user->rights->ecm->setup)
118 {
119  $ecmdir->ref = 'NOTUSEDYET';
120  $ecmdir->label = GETPOST("label");
121  $ecmdir->description = GETPOST("desc");
122 
123  $id = $ecmdir->create($user);
124  if ($id > 0)
125  {
126  header("Location: ".$_SERVER["PHP_SELF"]);
127  exit;
128  }
129  else
130  {
131  setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
132  $action = "create";
133  }
134 
135  clearstatcache();
136 }
137 
138 // Remove file (code similar but different than actions_linkedfiles.inc.php)
139 if ($action == 'confirm_deletefile')
140 {
141  if (GETPOST('confirm') == 'yes')
142  {
143  // GETPOST('urlfile','alpha') is full relative URL from ecm root dir. Contains path of all sections.
144  //var_dump(GETPOST('urlfile'));exit;
145 
146  $upload_dir = $conf->ecm->dir_output.($relativepath?'/'.$relativepath:'');
147  $file = $upload_dir . "/" . GETPOST('urlfile','alpha'); // Do not use urldecode here ($_GET and $_POST are already decoded by PHP).
148 
149  $ret=dol_delete_file($file); // This include also the delete from file index in database.
150  if ($ret)
151  {
152  setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile','alpha')), null, 'mesgs');
153  $result=$ecmdir->changeNbOfFiles('-');
154  }
155  else
156  {
157  setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile','alpha')), null, 'errors');
158  }
159 
160  clearstatcache();
161  }
162  $action='file_manager';
163 }
164 
165 // Remove directory
166 if ($action == 'confirm_deletesection' && GETPOST('confirm') == 'yes')
167 {
168  $result=$ecmdir->delete($user);
169  setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
170 
171  clearstatcache();
172 }
173 
174 // Refresh directory view
175 // This refresh list of dirs, not list of files (for preformance reason). List of files is refresh only if dir was not synchronized.
176 // To refresh content of dir with cache, just open the dir in edit mode.
177 if ($action == 'refreshmanual')
178 {
179  $ecmdirtmp = new EcmDirectory($db);
180 
181  // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
182  clearstatcache();
183 
184  $diroutputslash=str_replace('\\','/',$conf->ecm->dir_output);
185  $diroutputslash.='/';
186 
187  // Scan directory tree on disk
188  $disktree=dol_dir_list($conf->ecm->dir_output,'directories',1,'','^temp$','','',0);
189 
190  // Scan directory tree in database
191  $sqltree=$ecmdirstatic->get_full_arbo(0);
192 
193  $adirwascreated=0;
194 
195  // Now we compare both trees to complete missing trees into database
196  //var_dump($disktree);
197  //var_dump($sqltree);
198  foreach($disktree as $dirdesc) // Loop on tree onto disk
199  {
200  $dirisindatabase=0;
201  foreach($sqltree as $dirsqldesc)
202  {
203  if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname'])
204  {
205  $dirisindatabase=1;
206  break;
207  }
208  }
209 
210  if (! $dirisindatabase)
211  {
212  $txt="Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
213  dol_syslog($txt);
214  //print $txt."<br>\n";
215 
216  // We must first find the fk_parent of directory to create $dirdesc['fullname']
217  $fk_parent=-1;
218  $relativepathmissing=str_replace($diroutputslash,'',$dirdesc['fullname']);
219  $relativepathtosearchparent=$relativepathmissing;
220  //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
221  if (preg_match('/\//',$relativepathtosearchparent))
222  //while (preg_match('/\//',$relativepathtosearchparent))
223  {
224  $relativepathtosearchparent=preg_replace('/\/[^\/]*$/','',$relativepathtosearchparent);
225  $txt="Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
226  dol_syslog($txt);
227  //print $txt." -> ";
228  $parentdirisindatabase=0;
229  foreach($sqltree as $dirsqldesc)
230  {
231  if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent)
232  {
233  $parentdirisindatabase=$dirsqldesc['id'];
234  break;
235  }
236  }
237  if ($parentdirisindatabase > 0)
238  {
239  dol_syslog("Yes with id ".$parentdirisindatabase);
240  //print "Yes with id ".$parentdirisindatabase."<br>\n";
241  $fk_parent=$parentdirisindatabase;
242  //break; // We found parent, we can stop the while loop
243  }
244  else
245  {
246  dol_syslog("No");
247  //print "No<br>\n";
248  }
249  }
250  else
251  {
252  dol_syslog("Parent is root");
253  $fk_parent=0; // Parent is root
254  }
255 
256  if ($fk_parent >= 0)
257  {
258  $ecmdirtmp->ref = 'NOTUSEDYET';
259  $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
260  $ecmdirtmp->description = '';
261  $ecmdirtmp->fk_parent = $fk_parent;
262 
263  $txt="We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
264  dol_syslog($txt);
265  //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
266  $id = $ecmdirtmp->create($user);
267  if ($id > 0)
268  {
269  $newdirsql=array('id'=>$id,
270  'id_mere'=>$ecmdirtmp->fk_parent,
271  'label'=>$ecmdirtmp->label,
272  'description'=>$ecmdirtmp->description,
273  'fullrelativename'=>$relativepathmissing);
274  $sqltree[]=$newdirsql; // We complete fulltree for following loops
275  //var_dump($sqltree);
276  $adirwascreated=1;
277  }
278  else
279  {
280  dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
281  }
282  }
283  else {
284  $txt="Parent of ".$dirdesc['fullname']." not found";
285  dol_syslog($txt);
286  //print $txt."<br>\n";
287  }
288  }
289  }
290 
291  // Loop now on each sql tree to check if dir exists
292  foreach($sqltree as $dirdesc) // Loop on each sqltree to check dir is on disk
293  {
294  $dirtotest=$conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
295  if (! dol_is_dir($dirtotest))
296  {
297  $ecmdirtmp->id=$dirdesc['id'];
298  $ecmdirtmp->delete($user,'databaseonly');
299  //exit;
300  }
301  }
302 
303  $sql="UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cahce counting, we set to value -1 = "unknown"
304  dol_syslog("sql = ".$sql);
305  $db->query($sql);
306 
307  // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
308  // it to be sure that fulltree array is not used without reloading it.
309  if ($adirwascreated) $sqltree=null;
310 }
311 
312 
313 
314 /*
315  * View
316  */
317 
318 // Define height of file area (depends on $_SESSION["dol_screenheight"])
319 //print $_SESSION["dol_screenheight"];
320 $maxheightwin=(isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466)?($_SESSION["dol_screenheight"]-136):660; // Also into index_auto.php file
321 
322 $moreheadcss='';
323 $moreheadjs='';
324 
325 //$morejs=array();
326 $morejs=array('includes/jquery/plugins/blockUI/jquery.blockUI.js','core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
327 if (empty($conf->global->MAIN_ECM_DISABLE_JS)) $morejs[]="includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
328 
329 $moreheadjs.='<script type="text/javascript">'."\n";
330 $moreheadjs.='var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
331 $moreheadjs.='</script>'."\n";
332 
333 llxHeader($moreheadcss.$moreheadjs,$langs->trans("ECMArea"),'','','','',$morejs,'',0,0);
334 
335 $head = ecm_prepare_dasboard_head('');
336 dol_fiche_head($head, 'index', $langs->trans("ECMArea").' - '.$langs->trans("ECMFileManager"), -1, '');
337 
338 
339 // Add filemanager component
340 $module='ecm';
341 include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
342 
343 // End of page
344 dol_fiche_end();
345 
346 llxFooter();
347 
348 $db->close();
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
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_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="")
Scan a directory and return a list of files/directories.
Definition: files.lib.php:58
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
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:1478
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)
Remove a file or several files with a mask.
Definition: files.lib.php:1103
Class to manage ECM directories.
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $objcanvas=null)
Check permissions of a user to show a page and an object.