This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nfo2png.php
408 lines (360 loc) · 10.7 KB
/
nfo2png.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<?php
/**
* NFO2PNG Online Script
*
* @author NewEraCracker
* @license MIT
*
* With contributions by Crypt Xor
*/
// Directory where to host uploaded images
// Only enabled when setting is not empty
$hostDir = '';
// Default code pages for NFO files
// Make sure your iconv version supports them
$codepages = array(
437 => 'English',
866 => 'Russian'
);
// Variable where errors will be saved
$errors = array();
/**
* Test if PHP installation contains the required extensions for this script
*
* @return boolean (True on success. False on failure)
*/
function testphp()
{
global $errors;
// Check GD
if(function_exists('gd_info'))
{
$gdinfo = gd_info();
if(@(!($gdinfo['PNG Support'] && $gdinfo['FreeType Support'])))
{
$errors[] = 'Invalid GD configuration, unable to continue! Webmaster must fix this.';
return false;
}
}
else
{
$errors[] = 'PHP extension GD is not installed, unable to continue! Webmaster must fix this.';
return false;
}
// Check Iconv
if(!extension_loaded('iconv'))
{
$errors[] = 'PHP extension Iconv is not installed, unable to continue! Webmaster must fix this.';
return false;
}
// Check for MultiByte String
if(!extension_loaded('mbstring'))
{
$errors[] = 'PHP extension MultiByte String is not installed, unable to continue! Webmaster must fix this.';
return false;
}
// YAY !
return true;
}
/**
* Parse color based on hexadecimal code (#RRGGBB)
*
* @param string (Hexadecimal Color Value)
* @return array or boolean (Array with RGB components. False in case of failure)
*
* Based on implementation found at http://php.net/manual/en/function.hexdec.php#99478
*/
function parse_color($hexStr)
{
// Do not accept arrays, as they will cause an array to string conversion notice.
if(is_array($hexStr)) return false;
// Get proper hex string
$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr);
$rgbArray = array();
if(strlen($hexStr) == 6)
{
// If a proper hex code, convert using bitwise operation. No overhead... faster
$colorVal = hexdec($hexStr);
$rgbArray[] = 0xFF & ($colorVal >> 0x10);
$rgbArray[] = 0xFF & ($colorVal >> 0x8);
$rgbArray[] = 0xFF & $colorVal;
}
elseif(strlen($hexStr) == 3)
{
// If shorthand notation, need some string manipulations
$rgbArray[] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
$rgbArray[] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
$rgbArray[] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
}
else
{
// Invalid hex color code
return false;
}
// Return RGB array
return $rgbArray;
}
// Emulate SHA-256 on systems without Suhosin installed.
if(!function_exists('sha256'))
{
/**
* Calculate a SHA-256 hash
*
* @param string (Message to be hashed)
* @param boolean (When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits)
* @return string or boolean (Calculated message digest. False on failure)
*/
function sha256($data, $raw_output = false)
{
global $errors;
// Check for SHA-256 hashing support
if(!function_exists('hash'))
{
$errors[] = 'PHP does not have support for SHA-256 hashing. Webmaster must install Suhosin or Hash extension.';
return false;
}
// Hash and return
return hash('sha256', $data, $raw_output);
}
}
/**
* Custom SHA256 function which returns a 52 chars hash (0-9, a-v)
*
* @param string (Message to be hashed)
* @return string or boolean (Calculated message digest as lowercase. False on failure)
*
* Partially based on implementation found at http://www.revulo.com/blog/20080222.html
*/
function sha256_b32($str)
{
// Do not accept arrays, as they will cause an array to string conversion notice.
if(is_array($str)) return false;
// Hash
$str = sha256($str);
// Verify
if($str === false) return false;
// Encode sha256 in base32hex
for($res = '', $i = 0; $i < strlen($str); $i += 5)
{
$res .= str_pad(base_convert(substr($str, $i, 5), 16, 32), 4, '0', STR_PAD_LEFT);
}
return $res;
}
/**
* NFO2PNG Main function
*
* @param string (Path where NFO file is locaded)
* @param string (File name of the NFO file)
* @param string (Encoding. CP437, CP866 ...)
* @param string (Background Color Hex RGB)
* @param string (Text Color Hex RGB)
* @param boolean (Create an hosted image)
* @param string (Directory where image will be hosted)
* @return boolean (True on success. False on failure)
*/
function nfo2png_ttf($nfoFile, $nfoName, $encoding = 'CP437', $bgColor = 'FFFFFF', $txtColor = '000000', $hostImage = false, $hostDir = '')
{
global $errors;
define('NFO_FONT_FILE', './assets/luconP.ttf');
define('NFO_FONT_HEIGTH', 10);
define('NFO_FONT_WIDTH', 8);
define('NFO_LINE_SPACING', 3);
define('NFO_LINE_HEIGTH', (NFO_FONT_HEIGTH + NFO_LINE_SPACING));
define('NFO_SIDES_SPACING', 5);
// Deny files with invalid size or bigger than 1000 KiB (1024000 bytes)
if(filesize($nfoFile) <= 0 || filesize($nfoFile) > 1024000)
{
$errors[] = 'File with invalid size, try again with another file!';
return false;
}
// Grab all contents
$nfo = file_get_contents($nfoFile);
// Encoding corrections
if(strpos($nfo, "\xEF\xBB\xBF") === 0)
{
// UTF-8 - Remove BOM
$nfo = substr($nfo, 3);
}
elseif(strpos($nfo, "\xFF\xFE") === 0 || strpos($nfo, "\xFE\xFF") === 0)
{
// UTF-16 - Convert to UTF-8
$nfo = @mb_convert_encoding($nfo, 'UTF-8', 'UTF-16');
}
else
{
// ASCII - Convert to UTF-8
$nfo = str_replace("\xFF", "\x20", $nfo);
$nfo = @iconv($encoding, 'UTF-8', $nfo);
}
// Strictly check encoding correction for possible failures
if($nfo === '' || $nfo === false || $nfo === null)
{
$errors[] = 'Invalid encoding detected.';
return false;
}
// Reformat each line
$nfo = explode("\n", $nfo);
$nfo = array_map('rtrim', $nfo);
$xmax = 0;
// Calculate maximum line length
mb_internal_encoding('UTF-8');
foreach($nfo as $line)
{
if($xmax < mb_strlen($line))
$xmax = mb_strlen($line);
}
// Size of image in pixels
$xmax = (NFO_SIDES_SPACING * 2) + (NFO_FONT_WIDTH * $xmax);
$ymax = (NFO_SIDES_SPACING * 2) + (NFO_LINE_HEIGTH * count($nfo));
// Deny images bigger than 9 million pixels
if($xmax * $ymax > 9000000)
{
$errors[] = 'File too big, try again with a smaller file!';
return false;
}
// Create image
$im = imagecreatetruecolor($xmax, $ymax);
// Allocate colors to image
$bgColor = parse_color($bgColor);
if(!$bgColor)
{
@imagedestroy($im);
$errors[] = 'Invalid background color.';
return false;
}
$bgColor = imagecolorallocate($im, $bgColor[0], $bgColor[1], $bgColor[2]);
$txtColor = parse_color($txtColor);
if(!$txtColor)
{
@imagedestroy($im);
$errors[] = 'Invalid text color.';
return false;
}
$txtColor = imagecolorallocate($im, $txtColor[0], $txtColor[1], $txtColor[2]);
// Fill image background
imagefilledrectangle($im, 0, 0, $xmax, $ymax, $bgColor);
// Add each line to image
for($y = 0, $ycnt = count($nfo), $drawy = (NFO_SIDES_SPACING + NFO_LINE_HEIGTH); $y < $ycnt; $y++, $drawy += NFO_LINE_HEIGTH)
{
$drawx = NFO_SIDES_SPACING;
imagettftext($im, NFO_FONT_HEIGTH, 0, $drawx, $drawy, $txtColor, NFO_FONT_FILE, $nfo[$y]);
}
// Start output buffering
ob_start();
// Generate image
if(false === @imagepng($im))
{
imagedestroy($im);
ob_end_clean();
$errors[] = 'Image generation failed for unknown reasons.';
return false;
}
// Capture and reset buffer
$image = ob_get_clean();
imagedestroy($im);
if($hostImage && is_string($hostDir) && strlen($hostDir) > 0)
{
// Store file
$fileName = sha256_b32($image);
if($fileName === false) return false;
$fileName = $hostDir . '/' . $fileName . '.png';
if(false === @file_put_contents($fileName, $image))
{
$errors[] = 'It was not possible to write image into filesystem.';
return false;
}
// Redirect user
header("Location: {$fileName}", true, 303);
return true;
}
else
{
// Process PNG download
$fileName = pathinfo($nfoName, PATHINFO_FILENAME) . '.png';
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename={$fileName}");
echo $image;
// Image successfully generated and passed to users browser
return true;
}
return false;
}
// Run GD tests and check if we are processing a POST request
if(testphp() && $_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_FILES['nfofile']['error']) && $_FILES['nfofile']['error'] != UPLOAD_ERR_OK)
{
$errors[] = 'There has been an error while uploading the file.';
}
elseif(isset($_FILES['nfofile']['tmp_name'], $_FILES['nfofile']['name']))
{
// Inspect requested encoding
reset($codepages);
$encoding = isset($_POST['encoding']) ? intval($_POST['encoding']) : key($codepages);
$encoding = isset($codepages[$encoding]) ? 'CP' . $encoding : 'CP' . key($codepages);
// Colors
$bgColor = isset($_POST['bgColor']) ? $_POST['bgColor'] : 'FFFFFF';
$txtColor = isset($_POST['txtColor']) ? $_POST['txtColor'] : '000000';
// Host an image
$hostImage = isset($_POST['hostImage']) ? true : false;
// Process NFO 2 PNG action
$retval = nfo2png_ttf($_FILES['nfofile']['tmp_name'], $_FILES['nfofile']['name'], $encoding, $bgColor, $txtColor, $hostImage, $hostDir);
// Remove temporary file from our server
unlink($_FILES['nfofile']['tmp_name']);
// If NFO 2 PNG is successful, bail out to avoid unexpected output
if($retval) exit();
}
else
{
$errors[] = 'No file has been uploaded.';
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>NFO2PNG Online</title>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<style type="text/css">
<!--
body { background-color:#000000; color:#FFFFFF; font-family:Verdana, Geneva, sans-serif; }
.error { color:#FF9900; }
.file { background-color:#000000; color:#FFFFFF; border:1px; margin:0px; }
-->
</style>
<script src="assets/jscolor/jscolor.js"></script>
</head>
<body>
<div align="center">
<h1>NFO2PNG ONLINE</h1>
<br />
<?php
foreach($errors as $e)
{
echo '<p class="error">' . htmlspecialchars($e) . '</p>';
}
?>
<form enctype="multipart/form-data" action="" method="post">
<p>File: <input name="nfofile" type="file" class="file" /></p>
<p>Background Color: <input name="bgColor" type="text" class="color" value="FFFFFF"></p>
<p>Text Color: <input name="txtColor" type="text" class="color" value="000000"></p>
<p>Encoding: <select name="encoding"><?php
foreach($codepages as $number => $name)
{
echo "<option value='{$number}'>{$name}</option>";
}
?></select></p>
<?php
if(is_string($hostDir) && strlen($hostDir) > 0)
{
?>
<p><input type="checkbox" name="hostImage" />Create a image link?</p>
<?php
}
?>
<p><input type="submit" value="Convert to PNG" /></p>
</form>
</div>
</body>
</html>