index_medias.php 11 KB

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