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