dolibarr  19.0.0-dev
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 // Load Dolibarr environment
28 require '../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
34 
35 // Load translation files required by the page
36 $langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts'));
37 
38 // Get parameters
39 $socid = GETPOST('socid', 'int');
40 $action = GETPOST('action', 'aZ09');
41 $section = GETPOST('section', 'int') ?GETPOST('section', 'int') : GETPOST('section_id', 'int');
42 if (!$section) {
43  $section = 0;
44 }
45 $section_dir = GETPOST('section_dir', 'alpha');
46 $overwritefile = GETPOST('overwritefile', 'int');
47 
48 $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
49 $sortfield = GETPOST('sortfield', 'aZ09comma');
50 $sortorder = GETPOST('sortorder', 'aZ09comma');
51 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
52 if (empty($page) || $page == -1) {
53  $page = 0;
54 } // If $page is not defined, or '' or -1
55 $offset = $limit * $page;
56 $pageprev = $page - 1;
57 $pagenext = $page + 1;
58 if (!$sortorder) {
59  $sortorder = "ASC";
60 }
61 if (!$sortfield) {
62  $sortfield = "name";
63 }
64 
65 $ecmdir = new EcmDirectory($db);
66 if ($section > 0) {
67  $result = $ecmdir->fetch($section);
68  if (!($result > 0)) {
69  dol_print_error($db, $ecmdir->error);
70  exit;
71  }
72 }
73 
74 $form = new Form($db);
75 $ecmdirstatic = new EcmDirectory($db);
76 $userstatic = new User($db);
77 
78 $error = 0;
79 
80 // Security check
81 if ($user->socid) {
82  $socid = $user->socid;
83 }
84 $result = restrictedArea($user, 'ecm', 0);
85 
86 $permissiontoread = $user->hasRight('ecm', 'read');
87 $permissiontocreate = $user->hasRight('ecm', 'upload');
88 $permissiontocreatedir = $user->hasRight('ecm', 'setup');
89 $permissiontodelete = $user->hasRight('ecm', 'upload');
90 $permissiontodeletedir = $user->hasRight('ecm', 'setup');
91 
92 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
93 $hookmanager->initHooks(array('ecmindexcard', 'globalcard'));
94 
95 /*
96  * Actions
97  */
98 
99 // TODO Replace sendit and confirm_deletefile with
100 //$backtopage=$_SERVER["PHP_SELF"].'?file_manager=1&website='.$websitekey.'&pageid='.$pageid; // used after a confirm_deletefile into actions_linkedfiles.inc.php
101 //include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
102 
103 // Upload file (code similar but different than actions_linkedfiles.inc.php)
104 if (GETPOST("sendit", 'alphanohtml') && !empty($conf->global->MAIN_UPLOAD_DOC) && $permissiontocreate) {
105  // Define relativepath and upload_dir
106  $relativepath = '';
107  if ($ecmdir->id) {
108  $relativepath = $ecmdir->getRelativePath();
109  } else {
110  $relativepath = $section_dir;
111  }
112  $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
113 
114  if (is_array($_FILES['userfile']['tmp_name'])) {
115  $userfiles = $_FILES['userfile']['tmp_name'];
116  } else {
117  $userfiles = array($_FILES['userfile']['tmp_name']);
118  }
119 
120  foreach ($userfiles as $key => $userfile) {
121  if (empty($_FILES['userfile']['tmp_name'][$key])) {
122  $error++;
123  if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2) {
124  setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
125  } else {
126  setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("File")), null, 'errors');
127  }
128  }
129  }
130 
131  if (!$error) {
132  $generatethumbs = 0;
133  $res = dol_add_file_process($upload_dir, $overwritefile, 1, 'userfile', '', null, '', $generatethumbs);
134  if ($res > 0) {
135  $result = $ecmdir->changeNbOfFiles('+');
136  }
137  }
138 }
139 
140 // Remove file (code similar but different than actions_linkedfiles.inc.php)
141 if ($action == 'confirm_deletefile' && $permissiontodelete) {
142  if (GETPOST('confirm') == 'yes') {
143  // GETPOST('urlfile','alpha') is full relative URL from ecm root dir. Contains path of all sections.
144 
145  $upload_dir = $conf->ecm->dir_output.($relativepath ? '/'.$relativepath : '');
146  $file = $upload_dir."/".GETPOST('urlfile', 'alpha');
147  $ret = dol_delete_file($file); // This include also the delete from file index in database.
148  if ($ret) {
149  $urlfiletoshow = GETPOST('urlfile', 'alpha');
150  $urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfiletoshow);
151  setEventMessages($langs->trans("FileWasRemoved", $urlfiletoshow), null, 'mesgs');
152  $result = $ecmdir->changeNbOfFiles('-');
153  } else {
154  setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
155  }
156 
157  clearstatcache();
158  }
159  $action = 'file_manager';
160 }
161 
162 // Add directory
163 if ($action == 'add' && $permissiontocreatedir) {
164  $ecmdir->ref = 'NOTUSEDYET';
165  $ecmdir->label = GETPOST("label");
166  $ecmdir->description = GETPOST("desc");
167 
168  $id = $ecmdir->create($user);
169  if ($id > 0) {
170  header("Location: ".$_SERVER["PHP_SELF"]);
171  exit;
172  } else {
173  setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
174  $action = "create";
175  }
176 
177  clearstatcache();
178 }
179 
180 // Remove directory
181 if ($action == 'confirm_deletesection' && GETPOST('confirm', 'alpha') == 'yes' && $permissiontodeletedir) {
182  $result = $ecmdir->delete($user);
183  setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
184 
185  clearstatcache();
186 }
187 
188 // Refresh directory view
189 // This refresh list of dirs, not list of files (for preformance reason). List of files is refresh only if dir was not synchronized.
190 // To refresh content of dir with cache, just open the dir in edit mode.
191 if ($action == 'refreshmanual' && $permissiontoread) {
192  $ecmdirtmp = new EcmDirectory($db);
193 
194  // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
195  clearstatcache();
196 
197  $diroutputslash = str_replace('\\', '/', $conf->ecm->dir_output);
198  $diroutputslash .= '/';
199 
200  // Scan directory tree on disk
201  $disktree = dol_dir_list($conf->ecm->dir_output, 'directories', 1, '', '^temp$', '', '', 0);
202 
203  // Scan directory tree in database
204  $sqltree = $ecmdirstatic->get_full_arbo(0);
205 
206  $adirwascreated = 0;
207 
208  // Now we compare both trees to complete missing trees into database
209  //var_dump($disktree);
210  //var_dump($sqltree);
211  foreach ($disktree as $dirdesc) { // Loop on tree onto disk
212  $dirisindatabase = 0;
213  foreach ($sqltree as $dirsqldesc) {
214  if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
215  $dirisindatabase = 1;
216  break;
217  }
218  }
219 
220  if (!$dirisindatabase) {
221  $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
222  dol_syslog($txt);
223  //print $txt."<br>\n";
224 
225  // We must first find the fk_parent of directory to create $dirdesc['fullname']
226  $fk_parent = -1;
227  $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
228  $relativepathtosearchparent = $relativepathmissing;
229  //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
230  if (preg_match('/\//', $relativepathtosearchparent)) {
231  //while (preg_match('/\//',$relativepathtosearchparent))
232  $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
233  $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
234  dol_syslog($txt);
235  //print $txt." -> ";
236  $parentdirisindatabase = 0;
237  foreach ($sqltree as $dirsqldesc) {
238  if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
239  $parentdirisindatabase = $dirsqldesc['id'];
240  break;
241  }
242  }
243  if ($parentdirisindatabase > 0) {
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  } else {
249  dol_syslog("No");
250  //print "No<br>\n";
251  }
252  } else {
253  dol_syslog("Parent is root");
254  $fk_parent = 0; // Parent is root
255  }
256 
257  if ($fk_parent >= 0) {
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  $newdirsql = array('id'=>$id,
269  'id_mere'=>$ecmdirtmp->fk_parent,
270  'label'=>$ecmdirtmp->label,
271  'description'=>$ecmdirtmp->description,
272  'fullrelativename'=>$relativepathmissing);
273  $sqltree[] = $newdirsql; // We complete fulltree for following loops
274  //var_dump($sqltree);
275  $adirwascreated = 1;
276  } else {
277  dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
278  }
279  } else {
280  $txt = "Parent of ".$dirdesc['fullname']." not found";
281  dol_syslog($txt);
282  //print $txt."<br>\n";
283  }
284  }
285  }
286 
287  // Loop now on each sql tree to check if dir exists
288  foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
289  $dirtotest = $conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
290  if (!dol_is_dir($dirtotest)) {
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 cache 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) {
304  $sqltree = null;
305  }
306 }
307 
308 
309 
310 /*
311  * View
312  */
313 
314 // Define height of file area (depends on $_SESSION["dol_screenheight"])
315 //print $_SESSION["dol_screenheight"];
316 $maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466) ? ($_SESSION["dol_screenheight"] - 136) : 660; // Also into index_auto.php file
317 
318 $moreheadcss = '';
319 $moreheadjs = '';
320 
321 //$morejs=array();
322 $morejs = array('includes/jquery/plugins/blockUI/jquery.blockUI.js', 'core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
323 if (empty($conf->global->MAIN_ECM_DISABLE_JS)) {
324  $morejs[] = "includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
325 }
326 
327 $moreheadjs .= '<script type="text/javascript">'."\n";
328 $moreheadjs .= 'var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
329 $moreheadjs .= '</script>'."\n";
330 
331 llxHeader($moreheadcss.$moreheadjs, $langs->trans("ECMArea"), '', '', '', '', $morejs, '', 0, 0);
332 
333 $head = ecm_prepare_dasboard_head(null);
334 print dol_get_fiche_head($head, 'index', '', -1, '');
335 
336 
337 // Add filemanager component
338 $module = 'ecm';
339 if (empty($url)) {
340  $url = DOL_URL_ROOT.'/ecm/index.php'; // Must be an url without param
341 }
342 include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
343 
344 // End of page
345 print dol_get_fiche_end();
346 
347 llxFooter();
348 
349 $db->close();
Class to manage ECM directories.
Class to manage generation of HTML components Only common components must be here.
Class to manage Dolibarr users.
Definition: user.class.php:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
Definition: files.lib.php:37
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:1334
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:62
dol_is_dir($folder)
Test if filename is a directory.
Definition: files.lib.php:453
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:1737
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
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.
restrictedArea(User $user, $features, $object=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.