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