Пример #1
0
function getBestImage($width, $height, $person)
{
    $app = \Slim\Slim::getInstance();
    if (is_null($person) || $person == 'all') {
        $dir = $_SERVER["DOCUMENT_ROOT"] . '/img/';
    } else {
        $dir = $_SERVER["DOCUMENT_ROOT"] . '/img/' . $person . '/';
    }
    if (!is_dir($dir)) {
        handle404();
    }
    if ($person == 'all') {
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
    } else {
        $files = scandir($dir);
    }
    $requestedAspect = $width / $height;
    $allowed_file_types = array("jpg", "png");
    $files_with_difference = array();
    foreach ($files as $file) {
        if (in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), $allowed_file_types)) {
            if (strpos($file, $dir) === false) {
                $file = $dir . $file;
            }
            if (is_file($file)) {
                $aspect = getimagesize($file)[0] / getimagesize($file)[1];
                $files_with_difference[] = array('file' => $file, 'aspect_difference' => abs($requestedAspect - $aspect));
            }
        }
    }
    function sort_array($a, $b)
    {
        if ($a['aspect_difference'] == $b['aspect_difference']) {
            return 0;
        }
        return $a['aspect_difference'] < $b['aspect_difference'] ? -1 : 1;
    }
    usort($files_with_difference, 'sort_array');
    $possibilities = array();
    $random = $app->request()->params('random');
    $randomisation_range = isset($random) ? 5 : 1;
    $randomised = 0;
    foreach ($files_with_difference as $file) {
        $possibilities[] = $file['file'];
        $randomised++;
        if ($randomised == $randomisation_range) {
            break;
        }
    }
    $match = $possibilities[array_rand($possibilities)];
    if (!is_file($match)) {
        handle404();
    }
    return $match;
}
Пример #2
0
<?php

require_once __DIR__ . '/../../src/AcuityScheduling.php';
require_once '../utils.php';
// Config:
$config = loadConfig();
$secret = $config['apiKey'];
list($method, $path) = getRouteInfo();
// App:
if ($method === 'GET' && $path === '/') {
    include 'index.html';
} else {
    if ($method === 'POST' && $path === '/webhook') {
        // Handle webhook after verifying signature:
        try {
            AcuityScheduling::verifyMessageSignature($secret);
            error_log("The message is authentic:\n" . json_encode($_POST, JSON_PRETTY_PRINT));
        } catch (Exception $e) {
            trigger_error($e->getMessage(), E_USER_WARNING);
        }
    } else {
        handle404();
    }
}
$port = $args['port'];
$connections = [];
$numConnections = 0;
$app = function ($request, $response) use(&$connections, &$numConnections) {
    if ($request->getMethod() === 'OPTIONS') {
        return handleCors($request, $response);
    }
    switch ($request->getPath()) {
        case '/broadcast':
            return handleBroadcast($request, $response);
        case '/connections':
            return handleConnectionCount($request, $response);
        case '/sse':
            return handleSse($request, $response);
        default:
            return handle404($request, $response);
    }
};
function handleBroadcast($request, $response)
{
    $body = '';
    $request->on('data', function ($chunk) use(&$body) {
        $body .= $chunk;
    });
    $request->on('end', function () use(&$body) {
        broadcast($body);
    });
    $response->writeHead(202);
    $response->end();
}
function handleCors($request, $response)