xref: /web-php/images/logo.php (revision d9bcfed4)
1<?php
2
3$refresh = isset($_GET['refresh']);
4
5// Be 100% sure the timezone is set
6if (ini_get('date.timezone') === '' && function_exists('date_default_timezone_set')) {
7    date_default_timezone_set('UTC');
8}
9
10$now = $_SERVER['REQUEST_TIME'];
11
12if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
13    $last = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
14
15    // Use the same logo for a day
16    if (strtotime('+1 day', $last) > $now && !$refresh) {
17        header('HTTP/1.1 304 Not Modified');
18        exit;
19    }
20}
21
22function imgheader($filename) {
23    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
24    switch ($ext) {
25        case 'gif':
26            $hdr = 'image/gif';
27            break;
28        case 'png':
29            $hdr = 'image/png';
30            break;
31        case 'jpg':
32        case 'jpeg':
33            $hdr = 'image/jpeg';
34            break;
35        case 'svg':
36            $hdr = 'image/svg+xml';
37            break;
38        default:
39            return false;
40    }
41    header("Content-Type: $hdr");
42}
43
44function get_accepted_encodings() {
45    if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
46        $encodings = explode(',', $_SERVER['HTTP_ACCEPT_ENCODING']);
47        return array_map(function ($x) {
48            return trim($x);
49        }, $encodings);
50    }
51    return [];
52}
53
54function serve_compressed_if_available($logo): void {
55    $encodings = get_accepted_encodings();
56    if (!empty($encodings)) {
57        foreach ($encodings as $encoding) {
58            if ($encoding === 'gzip') {
59                $encoded_file = "$logo.gz";
60                if (file_exists($encoded_file)) {
61                    header("Content-Encoding: $encoding");
62                    readfile($encoded_file);
63                    return;
64                }
65            }
66        }
67    }
68    readfile($logo);
69}
70
71$logo = './logos/php-logo.svg';
72if (isset($_SERVER['QUERY_STRING'])) {
73    switch ($_SERVER['QUERY_STRING']) {
74        case 'QA':
75        case 'qa':
76            $logo = './logos/qa.jpg';
77            break;
78    }
79}
80
81// xmas season, december and the first week of January
82$day = date('z', $now) - date('L', $now);
83if ($day < 6 || 365 - $day < 32) {
84    $logo = './logos/php-xmas-2013.png';
85}
86
87$future = strtotime('+1 day', $now);
88$tsstring = gmdate('D, d M Y H:i:s ', $now) . 'GMT';
89header('Last-Modified: ' . $tsstring);
90header('Expires: ' . date(DATE_RSS, $future));
91imgheader($logo);
92serve_compressed_if_available($logo);
93