forked from kiwi3685/kiwitrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mediafirewall.php
441 lines (384 loc) · 12.6 KB
/
mediafirewall.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* Kiwitrees: Web based Family History software
* Copyright (C) 2012 to 2017 kiwitrees.net
*
* Derived from webtrees (www.webtrees.net)
* Copyright (C) 2010 to 2012 webtrees development team
*
* Derived from PhpGedView (phpgedview.sourceforge.net)
* Copyright (C) 2002 to 2010 PGV Development Team
*
* Kiwitrees is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 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 Kiwitrees. If not, see <http://www.gnu.org/licenses/>.
*/
define('KT_SCRIPT_NAME', 'mediafirewall.php');
require './includes/session.php';
Zend_Session::writeClose();
$mid = KT_Filter::get('mid', KT_REGEX_XREF);
$thumb = KT_Filter::getBool('thumb');
$media = KT_Media::getInstance($mid, KT_GED_ID);
/**
* Send a “Not found” error as an image
*/
function send404AndExit() {
$error = KT_I18N::translate('The media file was not found in this family tree.');
$width = mb_strlen($error) * 6.5 + 50;
$height = 60;
$im = imagecreatetruecolor($width, $height); /* Create a black image */
$bgc = imagecolorallocate($im, 255, 255, 255); /* set background color */
imagefilledrectangle($im, 2, 2, $width-4, $height-4, $bgc); /* create a rectangle, leaving 2 px border */
embedText($im, $error, 100, '255, 0, 0', KT_FONT_DEJAVU_SANS_TTF, 'top', 'left');
header('HTTP/1.0 404 Not Found');
header('Status: 404 Not Found');
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
/**
* The media firewall passes in an image
* this function can manipulate the image however it wants
* before returning it back to the media firewall
*
* @param resource $im
* @param Tree KT_GED_ID
*
* @return resource
*/
function applyWatermark($im) {
// text to watermark with
$word1_text = KT_TREE_TITLE;
// maximum font size for “word1” ; will be automaticaly reduced to fit in the image
$word1_maxsize = 100;
// rgb color codes for text
$word1_color = '0,0,0';
// ttf font file to use
$word1_font = KT_FONT_DEJAVU_SANS_TTF;
// vertical position for the text to past; possible values are: top, middle or bottom, across
$word1_vpos = 'across';
// horizontal position for the text to past in media file; possible values are: left, right, top2bottom, bottom2top
// this value is used only if $word1_vpos=across
$word1_hpos = 'left';
$word2_text = $_SERVER['HTTP_HOST'];
$word2_maxsize = 20;
$word2_color = '0,0,0';
$word2_font = KT_FONT_DEJAVU_SANS_TTF;
$word2_vpos = 'top';
$word2_hpos = 'top2bottom';
embedText($im, $word1_text, $word1_maxsize, $word1_color, $word1_font, $word1_vpos, $word1_hpos);
embedText($im, $word2_text, $word2_maxsize, $word2_color, $word2_font, $word2_vpos, $word2_hpos);
return $im;
}
/**
* Embed text into an image.
*
* @param resource $im
* @param string $text
* @param int $maxsize
* @param string $color
* @param string $font
* @param string $vpos
* @param string $hpos
*/
function embedText($im, $text, $maxsize, $color, $font, $vpos, $hpos) {
global $useTTF;
// there are two ways to embed text with PHP
// (preferred) using GD and FreeType you can embed text using any True Type font
// (fall back) if that is not available, you can insert basic monospaced text
$col = explode(',', $color);
$textcolor = imagecolorallocate($im, $col[0], $col[1], $col[2]);
// make adjustments to settings that imagestring and imagestringup can’t handle
if (!$useTTF) {
// imagestringup only writes up, can’t use top2bottom
if ($hpos === 'top2bottom') {
$hpos = 'bottom2top';
}
}
$text = reverseText($text);
$height = imagesy($im);
$width = imagesx($im);
$calc_angle=rad2deg(atan($height/$width));
$hypoth=$height/sin(deg2rad($calc_angle));
// vertical and horizontal position of the text
switch ($vpos) {
default:
case 'top':
$taille=textlength($maxsize, $width, $text);
$pos_y=$height*0.15+$taille;
$pos_x=$width*0.15;
$rotation=0;
break;
case 'middle':
$taille=textlength($maxsize, $width, $text);
$pos_y=($height+$taille)/2;
$pos_x=$width*0.15;
$rotation=0;
break;
case 'bottom':
$taille=textlength($maxsize, $width, $text);
$pos_y=($height*.85-$taille);
$pos_x=$width*0.15;
$rotation=0;
break;
case 'across':
switch ($hpos) {
default:
case 'left':
$taille=textlength($maxsize, $hypoth, $text);
$pos_y=($height*.85-$taille);
$pos_x=$width*0.15;
$rotation=$calc_angle;
break;
case 'right':
$taille=textlength($maxsize, $hypoth, $text);
$pos_y=($height*.15-$taille);
$pos_x=$width*0.85;
$rotation=$calc_angle+180;
break;
case 'top2bottom':
$taille=textlength($maxsize, $height, $text);
$pos_y=($height*.15-$taille);
$pos_x=($width*.90-$taille);
$rotation=-90;
break;
case 'bottom2top':
$taille=textlength($maxsize, $height, $text);
$pos_y = $height*0.85;
$pos_x = $width*0.15;
$rotation=90;
break;
}
break;
}
// apply the text
if ($useTTF) {
// if imagettftext throws errors, catch them with a custom error handler
set_error_handler('imagettftextErrorHandler');
imagettftext($im, $taille, $rotation, $pos_x, $pos_y, $textcolor, $font, $text);
restore_error_handler();
}
// Don’t use an ‘else’ here since imagettftextErrorHandler may have changed the value of $useTTF from true to false
if (!$useTTF) {
if ($rotation !== 90) {
imagestring($im, 5, $pos_x, $pos_y, $text, $textcolor);
} else {
imagestringup($im, 5, $pos_x, $pos_y, $text, $textcolor);
}
}
}
/**
* Generate an approximate length of text, in pixels.
*
* @param int $t
* @param int $mxl
* @param string $text
*
* @return int
*/
function textlength($t, $mxl, $text) {
$taille_c = $t;
$len = mb_strlen($text);
while (($taille_c - 2) * $len > $mxl) {
$taille_c--;
if ($taille_c == 2) {
break;
}
}
return $taille_c;
}
/**
* imagettftext is the function that is most likely to throw an error
* use this custom error handler to catch and log it
*
* @param int $errno
* @param string $errstr
*
* @return bool
*/
function imagettftextErrorHandler($errno, $errstr) {
global $useTTF, $serverFilename;
// log the error
AddToLog('Media Firewall error: >' . $errno . '/' . $errstr . '< while processing file >' . $serverFilename . '<', 'error');
// change value of useTTF to false so the fallback watermarking can be used.
$useTTF = false;
return true;
}
/**
* Determine if the system supports editing of that image type
*
* @param string $reqtype
*
* @return string|false
*/
function isImageTypeSupported($reqtype) {
$supportByGD = array('jpg'=>'jpeg', 'jpeg'=>'jpeg', 'gif'=>'gif', 'png'=>'png');
$reqtype = strtolower($reqtype);
if (empty($supportByGD[$reqtype])) {
return false;
}
$type = $supportByGD[$reqtype];
if (function_exists('imagecreatefrom' . $type) && function_exists('image' . $type)) {
return $type;
}
return false;
}
// this needs to be a global variable so imagettftextErrorHandler can set it
$useTTF = function_exists('imagettftext');
// Media object missing/private?
if (!$media || !$media->canDisplayDetails()) {
send404AndExit();
}
// media file somewhere else?
if ($media->isExternal()) {
header('Location: ' . $media->getFilename());
return;
}
$which = $thumb ? 'thumb' : 'main';
$serverFilename = $media->getServerFilename($which);
if (!file_exists($serverFilename)) {
send404AndExit();
return;
}
$mimetype = $media->mimeType();
$imgsize = $media->getImageAttributes($which);
$protocol = $_SERVER["SERVER_PROTOCOL"]; // determine if we are using HTTP/1.0 or HTTP/1.1
$filetime = $media->getFiletime($which);
$filetimeHeader = gmdate('D, d M Y H:i:s', $filetime) . ' GMT';
$expireOffset = 3600 * 24; // tell browser to cache this image for 24 hours
if (KT_Filter::get('cb')) {
$expireOffset = $expireOffset * 7;
} // if cb parameter was sent, cache for 7 days
$expireHeader = gmdate('D, d M Y H:i:s', KT_TIMESTAMP + $expireOffset) . ' GMT';
$type = isImageTypeSupported($imgsize['ext']);
$usewatermark = false;
// if this image supports watermarks and the watermark module is intalled...
if ($type) {
// if this is not a thumbnail, or WATERMARK_THUMB is true
if (($which === 'main') || $WATERMARK_THUMB ) {
// if the user’s priv’s justify it...
if (KT_USER_ACCESS_LEVEL > $SHOW_NO_WATERMARK ) {
// add a watermark
$usewatermark = true;
}
}
}
// determine whether we have enough memory to watermark this image
if ($usewatermark) {
if (!hasMemoryForImage($serverFilename)) {
// not enough memory to watermark this file
$usewatermark = false;
}
}
$watermarkfile = '';
$generatewatermark = false;
if ($usewatermark) {
if ($which == 'thumb') {
$watermarkfile = KT_DATA_DIR . 'media_watermarks_cache_' . KT_GEDCOM . '/thumb/' . $media->getFilename();
} else {
$watermarkfile = KT_DATA_DIR . 'media_watermarks_cache_' . KT_GEDCOM . '/main/' . $media->getFilename();
}
if (!file_exists($watermarkfile)) {
// no saved watermark file exists
// generate the watermark file
$generatewatermark = true;
} else {
$watermarktime = filemtime($watermarkfile);
if ($filetime > $watermarktime) {
// if the original image was updated after the saved file was created
// generate the watermark file
$generatewatermark = true;
}
}
}
$etag = $media->getEtag($which);
// parse IF_MODIFIED_SINCE header from client
$if_modified_since = 'x';
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
$if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
}
// parse IF_NONE_MATCH header from client
$if_none_match = 'x';
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
$if_none_match = str_replace('"', '', $_SERVER['HTTP_IF_NONE_MATCH']);
}
// add caching headers. allow browser to cache file, but not proxy
header('Last-Modified: ' . $filetimeHeader);
header('ETag: "'.$etag.'"');
header('Expires: ' . $expireHeader);
header('Cache-Control: max-age=' . $expireOffset . ', s-maxage=0, proxy-revalidate');
// if this file is already in the user’s cache, don’t resend it
// first check if the if_modified_since param matches
if ($if_modified_since === $filetimeHeader) {
// then check if the etag matches
if ($if_none_match === $etag) {
header($protocol." 304 Not Modified");
return;
}
}
// send headers for the image
header('Content-Type: ' . $mimetype);
header('Content-Disposition: filename="' . addslashes(basename($media->getFilename())) . '"');
if ($generatewatermark) {
// generate the watermarked image
$imCreateFunc = 'imagecreatefrom'.$type;
$imSendFunc = 'image' . $type;
if (function_exists($imCreateFunc) && function_exists($imSendFunc)) {
$im = $imCreateFunc($serverFilename);
$im = applyWatermark($im);
// save the image, if preferences allow
if ((($which=='thumb') && $SAVE_WATERMARK_THUMB) || (($which=='main') && $SAVE_WATERMARK_IMAGE)) {
// make sure the folder exists
if (!is_dir(dirname($watermarkfile))) {
KT_File::mkdir(dirname($watermarkfile), KT_PERM_EXE, true);
}
// save the image
$imSendFunc($im, $watermarkfile);
}
if ($which === 'thumb' && $SAVE_WATERMARK_THUMB || $which === 'main' && $SAVE_WATERMARK_IMAGE) {
// make sure the folder exists
KT_File::mkdir(dirname($watermarkfile));
// save the image
$imSendFunc($im, $watermarkfile);
}
// send the image
$imSendFunc($im);
imagedestroy($im);
return;
} else {
// this image is defective. log it
AddToLog('Media Firewall error: >' . KT_I18N::translate('This media file is broken and cannot be watermarked.') . '< in file >' . $serverFilename . '< memory used: ' . memory_get_usage(), 'media');
// set usewatermark to false so image will simply be passed through below
$usewatermark = false;
}
}
// pass the image through without manipulating it
if ($usewatermark) {
// the stored watermarked image is good, lets use it
$serverFilename = $watermarkfile;
}
// determine filesize of image (could be original or watermarked version)
$filesize = filesize($serverFilename);
// set content-length header, send file
header("Content-Length: " . $filesize);
// Some servers disable fpassthru() and readfile()
if (function_exists('readfile')) {
readfile($serverFilename);
} else {
$fp=fopen($serverFilename, 'rb');
if (function_exists('fpassthru')) {
fpassthru($fp);
} else {
while (!feof($fp)) {
echo fread($fp, 65536);
}
}
fclose($fp);
}