dolibarr 24.0.1
wrapper.php
1<?php
2/* Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
3 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19// BEGIN PHP File wrapper.php used to download rss, logo, shared files - DO NOT MODIFY - It is just a copy of file website/samples/wrapper.php
20$websitekey = basename(__DIR__);
21if (strpos($_SERVER["PHP_SELF"], 'website/samples/wrapper.php')) {
22 die("Sample file for website module. Can't be called directly.");
23}
24if (!defined('USEDOLIBARRSERVER') && !defined('USEDOLIBARREDITOR')) {
25 require_once './master.inc.php';
26} // Load master if not already loaded
32include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
33
34$encoding = '';
35
36// Parameters to download files
37$hashp = GETPOST('hashp', 'aZ09');
38$extname = GETPOST('extname', 'alpha', 1);
39$modulepart = GETPOST('modulepart', 'aZ09');
40$entity = GETPOSTINT('entity') ? GETPOSTINT('entity') : $conf->entity;
41$original_file = GETPOST("file", "alpha");
42$l = GETPOST('l', 'aZ09');
43$limit = GETPOSTINT('limit');
44if ($limit <= 0 || $limit > 100) {
45 $limit = 20;
46}
47
48// Security check
49if ($hashp == 'shared') {
50 httponly_accessforbidden('Bad link. Bad value for parameter hashp', 400);
51}
52
53// Parameters for RSS
54$rss = GETPOST('rss', 'aZ09');
55if ($rss) {
56 $original_file = 'blog'.(($limit > 0 && $limit <= 100) ? '-'.$limit : '').(preg_match('/^[a-z][a-z](_[A-Z][A-Z])?$/', $l) ? '-'.$l : '').'-'.$websitekey.'.rss.cache';
57}
58
59// If we have a hash public (hashp), we guess the original_file.
60if (!empty($hashp) && $hashp != 'shared') {
61 include_once DOL_DOCUMENT_ROOT.'/ecm/class/ecmfiles.class.php';
62 include_once DOL_DOCUMENT_ROOT.'/core/lib/images.lib.php';
63 $ecmfile = new EcmFiles($db);
64 $result = $ecmfile->fetch(0, '', '', '', $hashp);
65 if ($result > 0) {
66 $tmp = explode('/', $ecmfile->filepath, 2); // $ecmfile->filepath is relative to document directory
67 // filepath can be 'users/X' or 'X/propale/PR11111'
68 if (is_numeric($tmp[0])) { // If first tmp is numeric, it is subdir of company for multicompany, we take next part.
69 $tmp = explode('/', $tmp[1], 2);
70 }
71 $moduleparttocheck = $tmp[0]; // moduleparttocheck is first part of path
72
73 if ($modulepart) { // Not required, so often not defined, for link using public hashp parameter.
74 if ($moduleparttocheck == $modulepart) {
75 // We remove first level of directory
76 $original_file = (($tmp[1] ? $tmp[1].'/' : '').$ecmfile->filename); // this is relative to module dir
77 //var_dump($original_file); exit(0);
78 } else {
79 // Security options
80
81 // X-Content-Type-Options
82 header("X-Content-Type-Options: nosniff");
83
84 // X-Frame-Options
85 if (!getDolGlobalString('WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES')) {
86 header("X-Frame-Options: SAMEORIGIN");
87 }
88
89 http_response_code(401);
90 print 'Bad link. File is from another module part.';
91 exit(1);
92 }
93 } else {
94 $modulepart = $moduleparttocheck;
95 $original_file = (($tmp[1] ? $tmp[1].'/' : '').$ecmfile->filename); // this is relative to module dir
96 }
97
98 if ($extname) {
99 $original_file = getImageFileNameForSize($original_file, $extname);
100 }
101 } else {
102 // Security options
103
104 // X-Content-Type-Options
105 header("X-Content-Type-Options: nosniff");
106
107 // X-Frame-Options
108 if (!getDolGlobalString('WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES')) {
109 header("X-Frame-Options: SAMEORIGIN");
110 }
111
112 http_response_code(404);
113 print "ErrorFileNotFoundWithSharedLink";
114 exit(2);
115 }
116}
117
118// Define attachment (attachment=true to force choice popup 'open'/'save as')
119$attachment = true;
120if (preg_match('/\.(html|htm)$/i', $original_file)) {
121 $attachment = false;
122}
123if (GETPOSTISSET("attachment")) {
124 $attachment = (GETPOST("attachment", 'alphanohtml') ? true : false);
125}
126if (getDolGlobalString('MAIN_DISABLE_FORCE_SAVEAS_WEBSITE')) {
127 $attachment = false;
128}
129
130// Define mime type
131$type = 'application/octet-stream';
132if (GETPOSTISSET('type')) {
133 $type = GETPOST('type', 'alpha');
134} else {
135 $type = dol_mimetype($original_file);
136}
137
138// Security: Delete string ../ into $original_file
139$original_file = str_replace("../", "/", $original_file);
140
141
142// Cache or not
143$cachestring = GETPOST("cache", 'aZ09'); // May be 1, or an int (delay in second of the cache if < 999999, or a timestamp), or a hash
144$cachedelay = GETPOSTINT('cachedelay') ? GETPOSTINT('cachedelay') : ((is_numeric($cachestring) && (int) $cachestring > 1 && (int) $cachestring < 999999) ? $cachestring : '3600');
145if ($cachestring || image_format_supported($original_file) >= 0) {
146 // Important: Following code is to avoid page request by browser and PHP CPU at each Dolibarr page access.
147 header('Cache-Control: max-age='.$cachedelay.', public, must-revalidate');
148 header('Pragma: cache'); // This is to avoid having Pragma: no-cache
149 header('Expires: '.gmdate('D, d M Y H:i:s', time() + (int) $cachedelay).' GMT'); // This is to avoid to have Expires set by proxy or web server
150}
151
152$refname = basename(dirname($original_file)."/");
153
154// Get RSS news
155if ($rss) {
156 $format = 'rss';
157 $type = '';
158 $filename = $original_file;
159 $dir_temp = (string) $conf->website->dir_temp;
160
161 include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
162 include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
163 $website = new Website($db);
164 $websitepage = new WebsitePage($db);
165
166 $website->fetch(0, $websitekey);
167
168 $filters = array('type_container' => 'blogpost', 'status' => '1');
169 if ($l) {
170 $filters['lang'] = (string) $l;
171 }
172
173 $MAXNEWS = $limit;
174 $arrayofblogs = $websitepage->fetchAll($website->id, 'DESC', 'date_creation', $MAXNEWS, 0, $filters);
175 $eventarray = array();
176 if (is_array($arrayofblogs)) {
177 foreach ($arrayofblogs as $blog) {
178 $blog->fullpageurl = $website->virtualhost.'/'.$blog->pageurl.'.php';
179 $blog->image = preg_replace('/__WEBSITE_KEY__/', $websitekey, $blog->image);
180
181 $eventarray[] = $blog;
182 }
183 }
184
185 require_once DOL_DOCUMENT_ROOT."/core/lib/xcal.lib.php";
186 require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
187 require_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
188
189 dol_syslog("build_exportfile Build export file format=".$format.", type=".$type.", cachestring=".$cachestring.", filename=".$filename.", filters size=".count($filters), LOG_DEBUG);
190
191 // Clean parameters
192 if (!$filename) {
193 $extension = 'rss';
194 $filename = $format.'.'.$extension;
195 }
196
197 // Create dir and define output file (definitive and temporary)
198 $result = dol_mkdir($dir_temp);
199 $outputfile = $dir_temp.'/'.$filename;
200
201 $result = 0;
202
203 $buildfile = true;
204
205 if ($cachedelay) {
206 $nowgmt = dol_now();
207 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
208 if (dol_filemtime($outputfile) > ($nowgmt - $cachedelay)) {
209 dol_syslog("build_exportfile file ".$outputfile." is not older than now - cachedelay (".$nowgmt." - ".$cachedelay."). Build is canceled");
210 $buildfile = false;
211 }
212 }
213
214 if ($buildfile) {
215 $outputlangs = new Translate('', $conf);
216 $outputlangs->setDefaultLang($l);
217 $outputlangs->loadLangs(array("main", "other"));
218 $title = $outputlangs->transnoentities('LatestBlogPosts').' - '.$website->virtualhost;
219 $desc = $title.($l ? ' ('.$l.')' : '');
220
221 // Create temp file
222 $outputfiletmp = tempnam($dir_temp, 'tmp'); // Temporary file (allow call of function by different threads
223 dolChmod($outputfiletmp);
224
225 // Write file
226 $result = build_rssfile($format, $title, $desc, $eventarray, $outputfiletmp, '', $website->virtualhost.'/wrapper.php?rss=1'.($l ? '&l='.$l : ''), $l);
227
228 if ($result >= 0) {
229 if (dol_move($outputfiletmp, $outputfile, '0', 1, 0, 0)) {
230 $result = 1;
231 } else {
232 $error = 'Failed to rename '.$outputfiletmp.' into '.$outputfile;
233 dol_syslog("build_exportfile ".$error, LOG_ERR);
234 dol_delete_file($outputfiletmp, 0, 1);
235
236 http_response_code(500);
237 print $error;
238 exit(3);
239 }
240 } else {
241 dol_syslog("build_exportfile build_xxxfile function fails to for format=".$format." outputfiletmp=".$outputfile, LOG_ERR);
242 dol_delete_file($outputfiletmp, 0, 1);
243 $langs->load("errors");
244
245 http_response_code(500);
246 print $langs->trans("ErrorFailToCreateFile", $outputfile);
247 exit(4);
248 }
249 }
250
251 $attachment = false;
252 if (GETPOSTISSET("attachment")) {
253 $attachment = GETPOST("attachment");
254 }
255 //$attachment = false;
256 $contenttype = 'application/rss+xml';
257 if (GETPOSTISSET("contenttype")) {
258 $contenttype = GETPOST("contenttype");
259 }
260 //$contenttype='text/plain';
261 $outputencoding = 'UTF-8';
262
263 if ($contenttype) {
264 header('Content-Type: '.$contenttype.($outputencoding ? '; charset='.$outputencoding : ''));
265 }
266 if ($attachment) {
267 header('Content-Disposition: attachment; filename="'.$filename.'"');
268 }
269
270 // Ajout directives pour resoudre bug IE
271 //header('Cache-Control: Public, must-revalidate');
272 //header('Pragma: public');
273 if ($cachedelay) {
274 header('Cache-Control: max-age='.$cachedelay.', private, must-revalidate');
275 } else {
276 header('Cache-Control: private, must-revalidate');
277 }
278
279 // Clean parameters
280 $outputfile = $dir_temp.'/'.$filename;
281 $result = readfile($outputfile);
282 if (!$result) {
283 print 'File '.$outputfile.' was empty.';
284 }
285
286 // header("Location: ".DOL_URL_ROOT.'/document.php?modulepart=agenda&file='.urlencode($filename));
287 exit(5);
288} elseif ($modulepart == "mycompany" && preg_match('/^\/?logos\//', $original_file)) {
289 // Get logos
290 readfile(dol_osencode($conf->mycompany->dir_output."/".$original_file));
291} else {
292 // Find the subdirectory name as the reference
293 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
294 $check_access = dol_check_secure_access_document($modulepart, $original_file, $entity, null, $refname);
295 $accessallowed = empty($check_access['accessallowed']) ? '' : $check_access['accessallowed'];
296 $sqlprotectagainstexternals = empty($check_access['sqlprotectagainstexternals']) ? '' : $check_access['sqlprotectagainstexternals'];
297 $fullpath_original_file = empty($check_access['original_file']) ? '' : $check_access['original_file']; // $fullpath_original_file is now a full path name
298 if (!empty($hashp) && $hashp != 'shared') {
299 $accessallowed = 1; // When using hashp, link is public so we force $accessallowed
300 $sqlprotectagainstexternals = '';
301 }
302
303 // Security:
304 // Limit access if permissions are wrong
305 if (!$accessallowed) {
306 http_response_code(403);
307 print 'Access forbidden';
308 exit(6);
309 }
310
311 // For backward compatibility of old thumbs that were created with filename in lower case and with .png extension
312 if (image_format_supported($fullpath_original_file) && !dol_is_file($fullpath_original_file)) {
313 $fullpath_original_file = getImageFileNameForSize($fullpath_original_file, '', '.png');
314 }
315
316 clearstatcache();
317
318 $filename = basename($fullpath_original_file);
319
320 // Output file on browser
321 dol_syslog("wrapper.php download $fullpath_original_file filename=$filename content-type=$type");
322 $fullpath_original_file_osencoded = dol_osencode($fullpath_original_file); // New file name encoded in OS encoding charset
323
324 // This test if file exists should be useless. We keep it to find bug more easily
325 if (!file_exists($fullpath_original_file_osencoded)) {
326 http_response_code(404);
327 print "ErrorFileDoesNotExists: ".dol_escape_htmltag($original_file);
328 exit(7);
329 }
330
331 // Permissions are ok and file found, so we return it
332 //top_httphead($type);
333 header('Content-Type: '.$type);
334 header('Content-Description: File Transfer');
335 if ($encoding) {
336 header('Content-Encoding: '.$encoding);
337 }
338 // Add MIME Content-Disposition from RFC 2183 (inline=automatically displayed, attachment=need user action to open)
339 if ($attachment) {
340 header('Content-Disposition: attachment; filename="'.$filename.'"');
341 } else {
342 header('Content-Disposition: inline; filename="'.$filename.'"');
343 }
344 header('Content-Length: '.dol_filesize($fullpath_original_file));
345
346 readfile($fullpath_original_file_osencoded);
347}
348if (is_object($db)) {
349 $db->close();
350}
351// END PHP
Class to manage ECM files.
Class to manage translations.
Class Website.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_move($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array(), $entity=null)
Move a file into another name.
dol_filemtime($pathoffile)
Return time of a file.
dol_filesize($pathoffile)
Return size of a file.
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.
dol_check_secure_access_document($modulepart, $original_file, $entity, $fuser=null, $refname='', $mode='read')
Security check when accessing to a document (used by document.php, viewimage.php and webservices to g...
dol_is_file($pathoffile)
Return if path is a file.
dol_now($mode='gmt')
Return date for now.
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.
dolChmod($filepath, $newmask='')
Change mod of a file.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
getImageFileNameForSize($file, $extName, $extImgTarget='')
Return the filename of file to get the thumbs.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
httponly_accessforbidden($message='1', $http_response_code=403, $stringalreadysanitized=0)
Show a message to say access is forbidden and stop program.
build_rssfile($format, $title, $desc, $events_array, $outputfile, $filter='', $url='', $langcode='', $useurlforguid=false)
Build a file from an array of events.
Definition xcal.lib.php:325