ajaxdirpreview.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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@capnetworks.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 <http://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')) define('NOTOKENRENEWAL',1); // Disables token renewal
  29. if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
  30. if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
  31. if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
  32. if (! isset($mode) || $mode != 'noajax') // For ajax call
  33. {
  34. require_once '../../main.inc.php';
  35. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmdirectory.class.php';
  38. $action=GETPOST("action");
  39. $file=urldecode(GETPOST('file'));
  40. $section=GETPOST("section");
  41. $module=GETPOST("module");
  42. $urlsource=GETPOST("urlsource");
  43. $sortfield = GETPOST("sortfield",'alpha');
  44. $sortorder = GETPOST("sortorder",'alpha');
  45. $page = GETPOST("page",'int');
  46. if ($page == -1) { $page = 0; }
  47. $offset = $conf->liste_limit * $page;
  48. $pageprev = $page - 1;
  49. $pagenext = $page + 1;
  50. if (! $sortorder) $sortorder="ASC";
  51. if (! $sortfield) $sortfield="name";
  52. $upload_dir = dirname(str_replace("../","/", $conf->ecm->dir_output.'/'.$file));
  53. $ecmdir = new EcmDirectory($db);
  54. $result=$ecmdir->fetch($section);
  55. if (! $result > 0)
  56. {
  57. //dol_print_error($db,$ecmdir->error);
  58. //exit;
  59. }
  60. }
  61. else // For no ajax call
  62. {
  63. $ecmdir = new EcmDirectory($db);
  64. $relativepath='';
  65. if ($section > 0)
  66. {
  67. $result=$ecmdir->fetch($section);
  68. if (! $result > 0)
  69. {
  70. dol_print_error($db,$ecmdir->error);
  71. exit;
  72. }
  73. }
  74. $relativepath=$ecmdir->getRelativePath();
  75. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  76. }
  77. // Load traductions files
  78. $langs->load("ecm");
  79. $langs->load("companies");
  80. $langs->load("other");
  81. // Security check
  82. if ($user->societe_id > 0) $socid = $user->societe_id;
  83. //print 'xxx'.$upload_dir;
  84. // Security:
  85. // On interdit les remontees de repertoire ainsi que les pipe dans
  86. // les noms de fichiers.
  87. if (preg_match('/\.\./',$upload_dir) || preg_match('/[<>|]/',$upload_dir))
  88. {
  89. dol_syslog("Refused to deliver file ".$upload_dir);
  90. // Do no show plain path in shown error message
  91. dol_print_error(0,$langs->trans("ErrorFileNameInvalid",$upload_dir));
  92. exit;
  93. }
  94. /*
  95. * Action
  96. */
  97. // None
  98. /*
  99. * View
  100. */
  101. if (! isset($mode) || $mode != 'noajax')
  102. {
  103. // Ajout directives pour resoudre bug IE
  104. header('Cache-Control: Public, must-revalidate');
  105. header('Pragma: public');
  106. top_httphead();
  107. }
  108. $type='directory';
  109. // This test if file exists should be useless. We keep it to find bug more easily
  110. if (! dol_is_dir($upload_dir))
  111. {
  112. // dol_mkdir($upload_dir);
  113. /* $langs->load("install");
  114. dol_print_error(0,$langs->trans("ErrorDirDoesNotExists",$upload_dir));
  115. exit;*/
  116. }
  117. print '<!-- TYPE='.$type.' -->'."\n";
  118. print '<!-- Page called with mode='.(isset($mode)?$mode:'').' type='.$type.' module='.$module.' url='.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
  119. $param=($sortfield?'&sortfield='.$sortfield:'').($sortorder?'&sortorder='.$sortorder:'');
  120. $url=DOL_URL_ROOT.'/ecm/index.php';
  121. // Dir scan
  122. if ($type == 'directory')
  123. {
  124. $formfile=new FormFile($db);
  125. $maxlengthname=40;
  126. $excludefiles = array('^SPECIMEN\.pdf$','^\.','(\.meta|_preview\.png)$','^temp$','^payments$','^CVS$','^thumbs$');
  127. $sorting = (strtolower($sortorder)=='desc'?SORT_DESC:SORT_ASC);
  128. // Right area. If module is defined, we are in automatic ecm.
  129. $automodules = array('company', 'invoice', 'invoice_supplier', 'propal', 'order', 'order_supplier', 'contract', 'product', 'tax', 'project');
  130. // TODO change for multicompany sharing
  131. // Auto area for suppliers invoices
  132. if ($module == 'company') $upload_dir = $conf->societe->dir_output;
  133. // Auto area for suppliers invoices
  134. else if ($module == 'invoice') $upload_dir = $conf->facture->dir_output;
  135. // Auto area for suppliers invoices
  136. else if ($module == 'invoice_supplier')
  137. {
  138. $relativepath='facture';
  139. $upload_dir = $conf->fournisseur->dir_output.'/'.$relativepath;
  140. }
  141. // Auto area for customers orders
  142. else if ($module == 'propal') $upload_dir = $conf->propal->dir_output;
  143. // Auto area for customers orders
  144. else if ($module == 'order') $upload_dir = $conf->commande->dir_output;
  145. // Auto area for suppliers orders
  146. else if ($module == 'order_supplier')
  147. {
  148. $relativepath='commande';
  149. $upload_dir = $conf->fournisseur->dir_output.'/'.$relativepath;
  150. }
  151. // Auto area for suppliers invoices
  152. else if ($module == 'contract') $upload_dir = $conf->contrat->dir_output;
  153. // Auto area for products
  154. else if ($module == 'product') $upload_dir = $conf->product->dir_output;
  155. // Auto area for suppliers invoices
  156. else if ($module == 'tax') $upload_dir = $conf->tax->dir_output;
  157. // Auto area for projects
  158. else if ($module == 'project') $upload_dir = $conf->projet->dir_output;
  159. if (in_array($module, $automodules))
  160. {
  161. $param.='&module='.$module;
  162. $textifempty=($section?$langs->trans("NoFileFound"):($showonrightsize=='featurenotyetavailable'?$langs->trans("FeatureNotYetAvailable"):$langs->trans("NoFileFound")));
  163. $filearray=dol_dir_list($upload_dir,"files",1,'', $excludefiles, $sortfield, $sorting,1);
  164. $formfile->list_of_autoecmfiles($upload_dir,$filearray,$module,$param,1,'',$user->rights->ecm->upload,1,$textifempty,$maxlengthname,$url);
  165. }
  166. //Manual area
  167. else
  168. {
  169. $relativepath=$ecmdir->getRelativePath();
  170. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  171. // If $section defined with value 0
  172. if ($section === '0')
  173. {
  174. $filearray=array();
  175. $textifempty='<br><div align="center"><font class="warning">'.$langs->trans("DirNotSynchronizedSyncFirst").'</font></div><br>';
  176. }
  177. else $filearray=dol_dir_list($upload_dir,"files",0,'',array('^\.','(\.meta|_preview\.png)$','^temp$','^CVS$'),$sortfield, $sorting,1);
  178. if ($section)
  179. {
  180. $param.='&section='.$section;
  181. $textifempty = $langs->trans('NoFileFound');
  182. }
  183. else $textifempty=($showonrightsize=='featurenotyetavailable'?$langs->trans("FeatureNotYetAvailable"):$langs->trans("ECMSelectASection"));
  184. $formfile->list_of_documents($filearray,'','ecm',$param,1,$relativepath,$user->rights->ecm->upload,1,$textifempty,$maxlengthname,'',$url);
  185. }
  186. }
  187. if ($section)
  188. {
  189. $useajax=1;
  190. if (! empty($conf->dol_use_jmobile)) $useajax=0;
  191. if (empty($conf->use_javascript_ajax)) $useajax=0;
  192. if (! empty($conf->global->MAIN_ECM_DISABLE_JS)) $useajax=0;
  193. $param.=($param?'?':'').(preg_replace('/^&/','',$param));
  194. if ($useajax || $action == 'delete')
  195. {
  196. $urlfile='';
  197. if ($action == 'delete') $urlfile=GETPOST('urlfile');
  198. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  199. $useglobalvars=1;
  200. $form = new Form($db);
  201. $formquestion=array('urlfile'=>array('type'=>'hidden','value'=>$urlfile,'name'=>'urlfile'));
  202. print $form->formconfirm($url,$langs->trans("DeleteFile"),$langs->trans("ConfirmDeleteFile"),'confirm_deletefile',$formquestion,"no",($useajax?'deletefile':0));
  203. }
  204. if ($useajax)
  205. {
  206. // Enable jquery handlers on new generated HTML objects
  207. print '<script type="text/javascript">'."\n";
  208. print 'jQuery(document).ready(function() {'."\n";
  209. print 'jQuery(".deletefilelink").click(function(e) { jQuery("#urlfile").val(jQuery(this).attr("rel")); jQuery("#dialog-confirm-deletefile").dialog("open"); return false; });'."\n";
  210. print '});'."\n";
  211. print '</script>'."\n";
  212. }
  213. }
  214. // Close db if mode is not noajax
  215. if ((! isset($mode) || $mode != 'noajax') && is_object($db)) $db->close();