wrapper.php 9.0 KB

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