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