index.php 12 KB

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