ajaxdirpreview.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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)) {
  96. if (GETPOSTISSET('website')) {
  97. $url = DOL_URL_ROOT.'/website/index.php';
  98. } else {
  99. $url = DOL_URL_ROOT.'/ecm/index.php';
  100. }
  101. }
  102. // Load translation files required by the page
  103. $langs->loadLangs(array("ecm", "companies", "other"));
  104. // Security check
  105. if ($user->socid > 0) {
  106. $socid = $user->socid;
  107. }
  108. //print 'xxx'.$upload_dir;
  109. // Security:
  110. // On interdit les remontees de repertoire ainsi que les pipe dans les noms de fichiers.
  111. if (preg_match('/\.\./', $upload_dir) || preg_match('/[<>|]/', $upload_dir)) {
  112. dol_syslog("Refused to deliver file ".$upload_dir);
  113. // Do no show plain path in shown error message
  114. dol_print_error(0, $langs->trans("ErrorFileNameInvalid", $upload_dir));
  115. exit;
  116. }
  117. // Check permissions
  118. if ($modulepart == 'ecm') {
  119. if (!$user->rights->ecm->read) {
  120. accessforbidden();
  121. }
  122. }
  123. if ($modulepart == 'medias') {
  124. // Always allowed
  125. }
  126. /*
  127. * Action
  128. */
  129. // None
  130. /*
  131. * View
  132. */
  133. if (!isset($mode) || $mode != 'noajax') {
  134. // Ajout directives pour resoudre bug IE
  135. header('Cache-Control: Public, must-revalidate');
  136. header('Pragma: public');
  137. top_httphead();
  138. }
  139. $type = 'directory';
  140. // This test if file exists should be useless. We keep it to find bug more easily
  141. if (!dol_is_dir($upload_dir)) {
  142. //dol_mkdir($upload_dir);
  143. /*$langs->load("install");
  144. dol_print_error(0,$langs->trans("ErrorDirDoesNotExists",$upload_dir));
  145. exit;*/
  146. }
  147. print '<!-- ajaxdirpreview type='.$type.' -->'."\n";
  148. //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";
  149. $param = ($sortfield ? '&sortfield='.urlencode($sortfield) : '').($sortorder ? '&sortorder='.urlencode($sortorder) : '');
  150. if (!empty($websitekey)) {
  151. $param .= '&website='.urlencode($websitekey);
  152. }
  153. if (!empty($pageid)) {
  154. $param .= '&pageid='.urlencode($pageid);
  155. }
  156. // Dir scan
  157. if ($type == 'directory') {
  158. $formfile = new FormFile($db);
  159. $maxlengthname = 40;
  160. $excludefiles = array('^SPECIMEN\.pdf$', '^\.', '(\.meta|_preview.*\.png)$', '^temp$', '^payments$', '^CVS$', '^thumbs$');
  161. $sorting = (strtolower($sortorder) == 'desc' ?SORT_DESC:SORT_ASC);
  162. // Right area. If module is defined here, we are in automatic ecm.
  163. $automodules = array(
  164. 'company',
  165. 'invoice',
  166. 'invoice_supplier',
  167. 'propal',
  168. 'supplier_proposal',
  169. 'order',
  170. 'order_supplier',
  171. 'contract',
  172. 'product',
  173. 'tax',
  174. 'tax-vat',
  175. 'salaries',
  176. 'project',
  177. 'project_task',
  178. 'fichinter',
  179. 'user',
  180. 'expensereport',
  181. 'holiday',
  182. 'recruitment-recruitmentcandidature',
  183. 'banque',
  184. 'chequereceipt',
  185. 'mrp-mo'
  186. );
  187. $parameters = array('modulepart'=>$module);
  188. $reshook = $hookmanager->executeHooks('addSectionECMAuto', $parameters);
  189. if ($reshook > 0 && is_array($hookmanager->resArray) && count($hookmanager->resArray) > 0) {
  190. $automodules[] = $hookmanager->resArray['module'];
  191. }
  192. // TODO change for multicompany sharing
  193. if ($module == 'company') {
  194. $upload_dir = $conf->societe->dir_output;
  195. $excludefiles[] = '^contact$'; // The subdir 'contact' contains files of contacts.
  196. } elseif ($module == 'invoice') {
  197. $upload_dir = $conf->facture->dir_output;
  198. } elseif ($module == 'invoice_supplier') {
  199. $upload_dir = $conf->fournisseur->facture->dir_output;
  200. } elseif ($module == 'propal') {
  201. $upload_dir = $conf->propal->dir_output;
  202. } elseif ($module == 'supplier_proposal') {
  203. $upload_dir = $conf->supplier_proposal->dir_output;
  204. } elseif ($module == 'order') {
  205. $upload_dir = $conf->commande->dir_output;
  206. } elseif ($module == 'order_supplier') {
  207. $upload_dir = $conf->fournisseur->commande->dir_output;
  208. } elseif ($module == 'contract') {
  209. $upload_dir = $conf->contrat->dir_output;
  210. } elseif ($module == 'product') {
  211. $upload_dir = $conf->product->dir_output;
  212. } elseif ($module == 'tax') {
  213. $upload_dir = $conf->tax->dir_output;
  214. $excludefiles[] = '^vat$'; // The subdir 'vat' contains files of vats.
  215. } elseif ($module == 'tax-vat') {
  216. $upload_dir = $conf->tax->dir_output.'/vat';
  217. } elseif ($module == 'salaries') {
  218. $upload_dir = $conf->salaries->dir_output;
  219. } elseif ($module == 'project') {
  220. $upload_dir = $conf->project->dir_output;
  221. } elseif ($module == 'project_task') {
  222. $upload_dir = $conf->project->dir_output;
  223. } elseif ($module == 'fichinter') {
  224. $upload_dir = $conf->ficheinter->dir_output;
  225. } elseif ($module == 'user') {
  226. $upload_dir = $conf->user->dir_output;
  227. } elseif ($module == 'expensereport') {
  228. $upload_dir = $conf->expensereport->dir_output;
  229. } elseif ($module == 'holiday') {
  230. $upload_dir = $conf->holiday->dir_output;
  231. } elseif ($module == 'recruitment-recruitmentcandidature') {
  232. $upload_dir = $conf->recruitment->dir_output.'/recruitmentcandidature';
  233. } elseif ($module == 'banque') {
  234. $upload_dir = $conf->bank->dir_output;
  235. } elseif ($module == 'chequereceipt') {
  236. $upload_dir = $conf->bank->dir_output.'/checkdeposits';
  237. } elseif ($module == 'mrp-mo') {
  238. $upload_dir = $conf->mrp->dir_output;
  239. } else {
  240. $parameters = array('modulepart'=>$module);
  241. $reshook = $hookmanager->executeHooks('addSectionECMAuto', $parameters);
  242. if ($reshook > 0 && is_array($hookmanager->resArray) && count($hookmanager->resArray) > 0) {
  243. $upload_dir = $hookmanager->resArray['directory'];
  244. }
  245. }
  246. // Automatic list
  247. if (in_array($module, $automodules)) {
  248. $param .= '&module='.$module;
  249. if (isset($search_doc_ref) && $search_doc_ref != '') {
  250. $param .= '&search_doc_ref='.urlencode($search_doc_ref);
  251. }
  252. $textifempty = ($section ? $langs->trans("NoFileFound") : ($showonrightsize == 'featurenotyetavailable' ? $langs->trans("FeatureNotYetAvailable") : $langs->trans("NoFileFound")));
  253. $filter = preg_quote($search_doc_ref, '/');
  254. $filearray = dol_dir_list($upload_dir, "files", 1, $filter, $excludefiles, $sortfield, $sorting, 1);
  255. $perm = $user->rights->ecm->upload;
  256. $formfile->list_of_autoecmfiles($upload_dir, $filearray, $module, $param, 1, '', $perm, 1, $textifempty, $maxlengthname, $url, 1);
  257. } else {
  258. // Manual list
  259. if ($module == 'medias') {
  260. /*
  261. $_POST is array like
  262. 'token' => string '062380e11b7dcd009d07318b57b71750' (length=32)
  263. 'action' => string 'file_manager' (length=12)
  264. 'website' => string 'template' (length=8)
  265. 'pageid' => string '124' (length=3)
  266. 'section_dir' => string 'mydir/' (length=3)
  267. 'section_id' => string '0' (length=1)
  268. 'max_file_size' => string '2097152' (length=7)
  269. 'sendit' => string 'Envoyer fichier' (length=15)
  270. */
  271. $relativepath = GETPOST('file', 'alpha') ?GETPOST('file', 'alpha') : GETPOST('section_dir', 'alpha');
  272. if ($relativepath && $relativepath != '/') {
  273. $relativepath .= '/';
  274. }
  275. $upload_dir = $dolibarr_main_data_root.'/'.$module.'/'.$relativepath;
  276. if (GETPOSTISSET('website') || GETPOSTISSET('file_manager')) {
  277. $param .= '&file_manager=1';
  278. if (!preg_match('/website=/', $param)) {
  279. $param .= '&website='.urlencode(GETPOST('website', 'alpha'));
  280. }
  281. if (!preg_match('/pageid=/', $param)) {
  282. $param .= '&pageid='.urlencode(GETPOST('pageid', 'int'));
  283. }
  284. //if (!preg_match('/backtopage=/',$param)) $param.='&backtopage='.urlencode($_SERVER["PHP_SELF"].'?file_manager=1&website='.$websitekey.'&pageid='.$pageid);
  285. }
  286. } else {
  287. $relativepath = $ecmdir->getRelativePath();
  288. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  289. }
  290. // If $section defined with value 0
  291. if (($section === '0' || empty($section)) && ($module != 'medias')) {
  292. $filearray = array();
  293. } else {
  294. $filearray = dol_dir_list($upload_dir, "files", 0, '', array('^\.', '(\.meta|_preview.*\.png)$', '^temp$', '^CVS$'), $sortfield, $sorting, 1);
  295. }
  296. if ($section) {
  297. $param .= '&section='.$section;
  298. if (isset($search_doc_ref) && $search_doc_ref != '') {
  299. $param .= '&search_doc_ref='.urlencode($search_doc_ref);
  300. }
  301. $textifempty = $langs->trans('NoFileFound');
  302. } elseif ($section === '0') {
  303. if ($module == 'ecm') {
  304. $textifempty = '<br><div class="center"><span class="warning">'.$langs->trans("DirNotSynchronizedSyncFirst").'</span></div><br>';
  305. } else {
  306. $textifempty = $langs->trans('NoFileFound');
  307. }
  308. } else {
  309. $textifempty = ($showonrightsize == 'featurenotyetavailable' ? $langs->trans("FeatureNotYetAvailable") : $langs->trans("ECMSelectASection"));
  310. }
  311. if ($module == 'medias') {
  312. $useinecm = 6;
  313. $modulepart = 'medias';
  314. $perm = ($user->rights->website->write || $user->rights->emailing->creer);
  315. $title = 'none';
  316. } elseif ($module == 'ecm') { // DMS/ECM -> manual structure
  317. if ($user->rights->ecm->read) {
  318. // Buttons: Preview
  319. $useinecm = 2;
  320. }
  321. if ($user->rights->ecm->upload) {
  322. // Buttons: Preview + Delete
  323. $useinecm = 4;
  324. }
  325. if ($user->rights->ecm->setup) {
  326. // Buttons: Preview + Delete + Edit
  327. $useinecm = 5;
  328. }
  329. $perm = $user->rights->ecm->upload;
  330. $modulepart = 'ecm';
  331. $title = ''; // Use default
  332. } else {
  333. $useinecm = 5;
  334. $modulepart = 'ecm';
  335. $perm = $user->rights->ecm->upload;
  336. $title = ''; // Use default
  337. }
  338. // When we show list of files for ECM files, $filearray contains file list, and directory is defined with modulepart + section into $param
  339. // When we show list of files for a directory, $filearray ciontains file list, and directory is defined with modulepart + $relativepath
  340. //var_dump("section=".$section." title=".$title." modulepart=".$modulepart." useinecm=".$useinecm." perm=".$perm." relativepath=".$relativepath." param=".$param." url=".$url);
  341. $formfile->list_of_documents($filearray, '', $modulepart, $param, 1, $relativepath, $perm, $useinecm, $textifempty, $maxlengthname, $title, $url, 0, $perm, '', $sortfield, $sortorder);
  342. }
  343. }
  344. // Bottom of page
  345. $useajax = 1;
  346. if (!empty($conf->dol_use_jmobile)) {
  347. $useajax = 0;
  348. }
  349. if (empty($conf->use_javascript_ajax)) {
  350. $useajax = 0;
  351. }
  352. if (!empty($conf->global->MAIN_ECM_DISABLE_JS)) {
  353. $useajax = 0;
  354. }
  355. //$param.=($param?'?':'').(preg_replace('/^&/','',$param));
  356. if ($useajax || $action == 'deletefile') {
  357. $urlfile = '';
  358. if ($action == 'deletefile') {
  359. $urlfile = GETPOST('urlfile', 'alpha');
  360. }
  361. if (empty($section_dir)) {
  362. $section_dir = GETPOST("file", "alpha");
  363. }
  364. $section_id = $section;
  365. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  366. $form = new Form($db);
  367. $formquestion['urlfile'] = array('type'=>'hidden', 'value'=>$urlfile, 'name'=>'urlfile'); // We must always put field, even if empty because it is filled by javascript later
  368. $formquestion['section'] = array('type'=>'hidden', 'value'=>$section, 'name'=>'section'); // We must always put field, even if empty because it is filled by javascript later
  369. $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
  370. $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
  371. $formquestion['sortfield'] = array('type'=>'hidden', 'value'=>$sortfield, 'name'=>'sortfield'); // We must always put field, even if empty because it is filled by javascript later
  372. $formquestion['sortorder'] = array('type'=>'hidden', 'value'=>$sortorder, 'name'=>'sortorder'); // We must always put field, even if empty because it is filled by javascript later
  373. if (!empty($action) && $action == 'file_manager') {
  374. $formquestion['file_manager'] = array('type'=>'hidden', 'value'=>1, 'name'=>'file_manager');
  375. }
  376. if (!empty($websitekey)) {
  377. $formquestion['website'] = array('type'=>'hidden', 'value'=>$websitekey, 'name'=>'website');
  378. }
  379. if (!empty($pageid) && $pageid > 0) {
  380. $formquestion['pageid'] = array('type'=>'hidden', 'value'=>$pageid, 'name'=>'pageid');
  381. }
  382. print $form->formconfirm($url, $langs->trans("DeleteFile"), $langs->trans("ConfirmDeleteFile"), 'confirm_deletefile', $formquestion, "no", ($useajax ? 'deletefile' : 0));
  383. }
  384. if ($useajax) {
  385. print '<!-- ajaxdirpreview.php: js to manage preview of doc -->'."\n";
  386. print '<script type="text/javascript">';
  387. // Enable jquery handlers on new generated HTML objects (same code than into lib_footer.js.php)
  388. // Because the content is reloaded by ajax call, we must also reenable some jquery hooks
  389. // Wrapper to manage document_preview
  390. if ($conf->browser->layout != 'phone') {
  391. print "\n/* JS CODE TO ENABLE document_preview */\n";
  392. print '
  393. jQuery(document).ready(function () {
  394. jQuery(".documentpreview").click(function () {
  395. console.log("We click on preview for element with href="+$(this).attr(\'href\')+" mime="+$(this).attr(\'mime\'));
  396. document_preview($(this).attr(\'href\'), $(this).attr(\'mime\'), \''.dol_escape_js($langs->transnoentities("Preview")).'\');
  397. return false;
  398. });
  399. });
  400. ' . "\n";
  401. }
  402. // Enable jquery handlers button to delete files
  403. print 'jQuery(document).ready(function() {'."\n";
  404. print ' jQuery(".deletefilelink").click(function(e) { '."\n";
  405. print ' console.log("We click on button with class deletefilelink, param='.$param.', we set urlfile to "+jQuery(this).attr("rel"));'."\n";
  406. print ' jQuery("#urlfile").val(jQuery(this).attr("rel"));'."\n";
  407. //print ' jQuery("#section_dir").val(\'aaa\');'."\n";
  408. print ' jQuery("#dialog-confirm-deletefile").dialog("open");'."\n";
  409. print ' return false;'."\n";
  410. print ' });'."\n";
  411. print '});'."\n";
  412. print '</script>'."\n";
  413. }
  414. // Close db if mode is not noajax
  415. if ((!isset($mode) || $mode != 'noajax') && is_object($db)) {
  416. $db->close();
  417. }