index.php 12 KB

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