Example #1
0
})->before($validateRequest);
$StatTracker->match("/api/{token}/token", function (Request $request, $token) use($StatTracker) {
    $agent = Agent::lookupAgentByToken($token);
    if (!$agent->isValid()) {
        return $StatTracker->abort(403);
    }
    switch ($request->getMethod()) {
        case "GET":
            $name = strtoupper(substr(str_shuffle(md5(time() . $token . rand())), 0, 6));
            $token = $agent->createToken($name);
            $url = sprintf("%s://%s", $request->getScheme(), $request->getHost());
            $url = $url . $request->getBaseUrl() . "/";
            $uri = "stattracker://token?token=%s&name=%s&agent=%s&issuer=%s";
            $uri = sprintf($uri, $token, $name, $agent->name, urlencode($url));
            $qr = new QRCode();
            $qr->setText($uri)->setSize(200)->setPadding(10);
            if ($token === false) {
                return new Response(null, 202);
            } else {
                $data = array("name" => $name, "token" => $token, "qr" => $qr->getDataUri(), "uri" => $uri);
                return $StatTracker->json($data);
            }
            break;
        case "DELETE":
            if (!$request->request->has("name")) {
                return $StatTracker->abort(400);
            }
            $name = strtoupper($request->request->get("name"));
            $r = $agent->revokeToken($name);
            if ($r === true) {
                return new Response(null, 200);