Debian Codegraph @

templates/misc/download.php

raw
1 <?php
2 namespace LAM\DOWNLOAD;
3 use LAMException;
4 use LamTemporaryFilesManager;
5
6 /*
7
8 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
9 Copyright (C) 2023 - 2025 Roland Gruber
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
25 */
26
27 /**
28 * Manages download of files.
29 *
30 * @author Roland Gruber
31 * @package misc
32 */
33
34 /** security functions */
35 include_once(__DIR__ . "/../../lib/security.inc");
36
37 // start session
38 if (isset($_GET['selfservice'])) {
39 // self-service uses a different session name
40 session_name('SELFSERVICE');
41 }
42
43 // return 403 if session expired
44 if (startSecureSession(false, true) === false) {
45 http_response_code(403);
46 die();
47 }
48
49 setlanguage();
50
51 $temporaryFilesManager = new LamTemporaryFilesManager();
52 $fileParamName = 'file';
53
54 if (empty($_GET[$fileParamName]) || !$temporaryFilesManager->isRegisteredFile($_GET[$fileParamName])) {
55 http_response_code(403);
56 die();
57 }
58
59 try {
60 $fileName = $_GET[$fileParamName];
61 $handle = $temporaryFilesManager->openTemporaryFileForRead($fileName);
62 $extension = substr($fileName, strrpos($fileName, '.') + 1);
63 setMimeType($extension);
64 if ((isset($_GET['download']) && ($_GET['download'] === 'true'))
65 || !$temporaryFilesManager->isAcceptableResourceLinkExtension($extension)) {
66 header('content-disposition: attachment; filename="' . $fileName . '"');
67 }
68 $content = fread($handle, 100000);
69 while (($content !== false) && ($content !== '')) {
70 echo $content;
71 $content = fread($handle, 100000);
72 }
73 } catch (LAMException $e) {
74 logNewMessage(LOG_ERR, 'Unable to open file ' . $fileName . ': ' . $e->getMessage());
75 http_response_code(403);
76 die();
77 }
78
79 /**
80 * Sets the mime type.
81 *
82 * @param string $extension file extension
83 */
84 function setMimeType(string $extension): void {
85 if (headers_sent()) {
86 return;
87 }
88 $mimeType = null;
89 switch ($extension) {
90 case 'crt':
91 $mimeType = 'application/x-x509-user-cert';
92 break;
93 case 'csv':
94 $mimeType = 'text/csv; charset=UTF-8';
95 break;
96 case 'jpg':
97 $mimeType = 'image/jpeg';
98 break;
99 case 'ldif':
100 case 'pem':
101 $mimeType = 'text/plain; charset=UTF-8';
102 break;
103 case 'svg':
104 $mimeType = 'image/svg+xml';
105 break;
106 case 'zip':
107 $mimeType = 'application/zip';
108 break;
109 }
110 if ($mimeType !== null) {
111 header('Content-Type: ' . $mimeType);
112 }
113 }