ajaxdirpreview.php 17 KB

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