3 define('DOCROOT', __DIR__ .'/');
4 require_once(DOCROOT . 'config.php');
5 require_once(DOCROOT . 'inc/wfpl/upload.php');
7 function cms_images_autoresize_main_abort_404() {
8 http_response_code('404');
9 header('HTTP/1.0 404 File Not Found');
10 header('Content-Type: text/plain');
11 print('404: File not found');
15 function cms_images_autoresize_main() {
16 # figure out what file was requested
17 $out_fn = $_SERVER['REDIRECT_URL'];
18 $out_fn = preg_replace('|[?].*|', '', $out_fn); # apache 2.4.17
19 $out_fn = preg_replace('|.*/|', '', $out_fn);
21 if (!preg_match('/^([0-9a-f]+)w([0-9]+)[.](png|jpg)$/', $out_fn, $matches)) {
22 cms_images_autoresize_main_abort_404();
25 $basename = $matches[1];
26 $width = (int)$matches[2];
28 $in_fn = "$basename.$ext";
29 $in_path = DOCROOT . 'cms_images/' . $in_fn;
30 $out_path = DOCROOT . 'cms_images/' . $out_fn;
31 $lock_path = $out_path . '.lock';
33 if (!in_array($width, $GLOBALS['wfpl_image_widths'], true)) {
34 cms_images_autoresize_main_abort_404();
37 if (!file_exists($in_path)) {
38 cms_images_autoresize_main_abort_404();
41 @$lock = fopen($lock_path, 'x');
43 # delete lock file if it's stale
44 $s = stat($lock_path);
45 if ($s && $s['mtime'] + 3 < time()) {
48 # if it's fresh, exit with temporary error
49 header('HTTP/1.0 503 Service Unavailable');
50 header('Content-Type: text/plain');
51 header('Retry-After: 4');
52 print("503 Service Unavailable (try again soon)\n");
59 imagemagick_convert($in_path, $out_path,
71 if (!headers_sent()) {
72 header('Content-Type: ' . ($ext = 'jpg' ? 'image/jpeg' : 'image/png'));
76 cms_images_autoresize_main();