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