-
Notifications
You must be signed in to change notification settings - Fork 35
/
generate_sig.php
221 lines (189 loc) · 7.75 KB
/
generate_sig.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Note: if you wish to serve error image on lack of permission to the cache dir,
* set this variable to `true`.
*/
$FAIL_ON_UNAVAILABLE_CACHE = false;
function ReturnImage($ImagePath) {
if (!$ImagePath) {
die();
}
if (
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
(strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= filemtime($ImagePath))
) {
// Use Browser Cache
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($ImagePath)).' GMT', true, 304);
return die();
}
// Resend new version
header('Content-Type: image/png');
header('Content-Length: '.filesize($ImagePath));
header('Last-Modified: '.gmdate('D, d M Y H:i:s', filemtime($ImagePath)).' GMT', true, 200);
$Image = fopen($ImagePath, 'r');
fpassthru($Image);
die();
}
$_EnginePath = './';
define('INSIDE', true);
include($_EnginePath . 'common.minimal.php');
include($_EnginePath . 'common/_includes.php');
include($_EnginePath . 'includes/constants.php');
include($_EnginePath . 'includes/unlocalised.php');
include($_EnginePath . 'includes/helpers/_includes.php');
$UID = (isset($_GET['uid']) ? round($_GET['uid']) : 0);
$SigLang = (isset($_GET['lang']) ? $_GET['lang'] : null);
if (!in_array($SigLang, UNIENGINE_LANGS_AVAILABLE)) {
$SigLang = getDefaultUniLang();
}
$CachePath = './cache/img/signatures';
$CacheStaticsPath = "{$CachePath}/static";
$CacheLangPath = "{$CachePath}/{$SigLang}";
if($UID > 0)
{
$UserFile = "{$CacheLangPath}/user_".str_pad($UID, 6, 0, STR_PAD_LEFT).'.png';
if(file_exists($UserFile))
{
if(file_exists('./cache/data/last_stats_update.php'))
{
include('./cache/data/last_stats_update.php');
if(!isset($LastStatsUpdate))
{
$LastStatsUpdate = 0;
}
}
else
{
$LastStatsUpdate = 0;
}
if(filemtime($UserFile) >= $LastStatsUpdate)
{
// Return existing image
ReturnImage($UserFile);
}
}
// --- Generate new image ---
// Load DB Driver & Lang
/**
* TODO: this assumes that we need the directory to be writeable to "everyone",
* where writeable to owner or group might be sufficient. Fix that in the future
* to relax access requirements. This should also be changed to improve security.
*/
$UNIX_EVERYONE_WRITE_PERMISSION = 0x002;
if (!is_dir($CacheLangPath)) {
mkdir($CacheLangPath, 0755, true);
}
$isCacheDirWriteable = fileperms($CacheLangPath) & $UNIX_EVERYONE_WRITE_PERMISSION;
if (
!$isCacheDirWriteable &&
$FAIL_ON_UNAVAILABLE_CACHE
) {
ReturnImage("{$CacheStaticsPath}/signature_{$SigLang}_error4.png");
}
include("{$_EnginePath}includes/db.php");
include("{$_EnginePath}language/{$SigLang}/generate_sig.lang");
$Constants = array
(
'UserYTop' => 10,
'AllyYTop' => 34,
'UniPosYTop' => 3,
'Width' => 468,
'Padding' => 6,
);
// Get Data
$SQLResult_GetUserData = doquery("SELECT `username`, `ally_id` FROM {{table}} WHERE `id` = {$UID} LIMIT 1;", 'users');
if($SQLResult_GetUserData->num_rows != 1)
{
// Throw Error: User don't exist
copy("{$CacheStaticsPath}/signature_{$SigLang}_error2.png", $UserFile);
ReturnImage($UserFile);
}
$UserData = $SQLResult_GetUserData->fetch_assoc();
$UserStat = doquery("SELECT `total_rank`, `total_points` FROM {{table}} WHERE `id_owner` = {$UID} AND `stat_type` = 1 LIMIT 1;", 'statpoints', true);
if($UserStat['total_rank'] <= 0)
{
// Throw Error: Stats not calculated
copy("{$CacheStaticsPath}/signature_{$SigLang}_error3.png", $UserFile);
ReturnImage($UserFile);
}
// Generate Texts
if(strlen($UserData['username']) > 20)
{
$UserData['username'] = substr($UserData['username'], 0, 20).'...';
}
$Texts['User'] = $UserData['username'];
$Texts['Points'] = number_format($UserStat['total_points'], 0, '', '.').' '.$_Lang['Points'];
$Texts['Position'] = $_Lang['Position'].': '.number_format($UserStat['total_rank'], 0, '', '.');
$Texts['Uni'] = $_Lang['Universum'];
$Texts['Ally'] = null;
if($UserData['ally_id'] > 0)
{
$UserAlly = doquery("SELECT `ally_name`, `ally_tag` FROM {{table}} WHERE `id` = {$UserData['ally_id']} LIMIT 1;", 'alliance', true);
if(!empty($UserAlly['ally_tag']))
{
$Texts['Ally'] = "{$_Lang['Ally']}: {$UserAlly['ally_tag']}";
}
}
foreach($Texts as $ThisKey => &$ThisValue)
{
if($ThisKey == 'Uni')
{
continue;
}
$ThisValue = strtoupper($ThisValue);
}
// Load image and create White Color
$ImageCopy = imagecreatefrompng("{$CacheStaticsPath}/signature_userbg.png");
$FontLink = "{$CacheStaticsPath}/font_roboto_bold.ttf";
$Colors['white'] = imagecolorallocate($ImageCopy, 255, 255, 255);
// Calculate UserString Size
$Size = imagettfbbox(10, 0, $FontLink, $Texts['User']);
$Width = $Size[4] - $Size[0];
$Height = ($Size[5] - $Size[1]) * -1;
$CalcYPos['User'] = $Constants['UserYTop'] + $Height + 1;
// Calculate AllyString Size
$Size = imagettfbbox(10, 0, $FontLink, $Texts['Ally']);
$Width = $Size[4] - $Size[0];
$Height = ($Size[5] - $Size[1]) * -1;
$CalcYPos['Ally'] = $Constants['AllyYTop'] + $Height;
// Calculate PointsString Size
$Size = imagettfbbox(10, 0, $FontLink, $Texts['Points']);
$Width = $Size[4] - $Size[0];
$Height = ($Size[5] - $Size[1]) * -1;
$CalcYPos['Points'] = $Constants['UserYTop'] + $Height + 1;
$CalcXPos['Points'] = $Constants['Width'] - $Width - $Constants['Padding'];
// Calculate PositionString Size
$Size = imagettfbbox(10, 0, $FontLink, $Texts['Position']);
$Width = $Size[4] - $Size[0];
$Height = ($Size[5] - $Size[1]) * -1;
$CalcYPos['Position'] = $Constants['AllyYTop'] + $Height;
$CalcXPos['Position'] = $Constants['Width'] - $Width - $Constants['Padding'];
// Calculate UniString Size
$Size = imagettfbbox(10, 0, $FontLink, $Texts['Uni']);
$Width = $Size[4] - $Size[0];
$Height = ($Size[5] - $Size[1]) * -1;
$CalcYPos['Uni'] = $Constants['UniPosYTop'] + $Height;
$CalcXPos['Uni'] = ($Constants['Width'] - $Width) / 2;
// Insert Texts
imagettftext($ImageCopy, 10, 0, $Constants['Padding'], $CalcYPos['User'], $Colors['white'], $FontLink, $Texts['User']);
imagettftext($ImageCopy, 10, 0, $Constants['Padding'], $CalcYPos['Ally'], $Colors['white'], $FontLink, $Texts['Ally']);
imagettftext($ImageCopy, 10, 0, $CalcXPos['Points'], $CalcYPos['Points'], $Colors['white'], $FontLink, $Texts['Points']);
imagettftext($ImageCopy, 10, 0, $CalcXPos['Position'], $CalcYPos['Position'], $Colors['white'], $FontLink, $Texts['Position']);
imagettftext($ImageCopy, 10, 0, $CalcXPos['Uni'], $CalcYPos['Uni'], $Colors['white'], $FontLink, $Texts['Uni']);
// Serve directly to the browser in case of unavailable cache directory
if (!$isCacheDirWriteable) {
header('Content-Type: image/png');
imagepng($ImageCopy, null);
die();
}
// Save file, return it with headers & die
imagepng($ImageCopy, $UserFile);
ReturnImage($UserFile);
}
else
{
// Throw Error: BadData Given
$FilePath = "{$CacheStaticsPath}/signature_{$SigLang}_error1.png";
ReturnImage($FilePath);
}
?>