dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 *
19 * You can call this page with param module=medias to get a filemanager for medias.
20 */
21
28// Load Dolibarr environment
29require '../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
35
36// Load translation files required by the page
37$langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts'));
38
39// Get parameters
40$socid = GETPOSTINT('socid');
41$action = GETPOST('action', 'aZ09');
42$section = GETPOSTINT('section') ? GETPOSTINT('section') : GETPOSTINT('section_id');
43if (!$section) {
44 $section = 0;
45}
46$section_dir = GETPOST('section_dir', 'alpha');
47$overwritefile = GETPOSTINT('overwritefile');
48
49$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
50$sortfield = GETPOST('sortfield', 'aZ09comma');
51$sortorder = GETPOST('sortorder', 'aZ09comma');
52$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
53if (empty($page) || $page == -1) {
54 $page = 0;
55} // If $page is not defined, or '' or -1
56$offset = $limit * $page;
57$pageprev = $page - 1;
58$pagenext = $page + 1;
59if (!$sortorder) {
60 $sortorder = "ASC";
61}
62if (!$sortfield) {
63 $sortfield = "name";
64}
65
66$ecmdir = new EcmDirectory($db);
67if ($section > 0) {
68 $result = $ecmdir->fetch($section);
69 if (!($result > 0)) {
70 dol_print_error($db, $ecmdir->error);
71 exit;
72 }
73}
74
75$form = new Form($db);
76$ecmdirstatic = new EcmDirectory($db);
77$userstatic = new User($db);
78
79$error = 0;
80
81// Security check
82if ($user->socid) {
83 $socid = $user->socid;
84}
85// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
86$hookmanager->initHooks(array('ecmindexcard', 'globalcard'));
87
88$result = restrictedArea($user, 'ecm', 0);
89
90$permissiontoread = $user->hasRight('ecm', 'read');
91$permissiontocreate = $user->hasRight('ecm', 'upload');
92$permissiontocreatedir = $user->hasRight('ecm', 'setup');
93$permissiontodelete = $user->hasRight('ecm', 'upload');
94$permissiontodeletedir = $user->hasRight('ecm', 'setup');
95
96/*
97 * Actions
98 */
99
100// TODO Replace sendit and confirm_deletefile with
101//$backtopage=$_SERVER["PHP_SELF"].'?file_manager=1&website='.$websitekey.'&pageid='.$pageid; // used after a confirm_deletefile into actions_linkedfiles.inc.php
102//include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
103
104// Upload file (code similar but different than actions_linkedfiles.inc.php)
105if (GETPOST("sendit", 'alphanohtml') && getDolGlobalString('MAIN_UPLOAD_DOC') && $permissiontocreate) {
106 // Define relativepath and upload_dir
107 $relativepath = '';
108 if ($ecmdir->id) {
109 $relativepath = $ecmdir->getRelativePath();
110 } else {
111 $relativepath = $section_dir;
112 }
113 $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
114
115 $userfiles = [];
116 if (is_array($_FILES['userfile'])) {
117 if (is_array($_FILES['userfile']['tmp_name'])) {
118 $userfiles = $_FILES['userfile']['tmp_name'];
119 } else {
120 $userfiles = array($_FILES['userfile']['tmp_name']);
121 }
122 }
123
124 foreach ($userfiles as $key => $userfile) {
125 if (empty($_FILES['userfile']['tmp_name'][$key])) {
126 $error++;
127 if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2) {
128 setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
129 } else {
130 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("File")), null, 'errors');
131 }
132 }
133 }
134
135 if (!$error) {
136 $generatethumbs = 0;
137 $res = dol_add_file_process($upload_dir, $overwritefile, 1, 'userfile', '', null, '', $generatethumbs);
138 if ($res > 0) {
139 $result = $ecmdir->changeNbOfFiles('+');
140 }
141 }
142}
143
144// Remove file (code similar but different than actions_linkedfiles.inc.php)
145if ($action == 'confirm_deletefile' && $permissiontodelete) {
146 if (GETPOST('confirm') == 'yes') {
147 // GETPOST('urlfile','alpha') is full relative URL from ecm root dir. Contains path of all sections.
148
149 $upload_dir = $conf->ecm->dir_output.($relativepath ? '/'.$relativepath : '');
150 $file = $upload_dir."/".GETPOST('urlfile', 'alpha');
151 $ret = dol_delete_file($file); // This include also the delete from file index in database.
152 if ($ret) {
153 $urlfiletoshow = GETPOST('urlfile', 'alpha');
154 $urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfiletoshow);
155 setEventMessages($langs->trans("FileWasRemoved", $urlfiletoshow), null, 'mesgs');
156 $result = $ecmdir->changeNbOfFiles('-');
157 } else {
158 setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
159 }
160
161 clearstatcache();
162 }
163 $action = 'file_manager';
164}
165
166// Add directory
167if ($action == 'add' && $permissiontocreatedir) {
168 $ecmdir->ref = 'NOTUSEDYET';
169 $ecmdir->label = GETPOST("label");
170 $ecmdir->description = GETPOST("desc");
171
172 $id = $ecmdir->create($user);
173 if ($id > 0) {
174 header("Location: ".$_SERVER["PHP_SELF"]);
175 exit;
176 } else {
177 setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
178 $action = "create";
179 }
180
181 clearstatcache();
182}
183
184// Remove directory
185if ($action == 'confirm_deletesection' && GETPOST('confirm', 'alpha') == 'yes' && $permissiontodeletedir) {
186 $result = $ecmdir->delete($user);
187 setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
188
189 clearstatcache();
190}
191
192// Refresh directory view
193// This refresh list of dirs, not list of files (for performance reason). List of files is refresh only if dir was not synchronized.
194// To refresh content of dir with cache, just open the dir in edit mode.
195if ($action == 'refreshmanual' && $permissiontoread) {
196 $ecmdirtmp = new EcmDirectory($db);
197
198 // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
199 clearstatcache();
200
201 $diroutputslash = str_replace('\\', '/', $conf->ecm->dir_output);
202 $diroutputslash .= '/';
203
204 // Scan directory tree on disk
205 $disktree = dol_dir_list($conf->ecm->dir_output, 'directories', 1, '', '^temp$', '', 0, 0);
206
207 // Scan directory tree in database
208 $sqltree = $ecmdirstatic->get_full_arbo(0);
209
210 $adirwascreated = 0;
211
212 // Now we compare both trees to complete missing trees into database
213 //var_dump($disktree);
214 //var_dump($sqltree);
215 foreach ($disktree as $dirdesc) { // Loop on tree onto disk
216 $dirisindatabase = 0;
217 foreach ($sqltree as $dirsqldesc) {
218 if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
219 $dirisindatabase = 1;
220 break;
221 }
222 }
223
224 if (!$dirisindatabase) {
225 $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
226 dol_syslog($txt);
227 //print $txt."<br>\n";
228
229 // We must first find the fk_parent of directory to create $dirdesc['fullname']
230 $fk_parent = -1;
231 $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
232 $relativepathtosearchparent = $relativepathmissing;
233 //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
234 if (preg_match('/\//', $relativepathtosearchparent)) {
235 //while (preg_match('/\//',$relativepathtosearchparent))
236 $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
237 $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
238 dol_syslog($txt);
239 //print $txt." -> ";
240 $parentdirisindatabase = 0;
241 foreach ($sqltree as $dirsqldesc) {
242 if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
243 $parentdirisindatabase = $dirsqldesc['id'];
244 break;
245 }
246 }
247 if ($parentdirisindatabase > 0) {
248 dol_syslog("Yes with id ".$parentdirisindatabase);
249 //print "Yes with id ".$parentdirisindatabase."<br>\n";
250 $fk_parent = $parentdirisindatabase;
251 //break; // We found parent, we can stop the while loop
252 } else {
253 dol_syslog("No");
254 //print "No<br>\n";
255 }
256 } else {
257 dol_syslog("Parent is root");
258 $fk_parent = 0; // Parent is root
259 }
260
261 if ($fk_parent >= 0) {
262 $ecmdirtmp->ref = 'NOTUSEDYET';
263 $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
264 $ecmdirtmp->description = '';
265 $ecmdirtmp->fk_parent = $fk_parent;
266
267 $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
268 dol_syslog($txt);
269 //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
270 $id = $ecmdirtmp->create($user);
271 if ($id > 0) {
272 $newdirsql = array('id'=>$id,
273 'id_mere'=>$ecmdirtmp->fk_parent,
274 'label'=>$ecmdirtmp->label,
275 'description'=>$ecmdirtmp->description,
276 'fullrelativename'=>$relativepathmissing);
277 $sqltree[] = $newdirsql; // We complete fulltree for following loops
278 //var_dump($sqltree);
279 $adirwascreated = 1;
280 } else {
281 dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
282 }
283 } else {
284 $txt = "Parent of ".$dirdesc['fullname']." not found";
285 dol_syslog($txt);
286 //print $txt."<br>\n";
287 }
288 }
289 }
290
291 // Loop now on each sql tree to check if dir exists
292 foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
293 $dirtotest = $conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
294 if (!dol_is_dir($dirtotest)) {
295 $ecmdirtmp->id = $dirdesc['id'];
296 $ecmdirtmp->delete($user, 'databaseonly');
297 //exit;
298 }
299 }
300
301 $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cache counting, we set to value -1 = "unknown"
302 dol_syslog("sql = ".$sql);
303 $db->query($sql);
304
305 // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
306 // it to be sure that fulltree array is not used without reloading it.
307 if ($adirwascreated) {
308 $sqltree = null;
309 }
310}
311
312
313
314/*
315 * View
316 */
317
318// Define height of file area (depends on $_SESSION["dol_screenheight"])
319//print $_SESSION["dol_screenheight"];
320$maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466) ? ($_SESSION["dol_screenheight"] - 136) : 660; // Also into index_auto.php file
321
322$moreheadcss = '';
323$moreheadjs = '';
324
325//$morejs=array();
326$morejs = array('includes/jquery/plugins/blockUI/jquery.blockUI.js', 'core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
327if (!getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
328 $morejs[] = "includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
329}
330
331$moreheadjs .= '<script type="text/javascript">'."\n";
332$moreheadjs .= 'var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
333$moreheadjs .= '</script>'."\n";
334
335llxHeader($moreheadcss.$moreheadjs, $langs->trans("ECMArea"), '', '', 0, 0, $morejs, '', '', 'mod-ecm page-index');
336
337$head = ecm_prepare_dasboard_head();
338print dol_get_fiche_head($head, 'index', '', -1, '');
339
340
341// Add filemanager component
342$module = 'ecm';
343if (empty($url)) {
344 $url = DOL_URL_ROOT.'/ecm/index.php'; // Must be an url without param
345}
346include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
347
348// End of page
349print dol_get_fiche_end();
350
351llxFooter();
352
353$db->close();
$id
Definition account.php:39
Class to manage ECM directories.
Class to manage generation of HTML components Only common components must be here.
Class to manage Dolibarr users.
dol_basename($pathfile)
Make a basename working with all page code (default PHP basenamed fails with cyrillic).
Definition files.lib.php:38
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.
dol_add_file_process($upload_dir, $allowoverwrite=0, $updatesessionordb=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).
dol_dir_list($utf8_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:63
dol_is_dir($folder)
Test if filename is a directory.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.