dolibarr  17.0.4
wrapper.php
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 
12 $encoding = '';
13 
14 // Parameters to download files
15 $hashp = GETPOST('hashp', 'aZ09');
16 $modulepart = GETPOST('modulepart', 'aZ09');
17 $entity = GETPOST('entity', 'int') ?GETPOST('entity', 'int') : $conf->entity;
18 $original_file = GETPOST("file", "alpha");
19 $l = GETPOST('l', 'aZ09');
20 $limit = GETPOST('limit', 'int');
21 
22 // Parameters for RSS
23 $rss = GETPOST('rss', 'aZ09');
24 if ($rss) {
25  $original_file = 'blog.rss';
26 }
27 
28 // If we have a hash public (hashp), we guess the original_file.
29 if (!empty($hashp)) {
30  include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
31  $ecmfile = new EcmFiles($db);
32  $result = $ecmfile->fetch(0, '', '', '', $hashp);
33  if ($result > 0) {
34  $tmp = explode('/', $ecmfile->filepath, 2); // $ecmfile->filepath is relative to document directory
35  // filepath can be 'users/X' or 'X/propale/PR11111'
36  if (is_numeric($tmp[0])) { // If first tmp is numeric, it is subdir of company for multicompany, we take next part.
37  $tmp = explode('/', $tmp[1], 2);
38  }
39  $moduleparttocheck = $tmp[0]; // moduleparttocheck is first part of path
40 
41  if ($modulepart) { // Not required, so often not defined, for link using public hashp parameter.
42  if ($moduleparttocheck == $modulepart) {
43  // We remove first level of directory
44  $original_file = (($tmp[1] ? $tmp[1].'/' : '').$ecmfile->filename); // this is relative to module dir
45  //var_dump($original_file); exit;
46  } else {
47  print 'Bad link. File is from another module part.';
48  }
49  } else {
50  $modulepart = $moduleparttocheck;
51  $original_file = (($tmp[1] ? $tmp[1].'/' : '').$ecmfile->filename); // this is relative to module dir
52  }
53  } else {
54  print "ErrorFileNotFoundWithSharedLink";
55  exit;
56  }
57 }
58 
59 // Define attachment (attachment=true to force choice popup 'open'/'save as')
60 $attachment = true;
61 if (preg_match('/\.(html|htm)$/i', $original_file)) {
62  $attachment = false;
63 }
64 if (isset($_GET["attachment"])) {
65  $attachment = (GETPOST("attachment", 'alphanohtml') ? true : false);
66 }
67 if (!empty($conf->global->MAIN_DISABLE_FORCE_SAVEAS_WEBSITE)) {
68  $attachment = false;
69 }
70 
71 // Define mime type
72 $type = 'application/octet-stream';
73 if (GETPOSTISSET('type')) {
74  $type = GETPOST('type', 'alpha');
75 } else {
76  $type = dol_mimetype($original_file);
77 }
78 
79 // Security: Delete string ../ into $original_file
80 $original_file = str_replace("../", "/", $original_file);
81 
82 // Cache or not
83 if (GETPOST("cache", 'aZ09') || image_format_supported($original_file) >= 0) {
84  // Important: Following code is to avoid page request by browser and PHP CPU at
85  // each Dolibarr page access.
86  header('Cache-Control: max-age=3600, public, must-revalidate');
87  header('Pragma: cache'); // This is to avoid having Pragma: no-cache
88 }
89 
90 $refname = basename(dirname($original_file)."/");
91 
92 // Get RSS news
93 if ($rss) {
94  $format = 'rss';
95  $type = '';
96  $cachedelay = 0;
97  $filename = $original_file;
98  $dir_temp = $conf->website->dir_temp;
99 
100  include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
101  include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
102  $website = new Website($db);
103  $websitepage = new WebsitePage($db);
104 
105  $website->fetch('', $websitekey);
106 
107  $filters = array('type_container'=>'blogpost', 'status'=>1);
108  if ($l) {
109  $filters['lang'] = $l;
110  }
111 
112  $MAXNEWS = ($limit ? $limit : 20);
113  $arrayofblogs = $websitepage->fetchAll($website->id, 'DESC', 'date_creation', $MAXNEWS, 0, $filters);
114  $eventarray = array();
115  if (is_array($arrayofblogs)) {
116  foreach ($arrayofblogs as $blog) {
117  $blog->fullpageurl = $website->virtualhost.'/'.$blog->pageurl.'.php';
118  $eventarray[] = $blog;
119  }
120  }
121 
122  require_once DOL_DOCUMENT_ROOT."/core/lib/xcal.lib.php";
123  require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
124  require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
125 
126  dol_syslog("build_exportfile Build export file format=".$format.", type=".$type.", cachedelay=".$cachedelay.", filename=".$filename.", filters size=".count($filters), LOG_DEBUG);
127 
128  // Clean parameters
129  if (!$filename) {
130  $extension = 'rss';
131  $filename = $format.'.'.$extension;
132  }
133 
134  // Create dir and define output file (definitive and temporary)
135  $result = dol_mkdir($dir_temp);
136  $outputfile = $dir_temp.'/'.$filename;
137 
138  $result = 0;
139 
140  $buildfile = true;
141 
142  if ($cachedelay) {
143  $nowgmt = dol_now();
144  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
145  if (dol_filemtime($outputfile) > ($nowgmt - $cachedelay)) {
146  dol_syslog("build_exportfile file ".$outputfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay."). Build is canceled");
147  $buildfile = false;
148  }
149  }
150 
151  if ($buildfile) {
152  $outputlangs = new Translate('', $conf);
153  $outputlangs->setDefaultLang($l);
154  $outputlangs->loadLangs(array("main", "other"));
155  $title = $desc = $outputlangs->transnoentities('LatestBlogPosts');
156 
157  // Create temp file
158  $outputfiletmp = tempnam($dir_temp, 'tmp'); // Temporary file (allow call of function by different threads
159  @chmod($outputfiletmp, octdec($conf->global->MAIN_UMASK));
160 
161  // Write file
162  $result = build_rssfile($format, $title, $desc, $eventarray, $outputfiletmp, '', $website->virtualhost.'/wrapper.php?rss=1'.($l ? '&l='.$l : ''), $l);
163 
164  if ($result >= 0) {
165  if (dol_move($outputfiletmp, $outputfile, 0, 1, 0, 0)) {
166  $result = 1;
167  } else {
168  $error = 'Failed to rename '.$outputfiletmp.' into '.$outputfile;
169  dol_syslog("build_exportfile ".$error, LOG_ERR);
170  dol_delete_file($outputfiletmp, 0, 1);
171  print $error;
172  exit(-1);
173  }
174  } else {
175  dol_syslog("build_exportfile build_xxxfile function fails to for format=".$format." outputfiletmp=".$outputfile, LOG_ERR);
176  dol_delete_file($outputfiletmp, 0, 1);
177  $langs->load("errors");
178  print $langs->trans("ErrorFailToCreateFile", $outputfile);
179  exit(-1);
180  }
181  }
182 
183  if ($result >= 0) {
184  $attachment = false;
185  if (isset($_GET["attachment"])) {
186  $attachment = $_GET["attachment"];
187  }
188  //$attachment = false;
189  $contenttype = 'application/rss+xml';
190  if (isset($_GET["contenttype"])) {
191  $contenttype = $_GET["contenttype"];
192  }
193  //$contenttype='text/plain';
194  $outputencoding = 'UTF-8';
195 
196  if ($contenttype) {
197  header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
198  }
199  if ($attachment) {
200  header('Content-Disposition: attachment; filename="'.$filename.'"');
201  }
202 
203  // Ajout directives pour resoudre bug IE
204  //header('Cache-Control: Public, must-revalidate');
205  //header('Pragma: public');
206  if ($cachedelay) {
207  header('Cache-Control: max-age='.$cachedelay.', private, must-revalidate');
208  } else {
209  header('Cache-Control: private, must-revalidate');
210  }
211 
212  // Clean parameters
213  $outputfile = $dir_temp.'/'.$filename;
214  $result = readfile($outputfile);
215  if (!$result) {
216  print 'File '.$outputfile.' was empty.';
217  }
218 
219  // header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
220  exit;
221  }
222 } elseif ($modulepart == "mycompany" && preg_match('/^\/?logos\//', $original_file)) {
223  // Get logos
224  readfile(dol_osencode($conf->mycompany->dir_output."/".$original_file));
225 } else {
226  // Find the subdirectory name as the reference
227  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
228  $check_access = dol_check_secure_access_document($modulepart, $original_file, $entity, $refname);
229  $accessallowed = empty($check_access['accessallowed']) ? '' : $check_access['accessallowed'];
230  $sqlprotectagainstexternals = empty($check_access['sqlprotectagainstexternals']) ? '' : $check_access['sqlprotectagainstexternals'];
231  $fullpath_original_file = empty($check_access['original_file']) ? '' : $check_access['original_file']; // $fullpath_original_file is now a full path name
232  if ($hashp) {
233  $accessallowed = 1; // When using hashp, link is public so we force $accessallowed
234  $sqlprotectagainstexternals = '';
235  }
236 
237  // Security:
238  // Limit access if permissions are wrong
239  if (!$accessallowed) {
240  print 'Access forbidden';
241  exit;
242  }
243 
244  clearstatcache();
245 
246  $filename = basename($fullpath_original_file);
247 
248  // Output file on browser
249  dol_syslog("wrapper.php download $fullpath_original_file filename=$filename content-type=$type");
250  $fullpath_original_file_osencoded = dol_osencode($fullpath_original_file); // New file name encoded in OS encoding charset
251 
252  // This test if file exists should be useless. We keep it to find bug more easily
253  if (!file_exists($fullpath_original_file_osencoded)) {
254  print "ErrorFileDoesNotExists: ".$original_file;
255  exit;
256  }
257 
258  // Permissions are ok and file found, so we return it
259  //top_httphead($type);
260  header('Content-Type: '.$type);
261  header('Content-Description: File Transfer');
262  if ($encoding) {
263  header('Content-Encoding: '.$encoding);
264  }
265  // Add MIME Content-Disposition from RFC 2183 (inline=automatically displayed, attachment=need user action to open)
266  if ($attachment) {
267  header('Content-Disposition: attachment; filename="'.$filename.'"');
268  } else {
269  header('Content-Disposition: inline; filename="'.$filename.'"');
270  }
271  header('Content-Length: '.dol_filesize($fullpath_original_file));
272 
273  readfile($fullpath_original_file_osencoded);
274 }
275 if (is_object($db)) {
276  $db->close();
277 }
278 // END PHP
Class to manage ECM files.
Class to manage translations.
Class Website.
Class Websitepage.
dol_filemtime($pathoffile)
Return time of a file.
Definition: files.lib.php:597
dol_filesize($pathoffile)
Return size of a file.
Definition: files.lib.php:585
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
Definition: files.lib.php:1251
dol_check_secure_access_document($modulepart, $original_file, $entity, $fuser='', $refname='', $mode='read')
Security check when accessing to a document (used by document.php, viewimage.php and webservices to g...
Definition: files.lib.php:2448
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1)
Move a file into another name.
Definition: files.lib.php:875
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_now($mode='auto')
Return date for now.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
image_format_supported($file, $acceptsvg=0)
Return if a filename is file name of a supported image format.
Definition: images.lib.php:80
build_rssfile($format, $title, $desc, $events_array, $outputfile, $filter='', $url='', $langcode='')
Build a file from an array of events.
Definition: xcal.lib.php:311