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