ajaxdirpreview.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. /* Copyright (C) 2004-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Simon Tosser <simon@kornog-computing.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2010 Pierre Morin <pierre.morin@auguria.net>
  7. * Copyright (C) 2013 Marcos García <marcosgdf@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/core/ajax/ajaxdirpreview.php
  24. * \brief Service to return a HTML preview of a directory
  25. * Call of this service is made with URL:
  26. * ajaxdirpreview.php?mode=nojs&action=preview&module=ecm&section=0&file=xxx
  27. */
  28. if (!defined('NOTOKENRENEWAL')) {
  29. define('NOTOKENRENEWAL', 1); // Disables token renewal
  30. }
  31. if (!defined('NOREQUIREMENU')) {
  32. define('NOREQUIREMENU', '1');
  33. }
  34. if (!defined('NOREQUIREHTML')) {
  35. define('NOREQUIREHTML', '1');
  36. }
  37. if (!defined('NOREQUIREAJAX')) {
  38. define('NOREQUIREAJAX', '1');
  39. }
  40. if (!isset($mode) || $mode != 'noajax') { // For ajax call
  41. require_once '../../main.inc.php';
  42. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  43. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  44. require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
  45. $action = GETPOST('action', 'aZ09');
  46. $file = urldecode(GETPOST('file', 'alpha'));
  47. $section = GETPOST("section", 'alpha');
  48. $module = GETPOST("module", 'alpha');
  49. $urlsource = GETPOST("urlsource", 'alpha');
  50. $search_doc_ref = GETPOST('search_doc_ref', 'alpha');
  51. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  52. $sortfield = GETPOST("sortfield", 'aZ09comma');
  53. $sortorder = GETPOST("sortorder", 'aZ09comma');
  54. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  55. if (empty($page) || $page == -1) {
  56. $page = 0;
  57. } // If $page is not defined, or '' or -1
  58. $offset = $limit * $page;
  59. $pageprev = $page - 1;
  60. $pagenext = $page + 1;
  61. if (!$sortorder) {
  62. $sortorder = "ASC";
  63. }
  64. if (!$sortfield) {
  65. $sortfield = "name";
  66. }
  67. $rootdirfordoc = $conf->ecm->dir_output;
  68. $upload_dir = dirname(str_replace("../", "/", $rootdirfordoc.'/'.$file));
  69. $ecmdir = new EcmDirectory($db);
  70. if ($section > 0) {
  71. $result = $ecmdir->fetch($section);
  72. if (!($result > 0)) {
  73. //dol_print_error($db,$ecmdir->error);
  74. //exit;
  75. }
  76. }
  77. } else {
  78. // For no ajax call
  79. $rootdirfordoc = $conf->ecm->dir_output;
  80. $ecmdir = new EcmDirectory($db);
  81. $relativepath = '';
  82. if ($section > 0) {
  83. $result = $ecmdir->fetch($section);
  84. if (!($result > 0)) {
  85. dol_print_error($db, $ecmdir->error);
  86. exit;
  87. }
  88. $relativepath = $ecmdir->getRelativePath(); // Example 'mydir/'
  89. } elseif (GETPOST('section_dir')) {
  90. $relativepath = GETPOST('section_dir');
  91. }
  92. //var_dump($section.'-'.GETPOST('section_dir').'-'.$relativepath);
  93. $upload_dir = $rootdirfordoc.'/'.$relativepath;
  94. }
  95. if (empty($url)) { // autoset $url but it is better to have it defined before into filemanager.tpl.php (not possible when in auto tree)
  96. if (!empty($module) && $module == 'medias' && !GETPOST('website')) {
  97. $url = DOL_URL_ROOT.'/ecm/index_medias.php';
  98. } elseif (GETPOSTISSET('website')) {
  99. $url = DOL_URL_ROOT.'/website/index.php';
  100. } else {
  101. $url = DOL_URL_ROOT.'/ecm/index.php';
  102. }
  103. }
  104. // Load translation files required by the page
  105. $langs->loadLangs(array("ecm", "companies", "other"));
  106. // Security check
  107. if ($user->socid > 0) {
  108. $socid = $user->socid;
  109. }
  110. //print 'xxx'.$upload_dir;
  111. // Security:
  112. // On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
  113. if (preg_match('/\.\./', $upload_dir) || preg_match('/[<>|]/', $upload_dir)) {
  114. dol_syslog("Refused to deliver file ".$upload_dir);
  115. // Do no show plain path in shown error message
  116. dol_print_error(0, $langs->trans("ErrorFileNameInvalid", $upload_dir));
  117. exit;
  118. }
  119. if (empty($modulepart)) {
  120. $modulepart = $module;
  121. }
  122. // Check permissions
  123. if ($modulepart == 'ecm') {
  124. if (!$user->hasRight('ecm', 'read')) {
  125. accessforbidden();
  126. }
  127. } elseif ($modulepart == 'medias' || $modulepart == 'website') {
  128. // Always allowed
  129. } else {
  130. accessforbidden();
  131. }
  132. /*
  133. * Action
  134. */
  135. // None
  136. /*
  137. * View
  138. */
  139. if (!isset($mode) || $mode != 'noajax') {
  140. // Ajout directives pour resoudre bug IE
  141. header('Cache-Control: Public, must-revalidate');
  142. header('Pragma: public');
  143. top_httphead();
  144. }
  145. $type = 'directory';
  146. // This test if file exists should be useless. We keep it to find bug more easily
  147. if (!dol_is_dir($upload_dir)) {
  148. //dol_mkdir($upload_dir);
  149. /*$langs->load("install");
  150. dol_print_error(0,$langs->trans("ErrorDirDoesNotExists",$upload_dir));
  151. exit;*/
  152. }
  153. print '<!-- ajaxdirpreview type='.$type.' module='.$module.' modulepart='.$modulepart.'-->'."\n";
  154. //print '<!-- Page called with mode='.dol_escape_htmltag(isset($mode)?$mode:'').' type='.dol_escape_htmltag($type).' module='.dol_escape_htmltag($module).' url='.dol_escape_htmltag($url).' '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  155. $param = ($sortfield ? '&sortfield='.urlencode($sortfield) : '').($sortorder ? '&sortorder='.urlencode($sortorder) : '');
  156. if (!empty($websitekey)) {
  157. $param .= '&website='.urlencode($websitekey);
  158. }
  159. if (!empty($pageid)) {
  160. $param .= '&pageid='.urlencode($pageid);
  161. }
  162. // Dir scan
  163. if ($type == 'directory') {
  164. $formfile = new FormFile($db);
  165. $maxlengthname = 40;
  166. $excludefiles = array('^SPECIMEN\.pdf$', '^\.', '(\.meta|_preview.*\.png)$', '^temp$', '^payments$', '^CVS$', '^thumbs$');
  167. $sorting = (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC);
  168. // Right area. If module is defined here, we are in automatic ecm.
  169. $automodules = array(
  170. 'company',
  171. 'invoice',
  172. 'invoice_supplier',
  173. 'propal',
  174. 'supplier_proposal',
  175. 'order',
  176. 'order_supplier',
  177. 'contract',
  178. 'product',
  179. 'tax',
  180. 'tax-vat',
  181. 'salaries',
  182. 'project',
  183. 'project_task',
  184. 'fichinter',
  185. 'user',
  186. 'expensereport',
  187. 'holiday',
  188. 'recruitment-recruitmentcandidature',
  189. 'banque',
  190. 'chequereceipt',
  191. 'mrp-mo'
  192. );
  193. $parameters = array('modulepart'=>$module);
  194. $reshook = $hookmanager->executeHooks('addSectionECMAuto', $parameters);
  195. if ($reshook > 0 && is_array($hookmanager->resArray) && count($hookmanager->resArray) > 0) {
  196. $automodules[] = $hookmanager->resArray['module'];
  197. }
  198. // TODO change for multicompany sharing
  199. if ($module == 'company') {
  200. $upload_dir = $conf->societe->dir_output;
  201. $excludefiles[] = '^contact$'; // The subdir 'contact' contains files of contacts.
  202. } elseif ($module == 'invoice') {
  203. $upload_dir = $conf->facture->dir_output;
  204. } elseif ($module == 'invoice_supplier') {
  205. $upload_dir = $conf->fournisseur->facture->dir_output;
  206. } elseif ($module == 'propal') {
  207. $upload_dir = $conf->propal->dir_output;
  208. } elseif ($module == 'supplier_proposal') {
  209. $upload_dir = $conf->supplier_proposal->dir_output;
  210. } elseif ($module == 'order') {
  211. $upload_dir = $conf->commande->dir_output;
  212. } elseif ($module == 'order_supplier') {
  213. $upload_dir = $conf->fournisseur->commande->dir_output;
  214. } elseif ($module == 'contract') {
  215. $upload_dir = $conf->contrat->dir_output;
  216. } elseif ($module == 'product') {
  217. $upload_dir = $conf->product->dir_output;
  218. } elseif ($module == 'tax') {
  219. $upload_dir = $conf->tax->dir_output;
  220. $excludefiles[] = '^vat$'; // The subdir 'vat' contains files of vats.
  221. } elseif ($module == 'tax-vat') {
  222. $upload_dir = $conf->tax->dir_output.'/vat';
  223. } elseif ($module == 'salaries') {
  224. $upload_dir = $conf->salaries->dir_output;
  225. } elseif ($module == 'project') {
  226. $upload_dir = $conf->project->dir_output;
  227. } elseif ($module == 'project_task') {
  228. $upload_dir = $conf->project->dir_output;
  229. } elseif ($module == 'fichinter') {
  230. $upload_dir = $conf->ficheinter->dir_output;
  231. } elseif ($module == 'user') {
  232. $upload_dir = $conf->user->dir_output;
  233. } elseif ($module == 'expensereport') {
  234. $upload_dir = $conf->expensereport->dir_output;
  235. } elseif ($module == 'holiday') {
  236. $upload_dir = $conf->holiday->dir_output;
  237. } elseif ($module == 'recruitment-recruitmentcandidature') {
  238. $upload_dir = $conf->recruitment->dir_output.'/recruitmentcandidature';
  239. } elseif ($module == 'banque') {
  240. $upload_dir = $conf->bank->dir_output;
  241. } elseif ($module == 'chequereceipt') {
  242. $upload_dir = $conf->bank->dir_output.'/checkdeposits';
  243. } elseif ($module == 'mrp-mo') {
  244. $upload_dir = $conf->mrp->dir_output;
  245. } else {
  246. $parameters = array('modulepart'=>$module);
  247. $reshook = $hookmanager->executeHooks('addSectionECMAuto', $parameters);
  248. if ($reshook > 0 && is_array($hookmanager->resArray) && count($hookmanager->resArray) > 0) {
  249. $upload_dir = $hookmanager->resArray['directory'];
  250. }
  251. }
  252. // Automatic list
  253. if (in_array($module, $automodules)) {
  254. $param .= '&module='.$module;
  255. if (isset($search_doc_ref) && $search_doc_ref != '') {
  256. $param .= '&search_doc_ref='.urlencode($search_doc_ref);
  257. }
  258. $textifempty = ($section ? $langs->trans("NoFileFound") : ($showonrightsize == 'featurenotyetavailable' ? $langs->trans("FeatureNotYetAvailable") : $langs->trans("NoFileFound")));
  259. $filter = preg_quote($search_doc_ref, '/');
  260. $filearray = dol_dir_list($upload_dir, "files", 1, $filter, $excludefiles, $sortfield, $sorting, 1);
  261. $perm = $user->rights->ecm->upload;
  262. $formfile->list_of_autoecmfiles($upload_dir, $filearray, $module, $param, 1, '', $perm, 1, $textifempty, $maxlengthname, $url, 1);
  263. } else {
  264. // Manual list
  265. if ($module == 'medias') {
  266. /*
  267. $_POST is array like
  268. 'token' => string '062380e11b7dcd009d07318b57b71750' (length=32)
  269. 'action' => string 'file_manager' (length=12)
  270. 'website' => string 'template' (length=8)
  271. 'pageid' => string '124' (length=3)
  272. 'section_dir' => string 'mydir/' (length=3)
  273. 'section_id' => string '0' (length=1)
  274. 'max_file_size' => string '2097152' (length=7)
  275. 'sendit' => string 'Envoyer fichier' (length=15)
  276. */
  277. $relativepath = GETPOST('file', 'alpha') ?GETPOST('file', 'alpha') : GETPOST('section_dir', 'alpha');
  278. if ($relativepath && $relativepath != '/') {
  279. $relativepath .= '/';
  280. }
  281. $upload_dir = $dolibarr_main_data_root.'/'.$module.'/'.$relativepath;
  282. if (GETPOSTISSET('website') || GETPOSTISSET('file_manager')) {
  283. $param .= '&file_manager=1';
  284. if (!preg_match('/website=/', $param) && GETPOST('website', 'alpha')) {
  285. $param .= '&website='.urlencode(GETPOST('website', 'alpha'));
  286. }
  287. if (!preg_match('/pageid=/', $param)) {
  288. $param .= '&pageid='.urlencode(GETPOST('pageid', 'int'));
  289. }
  290. //if (!preg_match('/backtopage=/',$param)) $param.='&backtopage='.urlencode($_SERVER["PHP_SELF"].'?file_manager=1&website='.$websitekey.'&pageid='.$pageid);
  291. }
  292. } else {
  293. $relativepath = $ecmdir->getRelativePath();
  294. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  295. }
  296. // If $section defined with value 0
  297. if (($section === '0' || empty($section)) && ($module != 'medias')) {
  298. $filearray = array();
  299. } else {
  300. $filearray = dol_dir_list($upload_dir, "files", 0, '', array('^\.', '(\.meta|_preview.*\.png)$', '^temp$', '^CVS$'), $sortfield, $sorting, 1);
  301. }
  302. if ($section) {
  303. $param .= '&section='.$section;
  304. if (isset($search_doc_ref) && $search_doc_ref != '') {
  305. $param .= '&search_doc_ref='.urlencode($search_doc_ref);
  306. }
  307. $textifempty = $langs->trans('NoFileFound');
  308. } elseif ($section === '0') {
  309. if ($module == 'ecm') {
  310. $textifempty = '<br><div class="center"><span class="warning">'.$langs->trans("DirNotSynchronizedSyncFirst").'</span></div><br>';
  311. } else {
  312. $textifempty = $langs->trans('NoFileFound');
  313. }
  314. } else {
  315. $textifempty = ($showonrightsize == 'featurenotyetavailable' ? $langs->trans("FeatureNotYetAvailable") : $langs->trans("ECMSelectASection"));
  316. }
  317. if ($module == 'medias') {
  318. $useinecm = 6;
  319. $modulepart = 'medias';
  320. $perm = ($user->hasRight("website", "write") || $user->hasRight("emailing", "creer"));
  321. $title = 'none';
  322. } elseif ($module == 'ecm') { // DMS/ECM -> manual structure
  323. if ($user->hasRight("ecm", "read")) {
  324. // Buttons: Preview
  325. $useinecm = 2;
  326. }
  327. if ($user->hasRight("ecm", "upload")) {
  328. // Buttons: Preview + Delete
  329. $useinecm = 4;
  330. }
  331. if ($user->hasRight("ecm", "setup")) {
  332. // Buttons: Preview + Delete + Edit
  333. $useinecm = 5;
  334. }
  335. $perm = $user->hasRight("ecm", "upload");
  336. $modulepart = 'ecm';
  337. $title = ''; // Use default
  338. } else {
  339. $useinecm = 5;
  340. $modulepart = 'ecm';
  341. $perm = $user->hasRight("ecm", "upload");
  342. $title = ''; // Use default
  343. }
  344. // When we show list of files for ECM files, $filearray contains file list, and directory is defined with modulepart + section into $param
  345. // When we show list of files for a directory, $filearray ciontains file list, and directory is defined with modulepart + $relativepath
  346. //var_dump("section=".$section." title=".$title." modulepart=".$modulepart." useinecm=".$useinecm." perm=".$perm." relativepath=".$relativepath." param=".$param." url=".$url);
  347. $formfile->list_of_documents($filearray, '', $modulepart, $param, 1, $relativepath, $perm, $useinecm, $textifempty, $maxlengthname, $title, $url, 0, $perm, '', $sortfield, $sortorder);
  348. }
  349. }
  350. // Bottom of page
  351. $useajax = 1;
  352. if (!empty($conf->dol_use_jmobile)) {
  353. $useajax = 0;
  354. }
  355. if (empty($conf->use_javascript_ajax)) {
  356. $useajax = 0;
  357. }
  358. if (!empty($conf->global->MAIN_ECM_DISABLE_JS)) {
  359. $useajax = 0;
  360. }
  361. //$param.=($param?'?':'').(preg_replace('/^&/','',$param));
  362. if ($useajax || $action == 'deletefile') {
  363. $urlfile = '';
  364. if ($action == 'deletefile') {
  365. $urlfile = GETPOST('urlfile', 'alpha');
  366. }
  367. if (empty($section_dir)) {
  368. $section_dir = GETPOST("file", "alpha");
  369. }
  370. $section_id = $section;
  371. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  372. $form = new Form($db);
  373. $formquestion['urlfile'] = array('type'=>'hidden', 'value'=>$urlfile, 'name'=>'urlfile'); // We must always put field, even if empty because it is filled by javascript later
  374. $formquestion['section'] = array('type'=>'hidden', 'value'=>$section, 'name'=>'section'); // We must always put field, even if empty because it is filled by javascript later
  375. $formquestion['section_id'] = array('type'=>'hidden', 'value'=>$section_id, 'name'=>'section_id'); // We must always put field, even if empty because it is filled by javascript later
  376. $formquestion['section_dir'] = array('type'=>'hidden', 'value'=>$section_dir, 'name'=>'section_dir'); // We must always put field, even if empty because it is filled by javascript later
  377. $formquestion['sortfield'] = array('type'=>'hidden', 'value'=>$sortfield, 'name'=>'sortfield'); // We must always put field, even if empty because it is filled by javascript later
  378. $formquestion['sortorder'] = array('type'=>'hidden', 'value'=>$sortorder, 'name'=>'sortorder'); // We must always put field, even if empty because it is filled by javascript later
  379. if (!empty($action) && $action == 'file_manager') {
  380. $formquestion['file_manager'] = array('type'=>'hidden', 'value'=>1, 'name'=>'file_manager');
  381. }
  382. if (!empty($websitekey)) {
  383. $formquestion['website'] = array('type'=>'hidden', 'value'=>$websitekey, 'name'=>'website');
  384. }
  385. if (!empty($pageid) && $pageid > 0) {
  386. $formquestion['pageid'] = array('type'=>'hidden', 'value'=>$pageid, 'name'=>'pageid');
  387. }
  388. print $form->formconfirm($url, $langs->trans("DeleteFile"), $langs->trans("ConfirmDeleteFile"), 'confirm_deletefile', $formquestion, "no", ($useajax ? 'deletefile' : 0));
  389. }
  390. if ($useajax) {
  391. print '<!-- ajaxdirpreview.php: js to manage preview of doc -->'."\n";
  392. print '<script type="text/javascript">';
  393. // Enable jquery handlers on new generated HTML objects (same code than into lib_footer.js.php)
  394. // Because the content is reloaded by ajax call, we must also reenable some jquery hooks
  395. // Wrapper to manage document_preview
  396. if ($conf->browser->layout != 'phone') {
  397. print "\n/* JS CODE TO ENABLE document_preview */\n";
  398. print '
  399. jQuery(document).ready(function () {
  400. jQuery(".documentpreview").click(function () {
  401. console.log("We click on preview for element with href="+$(this).attr(\'href\')+" mime="+$(this).attr(\'mime\'));
  402. document_preview($(this).attr(\'href\'), $(this).attr(\'mime\'), \''.dol_escape_js($langs->transnoentities("Preview")).'\');
  403. return false;
  404. });
  405. });
  406. ' . "\n";
  407. }
  408. // Enable jquery handlers button to delete files
  409. print 'jQuery(document).ready(function() {'."\n";
  410. print ' jQuery(".deletefilelink").click(function(e) { '."\n";
  411. print ' console.log("We click on button with class deletefilelink, param='.$param.', we set urlfile to "+jQuery(this).attr("rel"));'."\n";
  412. print ' jQuery("#urlfile").val(jQuery(this).attr("rel"));'."\n";
  413. //print ' jQuery("#section_dir").val(\'aaa\');'."\n";
  414. print ' jQuery("#dialog-confirm-deletefile").dialog("open");'."\n";
  415. print ' return false;'."\n";
  416. print ' });'."\n";
  417. print '});'."\n";
  418. print '</script>'."\n";
  419. }
  420. // Close db if mode is not noajax
  421. if ((!isset($mode) || $mode != 'noajax') && is_object($db)) {
  422. $db->close();
  423. }