dolibarr  16.0.5
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  $userfiles = [];
106  if (is_array($_FILES['userfile'])) {
107  if (is_array($_FILES['userfile']['tmp_name'])) {
108  $userfiles = $_FILES['userfile']['tmp_name'];
109  } else {
110  $userfiles = array($_FILES['userfile']['tmp_name']);
111  }
112  }
113 
114  foreach ($userfiles as $key => $userfile) {
115  if (empty($_FILES['userfile']['tmp_name'][$key])) {
116  $error++;
117  if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2) {
118  setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
119  } else {
120  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("File")), null, 'errors');
121  }
122  }
123  }
124 
125  if (!$error) {
126  $generatethumbs = 0;
127  $res = dol_add_file_process($upload_dir, $overwritefile, 1, 'userfile', '', null, '', $generatethumbs);
128  if ($res > 0) {
129  $result = $ecmdir->changeNbOfFiles('+');
130  }
131  }
132 }
133 
134 // Remove file (code similar but different than actions_linkedfiles.inc.php)
135 if ($action == 'confirm_deletefile') {
136  if (GETPOST('confirm') == 'yes') {
137  // GETPOST('urlfile','alpha') is full relative URL from ecm root dir. Contains path of all sections.
138 
139  $upload_dir = $conf->ecm->dir_output.($relativepath ? '/'.$relativepath : '');
140  $file = $upload_dir."/".GETPOST('urlfile', 'alpha');
141  $ret = dol_delete_file($file); // This include also the delete from file index in database.
142  if ($ret) {
143  $urlfiletoshow = GETPOST('urlfile', 'alpha');
144  $urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfiletoshow);
145  setEventMessages($langs->trans("FileWasRemoved", $urlfiletoshow), null, 'mesgs');
146  $result = $ecmdir->changeNbOfFiles('-');
147  } else {
148  setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
149  }
150 
151  clearstatcache();
152  }
153  $action = 'file_manager';
154 }
155 
156 // Add directory
157 if ($action == 'add' && $user->rights->ecm->setup) {
158  $ecmdir->ref = 'NOTUSEDYET';
159  $ecmdir->label = GETPOST("label");
160  $ecmdir->description = GETPOST("desc");
161 
162  $id = $ecmdir->create($user);
163  if ($id > 0) {
164  header("Location: ".$_SERVER["PHP_SELF"]);
165  exit;
166  } else {
167  setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
168  $action = "create";
169  }
170 
171  clearstatcache();
172 }
173 
174 // Remove directory
175 if ($action == 'confirm_deletesection' && GETPOST('confirm', 'alpha') == 'yes') {
176  $result = $ecmdir->delete($user);
177  setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
178 
179  clearstatcache();
180 }
181 
182 // Refresh directory view
183 // This refresh list of dirs, not list of files (for preformance reason). List of files is refresh only if dir was not synchronized.
184 // To refresh content of dir with cache, just open the dir in edit mode.
185 if ($action == 'refreshmanual') {
186  $ecmdirtmp = new EcmDirectory($db);
187 
188  // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
189  clearstatcache();
190 
191  $diroutputslash = str_replace('\\', '/', $conf->ecm->dir_output);
192  $diroutputslash .= '/';
193 
194  // Scan directory tree on disk
195  $disktree = dol_dir_list($conf->ecm->dir_output, 'directories', 1, '', '^temp$', '', '', 0);
196 
197  // Scan directory tree in database
198  $sqltree = $ecmdirstatic->get_full_arbo(0);
199 
200  $adirwascreated = 0;
201 
202  // Now we compare both trees to complete missing trees into database
203  //var_dump($disktree);
204  //var_dump($sqltree);
205  foreach ($disktree as $dirdesc) { // Loop on tree onto disk
206  $dirisindatabase = 0;
207  foreach ($sqltree as $dirsqldesc) {
208  if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
209  $dirisindatabase = 1;
210  break;
211  }
212  }
213 
214  if (!$dirisindatabase) {
215  $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
216  dol_syslog($txt);
217  //print $txt."<br>\n";
218 
219  // We must first find the fk_parent of directory to create $dirdesc['fullname']
220  $fk_parent = -1;
221  $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
222  $relativepathtosearchparent = $relativepathmissing;
223  //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
224  if (preg_match('/\//', $relativepathtosearchparent)) {
225  //while (preg_match('/\//',$relativepathtosearchparent))
226  $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
227  $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
228  dol_syslog($txt);
229  //print $txt." -> ";
230  $parentdirisindatabase = 0;
231  foreach ($sqltree as $dirsqldesc) {
232  if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
233  $parentdirisindatabase = $dirsqldesc['id'];
234  break;
235  }
236  }
237  if ($parentdirisindatabase > 0) {
238  dol_syslog("Yes with id ".$parentdirisindatabase);
239  //print "Yes with id ".$parentdirisindatabase."<br>\n";
240  $fk_parent = $parentdirisindatabase;
241  //break; // We found parent, we can stop the while loop
242  } else {
243  dol_syslog("No");
244  //print "No<br>\n";
245  }
246  } else {
247  dol_syslog("Parent is root");
248  $fk_parent = 0; // Parent is root
249  }
250 
251  if ($fk_parent >= 0) {
252  $ecmdirtmp->ref = 'NOTUSEDYET';
253  $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
254  $ecmdirtmp->description = '';
255  $ecmdirtmp->fk_parent = $fk_parent;
256 
257  $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
258  dol_syslog($txt);
259  //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
260  $id = $ecmdirtmp->create($user);
261  if ($id > 0) {
262  $newdirsql = array('id'=>$id,
263  'id_mere'=>$ecmdirtmp->fk_parent,
264  'label'=>$ecmdirtmp->label,
265  'description'=>$ecmdirtmp->description,
266  'fullrelativename'=>$relativepathmissing);
267  $sqltree[] = $newdirsql; // We complete fulltree for following loops
268  //var_dump($sqltree);
269  $adirwascreated = 1;
270  } else {
271  dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
272  }
273  } else {
274  $txt = "Parent of ".$dirdesc['fullname']." not found";
275  dol_syslog($txt);
276  //print $txt."<br>\n";
277  }
278  }
279  }
280 
281  // Loop now on each sql tree to check if dir exists
282  foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
283  $dirtotest = $conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
284  if (!dol_is_dir($dirtotest)) {
285  $ecmdirtmp->id = $dirdesc['id'];
286  $ecmdirtmp->delete($user, 'databaseonly');
287  //exit;
288  }
289  }
290 
291  $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cahce counting, we set to value -1 = "unknown"
292  dol_syslog("sql = ".$sql);
293  $db->query($sql);
294 
295  // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
296  // it to be sure that fulltree array is not used without reloading it.
297  if ($adirwascreated) {
298  $sqltree = null;
299  }
300 }
301 
302 
303 
304 /*
305  * View
306  */
307 
308 // Define height of file area (depends on $_SESSION["dol_screenheight"])
309 //print $_SESSION["dol_screenheight"];
310 $maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466) ? ($_SESSION["dol_screenheight"] - 136) : 660; // Also into index_auto.php file
311 
312 $moreheadcss = '';
313 $moreheadjs = '';
314 
315 //$morejs=array();
316 $morejs = array('includes/jquery/plugins/blockUI/jquery.blockUI.js', 'core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
317 if (empty($conf->global->MAIN_ECM_DISABLE_JS)) {
318  $morejs[] = "includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
319 }
320 
321 $moreheadjs .= '<script type="text/javascript">'."\n";
322 $moreheadjs .= 'var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
323 $moreheadjs .= '</script>'."\n";
324 
325 llxHeader($moreheadcss.$moreheadjs, $langs->trans("ECMArea"), '', '', '', '', $morejs, '', 0, 0);
326 
327 $head = ecm_prepare_dasboard_head('');
328 print dol_get_fiche_head($head, 'index', '', -1, '');
329 
330 
331 // Add filemanager component
332 $module = 'ecm';
333 include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
334 
335 // End of page
336 print dol_get_fiche_end();
337 
338 llxFooter();
339 
340 $db->close();
dol_basename
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
Definition: files.lib.php:36
restrictedArea
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.
Definition: security.lib.php:234
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
dol_add_file_process
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:1634
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
dol_dir_list
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
dol_delete_file
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:1231
EcmDirectory
Class to manage ECM directories.
Definition: ecmdirectory.class.php:28
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
User
Class to manage Dolibarr users.
Definition: user.class.php:44
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
dol_is_dir
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:447