dolibarr  16.0.1
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 // Get parameters
38 $socid = GETPOST('socid', 'int');
39 $action = GETPOST('action', 'aZ09');
40 $section = GETPOST('section', 'int') ?GETPOST('section', 'int') : GETPOST('section_id', 'int');
41 if (!$section) {
42  $section = 0;
43 }
44 $section_dir = GETPOST('section_dir', 'alpha');
45 $overwritefile = GETPOST('overwritefile', 'int');
46 
47 $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
48 $sortfield = GETPOST('sortfield', 'aZ09comma');
49 $sortorder = GETPOST('sortorder', 'aZ09comma');
50 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
51 if (empty($page) || $page == -1) {
52  $page = 0;
53 } // If $page is not defined, or '' or -1
54 $offset = $limit * $page;
55 $pageprev = $page - 1;
56 $pagenext = $page + 1;
57 if (!$sortorder) {
58  $sortorder = "ASC";
59 }
60 if (!$sortfield) {
61  $sortfield = "name";
62 }
63 
64 $ecmdir = new EcmDirectory($db);
65 if ($section > 0) {
66  $result = $ecmdir->fetch($section);
67  if (!($result > 0)) {
68  dol_print_error($db, $ecmdir->error);
69  exit;
70  }
71 }
72 
73 $form = new Form($db);
74 $ecmdirstatic = new EcmDirectory($db);
75 $userstatic = new User($db);
76 
77 $error = 0;
78 
79 // Security check
80 if ($user->socid) {
81  $socid = $user->socid;
82 }
83 $result = restrictedArea($user, 'ecm', 0);
84 
85 
86 /*
87  * Actions
88  */
89 
90 // TODO Replace sendit and confirm_deletefile with
91 //$backtopage=$_SERVER["PHP_SELF"].'?file_manager=1&website='.$websitekey.'&pageid='.$pageid; // used after a confirm_deletefile into actions_linkedfiles.inc.php
92 //include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
93 
94 // Upload file (code similar but different than actions_linkedfiles.inc.php)
95 if (GETPOST("sendit", 'alphanohtml') && !empty($conf->global->MAIN_UPLOAD_DOC)) {
96  // Define relativepath and upload_dir
97  $relativepath = '';
98  if ($ecmdir->id) {
99  $relativepath = $ecmdir->getRelativePath();
100  } else {
101  $relativepath = $section_dir;
102  }
103  $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
104 
105  if (is_array($_FILES['userfile']['tmp_name'])) {
106  $userfiles = $_FILES['userfile']['tmp_name'];
107  } else {
108  $userfiles = array($_FILES['userfile']['tmp_name']);
109  }
110 
111  foreach ($userfiles as $key => $userfile) {
112  if (empty($_FILES['userfile']['tmp_name'][$key])) {
113  $error++;
114  if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2) {
115  setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
116  } else {
117  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("File")), null, 'errors');
118  }
119  }
120  }
121 
122  if (!$error) {
123  $generatethumbs = 0;
124  $res = dol_add_file_process($upload_dir, $overwritefile, 1, 'userfile', '', null, '', $generatethumbs);
125  if ($res > 0) {
126  $result = $ecmdir->changeNbOfFiles('+');
127  }
128  }
129 }
130 
131 // Remove file (code similar but different than actions_linkedfiles.inc.php)
132 if ($action == 'confirm_deletefile') {
133  if (GETPOST('confirm') == 'yes') {
134  // GETPOST('urlfile','alpha') is full relative URL from ecm root dir. Contains path of all sections.
135 
136  $upload_dir = $conf->ecm->dir_output.($relativepath ? '/'.$relativepath : '');
137  $file = $upload_dir."/".GETPOST('urlfile', 'alpha');
138  $ret = dol_delete_file($file); // This include also the delete from file index in database.
139  if ($ret) {
140  $urlfiletoshow = GETPOST('urlfile', 'alpha');
141  $urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfiletoshow);
142  setEventMessages($langs->trans("FileWasRemoved", $urlfiletoshow), null, 'mesgs');
143  $result = $ecmdir->changeNbOfFiles('-');
144  } else {
145  setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
146  }
147 
148  clearstatcache();
149  }
150  $action = 'file_manager';
151 }
152 
153 // Add directory
154 if ($action == 'add' && $user->rights->ecm->setup) {
155  $ecmdir->ref = 'NOTUSEDYET';
156  $ecmdir->label = GETPOST("label");
157  $ecmdir->description = GETPOST("desc");
158 
159  $id = $ecmdir->create($user);
160  if ($id > 0) {
161  header("Location: ".$_SERVER["PHP_SELF"]);
162  exit;
163  } else {
164  setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
165  $action = "create";
166  }
167 
168  clearstatcache();
169 }
170 
171 // Remove directory
172 if ($action == 'confirm_deletesection' && GETPOST('confirm', 'alpha') == 'yes') {
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  $ecmdirtmp = new EcmDirectory($db);
184 
185  // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
186  clearstatcache();
187 
188  $diroutputslash = str_replace('\\', '/', $conf->ecm->dir_output);
189  $diroutputslash .= '/';
190 
191  // Scan directory tree on disk
192  $disktree = dol_dir_list($conf->ecm->dir_output, 'directories', 1, '', '^temp$', '', '', 0);
193 
194  // Scan directory tree in database
195  $sqltree = $ecmdirstatic->get_full_arbo(0);
196 
197  $adirwascreated = 0;
198 
199  // Now we compare both trees to complete missing trees into database
200  //var_dump($disktree);
201  //var_dump($sqltree);
202  foreach ($disktree as $dirdesc) { // Loop on tree onto disk
203  $dirisindatabase = 0;
204  foreach ($sqltree as $dirsqldesc) {
205  if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
206  $dirisindatabase = 1;
207  break;
208  }
209  }
210 
211  if (!$dirisindatabase) {
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  $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
224  $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
225  dol_syslog($txt);
226  //print $txt." -> ";
227  $parentdirisindatabase = 0;
228  foreach ($sqltree as $dirsqldesc) {
229  if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
230  $parentdirisindatabase = $dirsqldesc['id'];
231  break;
232  }
233  }
234  if ($parentdirisindatabase > 0) {
235  dol_syslog("Yes with id ".$parentdirisindatabase);
236  //print "Yes with id ".$parentdirisindatabase."<br>\n";
237  $fk_parent = $parentdirisindatabase;
238  //break; // We found parent, we can stop the while loop
239  } else {
240  dol_syslog("No");
241  //print "No<br>\n";
242  }
243  } else {
244  dol_syslog("Parent is root");
245  $fk_parent = 0; // Parent is root
246  }
247 
248  if ($fk_parent >= 0) {
249  $ecmdirtmp->ref = 'NOTUSEDYET';
250  $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
251  $ecmdirtmp->description = '';
252  $ecmdirtmp->fk_parent = $fk_parent;
253 
254  $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
255  dol_syslog($txt);
256  //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
257  $id = $ecmdirtmp->create($user);
258  if ($id > 0) {
259  $newdirsql = array('id'=>$id,
260  'id_mere'=>$ecmdirtmp->fk_parent,
261  'label'=>$ecmdirtmp->label,
262  'description'=>$ecmdirtmp->description,
263  'fullrelativename'=>$relativepathmissing);
264  $sqltree[] = $newdirsql; // We complete fulltree for following loops
265  //var_dump($sqltree);
266  $adirwascreated = 1;
267  } else {
268  dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
269  }
270  } else {
271  $txt = "Parent of ".$dirdesc['fullname']." not found";
272  dol_syslog($txt);
273  //print $txt."<br>\n";
274  }
275  }
276  }
277 
278  // Loop now on each sql tree to check if dir exists
279  foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
280  $dirtotest = $conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
281  if (!dol_is_dir($dirtotest)) {
282  $ecmdirtmp->id = $dirdesc['id'];
283  $ecmdirtmp->delete($user, 'databaseonly');
284  //exit;
285  }
286  }
287 
288  $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cahce counting, we set to value -1 = "unknown"
289  dol_syslog("sql = ".$sql);
290  $db->query($sql);
291 
292  // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
293  // it to be sure that fulltree array is not used without reloading it.
294  if ($adirwascreated) {
295  $sqltree = null;
296  }
297 }
298 
299 
300 
301 /*
302  * View
303  */
304 
305 // Define height of file area (depends on $_SESSION["dol_screenheight"])
306 //print $_SESSION["dol_screenheight"];
307 $maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466) ? ($_SESSION["dol_screenheight"] - 136) : 660; // Also into index_auto.php file
308 
309 $moreheadcss = '';
310 $moreheadjs = '';
311 
312 //$morejs=array();
313 $morejs = array('includes/jquery/plugins/blockUI/jquery.blockUI.js', 'core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
314 if (empty($conf->global->MAIN_ECM_DISABLE_JS)) {
315  $morejs[] = "includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
316 }
317 
318 $moreheadjs .= '<script type="text/javascript">'."\n";
319 $moreheadjs .= 'var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
320 $moreheadjs .= '</script>'."\n";
321 
322 llxHeader($moreheadcss.$moreheadjs, $langs->trans("ECMArea"), '', '', '', '', $morejs, '', 0, 0);
323 
324 $head = ecm_prepare_dasboard_head('');
325 print dol_get_fiche_head($head, 'index', '', -1, '');
326 
327 
328 // Add filemanager component
329 $module = 'ecm';
330 include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
331 
332 // End of page
333 print dol_get_fiche_end();
334 
335 llxFooter();
336 
337 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
if($cancel &&!$id) if($action== 'add'&&!$cancel) if($action== 'delete') if($id) $form
Actions.
Definition: card.php:142
Class to manage Dolibarr users.
Definition: user.class.php:44
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:446
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 from a POST of a form...
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_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
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
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:1633
Class to manage ECM directories.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
Definition: files.lib.php:1230