index.php 12 KB

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