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