forked from emaijala/MLInvoice
/
base_logo.php
147 lines (138 loc) · 5.18 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 5
*
* Copyright (C) 2010-2018 Ere Maijala
*
* 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';
sesVerifySession();
$func = getPostOrQuery('func', 'view');
$baseId = getPostOrQuery('id', null);
if (!isset($baseId) || !is_numeric($baseId) || !isset($func)) {
exit();
}
$messages = '';
if ($func == 'clear') {
dbParamQuery(
'UPDATE {prefix}base set logo_filename=null, logo_filesize=null, logo_filetype=null, logo_filedata=null WHERE id=?',
[$baseId]
);
$messages .= Translator::translate('BaseLogoErased') . "<br>\n";
} elseif ($func == 'upload') {
if ($_FILES['logo']['error'] != UPLOAD_ERR_OK) {
$messages .= Translator::translate('ErrFileUploadFailed') . "<br>\n";
} else {
$imageInfo = getimagesize($_FILES['logo']['tmp_name']);
if (!$imageInfo || !in_array($imageInfo['mime'], ['image/jpeg','image/png'])
) {
$messages .= Translator::translate('ErrFileTypeInvalid') . "<br>\n";
} 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
]
);
$messages .= Translator::translate('BaseLogoSaved') . ' (' .
fileSizeToHumanReadable($fsize) . ")<br>\n";
}
}
} 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);
}
echo htmlPageStart();
?>
<div class="form_container ui-widget-content base-logo">
<div class="message"><?php echo $messages?></div>
<div class="form ui-widget">
<div class="image-link">
<img class="image" src="?func=view&id=<?php echo $baseId?>">
</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">
<input name="logo" type="file">
</div>
<div class="form_buttons">
<input type="submit"
value="<?php echo Translator::translate('BaseSaveLogo')?>">
</div>
</form>
<form id="form_erase" enctype="multipart/form-data"
action="base_logo.php" method="POST">
<input type="hidden" name="func" value="clear"> <input type="hidden"
name="id" value="<?php echo $baseId?>">
<div class="form_buttons">
<input type="submit"
value="<?php echo Translator::translate('BaseEraseLogo')?>">
</div>
</form>
</div>
</div>
</body>
</html>