summaryrefslogtreecommitdiff
path: root/b.php
blob: 9c4edfc509623591fe3f67702860437f90d9fa85 (plain)
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
<?php
// This script assumes there is at least one normal (non-priority)
// banner!

// Get the files in a directory, returns null if the directory does
// not exist.
function getFilesInDirectory($dir) {
    if (! is_dir($dir)) {
        return null;
    }

    return array_diff(scandir($dir), ['.', '..']);
}

// Serve a random banner and exit.
function serveRandomBanner($dir, $files): never {
    $name = $files[array_rand($files)];

    // snags the extension
    $ext = pathinfo((string) $name, PATHINFO_EXTENSION);

    // send the right headers
    header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1
    header('Pragma: no-cache'); // HTTP 1.0
    header('Expires: 0'); // Proxies
    header("Content-type: image/" . $ext);
    header("Content-Disposition: inline; filename=" . $name);

    // readfile displays the image, passthru seems to spits stream.
    readfile($dir.$name);
    exit;
}

// Get all the banners
$bannerDir = "banners/";
$priorityDir = "banners_priority/";

$banners = getFilesInDirectory($bannerDir);
$priority = getFilesInDirectory($priorityDir);

// If there are priority banners, serve 1/3rd of the time.
if($priority !== null && (is_countable($priority) ? count($priority) : 0) !== 0 && random_int(0,2) === 0) {
    serveRandomBanner($priorityDir, $priority);
}

serveRandomBanner($bannerDir, $banners);
?>