dolibarr  13.0.2
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 <https://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->socid) $socid = $user->socid;
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 $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
49 $sortfield = GETPOST("sortfield", 'alpha');
50 $sortorder = GETPOST("sortorder", 'alpha');
51 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
52 if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
53 $offset = $limit * $page;
54 $pageprev = $page - 1;
55 $pagenext = $page + 1;
56 if (!$sortorder) $sortorder = "ASC";
57 if (!$sortfield) $sortfield = "fullname";
58 
59 $ecmdir = new EcmDirectory($db);
60 if ($section > 0)
61 {
62  $result = $ecmdir->fetch($section);
63  if (!$result > 0)
64  {
65  dol_print_error($db, $ecmdir->error);
66  exit;
67  }
68 }
69 
70 $form = new Form($db);
71 $ecmdirstatic = new EcmDirectory($db);
72 $userstatic = new User($db);
73 
74 $error = 0;
75 
76 
77 /*
78  * Actions
79  */
80 
81 // TODO Replace sendit and confirm_deletefile with
82 //$backtopage=$_SERVER["PHP_SELF"].'?file_manager=1&website='.$websitekey.'&pageid='.$pageid; // used after a confirm_deletefile into actions_linkedfiles.inc.php
83 //include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
84 
85 // Upload file (code similar but different than actions_linkedfiles.inc.php)
86 if (GETPOST("sendit", 'alphanohtml') && !empty($conf->global->MAIN_UPLOAD_DOC))
87 {
88  // Define relativepath and upload_dir
89  $relativepath = '';
90  if ($ecmdir->id) $relativepath = $ecmdir->getRelativePath();
91  else $relativepath = $section_dir;
92  $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
93 
94  if (is_array($_FILES['userfile']['tmp_name'])) $userfiles = $_FILES['userfile']['tmp_name'];
95  else $userfiles = array($_FILES['userfile']['tmp_name']);
96 
97  foreach ($userfiles as $key => $userfile)
98  {
99  if (empty($_FILES['userfile']['tmp_name'][$key]))
100  {
101  $error++;
102  if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2) {
103  setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
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 
128  $upload_dir = $conf->ecm->dir_output.($relativepath ? '/'.$relativepath : '');
129  $file = $upload_dir."/".GETPOST('urlfile', 'alpha');
130  $ret = dol_delete_file($file); // This include also the delete from file index in database.
131  if ($ret)
132  {
133  $urlfiletoshow = GETPOST('urlfile', 'alpha');
134  $urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfiletoshow);
135  setEventMessages($langs->trans("FileWasRemoved", $urlfiletoshow), null, 'mesgs');
136  $result = $ecmdir->changeNbOfFiles('-');
137  } else {
138  setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
139  }
140 
141  clearstatcache();
142  }
143  $action = 'file_manager';
144 }
145 
146 // Add directory
147 if ($action == 'add' && $user->rights->ecm->setup)
148 {
149  $ecmdir->ref = 'NOTUSEDYET';
150  $ecmdir->label = GETPOST("label");
151  $ecmdir->description = GETPOST("desc");
152 
153  $id = $ecmdir->create($user);
154  if ($id > 0)
155  {
156  header("Location: ".$_SERVER["PHP_SELF"]);
157  exit;
158  } else {
159  setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
160  $action = "create";
161  }
162 
163  clearstatcache();
164 }
165 
166 // Remove directory
167 if ($action == 'confirm_deletesection' && GETPOST('confirm', 'alpha') == 'yes')
168 {
169  $result = $ecmdir->delete($user);
170  setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
171 
172  clearstatcache();
173 }
174 
175 // Refresh directory view
176 // This refresh list of dirs, not list of files (for preformance reason). List of files is refresh only if dir was not synchronized.
177 // To refresh content of dir with cache, just open the dir in edit mode.
178 if ($action == 'refreshmanual')
179 {
180  $ecmdirtmp = new EcmDirectory($db);
181 
182  // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
183  clearstatcache();
184 
185  $diroutputslash = str_replace('\\', '/', $conf->ecm->dir_output);
186  $diroutputslash .= '/';
187 
188  // Scan directory tree on disk
189  $disktree = dol_dir_list($conf->ecm->dir_output, 'directories', 1, '', '^temp$', '', '', 0);
190 
191  // Scan directory tree in database
192  $sqltree = $ecmdirstatic->get_full_arbo(0);
193 
194  $adirwascreated = 0;
195 
196  // Now we compare both trees to complete missing trees into database
197  //var_dump($disktree);
198  //var_dump($sqltree);
199  foreach ($disktree as $dirdesc) // Loop on tree onto disk
200  {
201  $dirisindatabase = 0;
202  foreach ($sqltree as $dirsqldesc)
203  {
204  if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname'])
205  {
206  $dirisindatabase = 1;
207  break;
208  }
209  }
210 
211  if (!$dirisindatabase)
212  {
213  $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
214  dol_syslog($txt);
215  //print $txt."<br>\n";
216 
217  // We must first find the fk_parent of directory to create $dirdesc['fullname']
218  $fk_parent = -1;
219  $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
220  $relativepathtosearchparent = $relativepathmissing;
221  //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
222  if (preg_match('/\//', $relativepathtosearchparent))
223  //while (preg_match('/\//',$relativepathtosearchparent))
224  {
225  $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
226  $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
227  dol_syslog($txt);
228  //print $txt." -> ";
229  $parentdirisindatabase = 0;
230  foreach ($sqltree as $dirsqldesc)
231  {
232  if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent)
233  {
234  $parentdirisindatabase = $dirsqldesc['id'];
235  break;
236  }
237  }
238  if ($parentdirisindatabase > 0)
239  {
240  dol_syslog("Yes with id ".$parentdirisindatabase);
241  //print "Yes with id ".$parentdirisindatabase."<br>\n";
242  $fk_parent = $parentdirisindatabase;
243  //break; // We found parent, we can stop the while loop
244  } else {
245  dol_syslog("No");
246  //print "No<br>\n";
247  }
248  } else {
249  dol_syslog("Parent is root");
250  $fk_parent = 0; // Parent is root
251  }
252 
253  if ($fk_parent >= 0)
254  {
255  $ecmdirtmp->ref = 'NOTUSEDYET';
256  $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
257  $ecmdirtmp->description = '';
258  $ecmdirtmp->fk_parent = $fk_parent;
259 
260  $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
261  dol_syslog($txt);
262  //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
263  $id = $ecmdirtmp->create($user);
264  if ($id > 0)
265  {
266  $newdirsql = array('id'=>$id,
267  'id_mere'=>$ecmdirtmp->fk_parent,
268  'label'=>$ecmdirtmp->label,
269  'description'=>$ecmdirtmp->description,
270  'fullrelativename'=>$relativepathmissing);
271  $sqltree[] = $newdirsql; // We complete fulltree for following loops
272  //var_dump($sqltree);
273  $adirwascreated = 1;
274  } else {
275  dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
276  }
277  } else {
278  $txt = "Parent of ".$dirdesc['fullname']." not found";
279  dol_syslog($txt);
280  //print $txt."<br>\n";
281  }
282  }
283  }
284 
285  // Loop now on each sql tree to check if dir exists
286  foreach ($sqltree as $dirdesc) // Loop on each sqltree to check dir is on disk
287  {
288  $dirtotest = $conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
289  if (!dol_is_dir($dirtotest))
290  {
291  $ecmdirtmp->id = $dirdesc['id'];
292  $ecmdirtmp->delete($user, 'databaseonly');
293  //exit;
294  }
295  }
296 
297  $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cahce counting, we set to value -1 = "unknown"
298  dol_syslog("sql = ".$sql);
299  $db->query($sql);
300 
301  // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
302  // it to be sure that fulltree array is not used without reloading it.
303  if ($adirwascreated) $sqltree = null;
304 }
305 
306 
307 
308 /*
309  * View
310  */
311 
312 // Define height of file area (depends on $_SESSION["dol_screenheight"])
313 //print $_SESSION["dol_screenheight"];
314 $maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466) ? ($_SESSION["dol_screenheight"] - 136) : 660; // Also into index_auto.php file
315 
316 $moreheadcss = '';
317 $moreheadjs = '';
318 
319 //$morejs=array();
320 $morejs = array('includes/jquery/plugins/blockUI/jquery.blockUI.js', 'core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
321 if (empty($conf->global->MAIN_ECM_DISABLE_JS)) $morejs[] = "includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
322 
323 $moreheadjs .= '<script type="text/javascript">'."\n";
324 $moreheadjs .= 'var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
325 $moreheadjs .= '</script>'."\n";
326 
327 llxHeader($moreheadcss.$moreheadjs, $langs->trans("ECMArea"), '', '', '', '', $morejs, '', 0, 0);
328 
329 $head = ecm_prepare_dasboard_head('');
330 print dol_get_fiche_head($head, 'index', $langs->trans("ECMArea").' - '.$langs->trans("ECMFileManager"), -1, '');
331 
332 
333 // Add filemanager component
334 $module = 'ecm';
335 include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
336 
337 // End of page
338 print dol_get_fiche_end();
339 
340 llxFooter();
341 
342 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Class to manage Dolibarr users.
Definition: user.class.php:44
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:432
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
Definition: files.lib.php:36
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:1144
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.
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
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_add_file_process($upload_dir, $allowoverwrite=0, $donotupdatesession=0, $varfiles= 'addedfile', $savingdocmask= '', $link=null, $trackid= '', $generatethumbs=1, $object=null)
Get and save an upload file (for example after submitting a new file a mail form).
Definition: files.lib.php:1528
Class to manage ECM directories.