index_medias.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. /**
  21. * \file htdocs/ecm/index_medias.php
  22. * \ingroup ecm
  23. * \brief Main page for ECM section of public media directories area
  24. */
  25. // Load Dolibarr environment
  26. require '../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/ecm.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/treeview.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
  32. // Load translation files required by the page
  33. $langs->loadLangs(array('ecm', 'companies', 'other', 'users', 'orders', 'propal', 'bills', 'contracts'));
  34. // Get parameters
  35. $action = GETPOST('action', 'aZ09');
  36. $backtopage = GETPOST('backtopage', 'alpha');
  37. $socid = GETPOST('socid', 'int');
  38. $file_manager = GETPOST('file_manager', 'alpha');
  39. $section = GETPOST('section', 'int') ? GETPOST('section', 'int') : GETPOST('section_id', 'int');
  40. if (!$section) {
  41. $section = 0;
  42. }
  43. $section_dir = GETPOST('section_dir', 'alpha');
  44. $overwritefile = GETPOST('overwritefile', 'int');
  45. if (empty($action) && $file_manager) {
  46. $action = 'file_manager';
  47. }
  48. $pageid = GETPOST('pageid', 'int');
  49. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  50. $sortfield = GETPOST('sortfield', 'aZ09comma');
  51. $sortorder = GETPOST('sortorder', 'aZ09comma');
  52. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  53. if (empty($page) || $page == -1) {
  54. $page = 0;
  55. } // If $page is not defined, or '' or -1
  56. $offset = $limit * $page;
  57. $pageprev = $page - 1;
  58. $pagenext = $page + 1;
  59. if (!$sortorder) {
  60. $sortorder = "ASC";
  61. }
  62. if (!$sortfield) {
  63. $sortfield = "name";
  64. }
  65. $ecmdir = new EcmDirectory($db);
  66. if ($section > 0) {
  67. $result = $ecmdir->fetch($section);
  68. if (!($result > 0)) {
  69. dol_print_error($db, $ecmdir->error);
  70. exit;
  71. }
  72. }
  73. $form = new Form($db);
  74. $ecmdirstatic = new EcmDirectory($db);
  75. $userstatic = new User($db);
  76. $error = 0;
  77. // Security check
  78. if ($user->socid) {
  79. $socid = $user->socid;
  80. }
  81. $result = restrictedArea($user, 'ecm', 0);
  82. $permissiontouploadfile = ($user->hasRight('ecm', 'setup') || $user->hasRight('mailing', 'creer') || $user->hasRight('website', 'write'));
  83. $diroutput = $conf->medias->multidir_output[$conf->entity];
  84. $relativepath = $section_dir;
  85. $upload_dir = preg_replace('/\/$/', '', $diroutput).'/'.preg_replace('/^\//', '', $relativepath);
  86. $websitekey = '';
  87. $permissiontoadd = $permissiontouploadfile; // Used by the include of actions_addupdatedelete.inc.php and actions_linkedfiles
  88. /*
  89. * Actions
  90. */
  91. $savbacktopage = $backtopage;
  92. $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
  93. if ($sortfield) {
  94. $backtopage .= '&sortfield='.urlencode($sortfield);
  95. }
  96. if ($sortorder) {
  97. $backtopage .= '&sortorder='.urlencode($sortorder);
  98. }
  99. include DOL_DOCUMENT_ROOT.'/core/actions_linkedfiles.inc.php'; // This manage 'sendit', 'confirm_deletefile', 'renamefile' action when submitting new file.
  100. $backtopage = $savbacktopage;
  101. 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'
  102. $action = 'file_manager';
  103. }
  104. // Add directory
  105. if ($action == 'add' && $permissiontouploadfile) {
  106. $ecmdir->ref = 'NOTUSEDYET';
  107. $ecmdir->label = GETPOST("label");
  108. $ecmdir->description = GETPOST("desc");
  109. $id = $ecmdir->create($user);
  110. if ($id > 0) {
  111. header("Location: ".$_SERVER["PHP_SELF"]);
  112. exit;
  113. } else {
  114. setEventMessages('Error '.$langs->trans($ecmdir->error), null, 'errors');
  115. $action = "create";
  116. }
  117. clearstatcache();
  118. }
  119. // Remove directory
  120. if ($action == 'confirm_deletesection' && GETPOST('confirm', 'alpha') == 'yes') {
  121. $result = $ecmdir->delete($user);
  122. setEventMessages($langs->trans("ECMSectionWasRemoved", $ecmdir->label), null, 'mesgs');
  123. clearstatcache();
  124. }
  125. // Refresh directory view
  126. // This refresh list of dirs, not list of files (for preformance reason). List of files is refresh only if dir was not synchronized.
  127. // To refresh content of dir with cache, just open the dir in edit mode.
  128. if ($action == 'refreshmanual') {
  129. $ecmdirtmp = new EcmDirectory($db);
  130. // This part of code is same than into file ecm/ajax/ecmdatabase.php TODO Remove duplicate
  131. clearstatcache();
  132. $diroutputslash = str_replace('\\', '/', $conf->ecm->dir_output);
  133. $diroutputslash .= '/';
  134. // Scan directory tree on disk
  135. $disktree = dol_dir_list($conf->ecm->dir_output, 'directories', 1, '', '^temp$', '', '', 0);
  136. // Scan directory tree in database
  137. $sqltree = $ecmdirstatic->get_full_arbo(0);
  138. $adirwascreated = 0;
  139. // Now we compare both trees to complete missing trees into database
  140. //var_dump($disktree);
  141. //var_dump($sqltree);
  142. foreach ($disktree as $dirdesc) { // Loop on tree onto disk
  143. $dirisindatabase = 0;
  144. foreach ($sqltree as $dirsqldesc) {
  145. if ($conf->ecm->dir_output.'/'.$dirsqldesc['fullrelativename'] == $dirdesc['fullname']) {
  146. $dirisindatabase = 1;
  147. break;
  148. }
  149. }
  150. if (!$dirisindatabase) {
  151. $txt = "Directory found on disk ".$dirdesc['fullname'].", not found into database so we add it";
  152. dol_syslog($txt);
  153. //print $txt."<br>\n";
  154. // We must first find the fk_parent of directory to create $dirdesc['fullname']
  155. $fk_parent = -1;
  156. $relativepathmissing = str_replace($diroutputslash, '', $dirdesc['fullname']);
  157. $relativepathtosearchparent = $relativepathmissing;
  158. //dol_syslog("Try to find parent id for directory ".$relativepathtosearchparent);
  159. if (preg_match('/\//', $relativepathtosearchparent)) {
  160. //while (preg_match('/\//',$relativepathtosearchparent))
  161. $relativepathtosearchparent = preg_replace('/\/[^\/]*$/', '', $relativepathtosearchparent);
  162. $txt = "Is relative parent path ".$relativepathtosearchparent." for ".$relativepathmissing." found in sql tree ?";
  163. dol_syslog($txt);
  164. //print $txt." -> ";
  165. $parentdirisindatabase = 0;
  166. foreach ($sqltree as $dirsqldesc) {
  167. if ($dirsqldesc['fullrelativename'] == $relativepathtosearchparent) {
  168. $parentdirisindatabase = $dirsqldesc['id'];
  169. break;
  170. }
  171. }
  172. if ($parentdirisindatabase > 0) {
  173. dol_syslog("Yes with id ".$parentdirisindatabase);
  174. //print "Yes with id ".$parentdirisindatabase."<br>\n";
  175. $fk_parent = $parentdirisindatabase;
  176. //break; // We found parent, we can stop the while loop
  177. } else {
  178. dol_syslog("No");
  179. //print "No<br>\n";
  180. }
  181. } else {
  182. dol_syslog("Parent is root");
  183. $fk_parent = 0; // Parent is root
  184. }
  185. if ($fk_parent >= 0) {
  186. $ecmdirtmp->ref = 'NOTUSEDYET';
  187. $ecmdirtmp->label = dol_basename($dirdesc['fullname']);
  188. $ecmdirtmp->description = '';
  189. $ecmdirtmp->fk_parent = $fk_parent;
  190. $txt = "We create directory ".$ecmdirtmp->label." with parent ".$fk_parent;
  191. dol_syslog($txt);
  192. //print $ecmdirtmp->cachenbofdoc."<br>\n";exit;
  193. $id = $ecmdirtmp->create($user);
  194. if ($id > 0) {
  195. $newdirsql = array('id'=>$id,
  196. 'id_mere'=>$ecmdirtmp->fk_parent,
  197. 'label'=>$ecmdirtmp->label,
  198. 'description'=>$ecmdirtmp->description,
  199. 'fullrelativename'=>$relativepathmissing);
  200. $sqltree[] = $newdirsql; // We complete fulltree for following loops
  201. //var_dump($sqltree);
  202. $adirwascreated = 1;
  203. } else {
  204. dol_syslog("Failed to create directory ".$ecmdirtmp->label, LOG_ERR);
  205. }
  206. } else {
  207. $txt = "Parent of ".$dirdesc['fullname']." not found";
  208. dol_syslog($txt);
  209. //print $txt."<br>\n";
  210. }
  211. }
  212. }
  213. // Loop now on each sql tree to check if dir exists
  214. foreach ($sqltree as $dirdesc) { // Loop on each sqltree to check dir is on disk
  215. $dirtotest = $conf->ecm->dir_output.'/'.$dirdesc['fullrelativename'];
  216. if (!dol_is_dir($dirtotest)) {
  217. $ecmdirtmp->id = $dirdesc['id'];
  218. $ecmdirtmp->delete($user, 'databaseonly');
  219. //exit;
  220. }
  221. }
  222. $sql = "UPDATE ".MAIN_DB_PREFIX."ecm_directories set cachenbofdoc = -1 WHERE cachenbofdoc < 0"; // If pb into cahce counting, we set to value -1 = "unknown"
  223. dol_syslog("sql = ".$sql);
  224. $db->query($sql);
  225. // If a directory was added, the fulltree array is not correctly completed and sorted, so we clean
  226. // it to be sure that fulltree array is not used without reloading it.
  227. if ($adirwascreated) {
  228. $sqltree = null;
  229. }
  230. }
  231. /*
  232. * View
  233. */
  234. // Define height of file area (depends on $_SESSION["dol_screenheight"])
  235. //print $_SESSION["dol_screenheight"];
  236. $maxheightwin = (isset($_SESSION["dol_screenheight"]) && $_SESSION["dol_screenheight"] > 466) ? ($_SESSION["dol_screenheight"] - 136) : 660; // Also into index_auto.php file
  237. $moreheadcss = '';
  238. $moreheadjs = '';
  239. //$morejs=array();
  240. $morejs = array('includes/jquery/plugins/blockUI/jquery.blockUI.js', 'core/js/blockUI.js'); // Used by ecm/tpl/enabledfiletreeajax.tpl.pgp
  241. if (empty($conf->global->MAIN_ECM_DISABLE_JS)) {
  242. $morejs[] = "includes/jquery/plugins/jqueryFileTree/jqueryFileTree.js";
  243. }
  244. $moreheadjs .= '<script type="text/javascript">'."\n";
  245. $moreheadjs .= 'var indicatorBlockUI = \''.DOL_URL_ROOT."/theme/".$conf->theme."/img/working.gif".'\';'."\n";
  246. $moreheadjs .= '</script>'."\n";
  247. llxHeader($moreheadcss.$moreheadjs, $langs->trans("ECMArea"), '', '', '', '', $morejs, '', 0, 0);
  248. $head = ecm_prepare_dasboard_head('');
  249. print dol_get_fiche_head($head, 'index_medias', '', -1, '');
  250. // Add filemanager component
  251. $module = 'medias';
  252. if (empty($url)) {
  253. $url = DOL_URL_ROOT.'/ecm/index_medias.php'; // Must be an url without param
  254. }
  255. include DOL_DOCUMENT_ROOT.'/core/tpl/filemanager.tpl.php';
  256. // End of page
  257. print dol_get_fiche_end();
  258. llxFooter();
  259. $db->close();