dolibarr 19.0.3
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * You can call this page with param module=medias to get a filemanager for medias.
19 */
20
27// Load Dolibarr environment
28require '../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
34
35// Load translation files required by the page
36$langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts'));
37
38// Get parameters
39$action = GETPOST('action', 'aZ09');
40$confirm = GETPOST('confirm', 'alpha');
41$backtopage = GETPOST('backtopage', 'alpha');
42
43$socid = GETPOST('socid', 'int');
44$file_manager = GETPOST('file_manager', 'alpha');
45$section = GETPOST('section', 'int') ? GETPOST('section', 'int') : GETPOST('section_id', 'int');
46if (!$section) {
47 $section = 0;
48}
49$section_dir = GETPOST('section_dir', 'alpha');
50$overwritefile = GETPOST('overwritefile', 'int');
51
52if (empty($action) && $file_manager) {
53 $action = 'file_manager';
54}
55$pageid = GETPOST('pageid', 'int');
56
57$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
58$sortfield = GETPOST('sortfield', 'aZ09comma');
59$sortorder = GETPOST('sortorder', 'aZ09comma');
60$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
61if (empty($page) || $page == -1) {
62 $page = 0;
63} // If $page is not defined, or '' or -1
64$offset = $limit * $page;
65$pageprev = $page - 1;
66$pagenext = $page + 1;
67if (!$sortorder) {
68 $sortorder = "ASC";
69}
70if (!$sortfield) {
71 $sortfield = "name";
72}
73
74
75$ecmdir = new EcmDirectory($db);
76if ($section > 0) {
77 $result = $ecmdir->fetch($section);
78 if (!($result > 0)) {
79 dol_print_error($db, $ecmdir->error);
80 exit;
81 }
82}
83
84$form = new Form($db);
85$ecmdirstatic = new EcmDirectory($db);
86$userstatic = new User($db);
87
88$error = 0;
89
90// Security check
91if ($user->socid) {
92 $socid = $user->socid;
93}
94$result = restrictedArea($user, 'ecm', 0);
95
96$permissiontouploadfile = ($user->hasRight('ecm', 'setup') || $user->hasRight('mailing', 'creer') || $user->hasRight('website', 'write'));
97$diroutput = $conf->medias->multidir_output[$conf->entity];
98
99$relativepath = $section_dir;
100$upload_dir = preg_replace('/\/$/', '', $diroutput).'/'.preg_replace('/^\//', '', $relativepath);
101
102$websitekey = '';
103
104$permissiontoadd = $permissiontouploadfile; // Used by the include of actions_addupdatedelete.inc.php and actions_linkedfiles
105
106// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
107$hookmanager->initHooks(array('ecmmediascard', 'globalcard'));
108
109/*
110 * Actions
111 */
112
113$savbacktopage = $backtopage;
114$backtopage = $_SERVER["PHP_SELF"].'?file_manager=1&website='.urlencode($websitekey).'&pageid='.urlencode($pageid).(GETPOST('section_dir', 'alpha') ? '&section_dir='.urlencode(GETPOST('section_dir', 'alpha')) : ''); // used after a confirm_deletefile into actions_linkedfiles.inc.php
115if ($sortfield) {
116 $backtopage .= '&sortfield='.urlencode($sortfield);
117}
118if ($sortorder) {
119 $backtopage .= '&sortorder='.urlencode($sortorder);
120}
121include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php'; // This manage 'sendit', 'confirm_deletefile', 'renamefile' action when submitting new file.
122
123$backtopage = $savbacktopage;
124
125if ($action == 'renamefile') { // Must be after include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php'; If action were renamefile, we set it to 'file_manager'
126 $action = 'file_manager';
127}
128
129
130// Add directory
131if ($action == 'add' && $permissiontouploadfile) {
132 $ecmdir->ref = 'NOTUSEDYET';
133 $ecmdir->label = GETPOST("label");
134 $ecmdir->description = GETPOST("desc");
135
136 $id = $ecmdir->create($user);
137 if ($id > 0) {
138 header("Location: ".$_SERVER["PHP_SELF"]);
139 exit;
140 } else {
141 setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
142 $action = "create";
143 }
144
145 clearstatcache();
146}
147
148// Remove directory
149if ($action == 'confirm_deletesection' && GETPOST('confirm', 'alpha') == 'yes') {
150 $result = $ecmdir->delete($user);
151 setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
152
153 clearstatcache();
154}
155
156// Refresh directory view
157// This refresh list of dirs, not list of files (for preformance reason). List of files is refresh only if dir was not synchronized.
158// To refresh content of dir with cache, just open the dir in edit mode.
159if ($action == 'refreshmanual') {
160 $ecmdirtmp = new EcmDirectory($db);
161
162 // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
163 clearstatcache();
164
165 $diroutputslash = str_replace('\\', '/', $conf->ecm->dir_output);
166 $diroutputslash .= '/';
167
168 // Scan directory tree on disk
169 $disktree = dol_dir_list($conf->ecm->dir_output, 'directories', 1, '', '^temp$', '', '', 0);
170
171 // Scan directory tree in database
172 $sqltree = $ecmdirstatic->get_full_arbo(0);
173
174 $adirwascreated = 0;
175
176 // Now we compare both trees to complete missing trees into database
177 //var_dump($disktree);
178 //var_dump($sqltree);
179 foreach ($disktree as $dirdesc) { // Loop on tree onto disk
180 $dirisindatabase = 0;
181 foreach ($sqltree as $dirsqldesc) {
182 if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
183 $dirisindatabase = 1;
184 break;
185 }
186 }
187
188 if (!$dirisindatabase) {
189 $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
190 dol_syslog($txt);
191 //print $txt."<br>\n";
192
193 // We must first find the fk_parent of directory to create $dirdesc['fullname']
194 $fk_parent = -1;
195 $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
196 $relativepathtosearchparent = $relativepathmissing;
197 //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
198 if (preg_match('/\//', $relativepathtosearchparent)) {
199 //while (preg_match('/\//',$relativepathtosearchparent))
200 $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
201 $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
202 dol_syslog($txt);
203 //print $txt." -> ";
204 $parentdirisindatabase = 0;
205 foreach ($sqltree as $dirsqldesc) {
206 if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
207 $parentdirisindatabase = $dirsqldesc['id'];
208 break;
209 }
210 }
211 if ($parentdirisindatabase > 0) {
212 dol_syslog("Yes with id ".$parentdirisindatabase);
213 //print "Yes with id ".$parentdirisindatabase."<br>\n";
214 $fk_parent = $parentdirisindatabase;
215 //break; // We found parent, we can stop the while loop
216 } else {
217 dol_syslog("No");
218 //print "No<br>\n";
219 }
220 } else {
221 dol_syslog("Parent is root");
222 $fk_parent = 0; // Parent is root
223 }
224
225 if ($fk_parent >= 0) {
226 $ecmdirtmp->ref = 'NOTUSEDYET';
227 $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
228 $ecmdirtmp->description = '';
229 $ecmdirtmp->fk_parent = $fk_parent;
230
231 $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
232 dol_syslog($txt);
233 //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
234 $id = $ecmdirtmp->create($user);
235 if ($id > 0) {
236 $newdirsql = array('id'=>$id,
237 'id_mere'=>$ecmdirtmp->fk_parent,
238 'label'=>$ecmdirtmp->label,
239 'description'=>$ecmdirtmp->description,
240 'fullrelativename'=>$relativepathmissing);
241 $sqltree[] = $newdirsql; // We complete fulltree for following loops
242 //var_dump($sqltree);
243 $adirwascreated = 1;
244 } else {
245 dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
246 }
247 } else {
248 $txt = "Parent of ".$dirdesc['fullname']." not found";
249 dol_syslog($txt);
250 //print $txt."<br>\n";
251 }
252 }
253 }
254
255 // Loop now on each sql tree to check if dir exists
256 foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
257 $dirtotest = $conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
258 if (!dol_is_dir($dirtotest)) {
259 $ecmdirtmp->id = $dirdesc['id'];
260 $ecmdirtmp->delete($user, 'databaseonly');
261 //exit;
262 }
263 }
264
265 $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cache counting, we set to value -1 = "unknown"
266 dol_syslog("sql = ".$sql);
267 $db->query($sql);
268
269 // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
270 // it to be sure that fulltree array is not used without reloading it.
271 if ($adirwascreated) {
272 $sqltree = null;
273 }
274}
275
276
277
278/*
279 * View
280 */
281
282// Define height of file area (depends on $_SESSION["dol_screenheight"])
283//print $_SESSION["dol_screenheight"];
284$maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466) ? ($_SESSION["dol_screenheight"] - 136) : 660; // Also into index_auto.php file
285
286$moreheadcss = '';
287$moreheadjs = '';
288
289//$morejs=array();
290$morejs = array('includes/jquery/plugins/blockUI/jquery.blockUI.js', 'core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
291if (!getDolGlobalString('MAIN_ECM_DISABLE_JS')) {
292 $morejs[] = "includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
293}
294
295$moreheadjs .= '<script type="text/javascript">'."\n";
296$moreheadjs .= 'var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
297$moreheadjs .= '</script>'."\n";
298
299llxHeader($moreheadcss.$moreheadjs, $langs->trans("ECMArea"), '', '', '', '', $morejs, '', 0, 0);
300
301$head = ecm_prepare_dasboard_head(null);
302print dol_get_fiche_head($head, 'index_medias', '', -1, '');
303
304
305// Add filemanager component
306$module = 'medias';
307if (empty($url)) {
308 $url = DOL_URL_ROOT.'/ecm/index_medias.php'; // Must be an url without param
309}
310include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
311
312// End of page
313print dol_get_fiche_end();
314
315llxFooter();
316
317$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:37
dol_dir_list($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:62
dol_is_dir($folder)
Test if filename is a directory.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.
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.