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