-
Notifications
You must be signed in to change notification settings - Fork 20
/
base_logo.php
147 lines (137 loc) · 5.26 KB
/
base_logo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* Logo handling
*
* PHP version 8
*
* Copyright (C) Ere Maijala 2010-2021
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category MLInvoice
* @package MLInvoice\Base
* @author Ere Maijala <ere@labs.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://labs.fi/mlinvoice.eng.php
*/
require_once 'sqlfuncs.php';
require_once 'miscfuncs.php';
require_once 'sessionfuncs.php';
require_once 'translator.php';
require_once 'htmlfuncs.php';
initDbConnection();
sesVerifySession();
$func = getPostOrQuery('func', 'view');
$baseId = getPostOrQuery('id', null);
if (!sesAdminAccess() || !isset($baseId) || !is_numeric($baseId) || !isset($func)) {
exit();
}
$message = '';
$error = '';
if ($func == 'clear') {
dbParamQuery(
'UPDATE {prefix}base set logo_filename=null, logo_filesize=null, logo_filetype=null, logo_filedata=null WHERE id=?',
[$baseId]
);
$message = Translator::translate('BaseLogoErased');
} elseif ($func == 'upload') {
if ($_FILES['logo']['error'] != UPLOAD_ERR_OK) {
$error = Translator::translate('ErrFileUploadFailed');
} else {
$imageInfo = getimagesize($_FILES['logo']['tmp_name']);
if (!$imageInfo || !in_array($imageInfo['mime'], ['image/jpeg','image/png'])
) {
$message = Translator::translate('ErrFileTypeInvalid');
} else {
$file = fopen($_FILES['logo']['tmp_name'], 'rb');
if ($file === false) {
die('Could not process file upload - temp file missing');
}
$fsize = filesize($_FILES['logo']['tmp_name']);
$data = fread($file, $fsize);
fclose($file);
dbParamQuery(
'UPDATE {prefix}base set logo_filename=?, logo_filesize=?, logo_filetype=?, logo_filedata=? WHERE id=?',
[
$_FILES['logo']['name'],
$fsize,
$imageInfo['mime'],
$data,
$baseId
]
);
$message = Translator::translate('BaseLogoSaved') . ' ('
. fileSizeToHumanReadable($fsize) . ')';
}
}
} elseif ($func == 'view') {
$rows = dbParamQuery(
'SELECT logo_filename, logo_filesize, logo_filetype, logo_filedata FROM {prefix}base WHERE id=?',
[
$baseId
]
);
if ($rows) {
$row = $rows[0];
if (isset($row['logo_filename']) && isset($row['logo_filesize'])
&& isset($row['logo_filetype']) && isset($row['logo_filedata'])
) {
header('Content-length: ' . $row['logo_filesize']);
header('Content-type: ' . $row['logo_filetype']);
header('Content-Disposition: inline; filename=' . $row['logo_filename']);
echo $row['logo_filedata'];
}
}
exit();
}
$maxUploadSize = getMaxUploadSize();
$res = dbQueryCheck('SELECT @@max_allowed_packet');
$maxPacket = dbFetchValue($res);
if ($maxPacket < $maxUploadSize) {
$maxFileSize = fileSizeToHumanReadable($maxPacket) . ' ' .
Translator::translate('BaseLogoSizeDBLimited');
} else {
$maxFileSize = fileSizeToHumanReadable($maxUploadSize);
}
$hasImage = getBaseLogoSize($baseId) > 0;
?>
<div class="form_container base-logo">
<?php if ($message) { ?>
<div class="alert alert-success" role="alert"><?php echo $message?></div>
<?php } ?>
<?php if ($error) { ?>
<div class="alert alert-danger" role="alert"><?php echo $error?></div>
<?php } ?>
<div class="form">
<div class="image-link">
<?php if ($hasImage) { ?>
<img class="image" src="base_logo.php?func=view&id=<?php echo $baseId?>">
<?php } else { ?>
<?php echo Translator::translate('BaseLogoNotSet')?>
<?php } ?>
</div>
<form id="form_upload" enctype="multipart/form-data" action="base_logo.php" method="POST">
<input type="hidden" name="func" value="upload">
<input type="hidden" name="id" value="<?php echo $baseId?>">
<div class="label file"><?php printf(Translator::translate('BaseLogo'), $maxFileSize)?></div>
<div class="long mb-2">
<input name="logo" type="file">
</div>
<input type="submit" class="btn btn-primary" value="<?php echo Translator::translate('BaseSaveLogo')?>">
<a href="base_logo.php?func=clear&id=<?php echo $baseId?>" role="button" class="btn btn-secondary<?php echo $hasImage ? '' : ' disabled'?>">
<?php echo Translator::translate('BaseEraseLogo')?>
</a>
</form>
</div>
</div>