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