index.php 11 KB

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