index.php 12 KB

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