<?php

require '../vendor/autoload.php';
include '../config.php';
$statsd = new League\StatsD\Client();
$statsd->configure(array('host' => isset($statsd_host) ? $statsd_host : '127.0.0.1', 'port' => isset($statsd_port) ? $statsd_port : 8125, 'namespace' => isset($statsd_namespace) ? $statsd_namespace : null));
if (isset($_SERVER['REQUEST_URI'])) {
    $filename = substr($_SERVER['REQUEST_URI'], 1);
    $noExt = str_replace('.gif', '', $filename);
    $parts = explode('_', $noExt);
    if (isset($parts[0]) && isset($parts[1]) && (strlen($parts[0]) == 3 || strlen($parts[0]) == 4) && strlen($parts[1]) >= 16) {
        $counters = array('cloudflare.errors', 'cloudflare.errors.' . $parts[0]);
        $statsd->increment($counters, 1, 1);
    }
}
$fp = fopen('../transparent.gif', 'rb');
header('Content-type: image/gif');
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Pragma: no-cache');
header('Expires: 0');
fpassthru($fp);
exit;
    if ($status == 200) {
        $dataAr = json_decode($data, true);
        $indexInResult = isset($query['indexInResult']) ? $query['indexInResult'] : 0;
        if (isset($dataAr['results'][$indexInResult][$query['keyToFetch']])) {
            $fetchedData = $dataAr['results'][$indexInResult][$query['keyToFetch']];
        }
    }
    if ($fetchedData !== null && $fetchedData >= 0) {
        switch ($query['statsdMetricType']) {
            case 'gauge':
                $statsd->gauge($query['statsdKey'], $fetchedData);
                echo "Updating metric type 'gauge', key: " . $query['statsdKey'] . ", value: " . $fetchedData . PHP_EOL;
                break;
            case 'set':
                $statsd->set($query['statsdKey'], $fetchedData);
                echo "Updating metric type 'set', key: " . $query['statsdKey'] . ", value: " . $fetchedData . PHP_EOL;
                break;
            case 'increment':
                $statsd->increment($query['statsdKey'], $fetchedData);
                echo "Updating metric type 'increment', key: " . $query['statsdKey'] . ", value: " . $fetchedData . PHP_EOL;
                break;
            case 'decrement':
                $statsd->decrement($query['statsdKey'], $fetchedData);
                echo "Updating metric type 'decrement', key: " . $query['statsdKey'] . ", value: " . $fetchedData . PHP_EOL;
                break;
            default:
                echo "Unknown or missing metric type '" . $query['statsdMetricType'] . "'";
                exit(1);
        }
    }
}