dolibarr 24.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-2025 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';
38require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
39require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
40require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
41require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
42require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
43
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 foreach ($disktree as $dirdesc) { // Loop on tree onto disk
192 $dirisindatabase = 0;
193 foreach ($sqltree as $dirsqldesc) {
194 if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
195 $dirisindatabase = 1;
196 break;
197 }
198 }
199
200 if (!$dirisindatabase) {
201 $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
202 dol_syslog($txt);
203
204 // We must first find the fk_parent of directory to create $dirdesc['fullname']
205 $fk_parent = -1;
206 $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
207 $relativepathtosearchparent = $relativepathmissing;
208 //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
209 if (preg_match('/\//', $relativepathtosearchparent)) {
210 //while (preg_match('/\//',$relativepathtosearchparent))
211 $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
212 $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
213 dol_syslog($txt);
214 //print $txt." -> ";
215 $parentdirisindatabase = 0;
216 foreach ($sqltree as $dirsqldesc) {
217 if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
218 $parentdirisindatabase = $dirsqldesc['id'];
219 break;
220 }
221 }
222 if ($parentdirisindatabase > 0) {
223 dol_syslog("Yes with id ".$parentdirisindatabase);
224 //print "Yes with id ".$parentdirisindatabase."<br>\n";
225 $fk_parent = $parentdirisindatabase;
226 //break; // We found parent, we can stop the while loop
227 } else {
228 dol_syslog("No");
229 //print "No<br>\n";
230 }
231 } else {
232 dol_syslog("Parent is root");
233 $fk_parent = 0; // Parent is root
234 }
235
236 if ($fk_parent >= 0) {
237 $ecmdirtmp->ref = 'NOTUSEDYET';
238 $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
239 $ecmdirtmp->description = '';
240 $ecmdirtmp->fk_parent = $fk_parent;
241
242 $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
243 dol_syslog($txt);
244 //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
245 $id = $ecmdirtmp->create($user);
246 if ($id > 0) {
247 $newdirsql = [
248 'id' => $id,
249 'id_mere' => $ecmdirtmp->fk_parent,
250 'label' => $ecmdirtmp->label,
251 'description' => $ecmdirtmp->description,
252 'fullrelativename' => $relativepathmissing,
253 ];
254 $sqltree[] = $newdirsql; // We complete fulltree for following loops
255 $adirwascreated = 1;
256 } else {
257 dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
258 }
259 } else {
260 $txt = "Parent of ".$dirdesc['fullname']." not found";
261 dol_syslog($txt);
262 }
263 }
264 }
265
266 // Loop now on each sql tree to check if dir exists
267 foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
268 $dirtotest = $conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
269 if (!dol_is_dir($dirtotest)) {
270 $ecmdirtmp->id = $dirdesc['id'];
271 $ecmdirtmp->delete($user, 'databaseonly');
272 //exit;
273 }
274 }
275
276 $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cache counting, we set to value -1 = "unknown"
277 dol_syslog("sql = ".$sql);
278 $db->query($sql);
279
280 // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
281 // it to be sure that fulltree array is not used without reloading it.
282 if ($adirwascreated) {
283 $sqltree = null;
284 }
285}
286
287
288
289/*
290 * View
291 */
292
293// Define height of file area (depends on $_SESSION["dol_screenheight"])
294//print $_SESSION["dol_screenheight"];
295$maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466) ? ($_SESSION["dol_screenheight"] - 136) : 660; // Also into index_auto.php file
296
297$moreheadcss = '';
298$moreheadjs = '';
299
300$morejs=array();
301if (!getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
302 $morejs[] = "public/includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
303}
304
305llxHeader($moreheadcss.$moreheadjs, $langs->trans("ECMArea"), '', '', 0, 0, $morejs, '', '', 'mod-ecm page-index_medias');
306
308print dol_get_fiche_head($head, 'index_medias', '', -1, '');
309
310
311// Add filemanager component
312$module = 'medias';
313if (empty($url)) {
314 $url = DOL_URL_ROOT.'/ecm/index_medias.php'; // Must be an url without param
315}
316include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
317
318// End of page
319print dol_get_fiche_end();
320
321llxFooter();
322
323$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.
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.