dolibarr 21.0.0-beta
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
44// Load translation files required by the page
45$langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts'));
46
47// Get parameters
48$socid = GETPOSTINT('socid');
49$action = GETPOST('action', 'aZ09');
50$section = GETPOSTINT('section') ? GETPOSTINT('section') : GETPOSTINT('section_id');
51if (!$section) {
52 $section = 0;
53}
54$section_dir = GETPOST('section_dir', 'alpha');
55$overwritefile = GETPOSTINT('overwritefile');
56
57$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
58$sortfield = GETPOST('sortfield', 'aZ09comma');
59$sortorder = GETPOST('sortorder', 'aZ09comma');
60$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
61if (empty($page) || $page == -1) {
62 $page = 0;
63} // If $page is not defined, or '' or -1
64$offset = $limit * $page;
65$pageprev = $page - 1;
66$pagenext = $page + 1;
67if (!$sortorder) {
68 $sortorder = "ASC";
69}
70if (!$sortfield) {
71 $sortfield = "name";
72}
73
74$ecmdir = new EcmDirectory($db);
75if ($section > 0) {
76 $result = $ecmdir->fetch($section);
77 if (!($result > 0)) {
78 dol_print_error($db, $ecmdir->error);
79 exit;
80 }
81}
82
83$form = new Form($db);
84$ecmdirstatic = new EcmDirectory($db);
85$userstatic = new User($db);
86
87$error = 0;
88
89// Security check
90if ($user->socid) {
91 $socid = $user->socid;
92}
93// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
94$hookmanager->initHooks(array('ecmindexcard', 'globalcard'));
95
96$result = restrictedArea($user, 'ecm', 0);
97
98$permissiontoread = $user->hasRight('ecm', 'read');
99$permissiontocreate = $user->hasRight('ecm', 'upload');
100$permissiontocreatedir = $user->hasRight('ecm', 'setup');
101$permissiontodelete = $user->hasRight('ecm', 'upload');
102$permissiontodeletedir = $user->hasRight('ecm', 'setup');
103
104/*
105 * Actions
106 */
107
108// TODO Replace sendit and confirm_deletefile with
109//$backtopage=$_SERVER["PHP_SELF"].'?file_manager=1&website='.$websitekey.'&pageid='.$pageid; // used after a confirm_deletefile into actions_linkedfiles.inc.php
110//include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php';
111
112// Upload file (code similar but different than actions_linkedfiles.inc.php)
113if (GETPOST("sendit", 'alphanohtml') && getDolGlobalString('MAIN_UPLOAD_DOC') && $permissiontocreate) {
114 // Define relativepath and upload_dir
115 $relativepath = '';
116 if ($ecmdir->id) {
117 $relativepath = $ecmdir->getRelativePath();
118 } else {
119 $relativepath = $section_dir;
120 }
121 $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
122
123 $userfiles = [];
124 if (is_array($_FILES['userfile'])) {
125 if (is_array($_FILES['userfile']['tmp_name'])) {
126 $userfiles = $_FILES['userfile']['tmp_name'];
127 } else {
128 $userfiles = array($_FILES['userfile']['tmp_name']);
129 }
130 }
131
132 foreach ($userfiles as $key => $userfile) {
133 if (empty($_FILES['userfile']['tmp_name'][$key])) {
134 $error++;
135 if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2) {
136 setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
137 } else {
138 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("File")), null, 'errors');
139 }
140 }
141 }
142
143 if (!$error) {
144 $generatethumbs = 0;
145 $res = dol_add_file_process($upload_dir, $overwritefile, 1, 'userfile', '', null, '', $generatethumbs);
146 if ($res > 0) {
147 $result = $ecmdir->changeNbOfFiles('+');
148 }
149 }
150}
151
152// Remove file (code similar but different than actions_linkedfiles.inc.php)
153if ($action == 'confirm_deletefile' && $permissiontodelete) {
154 if (GETPOST('confirm') == 'yes') {
155 // GETPOST('urlfile','alpha') is full relative URL from ecm root dir. Contains path of all sections.
156
157 $upload_dir = $conf->ecm->dir_output.($relativepath ? '/'.$relativepath : '');
158 $file = $upload_dir."/".GETPOST('urlfile', 'alpha');
159 $ret = dol_delete_file($file); // This include also the delete from file index in database.
160 if ($ret) {
161 $urlfiletoshow = GETPOST('urlfile', 'alpha');
162 $urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfiletoshow);
163 setEventMessages($langs->trans("FileWasRemoved", $urlfiletoshow), null, 'mesgs');
164 $result = $ecmdir->changeNbOfFiles('-');
165 } else {
166 setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
167 }
168
169 clearstatcache();
170 }
171 $action = 'file_manager';
172}
173
174// Add directory
175if ($action == 'add' && $permissiontocreatedir) {
176 $ecmdir->ref = 'NOTUSEDYET';
177 $ecmdir->label = GETPOST("label");
178 $ecmdir->description = GETPOST("desc");
179
180 $id = $ecmdir->create($user);
181 if ($id > 0) {
182 header("Location: ".$_SERVER["PHP_SELF"]);
183 exit;
184 } else {
185 setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
186 $action = "create";
187 }
188
189 clearstatcache();
190}
191
192// Remove directory
193if ($action == 'confirm_deletesection' && GETPOST('confirm', 'alpha') == 'yes' && $permissiontodeletedir) {
194 $result = $ecmdir->delete($user);
195 setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
196
197 clearstatcache();
198}
199
200// Refresh directory view
201// This refresh list of dirs, not list of files (for performance reason). List of files is refresh only if dir was not synchronized.
202// To refresh content of dir with cache, just open the dir in edit mode.
203if ($action == 'refreshmanual' && $permissiontoread) {
204 $ecmdirtmp = new EcmDirectory($db);
205
206 // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
207 clearstatcache();
208
209 $diroutputslash = str_replace('\\', '/', $conf->ecm->dir_output);
210 $diroutputslash .= '/';
211
212 // Scan directory tree on disk
213 $disktree = dol_dir_list($conf->ecm->dir_output, 'directories', 1, '', '^temp$', '', 0, 0);
214
215 // Scan directory tree in database
216 $sqltree = $ecmdirstatic->get_full_arbo(0);
217
218 $adirwascreated = 0;
219
220 // Now we compare both trees to complete missing trees into database
221 //var_dump($disktree);
222 //var_dump($sqltree);
223 foreach ($disktree as $dirdesc) { // Loop on tree onto disk
224 $dirisindatabase = 0;
225 foreach ($sqltree as $dirsqldesc) {
226 if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
227 $dirisindatabase = 1;
228 break;
229 }
230 }
231
232 if (!$dirisindatabase) {
233 $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
234 dol_syslog($txt);
235 //print $txt."<br>\n";
236
237 // We must first find the fk_parent of directory to create $dirdesc['fullname']
238 $fk_parent = -1;
239 $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
240 $relativepathtosearchparent = $relativepathmissing;
241 //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
242 if (preg_match('/\//', $relativepathtosearchparent)) {
243 //while (preg_match('/\//',$relativepathtosearchparent))
244 $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
245 $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
246 dol_syslog($txt);
247 //print $txt." -> ";
248 $parentdirisindatabase = 0;
249 foreach ($sqltree as $dirsqldesc) {
250 if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
251 $parentdirisindatabase = $dirsqldesc['id'];
252 break;
253 }
254 }
255 if ($parentdirisindatabase > 0) {
256 dol_syslog("Yes with id ".$parentdirisindatabase);
257 //print "Yes with id ".$parentdirisindatabase."<br>\n";
258 $fk_parent = $parentdirisindatabase;
259 //break; // We found parent, we can stop the while loop
260 } else {
261 dol_syslog("No");
262 //print "No<br>\n";
263 }
264 } else {
265 dol_syslog("Parent is root");
266 $fk_parent = 0; // Parent is root
267 }
268
269 if ($fk_parent >= 0) {
270 $ecmdirtmp->ref = 'NOTUSEDYET';
271 $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
272 $ecmdirtmp->description = '';
273 $ecmdirtmp->fk_parent = $fk_parent;
274
275 $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
276 dol_syslog($txt);
277 //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
278 $id = $ecmdirtmp->create($user);
279 if ($id > 0) {
280 $newdirsql = array('id'=>$id,
281 'id_mere'=>$ecmdirtmp->fk_parent,
282 'label'=>$ecmdirtmp->label,
283 'description'=>$ecmdirtmp->description,
284 'fullrelativename'=>$relativepathmissing);
285 $sqltree[] = $newdirsql; // We complete fulltree for following loops
286 //var_dump($sqltree);
287 $adirwascreated = 1;
288 } else {
289 dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
290 }
291 } else {
292 $txt = "Parent of ".$dirdesc['fullname']." not found";
293 dol_syslog($txt);
294 //print $txt."<br>\n";
295 }
296 }
297 }
298
299 // Loop now on each sql tree to check if dir exists
300 foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
301 $dirtotest = $conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
302 if (!dol_is_dir($dirtotest)) {
303 $ecmdirtmp->id = $dirdesc['id'];
304 $ecmdirtmp->delete($user, 'databaseonly');
305 //exit;
306 }
307 }
308
309 $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cache counting, we set to value -1 = "unknown"
310 dol_syslog("sql = ".$sql);
311 $db->query($sql);
312
313 // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
314 // it to be sure that fulltree array is not used without reloading it.
315 if ($adirwascreated) {
316 $sqltree = null;
317 }
318}
319
320
321
322/*
323 * View
324 */
325
326// Define height of file area (depends on $_SESSION["dol_screenheight"])
327//print $_SESSION["dol_screenheight"];
328$maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466) ? ($_SESSION["dol_screenheight"] - 136) : 660; // Also into index_auto.php file
329
330$moreheadcss = '';
331$moreheadjs = '';
332
333//$morejs=array();
334$morejs = array('includes/jquery/plugins/blockUI/jquery.blockUI.js', 'core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
335if (!getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
336 $morejs[] = "includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
337}
338
339$moreheadjs .= '<script type="text/javascript">'."\n";
340$moreheadjs .= 'var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
341$moreheadjs .= '</script>'."\n";
342
343llxHeader($moreheadcss.$moreheadjs, $langs->trans("ECMArea"), '', '', 0, 0, $morejs, '', '', 'mod-ecm page-index');
344
345$head = ecm_prepare_dasboard_head();
346print dol_get_fiche_head($head, 'index', '', -1, '');
347
348
349// Add filemanager component
350$module = 'ecm';
351if (empty($url)) {
352 $url = DOL_URL_ROOT.'/ecm/index.php'; // Must be an url without param
353}
354include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
355
356// End of page
357print dol_get_fiche_end();
358
359llxFooter();
360
361$db->close();
$id
Definition account.php:48
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, $forceFullTestIndexation='')
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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.