Completed
Push — 0713 ( c637fa...b1d0cb )
by Mikael
02:55
created

imgf.php ➔ debug1()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Fast track cache, read entries from the cache before processing image
4
 * the ordinary way.
5
 */
6
7
// Load the config file or use defaults
8
$configFile = __DIR__
9
    . "/"
10
    . basename(__FILE__, ".php")
11
    . "_config.php";
12
13
if (is_file($configFile) && is_readable($configFile)) {
14
    $config = require $configFile;
15
} elseif (!isset($config)) {
16
    $config = array(
17
        "cache_path" =>  __DIR__ . "/../cache/",
18
    );
19
}
20
21
// Prepare to check if fast cache should be used
22
$cachePath = $config["cache_path"] . "/fasttrack";
23
$query = $_GET;
24
25
// Do not use cache when no-cache is active
26
$useCache = !(array_key_exists("no-cache", $query) || array_key_exists("nc", $query));
27
28
// Remove parts from querystring that should not be part of filename
29
$clear = array("nc", "no-cache");
30
foreach ($clear as $value) {
31
    unset($query[$value]);
32
}
33
34
// Create the cache filename
35
arsort($query);
36
$queryAsString = http_build_query($query);
37
$filename = md5($queryAsString);
38
$filename = "$cachePath/$filename";
39
40
// Check cached item, if any
41
if ($useCache && is_readable($filename)) {
42
    $item = json_decode(file_get_contents($filename), true);
43
44
    if (is_readable($item["source"])) {
45
        foreach ($item["header"] as $value) {
46
            header($value);
47
        }
48
49
        if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])
50
            && strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]) == $item["last-modified"]) {
51
            header("HTTP/1.0 304 Not Modified");
52
            exit;
53
        }
54
55
        foreach ($item["header-output"] as $value) {
56
            header($value);
57
        }
58
59
        readfile($item["source"]);
60
        exit;
61
    }
62
}
63
64
// No fast track cache, proceed as usual
65
include __DIR__ . "/img.php";
66