index_medias.php 11 KB

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