wrapper.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. // BEGIN PHP File wrapper.php - DO NOT MODIFY - It is just a copy of file website/samples/wrapper.php
  3. $websitekey = basename(__DIR__);
  4. if (strpos($_SERVER["PHP_SELF"], 'website/samples/wrapper.php')) {
  5. die("Sample file for website module. Can be called directly.");
  6. }
  7. if (!defined('USEDOLIBARRSERVER') && !defined('USEDOLIBARREDITOR')) {
  8. require_once './master.inc.php';
  9. } // Load master if not already loaded
  10. include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
  11. $encoding = '';
  12. // Parameters to download files
  13. $hashp = GETPOST('hashp', 'aZ09');
  14. $modulepart = GETPOST('modulepart', 'aZ09');
  15. $entity = GETPOST('entity', 'int') ?GETPOST('entity', 'int') : $conf->entity;
  16. $original_file = GETPOST("file", "alpha");
  17. $l = GETPOST('l', 'aZ09');
  18. $limit = GETPOST('limit', 'int');
  19. // Parameters for RSS
  20. $rss = GETPOST('rss', 'aZ09');
  21. if ($rss) {
  22. $original_file = 'blog.rss';
  23. }
  24. // If we have a hash public (hashp), we guess the original_file.
  25. if (!empty($hashp)) {
  26. include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
  27. $ecmfile = new EcmFiles($db);
  28. $result = $ecmfile->fetch(0, '', '', '', $hashp);
  29. if ($result > 0) {
  30. $tmp = explode('/', $ecmfile->filepath, 2); // $ecmfile->filepath is relative to document directory
  31. // filepath can be 'users/X' or 'X/propale/PR11111'
  32. if (is_numeric($tmp[0])) { // If first tmp is numeric, it is subdir of company for multicompany, we take next part.
  33. $tmp = explode('/', $tmp[1], 2);
  34. }
  35. $moduleparttocheck = $tmp[0]; // moduleparttocheck is first part of path
  36. if ($modulepart) { // Not required, so often not defined, for link using public hashp parameter.
  37. if ($moduleparttocheck == $modulepart) {
  38. // We remove first level of directory
  39. $original_file = (($tmp[1] ? $tmp[1].'/' : '').$ecmfile->filename); // this is relative to module dir
  40. //var_dump($original_file); exit;
  41. } else {
  42. print 'Bad link. File is from another module part.';
  43. }
  44. } else {
  45. $modulepart = $moduleparttocheck;
  46. $original_file = (($tmp[1] ? $tmp[1].'/' : '').$ecmfile->filename); // this is relative to module dir
  47. }
  48. } else {
  49. print "ErrorFileNotFoundWithSharedLink";
  50. exit;
  51. }
  52. }
  53. // Define attachment (attachment=true to force choice popup 'open'/'save as')
  54. $attachment = true;
  55. if (preg_match('/\.(html|htm)$/i', $original_file)) {
  56. $attachment = false;
  57. }
  58. if (isset($_GET["attachment"])) {
  59. $attachment = (GETPOST("attachment", 'alphanohtml') ? true : false);
  60. }
  61. if (!empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS_WEBSITE)) {
  62. $attachment = false;
  63. }
  64. // Define mime type
  65. $type = 'application/octet-stream';
  66. if (GETPOSTISSET('type')) {
  67. $type = GETPOST('type', 'alpha');
  68. } else {
  69. $type = dol_mimetype($original_file);
  70. }
  71. // Security: Delete string ../ into $original_file
  72. $original_file = str_replace("../", "/", $original_file);
  73. // Cache or not
  74. if (GETPOST("cache", 'aZ09') || image_format_supported($original_file) >= 0) {
  75. // Important: Following code is to avoid page request by browser and PHP CPU at
  76. // each Dolibarr page access.
  77. header('Cache-Control: max-age=3600, public, must-revalidate');
  78. header('Pragma: cache'); // This is to avoid having Pragma: no-cache
  79. }
  80. $refname = basename(dirname($original_file)."/");
  81. // Get RSS news
  82. if ($rss) {
  83. $format = 'rss';
  84. $type = '';
  85. $cachedelay = 0;
  86. $filename = $original_file;
  87. $dir_temp = $conf->website->dir_temp;
  88. include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
  89. include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
  90. $website = new Website($db);
  91. $websitepage = new WebsitePage($db);
  92. $website->fetch('', $websitekey);
  93. $filters = array('type_container'=>'blogpost', 'status'=>1);
  94. if ($l) {
  95. $filters['lang'] = $l;
  96. }
  97. $MAXNEWS = ($limit ? $limit : 20);
  98. $arrayofblogs = $websitepage->fetchAll($website->id, 'DESC', 'date_creation', $MAXNEWS, 0, $filters);
  99. $eventarray = array();
  100. if (is_array($arrayofblogs)) {
  101. foreach ($arrayofblogs as $blog) {
  102. $blog->fullpageurl = $website->virtualhost.'/'.$blog->pageurl.'.php';
  103. $eventarray[] = $blog;
  104. }
  105. }
  106. require_once DOL_DOCUMENT_ROOT."/core/lib/xcal.lib.php";
  107. require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
  108. require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
  109. dol_syslog("build_exportfile Build export file format=".$format.", type=".$type.", cachedelay=".$cachedelay.", filename=".$filename.", filters size=".count($filters), LOG_DEBUG);
  110. // Clean parameters
  111. if (!$filename) {
  112. $extension = 'rss';
  113. $filename = $format.'.'.$extension;
  114. }
  115. // Create dir and define output file (definitive and temporary)
  116. $result = dol_mkdir($dir_temp);
  117. $outputfile = $dir_temp.'/'.$filename;
  118. $result = 0;
  119. $buildfile = true;
  120. if ($cachedelay) {
  121. $nowgmt = dol_now();
  122. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  123. if (dol_filemtime($outputfile) > ($nowgmt - $cachedelay)) {
  124. dol_syslog("build_exportfile file ".$outputfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay."). Build is canceled");
  125. $buildfile = false;
  126. }
  127. }
  128. if ($buildfile) {
  129. $outputlangs = new Translate('', $conf);
  130. $outputlangs->setDefaultLang($l);
  131. $outputlangs->loadLangs(array("main", "other"));
  132. $title = $desc = $outputlangs->transnoentities('LatestBlogPosts');
  133. // Create temp file
  134. $outputfiletmp = tempnam($dir_temp, 'tmp'); // Temporary file (allow call of function by different threads
  135. @chmod($outputfiletmp, octdec($conf->global->MAIN_UMASK));
  136. // Write file
  137. $result = build_rssfile($format, $title, $desc, $eventarray, $outputfiletmp, '', $website->virtualhost.'/wrapper.php?rss=1'.($l ? '&l='.$l : ''), $l);
  138. if ($result >= 0) {
  139. if (dol_move($outputfiletmp, $outputfile, 0, 1, 0, 0)) {
  140. $result = 1;
  141. } else {
  142. $error = 'Failed to rename '.$outputfiletmp.' into '.$outputfile;
  143. dol_syslog("build_exportfile ".$error, LOG_ERR);
  144. dol_delete_file($outputfiletmp, 0, 1);
  145. print $error;
  146. exit(-1);
  147. }
  148. } else {
  149. dol_syslog("build_exportfile build_xxxfile function fails to for format=".$format." outputfiletmp=".$outputfile, LOG_ERR);
  150. dol_delete_file($outputfiletmp, 0, 1);
  151. $langs->load("errors");
  152. print $langs->trans("ErrorFailToCreateFile", $outputfile);
  153. exit(-1);
  154. }
  155. }
  156. if ($result >= 0) {
  157. $attachment = false;
  158. if (isset($_GET["attachment"])) {
  159. $attachment = $_GET["attachment"];
  160. }
  161. //$attachment = false;
  162. $contenttype = 'application/rss+xml';
  163. if (isset($_GET["contenttype"])) {
  164. $contenttype = $_GET["contenttype"];
  165. }
  166. //$contenttype='text/plain';
  167. $outputencoding = 'UTF-8';
  168. if ($contenttype) {
  169. header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
  170. }
  171. if ($attachment) {
  172. header('Content-Disposition: attachment; filename="'.$filename.'"');
  173. }
  174. // Ajout directives pour resoudre bug IE
  175. //header('Cache-Control: Public, must-revalidate');
  176. //header('Pragma: public');
  177. if ($cachedelay) {
  178. header('Cache-Control: max-age='.$cachedelay.', private, must-revalidate');
  179. } else {
  180. header('Cache-Control: private, must-revalidate');
  181. }
  182. // Clean parameters
  183. $outputfile = $dir_temp.'/'.$filename;
  184. $result = readfile($outputfile);
  185. if (!$result) {
  186. print 'File '.$outputfile.' was empty.';
  187. }
  188. // header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
  189. exit;
  190. }
  191. } elseif ($modulepart == "mycompany" && preg_match('/^\/?logos\//', $original_file)) {
  192. // Get logos
  193. readfile(dol_osencode($conf->mycompany->dir_output."/".$original_file));
  194. } else {
  195. // Find the subdirectory name as the reference
  196. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  197. $check_access = dol_check_secure_access_document($modulepart, $original_file, $entity, $refname);
  198. $accessallowed = empty($check_access['accessallowed']) ? '' : $check_access['accessallowed'];
  199. $sqlprotectagainstexternals = empty($check_access['sqlprotectagainstexternals']) ? '' : $check_access['sqlprotectagainstexternals'];
  200. $fullpath_original_file = empty($check_access['original_file']) ? '' : $check_access['original_file']; // $fullpath_original_file is now a full path name
  201. if ($hashp) {
  202. $accessallowed = 1; // When using hashp, link is public so we force $accessallowed
  203. $sqlprotectagainstexternals = '';
  204. }
  205. // Security:
  206. // Limit access if permissions are wrong
  207. if (!$accessallowed) {
  208. print 'Access forbidden';
  209. exit;
  210. }
  211. clearstatcache();
  212. $filename = basename($fullpath_original_file);
  213. // Output file on browser
  214. dol_syslog("wrapper.php download $fullpath_original_file filename=$filename content-type=$type");
  215. $fullpath_original_file_osencoded = dol_osencode($fullpath_original_file); // New file name encoded in OS encoding charset
  216. // This test if file exists should be useless. We keep it to find bug more easily
  217. if (!file_exists($fullpath_original_file_osencoded)) {
  218. print "ErrorFileDoesNotExists: ".$original_file;
  219. exit;
  220. }
  221. // Permissions are ok and file found, so we return it
  222. //top_httphead($type);
  223. header('Content-Type: '.$type);
  224. header('Content-Description: File Transfer');
  225. if ($encoding) {
  226. header('Content-Encoding: '.$encoding);
  227. }
  228. // Add MIME Content-Disposition from RFC 2183 (inline=automatically displayed, attachment=need user action to open)
  229. if ($attachment) {
  230. header('Content-Disposition: attachment; filename="'.$filename.'"');
  231. } else {
  232. header('Content-Disposition: inline; filename="'.$filename.'"');
  233. }
  234. header('Content-Length: '.dol_filesize($fullpath_original_file));
  235. readfile($fullpath_original_file_osencoded);
  236. }
  237. if (is_object($db)) {
  238. $db->close();
  239. }
  240. // END PHP