dolibarr 20.0.0
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
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
33require_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 = GETPOSTINT('socid');
40$action = GETPOST('action', 'aZ09');
41$section = GETPOSTINT('section') ? GETPOSTINT('section') : GETPOSTINT('section_id');
42if (!$section) {
43 $section = 0;
44}
45$section_dir = GETPOST('section_dir', 'alpha');
46$overwritefile = GETPOSTINT('overwritefile');
47
48$limit = GETPOSTINT('limit') ? GETPOSTINT('limit') : $conf->liste_limit;
49$sortfield = GETPOST('sortfield', 'aZ09comma');
50$sortorder = GETPOST('sortorder', 'aZ09comma');
51$page = GETPOSTISSET('pageplusone') ? (GETPOSTINT('pageplusone') - 1) : GETPOSTINT("page");
52if (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;
58if (!$sortorder) {
59 $sortorder = "ASC";
60}
61if (!$sortfield) {
62 $sortfield = "name";
63}
64
65$ecmdir = new EcmDirectory($db);
66if ($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
81if ($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)
104if (GETPOST("sendit", 'alphanohtml') && getDolGlobalString('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 $userfiles = [];
115 if (is_array($_FILES['userfile'])) {
116 if (is_array($_FILES['userfile']['tmp_name'])) {
117 $userfiles = $_FILES['userfile']['tmp_name'];
118 } else {
119 $userfiles = array($_FILES['userfile']['tmp_name']);
120 }
121 }
122
123 foreach ($userfiles as $key => $userfile) {
124 if (empty($_FILES['userfile']['tmp_name'][$key])) {
125 $error++;
126 if ($_FILES['userfile']['error'][$key] == 1 || $_FILES['userfile']['error'][$key] == 2) {
127 setEventMessages($langs->trans('ErrorFileSizeTooLarge'), null, 'errors');
128 } else {
129 setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("File")), null, 'errors');
130 }
131 }
132 }
133
134 if (!$error) {
135 $generatethumbs = 0;
136 $res = dol_add_file_process($upload_dir, $overwritefile, 1, 'userfile', '', null, '', $generatethumbs);
137 if ($res > 0) {
138 $result = $ecmdir->changeNbOfFiles('+');
139 }
140 }
141}
142
143// Remove file (code similar but different than actions_linkedfiles.inc.php)
144if ($action == 'confirm_deletefile' && $permissiontodelete) {
145 if (GETPOST('confirm') == 'yes') {
146 // GETPOST('urlfile','alpha') is full relative URL from ecm root dir. Contains path of all sections.
147
148 $upload_dir = $conf->ecm->dir_output.($relativepath ? '/'.$relativepath : '');
149 $file = $upload_dir."/".GETPOST('urlfile', 'alpha');
150 $ret = dol_delete_file($file); // This include also the delete from file index in database.
151 if ($ret) {
152 $urlfiletoshow = GETPOST('urlfile', 'alpha');
153 $urlfiletoshow = preg_replace('/\.noexe$/', '', $urlfiletoshow);
154 setEventMessages($langs->trans("FileWasRemoved", $urlfiletoshow), null, 'mesgs');
155 $result = $ecmdir->changeNbOfFiles('-');
156 } else {
157 setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile', 'alpha')), null, 'errors');
158 }
159
160 clearstatcache();
161 }
162 $action = 'file_manager';
163}
164
165// Add directory
166if ($action == 'add' && $permissiontocreatedir) {
167 $ecmdir->ref = 'NOTUSEDYET';
168 $ecmdir->label = GETPOST("label");
169 $ecmdir->description = GETPOST("desc");
170
171 $id = $ecmdir->create($user);
172 if ($id > 0) {
173 header("Location: ".$_SERVER["PHP_SELF"]);
174 exit;
175 } else {
176 setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
177 $action = "create";
178 }
179
180 clearstatcache();
181}
182
183// Remove directory
184if ($action == 'confirm_deletesection' && GETPOST('confirm', 'alpha') == 'yes' && $permissiontodeletedir) {
185 $result = $ecmdir->delete($user);
186 setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
187
188 clearstatcache();
189}
190
191// Refresh directory view
192// This refresh list of dirs, not list of files (for performance reason). List of files is refresh only if dir was not synchronized.
193// To refresh content of dir with cache, just open the dir in edit mode.
194if ($action == 'refreshmanual' && $permissiontoread) {
195 $ecmdirtmp = new EcmDirectory($db);
196
197 // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
198 clearstatcache();
199
200 $diroutputslash = str_replace('\\', '/', $conf->ecm->dir_output);
201 $diroutputslash .= '/';
202
203 // Scan directory tree on disk
204 $disktree = dol_dir_list($conf->ecm->dir_output, 'directories', 1, '', '^temp$', '', '', 0);
205
206 // Scan directory tree in database
207 $sqltree = $ecmdirstatic->get_full_arbo(0);
208
209 $adirwascreated = 0;
210
211 // Now we compare both trees to complete missing trees into database
212 //var_dump($disktree);
213 //var_dump($sqltree);
214 foreach ($disktree as $dirdesc) { // Loop on tree onto disk
215 $dirisindatabase = 0;
216 foreach ($sqltree as $dirsqldesc) {
217 if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
218 $dirisindatabase = 1;
219 break;
220 }
221 }
222
223 if (!$dirisindatabase) {
224 $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
225 dol_syslog($txt);
226 //print $txt."<br>\n";
227
228 // We must first find the fk_parent of directory to create $dirdesc['fullname']
229 $fk_parent = -1;
230 $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
231 $relativepathtosearchparent = $relativepathmissing;
232 //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
233 if (preg_match('/\//', $relativepathtosearchparent)) {
234 //while (preg_match('/\//',$relativepathtosearchparent))
235 $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
236 $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
237 dol_syslog($txt);
238 //print $txt." -> ";
239 $parentdirisindatabase = 0;
240 foreach ($sqltree as $dirsqldesc) {
241 if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
242 $parentdirisindatabase = $dirsqldesc['id'];
243 break;
244 }
245 }
246 if ($parentdirisindatabase > 0) {
247 dol_syslog("Yes with id ".$parentdirisindatabase);
248 //print "Yes with id ".$parentdirisindatabase."<br>\n";
249 $fk_parent = $parentdirisindatabase;
250 //break; // We found parent, we can stop the while loop
251 } else {
252 dol_syslog("No");
253 //print "No<br>\n";
254 }
255 } else {
256 dol_syslog("Parent is root");
257 $fk_parent = 0; // Parent is root
258 }
259
260 if ($fk_parent >= 0) {
261 $ecmdirtmp->ref = 'NOTUSEDYET';
262 $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
263 $ecmdirtmp->description = '';
264 $ecmdirtmp->fk_parent = $fk_parent;
265
266 $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
267 dol_syslog($txt);
268 //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
269 $id = $ecmdirtmp->create($user);
270 if ($id > 0) {
271 $newdirsql = array('id'=>$id,
272 'id_mere'=>$ecmdirtmp->fk_parent,
273 'label'=>$ecmdirtmp->label,
274 'description'=>$ecmdirtmp->description,
275 'fullrelativename'=>$relativepathmissing);
276 $sqltree[] = $newdirsql; // We complete fulltree for following loops
277 //var_dump($sqltree);
278 $adirwascreated = 1;
279 } else {
280 dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
281 }
282 } else {
283 $txt = "Parent of ".$dirdesc['fullname']." not found";
284 dol_syslog($txt);
285 //print $txt."<br>\n";
286 }
287 }
288 }
289
290 // Loop now on each sql tree to check if dir exists
291 foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
292 $dirtotest = $conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
293 if (!dol_is_dir($dirtotest)) {
294 $ecmdirtmp->id = $dirdesc['id'];
295 $ecmdirtmp->delete($user, 'databaseonly');
296 //exit;
297 }
298 }
299
300 $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cache counting, we set to value -1 = "unknown"
301 dol_syslog("sql = ".$sql);
302 $db->query($sql);
303
304 // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
305 // it to be sure that fulltree array is not used without reloading it.
306 if ($adirwascreated) {
307 $sqltree = null;
308 }
309}
310
311
312
313/*
314 * View
315 */
316
317// Define height of file area (depends on $_SESSION["dol_screenheight"])
318//print $_SESSION["dol_screenheight"];
319$maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466) ? ($_SESSION["dol_screenheight"] - 136) : 660; // Also into index_auto.php file
320
321$moreheadcss = '';
322$moreheadjs = '';
323
324//$morejs=array();
325$morejs = array('includes/jquery/plugins/blockUI/jquery.blockUI.js', 'core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
326if (!getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
327 $morejs[] = "includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
328}
329
330$moreheadjs .= '<script type="text/javascript">'."\n";
331$moreheadjs .= 'var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
332$moreheadjs .= '</script>'."\n";
333
334llxHeader($moreheadcss.$moreheadjs, $langs->trans("ECMArea"), '', '', '', '', $morejs, '', 0, 0);
335
336$head = ecm_prepare_dasboard_head(null);
337print dol_get_fiche_head($head, 'index', '', -1, '');
338
339
340// Add filemanager component
341$module = 'ecm';
342if (empty($url)) {
343 $url = DOL_URL_ROOT.'/ecm/index.php'; // Must be an url without param
344}
345include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
346
347// End of page
348print dol_get_fiche_end();
349
350llxFooter();
351
352$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.
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.
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
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 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.