ajaxdirpreview.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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','aZ09');
  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. if (empty($url)) $url=DOL_URL_ROOT.'/ecm/index.php';
  78. // Load traductions files
  79. $langs->load("ecm");
  80. $langs->load("companies");
  81. $langs->load("other");
  82. // Security check
  83. if ($user->societe_id > 0) $socid = $user->societe_id;
  84. //print 'xxx'.$upload_dir;
  85. // Security:
  86. // On interdit les remontees de repertoire ainsi que les pipe dans
  87. // les noms de fichiers.
  88. if (preg_match('/\.\./',$upload_dir) || preg_match('/[<>|]/',$upload_dir))
  89. {
  90. dol_syslog("Refused to deliver file ".$upload_dir);
  91. // Do no show plain path in shown error message
  92. dol_print_error(0,$langs->trans("ErrorFileNameInvalid",$upload_dir));
  93. exit;
  94. }
  95. /*
  96. * Action
  97. */
  98. // None
  99. /*
  100. * View
  101. */
  102. if (! isset($mode) || $mode != 'noajax')
  103. {
  104. // Ajout directives pour resoudre bug IE
  105. header('Cache-Control: Public, must-revalidate');
  106. header('Pragma: public');
  107. top_httphead();
  108. }
  109. $type='directory';
  110. // This test if file exists should be useless. We keep it to find bug more easily
  111. if (! dol_is_dir($upload_dir))
  112. {
  113. // dol_mkdir($upload_dir);
  114. /* $langs->load("install");
  115. dol_print_error(0,$langs->trans("ErrorDirDoesNotExists",$upload_dir));
  116. exit;*/
  117. }
  118. print '<!-- ajaxdirpreview type='.$type.' -->'."\n";
  119. print '<!-- Page called with mode='.(isset($mode)?$mode:'').' type='.$type.' module='.$module.' url='.$url.' '.$_SERVER["PHP_SELF"].'?'.$_SERVER["QUERY_STRING"].' -->'."\n";
  120. $param=($sortfield?'&sortfield='.$sortfield:'').($sortorder?'&sortorder='.$sortorder:'');
  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', 'fichinter', 'user', 'expensereport');
  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. // Auto area for interventions
  160. else if ($module == 'fichinter') $upload_dir = $conf->ficheinter->dir_output;
  161. // Auto area for users
  162. else if ($module == 'user') $upload_dir = $conf->user->dir_output;
  163. // Auto area for expense report
  164. else if ($module == 'expensereport') $upload_dir = $conf->expensereport->dir_output;
  165. // Automatic list
  166. if (in_array($module, $automodules))
  167. {
  168. $param.='&module='.$module;
  169. $textifempty=($section?$langs->trans("NoFileFound"):($showonrightsize=='featurenotyetavailable'?$langs->trans("FeatureNotYetAvailable"):$langs->trans("NoFileFound")));
  170. $filearray=dol_dir_list($upload_dir,"files",1,'', $excludefiles, $sortfield, $sorting,1);
  171. $formfile->list_of_autoecmfiles($upload_dir,$filearray,$module,$param,1,'',$user->rights->ecm->upload,1,$textifempty,$maxlengthname,$url);
  172. }
  173. // Manual list
  174. else
  175. {
  176. $relativepath=$ecmdir->getRelativePath();
  177. $upload_dir = $conf->ecm->dir_output.'/'.$relativepath;
  178. // If $section defined with value 0
  179. if ($section === '0')
  180. {
  181. $filearray=array();
  182. }
  183. else $filearray=dol_dir_list($upload_dir,"files",0,'',array('^\.','(\.meta|_preview.*\.png)$','^temp$','^CVS$'),$sortfield, $sorting,1);
  184. if ($section)
  185. {
  186. $param.='&section='.$section;
  187. $textifempty = $langs->trans('NoFileFound');
  188. }
  189. else if ($section === '0') $textifempty='<br><div align="center"><font class="warning">'.$langs->trans("DirNotSynchronizedSyncFirst").'</font></div><br>';
  190. else $textifempty=($showonrightsize=='featurenotyetavailable'?$langs->trans("FeatureNotYetAvailable"):$langs->trans("ECMSelectASection"));
  191. $formfile->list_of_documents($filearray,'','ecm',$param,1,$relativepath,$user->rights->ecm->upload,1,$textifempty,$maxlengthname,'',$url);
  192. }
  193. }
  194. if ($section)
  195. {
  196. $useajax=1;
  197. if (! empty($conf->dol_use_jmobile)) $useajax=0;
  198. if (empty($conf->use_javascript_ajax)) $useajax=0;
  199. if (! empty($conf->global->MAIN_ECM_DISABLE_JS)) $useajax=0;
  200. $param.=($param?'?':'').(preg_replace('/^&/','',$param));
  201. if ($useajax || $action == 'delete')
  202. {
  203. $urlfile='';
  204. if ($action == 'delete') $urlfile=GETPOST('urlfile');
  205. require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
  206. $useglobalvars=1;
  207. $form = new Form($db);
  208. $formquestion=array(
  209. 'urlfile'=>array('type'=>'hidden','value'=>$urlfile,'name'=>'urlfile'),
  210. 'section'=>array('type'=>'hidden','value'=>$section,'name'=>'section')
  211. );
  212. print $form->formconfirm($url,$langs->trans("DeleteFile"),$langs->trans("ConfirmDeleteFile"),'confirm_deletefile',$formquestion,"no",($useajax?'deletefile':0));
  213. }
  214. if ($useajax)
  215. {
  216. // Enable jquery handlers on new generated HTML objects
  217. print '<script type="text/javascript">'."\n";
  218. print 'jQuery(document).ready(function() {'."\n";
  219. print 'jQuery(".deletefilelink").click(function(e) { jQuery("#urlfile").val(jQuery(this).attr("rel")); jQuery("#dialog-confirm-deletefile").dialog("open"); return false; });'."\n";
  220. print '});'."\n";
  221. print '</script>'."\n";
  222. }
  223. }
  224. // Close db if mode is not noajax
  225. if ((! isset($mode) || $mode != 'noajax') && is_object($db)) $db->close();