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